<template>
	<a-card :bordered="false" style="margin-bottom: 10px">
		<a-form ref="searchFormRef" name="advanced_search" class="ant-advanced-search-form" :model="searchFormState">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item name="nickName" label="工号">
						<a-input v-model:value="searchFormState.nickName" placeholder="请输入工号" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item name="userName" label="姓名">
						<a-input v-model:value="searchFormState.userName" placeholder="请输入姓名"></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="8">
					<a-space :size="10">
						<a-button type="primary" @click="table.refresh(true)">
							<template #icon><SearchOutlined /></template>
							查询
						</a-button>
						<a-button @click="reset">重置</a-button>
					</a-space>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:expand-row-by-click="true"
			:alert="options.alert.show"
			bordered
			:tool-config="toolConfig"
			:row-key="(record) => record.id"
			:row-selection="options.rowSelection"
			@resizeColumn="(w, col) => {
				col.width = w
			}"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen(undefined, searchFormState)">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-delete v-if="hasPerm('bizBatchHouseDelete')" :selectedRowKeys="selectedRowKeys" @batchDelete="deleteBatchStaff" />
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'sex'">
					<a-tag v-if="record.sex > 0" color="#2db7f5">男性</a-tag>
					<a-tag v-else color="red">女性</a-tag>
				</template>
				<template v-if="column.dataIndex === 'imgUrl'">
					<div v-viewer>
						<img v-if="record.imgUrl" class="record-img" :src="record.imgUrl" alt="/">
						<span v-else>暂无图片</span>
					</div>
				</template>
				<template v-if="column.dataIndex === 'workStatus'">
					<a-tag v-if="record.workStatus === 0" color="#2db7f5">在职</a-tag>
					<a-tag v-else color="red">离职</a-tag>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a @click="formRef.onOpen(record, searchFormState)">编辑</a>
					<a-divider type="vertical"/>
					<a-popconfirm title="删除这个员工吗？" @confirm="deleteStaff(record)">
						<a-button type="link" danger size="small">删除</a-button>
					</a-popconfirm>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="table.refresh(true)" />
</template>

<script setup name="staff">
	import Form from './form.vue'
	import bizStaffApi from "@/api/biz/bizStaffApi"
	const table = ref()
	const formRef = ref()
	const giveRef = ref()
	const searchFormRef = ref()
	const searchFormState = ref({})
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const show = ref(true)
	const columns = [
		{
			title: '姓名',
			dataIndex: 'userName',
			width: 100,
			resizable: true,
			ellipsis: true,
			fixed: true
		},
		{
			title: '工号',
			dataIndex: 'nickName',
			width: 100,
			resizable: true,
			ellipsis: true
		},
		{
			title: '员工照片',
			dataIndex: 'imgUrl',
			width: 100
		},
		{
			title: '性别',
			dataIndex: 'sex',
			resizable: true,
			width: 100,
			ellipsis: true
		},
		{
			title: '身份证',
			dataIndex: 'idCard',
			resizable: true,
			width: 200,
			ellipsis: true
		},
		{
			title: '员工电话',
			dataIndex: 'phone',
			resizable: true,
			width: 150,
			ellipsis: true
		},
		{
			title: '紧急联系人电话',
			dataIndex: 'publicPhone',
			resizable: true,
			width: 150,
			ellipsis: true
		},
		{
			title: '省市区',
			dataIndex: 'province',
			resizable: true,
			width: 200,
			ellipsis: true
		},
		{
			title: '详细地址',
			dataIndex: 'address',
			resizable: true,
			width: 200,
			ellipsis: true
		},
		{
			title: '入职时间',
			dataIndex: 'entryDate',
			resizable: true,
			width: 200,
			ellipsis: true
		},
		{
			title: '员工状态',
			dataIndex: 'workStatus',
			width: 100,
			resizable: true,
			ellipsis: true
		},
		{
			title: '工作岗位',
			dataIndex: 'job',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '部门',
			dataIndex: 'departmentName',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '工作地点',
			dataIndex: 'workLocation',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '操作',
			dataIndex: 'action',
			width: 200,
			fixed: 'right'
		}
	]

	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}

	const reset = () => {
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}

	const loadData = async (parameter) => {
		return bizStaffApi.getStaffPage(Object.assign(parameter, searchFormState.value)).then(data => {
			return data
		})
	}

	const deleteBatchStaff = (params) => {
		bizStaffApi.delStaff(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}

	const deleteStaff = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizStaffApi.delStaff(params).then(() => {
			table.value.refresh(true)
		})
	}
</script>

<style scoped>
.record-img {
	width: 40px;
	height: 40px;
}
</style>
