<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="getDataList">
			<method-btn
				v-if="tableBtn.length"
				slot="handleBtn"
				:width="90"
				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 changeStatus from './changeStatus.vue';
import { parseTime } from '@/filter/code-filter';
import {
	deleteFactory,
	getFactoryPage,
	exportFactoryExcel,
} from '@/api/core/base/factory';

const tableProps = [
	{
		prop: 'code',
		label: '产品编码',
	},
	{
		prop: 'name',
		label: '产品名称',
	},
	{
		prop: 'address',
		label: '规格型号',
	},
	{
		prop: 'remark',
		label: '产品分类',
	},
	{
		prop: 'remark1',
		label: '创建人',
		width: 90,
	},
	{
		prop: 'createTime',
		label: '时间',
		filter: parseTime,
		width: 150,
	},
	{
		prop: 'enabled',
		label: '状态',
		subcomponent: changeStatus,
		width: 80,
	},
];

export default {
	mixins: [basicPage],
	data() {
		return {
			urlOptions: {
				getDataListURL: getFactoryPage,
				deleteURL: deleteFactory,
				exportURL: exportFactoryExcel,
			},
			tableProps,
			tableBtn: [
				this.$auth.hasPermi(`base:factory:update`)
					? {
							type: 'edit',
							btnName: '编辑',
					  }
					: undefined,
				this.$auth.hasPermi(`base:factory:delete`)
					? {
							type: 'delete',
							btnName: '删除',
					  }
					: undefined,
			].filter((v) => v),
			tableData: [],
			formConfig: [
				{
					type: 'select',
					label: '适用框绞线',
					selectOptions: [],
					param: 'val1',
				},
				{
					type: 'select',
					label: '修改人',
					selectOptions: [],
					param: 'val2',
				},
				{
					type: 'select',
					label: '状态',
					selectOptions: [
						{ id: 1, name: '正常' },
						{ id: 2, name: '停用' },
					],
					param: 'val3',
				},
				{
					type: 'datePicker',
					label: '最后修改时间',
					dateType: 'daterange',
					format: 'yyyy-MM-dd',
					valueFormat: 'yyyy-MM-dd HH:mm:ss',
					rangeSeparator: '-',
					startPlaceholder: '开始时间',
					endPlaceholder: '结束时间',
					param: 'searchTime',
				},
				{
					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: 'separate',
				},
				{
					type: 'button',
					btnName: '导出',
					name: 'export',
					color: 'warning',
				},
			],
		};
	},
	components: {
		AddOrUpdate,
	},
	created() {},
	methods: {
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					this.listQuery.pageNo = 1;
					this.listQuery.pageSize = 10;
					this.listQuery.name = val.name;
					this.listQuery.val1 = val.val1;
					this.listQuery.val2 = val.val2;
					this.listQuery.val3 = val.val3;
					this.listQuery.startTime = val.searchTime ? val.searchTime[0] : null;
					this.listQuery.endTime = val.searchTime ? val.searchTime[1] : null;
					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>
