<template>
	<div class="app-container">
		<search-bar
			:formConfigs="formConfig"
			ref="searchBarForm"
			@headBtnClick="buttonClick" />
		<base-table
			v-loading="dataListLoading"
			:table-props="tableProps"
			:page="listQuery.pageNo"
			:limit="listQuery.pageSize"
			:table-data="tableData"
			@emitFun="handleEmitFun">
			<method-btn
				v-if="tableBtn.length"
				slot="handleBtn"
				:width="120"
				label="操作"
				:method-list="tableBtn"
				@clickBtn="handleClick" />
		</base-table>
		<pagination
			:limit.sync="listQuery.pageSize"
			:page.sync="listQuery.pageNo"
			:total="listQuery.total"
			@pagination="getDataList" />
		<base-dialog
			:dialogTitle="addOrEditTitle"
			:dialogVisible="addOrUpdateVisible"
			@cancel="handleCancel"
			@confirm="handleConfirm"
			:before-close="handleCancel"
			width="50%">
			<add-or-update
				ref="addOrUpdate"
				@refreshDataList="successSubmit"></add-or-update>
		</base-dialog>
	</div>
</template>

<script>
import AddOrUpdate from './add-or-updata';
import basicPage from '@/mixins/basic-page';
import { parseTime } from '@/filter/code-filter';
import {
	deleteEquipmentPlc,
	getEquipmentPlcPage,
	exportEquipmentPlcExcel,
	updateEquipmentPlc,
} from '@/api/base/equipmentPlc';

const switchBtn = {
	name: 'SwitchBtn',
	props: ['injectData'],
	data() {
		return {};
	},
	computed: {
		active() {
			return +this.injectData[this.injectData.prop] == 1 ? true : false;
		},
	},
	methods: {},
	render: function (h) {
		return h(
			'el-switch',
			{
				props: {
					value: this.active,
				},
				on: {
					change: (newVal) => {
						this.$emit('emitData', {
							action: 'update-collect',
							payload: {
								...this.injectData,
								collection: newVal ? 1 : 0,
							},
						});
					},
				},
			},
			null
		);
	},
};
const tableProps = [
	{ prop: 'code', label: '编码', width: 220 },
	{ prop: 'plcTableName', label: '关联表名' },
	{ prop: 'name', label: '标识名称' },
	{ prop: 'enName', label: '英文名称' },
	{
		prop: 'collection',
		label: '是否采集',
		subcomponent: switchBtn,
	},
	{ prop: 'description', label: '描述' },
];

export default {
	mixins: [basicPage],
	data() {
		return {
			urlOptions: {
				getDataListURL: getEquipmentPlcPage,
				deleteURL: deleteEquipmentPlc,
				exportURL: exportEquipmentPlcExcel,
			},
			tableProps,
			tableBtn: [
				this.$auth.hasPermi(`base:equipment-plc:update`)
					? {
							type: 'edit',
							btnName: '编辑',
					  }
					: undefined,
				this.$auth.hasPermi(`base:equipment-plc:delete`)
					? {
							type: 'delete',
							btnName: '删除',
					  }
					: undefined,
			].filter((v) => v),
			tableData: [],
			formConfig: [
				{
					type: 'input',
					label: '表名',
					placeholder: '请输入表名',
					param: 'plcTableName',
				},
				{
					type: 'input',
					label: '标识',
					placeholder: '请输入标识',
					param: 'name',
				},
				{
					type: 'button',
					btnName: '搜索',
					name: 'search',
					color: 'primary',
				},
				// {
				// 	type: 'separate',
				// },
				// {
				// 	type: 'button',
				// 	btnName: '重置',
				// 	name: 'reset',
				// },
				{
					type: 'separate',
				},
				{
					type: this.$auth.hasPermi('base:factory:create') ? 'button' : '',
					btnName: '新增',
					name: 'add',
					color: 'success',
					plain: true,
				},
				// {
				// 	type: this.$auth.hasPermi('base:factory:create') ? 'separate' : '',
				// },
				// {
				// 	type: this.$auth.hasPermi('base:factory:export') ? 'button' : '',
				// 	btnName: '导出',
				// 	name: 'export',
				// 	color: 'warning',
				// },
			],
		};
	},
	components: {
		AddOrUpdate,
	},
	created() {},
	methods: {
		/** 覆盖 handleEmitFun 的默认实现 */
		handleEmitFun({ action, payload }) {
      console.log(payload)
			switch (action) {
				case 'update-collect':
					updateEquipmentPlc(payload).then((response) => {
						this.$modal.msgSuccess('修改成功');
						this.getDataList();
					});
					break;
			}
		},
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					this.listQuery.pageNo = 1;
					this.listQuery.pageSize = 10;
					this.listQuery.name = val.name;
					this.listQuery.plcTableName = val.plcTableName;
					this.getDataList();
					break;
				case 'reset':
					this.$refs.searchBarForm.resetForm();
					this.listQuery = {
						pageSize: 10,
						pageNo: 1,
						total: 1,
					};
					this.getDataList();
					break;
				case 'add':
					this.addOrEditTitle = '新增';
					this.addOrUpdateVisible = true;
					this.addOrUpdateHandle();
					break;
				case 'export':
					this.handleExport();
					break;
				default:
					console.log(val);
			}
		},
	},
};
</script>
