<!--
 * @Author: zwq
 * @Date: 2021-11-18 14:16:25
 * @LastEditors: zwq
 * @LastEditTime: 2025-11-04 14:08:47
 * @Description:
-->
<template>
	<el-form
		:model="dataForm"
		:rules="dataRule"
		ref="dataForm"
		@keyup.enter.native="dataFormSubmit()"
		label-position="top"
		label-width="80px">
		<el-row :gutter="20">
			<el-col :span="12">
				<el-form-item label="排序" prop="sort">
					<el-input-number
						style="width: 100%"
						v-model="dataForm.sort"
						:step="1"
						:min="1"
						step-strictly />
				</el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item label="班次名称" prop="name">
					<el-input
						v-model="dataForm.name"
						clearable
						placeholder="请输入班次名称" />
				</el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item label="开始时间" prop="startTime">
					<el-time-picker
						style="width: 100%"
						format="H:mm"
						value-format="HH:mm:ss"
						v-model="dataForm.startTime"
            @change="timeFun('start')"
						placeholder="选择时间"></el-time-picker>
				</el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item label="结束时间" prop="endTime">
					<el-time-picker
						style="width: 100%"
						format="H:mm"
						value-format="HH:mm:ss"
						v-model="dataForm.endTime"
            @change="timeFun('end')"
						placeholder="选择时间"></el-time-picker>
				</el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item label="是否跨天" prop="daySpan">
					<el-switch
						v-model="dataForm.daySpan"
            disabled
						:active-value="1"
						:inactive-value="0"
						active-text="隔天"
						inactive-text="当天"></el-switch>
				</el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item label="备注" prop="remark">
					<el-input
						v-model="dataForm.remark"
						clearable
						placeholder="请输入备注" />
				</el-form-item>
			</el-col>
		</el-row>
	</el-form>
</template>

<script>
export default {
	components: {},
	data() {
		return {
			dataForm: {
				id: undefined,
				sort: undefined,
				name: undefined,
				startTime: undefined,
				endTime: undefined,
				daySpan: 0,
				code: 1,
				remark: undefined,
			},
			_pageIndex: 1,
			dataRule: {
				sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
				name: [
					{ required: true, message: '班次名称不能为空', trigger: 'blur' },
				],
				startTime: [
					{ required: true, message: '开始时间不能为空', trigger: 'change' },
				],
				endTime: [
					{ required: true, message: '结束时间不能为空', trigger: 'change' },
				],
			},
		};
	},
	methods: {
		init(val) {
			this._pageIndex = val._pageIndex - 1;
			this.dataForm.sort = val._pageIndex || 1;
			this.dataForm.name = val.name || undefined;
			this.dataForm.startTime = val.startTime || undefined;
			this.dataForm.endTime = val.endTime || undefined;
			this.dataForm.remark = val.remark || undefined;
			this.dataForm.daySpan = val.daySpan || 0;
			this.dataForm.code = val.code || 1;
		},
    timeFun(val) {
      if (this.dataForm.startTime && this.dataForm.endTime) {
        if (this.dataForm.startTime > this.dataForm.endTime) {
          this.dataForm.daySpan = 1
        } else if (this.dataForm.startTime < this.dataForm.endTime) {
          this.dataForm.daySpan = 0
        } else {
          if (val === 'start') {
            this.dataForm.startTime = ''
          } else {
            this.dataForm.endTime = ''
          }
          this.$modal.msgWarning('班次开始时间和结束时间不能相同')
        }
      }
    },
		// 表单提交
		dataFormSubmit() {
			this.$refs['dataForm'].validate((valid) => {
				if (!valid) {
					return false;
				}
				this.$emit('refreshTableData', this._pageIndex, this.dataForm);
			});
		},
	},
};
</script>
