<template>
  <div>
    <vxe-modal
      :value="isShow"
      show-footer
      title="特殊日期"
      show-confirm-button
      transfer
      show-zoom
      resize
      :z-index="zIndex"
      :width="defaultModalSize.sWidth"
      :height="defaultModalSize.sHeight"
      esc-closable
      :fullscreen="$store.getters.device === 'mobile'"
      @input="e => $emit('update:isShow',e)"
    >
      <el-form ref="elForm" :model="form" :rules="rules" label-position="top" label-width="100">
        <el-form-item label="日期" prop="date" required>
          <el-date-picker v-model="form.date" value-format="yyyy-MM-dd" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type">
            <el-option value="workday" label="工作日" />
            <el-option value="holiday" label="节假日" />
          </el-select>
        </el-form-item>
        <el-form-item label="说明" prop="reason">
          <el-input v-model="form.reason" type="textarea" :autosize="{ minRows: 2 }" />
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button type="primary" :loading="loading" @click="submit">确认</el-button>
      </template>
    </vxe-modal>
  </div>
</template>

<script>
import { save } from '@/api/specialDays'
import {defaultModalSize} from "@/settings";
import { PopupManager } from 'element-ui/lib/utils/popup'

export default {
  props: {
    isShow: {
      type: Boolean,
      default: false,
      required: true
    }
  },
  data() {
    return {
      defaultModalSize,
      zIndex: PopupManager.nextZIndex(),
      loading: false,
      form: {
        date: '',
        type: '',
        reason: ''
      },
      rules: {
        date: [
          { required: true, message: '请选择日期' }
        ],
        type: [
          { required: true, message: '请选择类型' }
        ]
      }
    }
  },
  computed: {},
  methods: {
    submit() {
      this.$refs['elForm'].validate(async valid => {
        if (valid) {
          this.loading = true
          try {
            await save(this.form)
            this.$message.success('新增成功')
            this.$emit('refresh')
            this.$emit('update:isShow', false)
            this.loading = false
            this.$refs['elForm'].resetFields()
          } catch (err) {
            this.loading = false
          }
        }
      })
    }
  },
  watch: {
    isShow(newVal) {
      if (newVal) {
        this.zIndex = PopupManager.nextZIndex()
      }
    }
  }
}
</script>

<style scoped lang="scss">
</style>
