<template>
	<a-card title="四季设置">
		<template #extra><a-button type="primary" @click="save">保存</a-button></template>
		<a-radio-group v-model:value="type" name="radioGroup">
			<a-radio :value="0">自定义</a-radio>
			<a-radio :value="1">两立两至法</a-radio>
			<a-radio :value="2">阳历划分法</a-radio>
			<a-radio disabled :value="3">四立划分法</a-radio>
			<a-radio disabled :value="4">农历划分法</a-radio>
		</a-radio-group>
		<div style="height: 50px"></div>
		<a-form ref="formRef" :model="data" :rules="formRules">
			<a-form-item label="春季">
				<a-row :gutter="20">
					<a-col :span="3">
						<a-form-item name="springStartTime">
							<a-input :disabled="type !== 0" v-model:value="data.springStartTime" placeholder="请输入春季开始时间"></a-input>
						</a-form-item>
					</a-col>
					<a-col :span="1" style="text-align: center">---</a-col>
					<a-col :span="3">
						<a-form-item name="springEndTime">
							<a-input :disabled="type !== 0" v-model:value="data.springEndTime" placeholder="请输入春季结束时间"></a-input>
						</a-form-item>
					</a-col>
				</a-row>
			</a-form-item>
			<a-form-item label="夏季">
				<a-row :gutter="20">
					<a-col :span="3">
						<a-form-item name="summerStartTime">
							<a-input :disabled="type !== 0" v-model:value="data.summerStartTime" placeholder="请输入夏季开始时间"></a-input>
						</a-form-item>
					</a-col>
					<a-col :span="1" style="text-align: center">---</a-col>
					<a-col :span="3">
						<a-form-item name="summerEndTime">
							<a-input :disabled="type !== 0" v-model:value="data.summerEndTime" placeholder="请输入夏季结束时间"></a-input>
						</a-form-item>
					</a-col>
				</a-row>
			</a-form-item>
			<a-form-item label="秋季">
				<a-row :gutter="20">
					<a-col :span="3">
						<a-form-item name="autumnStartTime">
							<a-input :disabled="type !== 0" v-model:value="data.autumnStartTime" placeholder="请输入秋季开始时间"></a-input>
						</a-form-item>
					</a-col>
					<a-col :span="1" style="text-align: center">---</a-col>
					<a-col :span="3">
						<a-form-item name="autumnEndTime">
							<a-input :disabled="type !== 0" v-model:value="data.autumnEndTime" placeholder="请输入秋季结束时间"></a-input>
						</a-form-item>
					</a-col>
				</a-row>
			</a-form-item>
			<a-form-item label="冬季">
				<a-row :gutter="20">
					<a-col :span="3">
						<a-form-item name="winterStartTime">
							<a-input :disabled="type !== 0" v-model:value="data.winterStartTime" placeholder="请输入冬季开始时间"></a-input>
						</a-form-item>
					</a-col>
					<a-col :span="1" style="text-align: center">---</a-col>
					<a-col :span="3">
						<a-form-item name="winterEndTime">
							<a-input :disabled="type !== 0" v-model:value="data.winterEndTime" placeholder="请输入冬季结束时间"></a-input>
						</a-form-item>
					</a-col>
				</a-row>
			</a-form-item>
		</a-form>
	</a-card>
</template>

<script setup name="season">
	import bizSeasonApi from "@/api/biz/bizSeasonApi";
	import { required } from "@/utils/formRules";
	const type = ref(0)
	const data = ref({})
	const emit = defineEmits({ successful: null })
	const formRef = ref()

	let checkDate = async (_rule, value) => {
		var pattern = /^(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/
		if (pattern.test(value)) {
			return Promise.resolve()
		} else {
			return Promise.reject('无效的日期，必须是 xx-xx格式(03-01, 10-31)')
		}
	}
	watch(type, (newVal) => {
		formRef.value.resetFields()
		initCustom()
	})

	const formRules = ref({
		springStartTime: [required('请输入春季开始时间'), { validator: checkDate, trigger: 'blur'}],
		springEndTime: [required('请输入春季结束时间'), { validator: checkDate, trigger: 'blur'}],
		summerStartTime: [required('请输入夏季开始时间'), { validator: checkDate, trigger: 'blur'}],
		summerEndTime: [required('请输入夏季结束时间'), { validator: checkDate, trigger: 'blur'}],
		autumnStartTime: [required('请输入秋季开始时间'), { validator: checkDate, trigger: 'blur'}],
		autumnEndTime: [required('请输入秋季结束时间'), { validator: checkDate, trigger: 'blur'}],
		winterStartTime: [required('请输入冬季开始时间'), { validator: checkDate, trigger: 'blur'}],
		winterEndTime: [required('请输入冬季结束时间'), { validator: checkDate, trigger: 'blur'}]
	})

	const init = async () => {
		await bizSeasonApi.getEnableSeason().then(res => {
			type.value = res
		})
		await initCustom()
	}
	const initCustom = () => {
		let params = {
			seasonType: type.value
		}
		bizSeasonApi.getBaseSeason(params).then(res => {
			data.value = res
		})
	}

	const save = () => {
		if (type.value !== 0) {
			let params = {
				id: data.value.id,
				seasonType: type.value,
				status: 1
			}
			bizSeasonApi.saveBaseSeason(params).then(() => {
				emit('successful')
			})
		} else {
			formRef.value.validate().then(() => {
				let params = {
					id: data.value.id,
					seasonType: type.value,
					status: 1,
					springStartTime: data.value.springStartTime,
					springEndTime: data.value.springEndTime,
					summerStartTime: data.value.summerStartTime,
					summerEndTime: data.value.summerEndTime,
					autumnStartTime: data.value.autumnStartTime,
					autumnEndTime: data.value.autumnEndTime,
					winterStartTime: data.value.winterStartTime,
					winterEndTime: data.value.winterEndTime,
				}
				bizSeasonApi.saveBaseSeason(params).then(() => {
					emit('successful')
				})
			})
		}
	}

	onMounted(() => {
		init()
	})
</script>

<style scoped>

</style>
