<template>
	<a-card>
		<a-form>
			<a-row :gutter="16">
				<a-col :span="4">
					<a-form-item label="关键字">
						<a-input v-model:value="name" placeholder="请输入姓名或手机号"></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="工作岗位">
						<a-select v-model:value="job">
							<a-select-option :value="1">场长</a-select-option>
							<a-select-option :value="2">紧急入场管理员</a-select-option>
							<a-select-option :value="3">生物安全负责人</a-select-option>
							<a-select-option :value="4">PCR检测人员</a-select-option>
							<a-select-option :value="5">洗消站点工作人员</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="工作地点">
						<a-select v-model:value="workLocationType">
							<a-select-option v-for="item in locationList" :key="item.id" :value="item.locationType">{{item.locationName}}</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-space>
						<a-button type="primary" @click="search">查询</a-button>
						<a-button @click="reset">重置</a-button>
<!--						<a-button type="primary" @click="derive">导出</a-button>-->
					</a-space>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:expand-row-by-click="true"
			:alert="options.alert.show"
			bordered
			: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 :selectedRowKeys="selectedRowKeys" @batchDelete="deleteBatchAccount" />
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'status'">
					<a-tag v-if="record.status" color="#87d068">在线</a-tag>
					<a-tag v-else color="red">离线</a-tag>
				</template>
				<template v-if="column.dataIndex === 'job'">
					<span>{{getJobName(record.jobs)}}</span>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a @click="formRef.onOpen(record, searchFormState)">编辑</a>
					<a-divider type="vertical" />
					<a-popconfirm title="确定删除吗？" @confirm="deleteAccount(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)"></Form>
</template>

<script setup name="washAccount">
	import washEntranceApi from "@/api/wash/washEntranceApi";
	import Form from './form.vue'
	const name = ref('')
	const job = ref('')
	const workLocationType = ref('')
	const locationList = ref([])

	const search = () => {
		table.value.refresh(true)
	}
	const reset = () => {
		name.value = ''
		job.value = ''
		workLocationType.value = ''
		table.value.refresh(true)
	}
	const derive = () => {}

	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: 'userId',
			width: 150,
			fixed: true
		},
		{
			title: '员工姓名',
			dataIndex: 'userName',
			width: 200,
			resizable: true,
			ellipsis: true
		},
		{
			title: '工作岗位',
			dataIndex: 'job',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '工作地点',
			dataIndex: 'workLocation',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '小程序账号',
			dataIndex: 'account',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '小程序密码',
			dataIndex: 'password',
			width: 150,
			resizable: true,
			ellipsis: true,
			sorter: true
		},
		{
			title: '操作',
			dataIndex: 'action',
			width: 200,
			fixed: 'right'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['basePigpenEdit', 'basePigpenDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '150px'
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}

	const loadData = async (parameter) => {
		let params = {
			job: job.value,
			workLocationType: workLocationType.value,
			searchStr: name.value
		}
		return washEntranceApi.getAccountPage(Object.assign(parameter, params)).then((res) => {
			return res
		})
	}

	const deleteBatchAccount = (list) => {
		let arr = [];
		list.forEach(item => {
			arr.push(item.id)
		})
		let params = {
			ids: arr.join(',')
		}
		washEntranceApi.delAccountPage(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}

	const deleteAccount = (record) => {
		let params = {
			ids: record.id
		}
		washEntranceApi.delAccountPage(params).then(() => {
			table.value.refresh(true)
		})
	}

	const getJobName = (val) => {
		let arr = [
			{ id: 1, name: '场长' },
			{ id: 2, name: '紧急入场管理员' },
			{ id: 3, name: '生物安全负责人' },
			{ id: 4, name: 'PCR检测人员' },
			{ id: 5, name: '洗消站点工作人员' }
		]
		let ids = val.split(',').map(Number)
		let sub = []
		arr.forEach(item => {
			ids.forEach(id => {
				if(item.id === id) {
					sub.push(item.name)
				}
			})
		})
		return sub.join(',')

	}

	const initLocation = () => {
		washEntranceApi.getPersonLocation().then(res => {
			locationList.value = res
		})
	}
	onMounted(() => {
		initLocation()
	})
</script>

<style scoped>

</style>
