<template>
	<a-card style="margin-bottom: 10px">
		<a-row :gutter="16">
			<a-col :span="12">
				<a-card title="性别年龄分布">
					<div class="chart_content">
						<chart-age :id="0" :list="ageChart"></chart-age>
					</div>
				</a-card>
			</a-col>
			<a-col :span="12">
				<a-card title="岗位分布">
					<div class="chart_content">
						<chart-post :id="0" :list="postChart"></chart-post>
					</div>
				</a-card>
			</a-col>
		</a-row>
	</a-card>
	<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="gender">
							<a-select-option :value="0">男性</a-select-option>
							<a-select-option :value="1">女性</a-select-option>
						</a-select>
					</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.locationName" :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="table.refresh(true)">查询</a-button>
						<a-button @click="reset">重置</a-button>
<!--						<a-button type="primary">导出</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="deleteAll" />
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'gender'">
					<a-tag :color="record.gender === 0 ? '#2db7f5' : '#f50'"> {{record.gender == 0 ? '男性' : '女性'}}</a-tag>
				</template>
				<template v-if="column.dataIndex === 'job'">
					<span>{{getJobName(record.job)}}</span>
				</template>
				<template v-if="column.dataIndex === 'img'">
					<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 === 'action'">
					<a @click="open(record)">修改照片</a>
					<a-divider type="vertical" />
					<a @click="formRef.onOpen(record)">编辑</a>
					<a-divider type="vertical" />
					<a-popconfirm title="删除此消息吗？" @confirm="del(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)" />
	<xn-form-container
		title="修改照片"
		:width="800"
		:visible="showModal">
		<x-img-upload @onImgChange="onImgChange"></x-img-upload>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="washPerson">
	import Form from './form.vue'
	import chartAge from '../chart/chartAge.vue'
	import chartPost from '../chart/chartPost.vue'
	import tool from "@/utils/tool"
	import XImgUpload from "@/components/XnUpload/XImgUpload.vue"
	import washEntranceApi from "@/api/wash/washEntranceApi"
	import { message } from "ant-design-vue"

	const formRef = ref()
	const ageChart = ref([])
	const postChart = ref([])

	const name = ref('')
	const gender = ref('')
	const job = ref('')
	const workLocationType = ref('')
	const locationList = ref([])

	const table = 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: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '手机号',
			dataIndex: 'userPhone',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '性别',
			dataIndex: 'gender',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '工作岗位',
			dataIndex: 'job',
			width: 150,
			resizable: true,
			ellipsis: true
		},
		{
			title: '工作地点',
			dataIndex: 'workLocation',
			width: 150,
			resizable: true,
			ellipsis: true,
			sorter: true
		},
		{
			title: '员工照片',
			dataIndex: 'img',
			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 loadData = async (parameter) => {
		let params = {
			searchStr: name.value,
			gender: gender.value,
			job: job.value,
			workLocation: workLocationType.value
		}
		return washEntranceApi.getPersonPage(Object.assign(parameter, params)).then((res) => {
			return res
		})
	}

	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 reset = () => {
		name.value = ''
		gender.value = ''
		job.value = ''
		workLocationType.value = ''
		table.value.refresh(true)
	}

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

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

	const initLocation = () => {
		washEntranceApi.getPersonLocation().then(res => {
			locationList.value = res
		})
	}

	const showModal = ref(false)
	const submitLoading = ref(false)
	const selectId = ref('')
	// 照片
	const imgList = ref([])
	const onImgChange = (item) => {
		imgList.value = item
	}

	const onClose = () => {
		showModal.value = false
		imgList.value = []
		selectId.value = ''
	}

	const open = (row) => {
		showModal.value = true
		selectId.value = row.id
	}

	const onSubmit = () => {
		if (imgList.value.length < 0) {
			message.error('请先上传照片')
			return
		} else {
			submitLoading.value = true
			let formData = new FormData()
			formData.append('id', selectId.value)
			formData.append('imgUrl', imgList.value[0])
			formData.append('farmId', tool.data.get('SNOWY_ORG_ID'))
			washEntranceApi.savePersonImg(formData).then(() => {
				message.success('修改成功')
				onClose()
				table.value.refresh(true)
			}).finally(() => {
				submitLoading.value = false
			})
		}
	}
	const initAgeChart = () => {
		washEntranceApi.getPersonAgeChart({}).then(res => {
			ageChart.value = res
		})
	}

	const initPostChart = () => {
		washEntranceApi.getPersonPostChart({}).then(res => {
			postChart.value = res
		})
	}

	onMounted(() => {
		initLocation()
		initAgeChart()
		initPostChart()
	})
</script>

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