<template>
	<div>
		<a-modal v-model:visible="visible" width="80%" :closable="false" :maskClosable="false" :title="titleText"
			:footer="null">
			<a-form ref="formRef" :model="formState" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
				<a-row>
					<a-col :span="16"><a-form-item label="备案标题" name="title">
							<a-input class="title-box" autocomplete="off" :disabled="showLock" v-model:value="formState.title" />
						</a-form-item></a-col>
					<a-col :span="8">
						<a-form-item label="申报部门" name="sysOrgId">
							<a-select :disabled="showLock" class="select-box" v-model:value="formState.sysOrgId" :size="size"
								placeholder="请选择">
								<a-select-option v-for="item in unitTypeList" :key="item.id" :value="item.id">
									{{ item.name }}
								</a-select-option>
							</a-select>
						</a-form-item>
					</a-col>
				</a-row>
				<a-row>
					<a-col :span="8"><a-form-item label="备案人" name="registrant">
							<a-input autocomplete="off" :disabled="showLock" v-model:value="formState.registrant" />
						</a-form-item></a-col>
					<a-col :span="8"><a-form-item label="联系方式" name="contactInfo">
							<a-input autocomplete="off" :disabled="showLock" v-model:value="formState.contactInfo" />
						</a-form-item></a-col>
					<a-col :span="8">
						<a-form-item label="备案日期" name="registrationDate">
							<a-date-picker :disabled="showLock" v-model:value="formState['registrationDate']"
								value-format="YYYY-MM-DD 00:00:00" />
						</a-form-item>
					</a-col>
				</a-row>

				<a-row>
					<a-col :span="12">
						<a-form-item label="附件" name="attachment">
							<DIYUPLoad :disabled="showLock" listType="picture-card" :change="changeUploadVal" multiple
								v-model:urls="formState.attachment" />
						</a-form-item>
					</a-col>
				</a-row>
			</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 { message } from 'ant-design-vue'
import { spaceCheck, isMobile } from '@/utils/validator'
import global from '@/api/basic.info/global'
import alPolicyFileReportApi from '@/api/policy.info/alPolicyFileReportApi'
import alFileRegistrationApi from '@/api/policy.info/alFileRegistrationApi'
const props = defineProps({
	dataObj: {
		type: Object,
		default: () => { }
	}
})
const formState = ref({
	title: '',
	sysOrgId: '',
	registrant: '',
	contactInfo: '',
	attachment: '',
	registrationDate: new Date()
})
const rules = ref({
	title: [
		{ required: true, message: '请输入标题', trigger: 'blur' },
		{ validator: spaceCheck, trigger: 'blur' }
	],
	sysOrgId: [
		{ required: true, message: '请选择申报部门', trigger: 'change' },
		{ validator: spaceCheck, trigger: 'blur' }
	],
	registrant: [
		{ required: true, message: '请输入申报人', trigger: 'blur' },
		{ validator: spaceCheck, trigger: 'blur' }
	],
	contactInfo: [
		{ required: true, message: '请输入联系方式', trigger: 'blur' },
		{ validator: spaceCheck, trigger: 'blur' },
		{ validator: isMobile, trigger: 'blur' }
	],
	registrationDate: [{ required: true, message: '请选择申报时间', trigger: 'change' }],
	attachment: [{ required: true, message: '请上传文件', trigger: 'blur' }]
})
const unitTypeList = ref([])
const isTypeData = ref('')
const showLock = ref(false)
const visible = ref(false)
const titleText = ref('')
const formRef = ref(null)
const emit = defineEmits(['isVisible'])
// 获取申报部门
const getReportData = () => {
	alPolicyFileReportApi.getReport().then((res) => {
		unitTypeList.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 changeUploadVal = (val) => {
	return global.uploadFile(val).then((reg) => {
		return reg
	})
}
// 新增
const addData = () => {
	formState.value.attachment = formState.value.attachment.join(',')
	alFileRegistrationApi.addInfoForm(formState.value).then((res) => {
		closeData()
	})
}
// 编辑
const editData = () => {
	formState.value.attachment = formState.value.attachment.join(',')
	alFileRegistrationApi.editUnitForm(formState.value).then((res) => {
		closeData()
	})
}
// 用于查看调用
const detailData = (val) => {
	alFileRegistrationApi.unitInfoDetail({ id: val }).then((res) => {
		formState.value = { ...res }
		formState.value.attachment = []
		formState.value.attachment = res.attachment.split(',') || ''
	})
}

const closeData = () => {
	visible.value = false
	emit('isVisible', false)
	formRef.value.resetFields() //resetFields()函数是组件库提供的
	formState.value = {}
}

// 监听点击事件更新数据
watch(
	() => props.dataObj,
	async (newValue, oldvalue) => {
		await nextTick()
		titleText.value = newValue.titleText
		visible.value = newValue.visible
		showLock.value = newValue.showLock
		isTypeData.value = newValue.isType
		getReportData()
		// isType字段用于查看接口
		if (newValue.isType == '2' || newValue.isType == '3') {
			detailData(newValue.id)
		} else {
			// 用于新增时清空表格数据
			formState.value = {}
		}
	},
	{ deep: true, immediate: true }
)
</script>
<style lang="less" scoped>
.ant-input {
	width: 80%;
}

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

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

.title-box {
	width: 91.7%;
}

.ant-picker {
	width: 80%;
}

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

.from-input {
	width: 101.4%;
}

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

.text-box {
	width: 94.6%;
}

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

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