<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-input v-model="search.name" placeholder="关键词" style="width:150px; margin-right: 10px;" clearable></el-input>
				<sc-select v-model="search.code" dic="ads_places" placeholder="请选择广告位" style="width: 240px;" clearable />
				<el-button-group style="margin-left: 10px;">
					<el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
				</el-button-group>
			</div>
			<div class="right-panel">
				<el-button-group>
					<el-button type="danger" icon="el-icon-plus" @click="add">添加</el-button>
				</el-button-group>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="list.apiObj" :column="list.column" row-key="id" @selection-change="selectionChange" stripe>
				<el-table-column type="selection" width="50"></el-table-column>
				<template #img_url="scope">
					<img :src="scope.row.img_url" width="40" height="40" v-if="scope.row.img_url" />
				</template>
				<el-table-column label="操作" fixed="right" align="center" width="140">
					<template #default="scope">
						<el-button-group>
							<el-button type="success" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
							<el-button type="danger" size="small" @click="table_delete(scope.row, scope.$index)">删除</el-button>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>

		<save v-if="dialog.save" ref="saveDialog" @closed="dialog.save = false" @success="upsearch"></save>
	</el-container>
</template>

<script>
import save from './save';

export default {
	name: 'operateAds',
	components: {
		save
	},
	data() {
		return {
			dialog: {save: false, detail: false, insert: false, match: false},
			list: {
				apiObj: this.$API.ads.list,
				column: [
					{ label: '广告标题', prop: 'title', align: 'center' },
					{ label: '广告位名称', prop: 'place_name', align: 'center' },
					{ label: '广告图片', width: 160, prop: 'img_url', align: 'center' },
					{ label: '添加时间', width: 160, prop: 'add_time', align: 'center' },
				]
			},
			search: {},
			selection: [],
		}
	},
	mounted() {
	},
	methods: {
		upsearch(){
			this.$refs.table.reload(this.search);
		},
		//增加
		add(){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//编辑
		table_edit(row){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//删除
		table_delete(row, index){
			this.$confirm(`确定删除吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				var reqData = {id: row.id}
				var res = await this.$API.ads.delete.post(reqData);
				if(res.code == 1){
					//这里选择刷新整个表格 OR 插入/编辑现有表格数据
					this.$refs.table.tableData.splice(index, 1);
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			})
		},
		//批量删除
		async batch_del(){
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				const loading = this.$loading();
				this.selection.forEach(item => {
					this.$refs.table.tableData.forEach((itemI, indexI) => {
						if (item.id === itemI.id) {
							this.$refs.table.tableData.splice(indexI, 1)
						}
					})
				})
				loading.close();
				this.$message.success("操作成功")
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection){
			this.selection = selection;
		}
	}
}
</script>
