<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @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="['name', validatorRules.name]" style="width: 180px;"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="假期类型">
          <a-select  placeholder="请选择假期类型" v-model="type" :getPopupContainer= "(target) => target.parentNode">
            <a-select-option value="1">班</a-select-option>
            <a-select-option value="2">休</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="假期开始时间">
          <a-date-picker  format='YYYY-MM-DD' v-decorator="[ 'startDate', validatorRules.startDate]" />
        </a-form-item>
        
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="日薪资倍数" v-if="type=='1'">
          <a-input-number id="inputNumber" :min="1" placeholder="请输入日薪资倍数" v-decorator="['sum',{}]" style="width: 180px;"/>
        </a-form-item>
		
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "StatutoryLeaveModal",
    data () {
      return {
        title:"操作",
        visible: false,
        type:"2",
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          name:{
            rules: [{
              required: true, message: '请输入假期名称!'
            }]
          },
          startDate:{
            rules: [{
              required: true, message: '请选择开始时间!'
            }]
          },
          endDate:{
            rules: [{
              required: true, message: '请选择结束时间!'
            }]
          }
          },
          
        url: {
          add: "/statutoryleave/statutoryLeave/add",
          edit: "/statutoryleave/statutoryLeave/edit",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        if(record.type!=null&&record.type!=""){
          this.type=record.type;
        }
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'name','sum'))
		  //时间格式化
          this.form.setFieldsValue({startDate:this.model.startDate?moment(this.model.startDate):null})
          this.form.setFieldsValue({endDate:this.model.endDate?moment(this.model.endDate):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.type="2";
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        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.startDate = formData.startDate?formData.startDate.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.endDate = formData.endDate?formData.endDate.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.type=this.type;
            console.log(formData)
            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>