<template>
	<div>
		<a-modal v-model:visible="visible" width="50%" :closable="false" :maskClosable="false" :title="titleText"
			:footer="null">
			<a-form style="margin-left: 16%;" ref="formRef" :label-col="labelCol" :wrapper-col="wrapperCol" :model="formData"
				:rules="formRules">
				<a-form-item label="人员姓名：" name="name">
					<a-input autocomplete="off" v-model:value="formData.name" :disabled="showLock" placeholder="请输入人员姓名"
						allow-clear />
				</a-form-item>
				<a-form-item label="手机号：" name="phone">
					<a-input autocomplete="off" v-model:value="formData.phone" :disabled="showLock" placeholder="请输入手机号"
						allow-clear />
				</a-form-item>
				<a-form-item label="联系地址：" name="address">
					<a-input autocomplete="off" @input="handleInput" :maxlength="maxLength" v-model:value="formData.address"
						:disabled="showLock" placeholder="请输入地址" allow-clear />
				</a-form-item>
				<a-form-item label="所属行政管理部门" name="orgName">
					<a-select class="select-box" :disabled="showLock" v-model:value="formData.orgName" :size="size"
						style="width: 100%" @change="monitoringCahnge" placeholder="请选择">
						<a-select-option v-for="item in auditStatus" :key="item.id" :value="item.name">{{ item.name }}
						</a-select-option>
					</a-select>
				</a-form-item>
				<a-form-item label="站点" name="priceMonitorName">
					<a-select mode="multiple" class="select-box" :disabled="showLock" v-model:value="formData.priceMonitorName"
						:size="size" style="width: 100%" placeholder="请选择">
						<a-select-option v-for="item in siteList" :key="item.id" :value="item.id">
							{{ item.name }}
						</a-select-option>
					</a-select>
					<!-- <a-input v-model:value="formData.priceMonitorName" :disabled="showLock" placeholder="请输入站点" allow-clear /> -->
				</a-form-item>
			</a-form>
			<div class="btn-box">
				<a-button v-if="dataObj.isType !== '3'" style="margin-right: 16px" type="primary" @click="handleOk">保存</a-button>
				<a-button @click="handleCancel">关闭</a-button>
			</div>
		</a-modal>
	</div>
</template>
<script setup>
import { isMobile } from '@/utils/validator'
import { message } from 'ant-design-vue'
import userCollectApi from '@/api/basic.info/userCollectApi'
import unReporCityApi from '@/api/basic.info/unReporCityApi.js'
const props = defineProps({
	dataObj: {
		type: Object,
		default: () => { }
	}
})
const formData = ref({
	name: '',
	phone: '',
	orgName: '',
	priceMonitorName: '',
	address: ''
})
const formRules = ref({
	name: [{ required: true, message: '请输入人员姓名', trigger: 'blur' }],
	phone: [{ required: true, message: '请输入手机号码', trigger: 'blur' },
	{ validator: isMobile, trigger: 'blur' }
	],
	orgName: [{ required: true, message: '请选择所属行政管理部门', trigger: 'change' }],
	priceMonitorName: [{ required: true, message: '请选择所属站点', trigger: 'change' }],
	address: [{ required: true, message: '请输入联系地址', trigger: 'blur' }]
})
const isTypeData = ref('')
const showLock = ref(false)
const visible = ref(false)
const titleText = ref('')
const formRef = ref(null)
const emit = defineEmits(['isVisible'])

const auditStatus = ref([])
const siteList = ref([])
// 所属行政管理部门
const getSearchData = () => {
	unReporCityApi.unReporCityquery().then((res) => {
		auditStatus.value = res
	})
}
const orgId = ref('')
const monitoringCahnge = (val) => {
	auditStatus.value.filter(item => {
		item.name === val ? orgId.value = item.id : ''
	})
	formData.value.priceMonitorName = [] || ''
	siteList.value = []
	monitorList(orgId.value)
}
const monitorList = (val) => {
	userCollectApi.monitoringPoints({
		orgId: val
	}).then((res) => {
		siteList.value = res
	})
}

// 取消按钮
const handleCancel = () => {
	closeData()
}
// 确定按钮
const handleOk = () => {
	formRef.value
		.validate()
		.then(() => {
			// isTypeData === '1' 新增  2是编辑 3是查看
			if (isTypeData.value === '1') return addData()
			if (isTypeData.value === '2') return editData()
		})
		.catch((error) => {
			message.warning('请填写完善')
			console.log('error', error)
		})
}
// 新增
const addData = () => {
	formData.value.priceMonitorName = formData.value.priceMonitorName.join(',')
	userCollectApi.submitAddForm(formData.value).then((res) => {
		closeData()
	})
}
// 编辑
const editData = () => {
	formData.value.priceMonitorName = formData.value.priceMonitorName.join(',')
	userCollectApi.submitEditForm(formData.value).then((res) => {
		closeData()
	})
}
// 用于查看调用
const detailData = (val) => {
	userCollectApi.userCollectDetail({ id: val }).then((res) => {
		formData.value = { ...res }
		formData.value.priceMonitorName = []
		formData.value.priceMonitorName = res.priceMonitorName.split(',')
	})
}

const closeData = () => {
	visible.value = false
	formData.value = {}
	emit('isVisible', false)
	formRef.value.resetFields() //resetFields()函数是组件库提供的
}
// 监听点击事件更新数据
watch(
	() => props.dataObj,
	async (newValue, oldvalue) => {
		await nextTick()
		titleText.value = newValue.titleText
		visible.value = newValue.visible
		showLock.value = newValue.showLock
		isTypeData.value = newValue.isType
		// isType字段用于查看接口
		if (newValue.isType == '2' || newValue.isType == '3') {
			detailData(newValue.id)
		} else {
			// 用于新增时清空表格数据
			formData.value = {}
		}
	},
	{ deep: true, immediate: true }
)
onMounted(() => {
	getSearchData()
});
</script>
<style lang="less" scoped>
.ant-input {
	width: 80%;
}

/* 针对 a-form-item 的 label 应用样式 */
::v-deep .ant-form-item-label {
	width: 140px;
	text-align: right;
}

::v-deep .ant-form-item-control-input {
	width: 60%;
}

.title-box {
	width: 91.7%;
}

.ant-picker {
	width: 92%;
}

.ant-select {
	width: 100% !important;
}

.from-input {
	width: 60%;
}

::v-deep.ant-modal-footer {
	display: none !important;
}

.text-box {
	margin: 0 auto;
	width: 60%;
}

.btn-box {
	text-align: center;
}

.btn-type {
	background-color: #22ce8d;
	color: #fff;
}
</style>
