<template>
  <el-form ref="form" :rules="rules" label-width="110px" :model="form">
    <el-row>
      <el-col :span="12">
        <el-form-item label="班次名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" disabled></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="生效时间" prop="enableTime">
          <el-date-picker
          v-model="form.enableTime"
          type="datetime"
          placeholder="选择日期时间"
          label-format="yyyy-MM-dd HH:mm:ss"
          value-format="timestamp"
          style="width: 100%;">
        </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="失效时间" prop="disableTime">
          <el-date-picker
            v-model="form.disableTime"
            type="datetime"
            placeholder="选择日期时间"
            label-format="yyyy-MM-dd HH:mm:ss"
            value-format="timestamp"
            style="width: 100%;">
          </el-date-picker>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="班次开始时间" prop="startTime">
          <el-time-picker
            v-model="form.startTime"
            format='HH:mm'
            value-format='HH:mm'
            style="width: 100%;"
            @change="timeFun('start')"
          >
          </el-time-picker>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="班次结束时间" prop="endTime">
          <el-time-picker
            v-model="form.endTime"
            format='HH:mm'
            value-format='HH:mm'
            style="width: 100%;"
            @change="timeFun('end')"
          >
          </el-time-picker>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="是否跨天" prop="daySpan">
          <el-select v-model="form.daySpan" placeholder="请选择" disabled style="width: 100%;">
            <el-option label="否" :value= '0' ></el-option>
            <el-option label="是" :value= '1' ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import { getGroupClasses, updateGroupClasses, createGroupClasses, getCode } from '@/api/base/groupClasses'
export default {
  name: 'groupClassAdd',
  data() {
    return {
      form: {
        id: '',
        name: '',
        code: '',
        enableTime: '',
        disableTime: '',
        startTime: '',
        endTime: '',
        daySpan: '',
        remark: ''
      },
      isEdit: false, //是否是编辑
      rules: {
        name: [{ required: true, message: '请输入班组名称', trigger: 'blur' }],
        enableTime: [{ required: true, message: '请选择班次开始时间', trigger: 'change' }],
        code: [{ required: true, message: '请输入编码', trigger: 'blur' }],
        startTime: [{ required: true, message: '请输入生效时间', trigger: 'change' }],
        endTime: [{ required: true, message: '请选择班次结束时间', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(id) {
      if (id) {
        this.isEdit = true
        this.form.id = id
        getGroupClasses(id).then((res) => {
          if (res.code === 0) {
            this.form = res.data
            this.form.disableTime = res.data.disableTime || ''
          }
        })
      } else {
        this.isEdit = false
        this.form.id = ''
        getCode().then((res) => {
          this.form.code = res.data
        })
      }
    },
    timeFun(val) {
      if (this.form.startTime && this.form.endTime) {
        if (this.form.startTime > this.form.endTime) {
          this.form.daySpan = 1
        } else if (this.form.startTime < this.form.endTime) {
          this.form.daySpan = 0
        } else {
          if (val === 'start') {
            this.form.startTime = ''
          } else {
            this.form.endTime = ''
          }
          this.$modal.msgWarning('班次开始时间和结束时间不能相同')
        }
      }
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.disableTime = this.form.disableTime || ''
          if (this.isEdit) {
            //编辑
            updateGroupClasses({ ...this.form }).then((res) => {
              if (res.code === 0) {
                this.$modal.msgSuccess("操作成功");
                this.$emit('successSubmit')
              }
            })
          } else {
            createGroupClasses({ ...this.form }).then((res) => {
              if (res.code === 0) {
                this.$modal.msgSuccess("操作成功");
                this.$emit('successSubmit')
              }
            })
          }
        } else {
          return false
        }
      })
    },
    formClear() {
      this.$refs.form.resetFields()
      this.isEdit = false
    }
  }
}
</script>
