<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="年度">
          <a-input placeholder="请输入年度" v-decorator="['yearName', validatorRules.yearName]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="年假生效日期">
          <a-date-picker format="YYYY-MM-DD" v-decorator="['beginDate', validatorRules.beginDate]"/>
          <!-- <a-input placeholder="请输入年假生效日期" v-decorator="['beginDate', validatorRules.beginDate]"/> -->
        </a-form-item>
      
        <a-form-item
         :labelCol="labelCol"
         :wrapperCol="wrapperCol"
         label="年假失效日期">
         <a-date-picker format="YYYY-MM-DD" v-decorator="['endDate', validatorRules.endDate]"/>
         <!-- <a-input placeholder="请输入年假失效日期" v-decorator="['endDate', validatorRules.endDate]"/> -->
        </a-form-item>
        
        <a-form-item
         :labelCol="labelCol"
         :wrapperCol="wrapperCol"
         label="备注">
         <a-input placeholder="请输入备注" v-decorator="['demo', validatorRules.demo]"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
  
</template>

<script>
  import moment from "moment"
  import pick from 'lodash.pick'
  import { httpAction } from '@/api/manage'
  import { duplicateCheck } from '@/api/api'
  import JDictSelectTag from '@/components/dict/JDictSelectTag'
  // import departWindow from './DepartWindow'
import { queryIdTree } from '@/api/api'
  let validatorCodeTimer = null

  export default {
    name: 'BdAnnualLeaveModal',
    components: { 
    JDictSelectTag,
    // departWindow
    },
    data() {
      return {
        departTree:[],
        checkedDepartKeys:[],
        selectDeptKeys:[],
        visibles:false,
        title: '操作',
        visible: false,
        deptname:"",
        deptid:"",
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          yearName: {
            rules: [
              {required: true, message: '请输入年度' },
              {
                validator: this.validateName
              }
            ]
          },
          beginDate: { rules: [{ required: true, message: '请输入年假生效日期' }] },
          endDate: { rules: [{ required: true, message: '请输入年假失效日期' }] },
        },
        url: {
          add: '/schedulingInformation/bdAnnualLeave/add',
          edit: '/schedulingInformation/bdAnnualLeave/edit',
        },
        readOnly:false
      }
    },
    created() {
      
    },
    methods: {
      validateName(rule, value, callback) {
        if (!value) {
          callback()
        } else {
          
          if (value.length != 4) {
            callback("请输入正确格式的年度");
          }
          if (!RegExp(/^[0-9]*$/).test(value)) {
           callback("请输入正确格式的年度");
          }
          callback()
        }
      },
      add() {
        this.edit({})
      },
      edit(record) {
        this.form.resetFields()
        console.log(record)
        this.model = Object.assign({}, record)
        this.visible = true
        if(record.id){
          this.readOnly=true
        }else{
          this.readOnly=false
        }
       
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,
            'yearName',
            'beginDate',
            'endDate',
            'demo'
          ))
          this.form.setFieldsValue({beginDate:this.model.beginDate?moment(this.model.beginDate):null})
          this.form.setFieldsValue({endDate:this.model.endDate?moment(this.model.endDate):null})
        })
       
      },
      close() {
        this.$emit('close')
        this.visible = false
      },
      handleOk() {
        const that = this
        // 触发表单验证
        // if(this.model.deptId==null||this.model.deptId==""){
        //   that.$message.warning("请选择部门！");
        //   return;
        // }
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }

            let formData = Object.assign(this.model, values)
            formData.beginDate = formData.beginDate?formData.beginDate.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.endDate = formData.endDate?formData.endDate.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.deptId=this.deptid;
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
    }
  }
</script>

<style lang="less" scoped>

</style>
