<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.keyword" placeholder="名称,标识,备注" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<xTable ref="table" :api="api" row-key="id" @selection-change="selectionChange" :hideSetting="true" remoteSort remoteFilter stripe>
				<el-table-column type="selection" width="30"></el-table-column>
				<el-table-column label="ID" prop="id" width="50"></el-table-column>
				<el-table-column label="" prop="status" width="38">
					<template #default="scope">
						<x-status-indicator v-if="scope.row.status==1" pulse type="success"></x-status-indicator>
						<x-status-indicator v-if="scope.row.status==0" pulse type="info"></x-status-indicator>
					</template>
				</el-table-column>
				<el-table-column label="名称" prop="title"  minWidth="100">
					<template #default="scope">
						<input class="el-input__inner" type="text" readonly :value="scope.row.title">
					</template>
				</el-table-column>
				<el-table-column label="标识" prop="title"  minWidth="100">
					<template #default="scope">
						<input class="el-input__inner" type="text" readonly :value="scope.row.name">
					</template>
				</el-table-column>
				<el-table-column label="主键" prop="key" width="80"></el-table-column>
				<el-table-column label="请求" prop="views" width="80">
					<template #default="scope">
						<el-link :underline="false" size="small" @click="setImage(scope.row)">
							<el-badge :value="scope.row.views" type="primary"></el-badge>
						</el-link>
					</template>
				</el-table-column>
				<el-table-column label="Column" prop="column" width="80">
					<template #default="scope">
						<el-link :underline="false" size="small" @click="setImage(scope.row)">
							<el-badge :value="scope.row.column.length" type="danger"></el-badge>
						</el-link>
					</template>
				</el-table-column>
				<el-table-column label="tabs" prop="tabs" width="80">
					<template #default="scope">
						<el-link :underline="false" size="small" @click="setImage(scope.row)">
							<el-badge :value="scope.row.tabs.length" type="info"></el-badge>
						</el-link>
					</template>
				</el-table-column>
				<el-table-column label="操作" prop="operationtype" width="160">
					<template #default="scope">
						<input class="el-input__inner" type="text" readonly :value="scope.row.operationtype">
					</template>
				</el-table-column>
				<el-table-column label="Api" prop="api" width="190">
					<template #default="scope">
						<input class="el-input__inner" type="text" readonly :value="scope.row.api">
					</template>
				</el-table-column>
				<el-table-column label="添加时间" prop="timestamp" width="150"></el-table-column>

				<el-table-column label="操作" fixed="right" align="left" width="170">
					<template #default="scope">
						<el-button-group>
							<el-popconfirm :title="'确定复制吗？'" @confirm="table_copy(scope.row, scope.$index)">
								<template #reference>
									<el-button :type="'warning'" :size="'small'">复制</el-button>
								</template>
							</el-popconfirm>

							<el-button type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
							<el-popconfirm :title="'确定删除吗？'" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button :type="'info'" :size="'small'">{{ '删除' }}</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column> 
			</xTable>
		</el-main>
	</el-container>

	<column-dialog v-if="dialog.column" ref="column" @success="handleSuccess" @closed="dialog.column=false"></column-dialog>
</template>

<script>
	import columnDialog from './column'

	export default {
		name: 'tablegen',
		components: {
			columnDialog,
		},
		data() {
			return {
				dialog: {
					column: false,
				},
				api: 'system/table/lists',
				selection: [],
				search: {
					keyword: "",
					status:'0',
				}
			}
		},
		mounted() {

		},
		methods: {
			open(o){
				window.open(o.apartmentposter) 
			},
			//标签切换
			tabChange(status){
				this.search.status = status;
				this.$refs.table.reload(this.search)
			},
			//搜索
			upsearch(){
				this.$refs.table.reload(this.search)
			},
			//增加
			add(){
				this.dialog.column = true
				this.$nextTick(() => {
					this.$refs.column.open()
				})
			},
			//编辑
			table_edit(row){
				this.dialog.column = true
				this.$nextTick(() => {
					this.$refs.column.open('edit').setData(row)
				})
			},
			//复制
			async table_copy(row){
				var res = await this.$http.post('system/table/copy', {id: row.id});
				if(res.code == 200){
					this.$refs.table.reload({})

					if (res.data) {
						this.table_edit(res.data)
					}

					this.$message.success("复制成功")
					return ;
				}
				this.$alert(res.message, "提示", {type: 'error'})
			},
			//删除
			async table_del(row, index){
				var res = await this.$http.post('system/table/delete', {id: row.id});
				if(res.code == 200){
					//这里选择刷新整个表格 OR 插入/编辑现有表格数据
					this.$refs.table.tableData.splice(index, 1);
					this.$message.success("删除成功")
					return ;
				}
				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;
			},
			//本地更新数据
			async handleSuccess(){
				this.$refs.table.reload({})
			}
		}
	}
</script>

<style>
</style>
