<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="reporter">
							<a-input autocomplete="off" :disabled="showLock" v-model:value="formState.reporter" />
						</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="reportNumber">
							<a-input autocomplete="off" :disabled="showLock" v-model:value="formState.reportNumber" />
						</a-form-item></a-col>
				</a-row>
				<a-row>
					<a-col :span="8">
						<a-form-item label="申报日期" name="reportDate">
							<a-date-picker :disabled="showLock" v-model:value="formState['reportDate']"
								value-format="YYYY-MM-DD 00:00:00" />
						</a-form-item>
					</a-col>
				</a-row>
				<a-row>
					<a-col :span="24">
						<a-form-item label="申报正文" name="reportContent">
							<a-textarea :disabled="showLock" class="text-box" v-model:value="formState.reportContent" :rows="8"
								placeholder="请输入" />
						</a-form-item>
					</a-col>
				</a-row>
				<a-row>
					<a-col :span="12">
						<a-form-item ref="uploadDataList" label="附件" name="attachment">
							<UPLoadData :beforeUpload="beforeUpload" :disabled="showLock" listType="picture-card" multiple
								v-model:urls="formState.attachment" :change="changeUpload" />
						</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 v-if="dataObj.isType !== '3'" style="margin-right: 16px" class="btn-type" @click="submit">提交</a-button>
				<a-button @click="handleCancel">关闭</a-button>
			</div>
		</a-modal>
	</div>
</template>
<script setup>
import { message } from 'ant-design-vue'
import { isSocialCreditOrIdCode, isMobileData } from '@/utils/validator'
import alPolicyFileReportApi from '@/api/policy.info/alPolicyFileReportApi'
import global from '@/api/basic.info/global'
const props = defineProps({
	dataObj: {
		type: Object,
		default: () => { }
	}
})
const formState = ref({
	title: '',
	sysOrgId: '',
	reporter: '',
	contactInfo: '',
	reportNumber: '',
	reportDate: new Date(),
	reportContent: '',
	attachment: ''
})
const rules = ref({
	title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
	sysOrgId: [{ required: true, message: '请选择申报部门', trigger: 'change' }],
	reporter: [{ required: true, message: '请输入申报人', trigger: 'blur' }],
	contactInfo: [{ required: true, message: '请输入联系方式', trigger: 'blur' },
	{ validator: isMobileData, trigger: 'blur' }
	],
	reportNumber: [{ required: true, message: '请输入申报编号', trigger: 'blur' }],
	reportDate: [{ required: true, message: '请选择申报时间', trigger: 'change' }],
	reportContent: [{ required: true, message: '请输入申报正文', trigger: 'blur' }],
	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 handleCancel = () => {
	visible.value = false
	emit('isVisible', false)
	formRef.value.resetFields() //resetFields()函数是组件库提供的
	formState.value = {}
}

// 确定按钮
const handleOk = () => {
	formRef.value
		.validate()
		.then(() => {
			formState.value.id = props.dataObj.id
			if (Array.isArray(formState.value.attachment)) {
				if (formState.value.attachment[0].url) {
					let arrList = formState.value.attachment
					formState.value.attachment = []
					arrList.forEach(item => {
						formState.value.attachment.push(item.url)
					})
					formState.value.attachment = formState.value.attachment.join(',')
				} else {
					formState.value.attachment = formState.value.attachment.join(',')
				}
			}
			// isTypeData === '1' 新增  2是编辑 3是查看
			if (isTypeData.value === '1') return addData()
			if (isTypeData.value === '2') return editData()
		})
		.catch((error) => {
			message.warning('请填写完善')
			console.log('error', error)
		})
}

// 文件预览方式
function changeUpload(ev) {
	return global.uploadFile(ev).then((reg) => {
		return reg
	})
}
const uploadDataList = ref(null)
function beforeUpload() {
	uploadDataList.value.clearValidate()
}
// 提交事件
const submit = () => {
	formRef.value
		.validate()
		.then(() => {
			formState.value.id = props.dataObj.id
			if (Array.isArray(formState.value.attachment)) {
				if (formState.value.attachment[0].url) {
					let arrList = formState.value.attachment
					formState.value.attachment = []
					arrList.forEach(item => {
						formState.value.attachment.push(item.url)
					})
					formState.value.attachment = formState.value.attachment.join(',')
				} else {
					formState.value.attachment = formState.value.attachment.join(',')
				}
			}
			// isTypeData === '1' 新增  2是编辑 3是查看
			if (isTypeData.value === '1') return submitAdd()
			if (isTypeData.value === '2') return submitEdit()
		})
		.catch((error) => {
			message.warning('请填写完善')
			console.log('error', error)
		})
}
// 提交的新增
const submitAdd = () => {
	formState.value.status = 1
	alPolicyFileReportApi.addInfoForm(formState.value).then((res) => {
		visible.value = false
		emit('isVisible', false)
		formRef.value.resetFields() //resetFields()函数是组件库提供的
		formState.value = {}
	})
}
//提交的修改
const submitEdit = () => {
	formState.value.status = 1
	alPolicyFileReportApi.editUnitForm(formState.value).then((res) => {
		visible.value = false
		emit('isVisible', false)
		formRef.value.resetFields() //resetFields()函数是组件库提供的
		formState.value = {}
	})
}
// 新增
const addData = () => {
	formState.value.status = 0
	alPolicyFileReportApi.addInfoForm(formState.value).then((res) => {
		visible.value = false
		emit('isVisible', false)
		formRef.value.resetFields() //resetFields()函数是组件库提供的
		formState.value = {}
	})
}
// 编辑
const editData = () => {
	alPolicyFileReportApi.editUnitForm(formState.value).then((res) => {
		visible.value = false
		emit('isVisible', false)
		formRef.value.resetFields() //resetFields()函数是组件库提供的
		formState.value = {}
	})
}
// 用于查看调用
const detailData = (id) => {
	alPolicyFileReportApi.unitInfoDetail({ id: id }).then((res) => {
		formState.value = { ...res }
		if (res.attachment) {
			formState.value.attachment = []
			formState.value.attachment = res.attachment.split(',') || ''
		}
	})
}
// 获取申报部门
const getReportData = () => {
	alPolicyFileReportApi.getReport().then((res) => {
		console.log(res)
		unitTypeList.value = res
	})
}
// 监听点击事件更新数据
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') {
			formState.value.id = newValue.id
			formState.value.status = newValue.status
			detailData(newValue.id)
		} else {
			// 用于新增时清空表格数据
			formState.value = {}
		}
	},
	{ deep: true, immediate: true }
)
onMounted(() => {
	getReportData()
})
</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>
