<template>
	<el-container class="mainBox mainHeaderNoBorderPadding">
		<el-header>
			<div class="left-panel">
				<el-button type="primary" :size="size" icon="el-icon-plus" @click="add">新增公司</el-button>
				<el-button type="primary" :size="size" plain>批量导入</el-button>
			</div>
			<div class="right-panel">

			</div>
		</el-header>
		<el-main class="nopadding">
			<div class="searchMain searchMainNoTop">
				<div class="searchItem">
					<label class="name">开通日期</label>
					<el-date-picker class="input" type="daterange" :size="size" v-model="params.activation_date" :popper-options="{ placement: 'bottom-start' }" start-placeholder="开始日期" end-placeholder="结束日期" clearable></el-date-picker>
				</div>
				<div class="searchItem">
					<label class="name">公司名称</label>
					<el-select class="input" :size="size" v-model="params.id" @visible-change="getCompanyList" placeholder="请选择公司名称" filterable clearable>
						<el-option v-for="item in setMap.full_name" :key="item" :label="item.full_name" :value="item.id"></el-option>
					</el-select>
				</div>
				<div class="searchItem">
					<label class="name">状态</label>
					<el-select class="input" :size="size" v-model="params.active_status" @visible-change="getStatusList" placeholder="请选择状态" filterable clearable>
						<el-option v-for="item in setMap.active_status" :key="item" :label="item.label" :value="item.active_status"></el-option>
					</el-select>
				</div>
				<div class="searchItem searchBtn">
					<el-button :size="size" type="primary" icon="el-icon-search" @click="upSearch">查询</el-button>
					<el-button :size="size" type="info" icon="el-icon-RefreshRight" @click="reset">重置</el-button>
				</div>
			</div>
			<scTable ref="table" :apiObj="list.apiObj" :column="list.column" row-key="id" stripe :size="size" @selection-change="selectionChange">
				<el-table-column type="selection" align="center" width="40"></el-table-column>
				<sc-table-column label="序号" align="center" type="index"></sc-table-column>
				<template #logo="scope">
					<el-image class="logoCell" :src="scope.row.logo" fit="contain"></el-image>
				</template>
				<template #active_status="scope">
					<el-switch :size="size" v-model="scope.row.active_status" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" :active-value="true" :inactive-value="false"></el-switch>
				</template>
				<el-table-column label="操作" fixed="right" align="center" width="150">
					<template #default="scope">
						<el-dropdown>
							<el-button class="noBorderBtn" icon="el-icon-more" :size="size"></el-button>
							<template #dropdown>
								<el-dropdown-menu>
									<el-dropdown-item @click="table_show(scope.row, 'see')" icon="sc-icon-See">查看详情</el-dropdown-item>
									<el-dropdown-item @click="table_edit(scope.row, 'edit')" icon="sc-icon-Edit">编辑公司</el-dropdown-item>
									<el-dropdown-item  icon="sc-icon-AbilityAuthorization" @click="table_empower(scope.row)" divided>功能授权</el-dropdown-item>
									<el-dropdown-item  icon="sc-icon-DataAuthorization">数据授权</el-dropdown-item>
									<el-dropdown-item  icon="sc-icon-UserList" @click="table_user_ist(scope.row)" divided>用户列表</el-dropdown-item>
									<el-dropdown-item @click="table_del(scope.row, 'delete')" icon="sc-icon-Delete">删除公司</el-dropdown-item>
								</el-dropdown-menu>
							</template>
						</el-dropdown>
					</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>
	<see-dialog v-if="dialog.show" ref="showDialog" @closed="dialog.show=false"></see-dialog>

</template>

<script>
import saveDialog from './save'
import seeDialog from  './see'

export default {
	name: 'company',
	components: {
		saveDialog,
		seeDialog
	},
	data() {
		return {
			size:'small',
			setMap:{
				full_name:[],
				active_status:[],
			},

			dialog: {
				save: false,
				show: false
			},
			list: {
				apiObj: this.$API.system.company.list,
				column: [],
			},
			selection: [],
			params: {
				activation_date: '',
				id:'',
				active_status:'',
			}
		}
	},
	methods: {
		async getCompanyList(e) {
			if(!e) return
			const res = await this.$API.system.company.select.post(this.params);
			if(res.code == 200){
				this.setMap['full_name'] = res.data;
			}
		},
		async getStatusList(e) {
			if(!e) return
			const res = await this.$API.oss.status.post(this.params);
			if(res.code == 200){
				this.setMap['active_status'] = res.data;
			}
		},

		//添加
		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_show(row){
			this.dialog.show = true
			this.$nextTick(() => {
				this.$refs.showDialog.open('show').setData(row);
			})
		},
		//删除
		async table_del(row){
			this.$confirm(`确定删除 ${row.name} 吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				const reqData = {id: row.id};
				const res = await this.$API.system.company.delete.post(reqData);
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}else{
					await this.$alert(res.message, "提示", {type: 'error'})
				}
			}).catch(()=>{})
		},
		// 功能授权
		table_empower(row){
			this.$router.push({
				path: '/setting/company/add-permission',
				query: {
					id: row.id,
					name:row.name
				}
			})
		},
		// 公司用户列表
		table_user_ist(row){
			this.$router.push({
				path: '/setting/user/company-user-list',
				query: {
					id: row.id,
					name:row.name
				}
			})
		},
		//表格选择后回调事件
		selectionChange(selection){
			this.selection = selection;
		},
		// 状态
		changeSwitch(val, row) {
			row.$switch_yx = true;
			setTimeout(async () => {
				let params = {
					id: row.id,
					status: row.active_status,
				};
				const res = await this.$API.system.company.status.post(params);
				if(res.code !=200){
					row.active_status = !row.active_status;
				}
				delete row.$switch_status;
				delete row.$switch_yx;
			}, 500);
		},
		upSearch(){
			this.$refs.table.upData(this.params);
		},
		reset(){
			this.params = {
				activation_date: '',
				id:'',
				active_status:'',
			};
			this.$refs.table.reload();
		},

		handleSaveSuccess(){
			this.$refs.table.refresh();
		},
	}
}
</script>

<style lang="scss" scoped>
	.logoCell{
		display: flex;
		align-items: center;
		height: 20px;
		margin: 0 auto;
	}
</style>
