<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="specialDayForm" label-well-code="right" label-width="100px">
      <el-row type="flex" justify="center">
        <el-col :span="18">
          <el-form-item label="名称" prop="name">
            <el-input v-model.trim="specialDayForm.name" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="18">
          <el-form-item label="开始日期" prop="beginDate">
            <el-date-picker
              v-model="specialDayForm.beginDate"
              :picker-options="beginDateOptions"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="开始日期"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="18">
          <el-form-item label="结束日期" prop="endDate">
            <el-date-picker
              v-model="specialDayForm.endDate"
              :picker-options="endDateOptions"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="结束日期"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="18">
          <el-form-item label="是否考勤" prop="isKaoqin">
            <el-radio v-model="specialDayForm.isKaoqin" label="1">是</el-radio>
            <el-radio v-model="specialDayForm.isKaoqin" label="0">否</el-radio>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveData">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addSpecialDay, updateSpecialDay } from '@/api/attendance'

export default {
  name: 'EditSpecialDay',
  data() {
    return {
      beginDateOptions: {
        disabledDate: this.disabledBeginDate
      },
      endDateOptions: {
        disabledDate: this.disabledEndDate
      },
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      specialDayForm: {
        id: '',
        name: '',
        beginDate: '',
        endDate: '',
        isKaoqin: ''
      }, // 表单
      textMap: {
        update: '编辑',
        create: '新增'
      }, // 表头显示标题
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: ['blur', 'change'] }],
        beginDate: [{ required: true, message: '请选择开始日期', trigger: ['blur', 'change'] }],
        endDate: [{ required: true, message: '请选择结束日期', trigger: ['blur', 'change'] }],
        isKaoqin: [{ required: true, message: '请选择是否考勤', trigger: ['blur'] }]
      }, // 前端校验规则
      isEditMode: false
    }
  },
  watch: { },
  mounted() {
    // this.fetchBusinessType()
    // this.fetchDeviceModel()
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      if (dialogStatus === 'create') { // 如果是新增，清除验证
        this.resetForm()
        this.isEditMode = false
      } else if (dialogStatus === 'update') { // 如果是修改，将row中数据填写到输入框中
        this.specialDayForm = {
          name: row.name,
          beginDate: row.beginDate,
          endDate: row.endDate,
          isKaoqin: row.isKaoqin,
          id: row.id
        }
        this.isEditMode = true
      }
    },
    disabledBeginDate(time) {
      if (this.specialDayForm.endDate !== '') {
        return time.getTime() > new Date(this.specialDayForm.endDate)
      }
    },
    disabledEndDate(time) {
      if (this.specialDayForm.beginDate !== '') {
        return time.getTime() < new Date(this.specialDayForm.beginDate + ' 00:00:00')
      }
    },
    // 重置表单
    resetForm() {
      this.specialDayForm = {
        name: '',
        beginDate: '',
        endDate: '',
        isKaoqin: ''
      }
    },
    // 保存数据
    saveData: function() {
      console.log(this.dialogStatus)
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {

          addSpecialDay(this.specialDayForm).then(response => {
            if (response.code === 200) {
              this.$confirm('新增成功，是否继续新增?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'info'
              }).then(() => {
                this.resetForm()
                this.$nextTick(() => {
                  this.$refs['dataForm'].clearValidate()
                })
              }).catch(() => {
                this.$emit('watchChild')
                this.dialogFormVisible = false
              })
            }
          })
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          console.log(this.specialDayForm.beginDate)
          updateSpecialDay(this.specialDayForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            } else {
              this.$message.error(response.message)
            }
          })
        }
      })
    },
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select{
    width: 100%;
  }
  .el-date-editor{
    width: 100%;
  }
</style>
