<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="handleForm('add')"></el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.keyword" placeholder="姓名/手机号" clearable @click="upsearch"></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" stripe @resetQuery="resetQuery"  >
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="序号" type="index" width="50"></el-table-column>
				<el-table-column label="区域编号" prop="number" min-width="100"></el-table-column>
				<el-table-column label="区域名称" prop="name" min-width="150"></el-table-column>
				<el-table-column label="区域类型" min-width="100">
					<template #default="scope">{{cates[scope.row.cate]}}</template>
				</el-table-column>
				<el-table-column label="风险等级" min-width="100">
					<template #default="scope">{{levels[scope.row.level]}}</template>
				</el-table-column>
				<el-table-column label="访客人员" min-width="100">
					<template #default="scope">
						<span v-if="scope.row.visitor_yes">允许进入</span>
						<span v-else>禁止进入</span>
					</template>
				</el-table-column>
				<el-table-column label="相关方人员" min-width="100">
					<template #default="scope">
						<span v-if="scope.row.remployee_yes">允许进入</span>
						<span v-else>禁止进入</span>
					</template>
				</el-table-column>
				<el-table-column label="全部员工"  min-width="100">
					<template #default="scope">
						<span v-if="scope.row.employee_yes">允许进入</span>
						<span v-else>审批进入</span>
					</template>
				</el-table-column>
				<el-table-column label="最小人员数" prop="count_people_min" min-width="90"></el-table-column>
				<el-table-column label="最大人员数" prop="count_people_max" min-width="90"></el-table-column>
				<el-table-column label="当前人数" prop="count_people" min-width="90"></el-table-column>
				<el-table-column label="所属部门" prop="create_time" min-width="180"></el-table-column>
				<el-table-column label="创建时间" prop="create_time" width="160"></el-table-column>
				<el-table-column label="操作" fixed="right" align="center" width="120">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="handleRail(scope.row)">围栏</el-button>
							<el-button text type="warning" size="small" @click="handleForm('edit',scope.row)">编辑</el-button>
							 <el-popconfirm title="确定删除吗？" @confirm="handleDel(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="danger" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
	<save-rail-dialog v-if="dialog.saveRail" ref="saveRailDialog" :areaId="areaId" :areaName="areaName" @success="handleSaveSuccess" @closed="dialog.saveRail=false"></save-rail-dialog>
</template>
<script>
	import saveDialog from './area_form.vue'
	import saveRailDialog from './area_rail_form.vue'
	export default {
		name: 'amArea',
		components: {
			saveDialog,
			saveRailDialog
		},
		data() {
			return {
				dialog: {
					save: false,
					saveRail: false,
					permission: false
				},
				apiObj: this.$API.am.area.list,
				query: {},
				selection: [],
				search: {
					keyword: null
				},
				levels:{
					10:'低风险',
					20:'一般风险',
					30:'较大风险',
					40:'重大风险',
				},
				cates:{
					10:'生活区域',
					20:'一般区域',
					30:'重点区域',
				},
				areaId:null,
				areaName:'',
			}
		},
		methods: {
			//添加/编辑区域
			handleForm(type,row) {
				this.dialog.save = true;
				this.$nextTick(() => {
					if(type==='add'){
						this.$refs.saveDialog.open(type)
					}else{
						this.$refs.saveDialog.open(type).setData(row)
					}
				})
			},
			handleRail(row){
				this.areaId = row.id;
				this.areaName = row.name;
				this.dialog.saveRail = true;
			},
			//删除区域
			async handleDel(row){
				await this.$API.am.area.delete.req(row.id).then(res=>{
					if(res.err_msg){
						this.$message.error(res.err_msg);
					}else{
						this.$refs.table.refresh();
						this.$message.success("删除成功")
					}
				});
			},

			//批量删除区域
			async batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？如果删除项中含有子集将会被一并删除`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					let params = {pk:{pks:this.selection}};
					this.$API.am.area.deletes.req(params).then(res=>{
						loading.close();
						if(res.err_msg){
							this.$message.error(res.err_msg);
						}else{
							this.$refs.table.refresh();
							this.$message.success("删除成功")
						}
					});
				}).catch(() => {
				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},

			//表格内开关
			changeSwitch(val, row){
				row.status = row.status == '1'?'0':'1'
				row.$switch_status = true;
				setTimeout(()=>{
					delete row.$switch_status;
					row.status = val;
					this.$message.success("操作成功")
				}, 500)
			},

			//搜索
			upsearch(){

			},

			//根据ID获取树结构
			filterTree(id){
				var target = null;
				function filter(tree){
					tree.forEach(item => {
						if(item.id == id){
							target = item
						}
						if(item.children){
							filter(item.children)
						}
					})
				}
				filter(this.$refs.table.tableData)
				return target
			},

			//本地更新数据
			handleSaveSuccess(data, mode){
				if(mode=='add'){
					this.$refs.table.refresh()
				}else if(mode=='edit'){
					this.$refs.table.refresh()
				}
			},

			resetQuery(){
				this.query = {}
			}
		}
	}
</script>
