<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">
			<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 '../../core/mixins/basic-page';
import { parseTime } from '../../core/mixins/code-filter';
import {
	getCoreWorkerPage,
	deleteCoreWorker
} from '@/api/base/coreWorker';
import moment from 'moment';

const tableProps = [
	{
		prop: 'createTime',
		label: '创建时间',
		filter: parseTime
	},
	{
		prop: 'name',
		label: '姓名'
	},
	{
		prop: 'code',
		label: '员工号'
	},
	{
		prop: 'sex',
		label: '性别',
		filter: (val) => ['男', '女'][val]
	},
	{
		prop: 'entryTime',
		label: '入职时间',
		filter: (val) => val ? moment(val).format('yyyy-MM-DD') : '',
	},
	{
		prop: 'telephone',
		label: '联系电话'
	},
	{
		prop: 'status',
		label: '状态',
		filter: (val) => ['', '在职', '离职'][val]
	},
	{
		prop: 'departmentName',
		label: '部门'
	},
	{
		prop: 'majorName',
		label: '专业'
	},
	{
		prop: 'remark',
		label: '备注'
	},
];

export default {
	mixins: [basicPage],
	data() {
		return {
			urlOptions: {
				getDataListURL: getCoreWorkerPage,
				deleteURL: deleteCoreWorker
			},
			tableProps,
			tableBtn: [
				this.$auth.hasPermi(`base:core-worker:update`)
					? {
							type: 'edit',
							btnName: '编辑',
					  }
					: undefined,
          this.$auth.hasPermi(`base:core-worker:delete`)
					? {
							type: 'delete',
							btnName: '删除',
					  }
					: undefined,
			].filter((v)=>v),
			tableData: [],
			formConfig: [
				{
					type: 'input',
					label: '员工姓名',
					placeholder: '员工姓名',
					param: 'name',
				},
				{
					type: 'select',
					label: '状态',
					placeholder: '请选择状态',
					param: 'status',
					selectOptions: [
						{ id: 1, name: '在职' },
						{ id: 2, name: '离职' }
					],
					filterable: true
				},
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				{
					type: 'separate',
				},
				{
					type: this.$auth.hasPermi('base:core-worker:create') ? 'button' : '',
					btnName: '新增',
					name: 'add',
					color: 'success',
					plain: true
				},
			],
		};
	},
	components: {
		AddOrUpdate,
	},
	created() {},
	methods: {
		// 获取数据列表
    // getDataList() {
    //   this.dataListLoading = true;
    //   this.urlOptions.getDataListURL(this.listQuery).then(response => {
    //     this.tableData = response.data.list;
    //     this.listQuery.total = response.data.total;
    //     this.dataListLoading = false;
    //   });
    // },
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					this.listQuery.pageNo = 1;
					this.listQuery.pageSize = 10;
					this.listQuery.name = val.name ? val.name : undefined;
					this.listQuery.status = val.status ? val.status : undefined;
					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>
