<template>
	<div>
		<a-modal v-model:visible="visible" width="50%" :closable="false" :maskClosable="false" :title="titleText"
			:footer="null">
			<a-form style="margin-left: 8%;" ref="formRef" :label-col="labelCol" :wrapper-col="wrapperCol" :model="formData"
				:rules="formRules">
				<a-form-item label="报送周期；周一：1.....周日：7；：" name="dayOfWeek">
					<a-select class="select-box" :disabled="showLock" v-model:value="formData.dayOfWeek" :size="size"
						style="width: 90%" placeholder="请选择">
						<a-select-option v-for="item in cycleList" :key="item.dayOfWeek" :value="item.dayOfWeek">{{ item.name }}
						</a-select-option>
					</a-select>
				</a-form-item>
				<a-form-item label="报送开始时间：" name="startTime">
					<a-time-picker :disabled="showLock" v-model:value="formData.startTime" value-format="HH:mm:ss"
						placeholder="请选择报送开始时间" style="width: 90%" />
				</a-form-item>
				<a-form-item label="报送结束时间：" name="endTime">
					<a-time-picker :disabled="showLock" v-model:value="formData.endTime" value-format="HH:mm:ss"
						placeholder="请选择报送结束时间" style="width: 90%" />
				</a-form-item>
				<a-form-item label="提前提醒；提前小时数：" name="advanceHour">
					<a-select class="select-box" :disabled="showLock" v-model:value="formData.advanceHour" :size="size"
						style="width: 90%" placeholder="请选择">
						<a-select-option v-for="item in advanceHoursList" :key="item.advanceHour" :value="item.advanceHour">{{
							item.name }}
						</a-select-option>
					</a-select>
				</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 remindCycleApi from '@/api/basic.info/remindCycleApi'
const props = defineProps({
	dataObj: {
		type: Object,
		default: () => { }
	}
})
const cycleList = ref([
	{
		name: '周一',
		dayOfWeek: 1
	},
	{
		name: '周二',
		dayOfWeek: 2
	},
	{
		name: '周三',
		dayOfWeek: 3
	},
	{
		name: '周四',
		dayOfWeek: 4
	},
	{
		name: '周五',
		dayOfWeek: 5
	},
	{
		name: '周六',
		dayOfWeek: 6
	},
	{
		name: '周天',
		dayOfWeek: 7
	},
])
const advanceHoursList = ref([
	{
		name: '提前2小时',
		advanceHour: 1
	},
	{
		name: '提前4小时',
		advanceHour: 4
	},
	{
		name: '提前6小时',
		advanceHour: 6
	},
	{
		name: '提前8小时',
		advanceHour: 8
	},
	{
		name: '提前10小时',
		advanceHour: 10
	},
	{
		name: '提前12小时',
		advanceHour: 12
	}
])
const formData = ref({})
const formRules = ref({
	dayOfWeek: [{ required: true, message: '请选择报送周期', trigger: 'change' }],
	cityAddress: [{ required: true, message: '请输入所属市州', trigger: 'change' }],
	sysOrgId: [{ required: true, message: '请选择所属行政管理部门', trigger: 'change' }],
	advanceHour: [{ required: true, message: '请选择提前提醒', trigger: 'change' }],
})
const isTypeData = ref('')
const showLock = ref(false)
const visible = ref(false)
const titleText = ref('')
const formRef = ref(null)
const emit = defineEmits(['isVisible'])


// 取消按钮
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 = () => {
	remindCycleApi.remindCycleAdd(formData.value).then((res) => {
		closeData()
	})
}
// 编辑
const editData = () => {
	formData.value.id = props.dataObj.id
	remindCycleApi.remindCycleEdit(formData.value).then((res) => {
		closeData()
	})
}
// 用于查看调用
const detailData = (val) => {
	remindCycleApi.remindCycleDetail({ id: val }).then((res) => {
		formData.value = {}
		formData.value = { ...res }
	})
}

const closeData = () => {
	visible.value = false
	emit('isVisible', false)
	formRef.value.resetFields() //resetFields()函数是组件库提供的
	formData.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
		// isType字段用于查看接口
		if (newValue.isType == '2' || newValue.isType == '3') {
			detailData(newValue.id)
		} else {
			// 用于新增时清空表格数据
			formData.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: 230px;
	text-align: right;
}

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

.title-box {
	width: 91.7%;
}

.ant-picker {
	width: 92%;
}

.ant-select {
	width: 90% !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>
