<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-row>
        <a-col :md="6" :sm="8" :lg="12">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="名称">
          <a-input :maxLength="10" placeholder="请输入名称" v-decorator="['name', validatorRules.name]" />
        </a-form-item>
        </a-col>
        <a-col :md="6" :sm="8" :lg="12">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否分段">
         <a-select @change="changeType" v-decorator="['shiftType',{initialValue:'0',rules: [{ required: true, message: '请选择是否分段!'}]}]" placeholder="请选择"
           :getPopupContainer="(target) => target.parentNode">
           <a-select-option value="0">否</a-select-option>
           <a-select-option value="1">是</a-select-option>
         </a-select>
        </a-form-item>
        </a-col>
        </a-row>
        
        <a-row>
          <a-col :md="6" :sm="8" :lg="12">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="上班时间1">
         
          <a-time-picker showTime 
          format='HH:mm' v-decorator="[ 'startDate', validatorRules.startDate]">
           <div slot="addon" style="width:100%;overflow:hidden">
            <div style="width:50%;float:left;border-right:solid 1px ;text-align:center">
                 <div style="padding-top:10%;padding-bottom:10%">
                  <span style="width:100%;">时</span>
                 </div>
                </div>
                <div style="width:50%;float:right;text-align:center">
                 <div style="padding-top:10%;padding-bottom:10%">
                  <span style="width:100%;">分</span>
                 </div>
                </div>
           </div>
           </a-time-picker >
        </a-form-item>
        </a-col>
        <a-col :md="6" :sm="8" :lg="12">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="下班时间1">
          <a-time-picker showTime format='HH:mm' v-decorator="[ 'endDate', validatorRules.endDate]" >
          <div slot="addon" style="width:100%;overflow:hidden">
           <div style="width:50%;float:left;border-right:solid 1px ;text-align:center">
                <div style="padding-top:10%;padding-bottom:10%">
                 <span style="width:100%;">时</span>
                </div>
               </div>
               <div style="width:50%;float:right;text-align:center">
                <div style="padding-top:10%;padding-bottom:10%">
                 <span style="width:100%;">分</span>
                </div>
               </div>
          </div>
          </a-time-picker>
        </a-form-item>
        </a-col>
        </a-row>
        <a-row >
          <a-col :md="6" :sm="8" :lg="12">
       <a-form-item
         :labelCol="labelCol"
         :wrapperCol="wrapperCol"
         label="上班时间2">
        
         <a-time-picker showTime  :disabled="!isShow"
         format='HH:mm' v-decorator="[ 'start2Date', isShow?validatorRules.start2Date:{}]">
          <div slot="addon" style="width:100%;overflow:hidden">
           <div style="width:50%;float:left;border-right:solid 1px ;text-align:center">
                <div style="padding-top:10%;padding-bottom:10%">
                 <span style="width:100%;">时</span>
                </div>
               </div>
               <div style="width:50%;float:right;text-align:center">
                <div style="padding-top:10%;padding-bottom:10%">
                 <span style="width:100%;">分</span>
                </div>
               </div>
          </div>
          </a-time-picker >
       </a-form-item>
       </a-col>
        <a-col :md="6" :sm="8" :lg="12">
       <a-form-item
         :labelCol="labelCol"
         :wrapperCol="wrapperCol"
         label="下班时间2">
         <a-time-picker showTime format='HH:mm'  :disabled="!isShow" v-decorator="[ 'end2Date', isShow?validatorRules.end2Date:{}]" >
         <div slot="addon" style="width:100%;overflow:hidden">
          <div style="width:50%;float:left;border-right:solid 1px ;text-align:center">
               <div style="padding-top:10%;padding-bottom:10%">
                <span style="width:100%;">时</span>
               </div>
              </div>
              <div style="width:50%;float:right;text-align:center">
               <div style="padding-top:10%;padding-bottom:10%">
                <span style="width:100%;">分</span>
               </div>
              </div>
         </div>
         </a-time-picker>
       </a-form-item>
		</a-col>
		</a-row>
      </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: "ShiftModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        isShow:false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          name:{
            rules: [{
              required: true, message: '请输入班次名称!'
            }]
          },
          startDate:{
            rules: [{
              required: true, message: '请选择上班时间!'
            }]
          },
          endDate:{
            rules: [{
              required: true, message: '请选择下班时间!'
            }]
          },
          start2Date:{
            rules: [{
              required: true, message: '请选择上班时间!'
            }]
          },
          end2Date:{
            rules: [{
              required: true, message: '请选择下班时间!'
            }]
          },
          
        },
        url: {
          add: "/shift/shift/add",
          edit: "/shift/shift/edit",
        },
      }
    },
    created () {
    },
    methods: {
      changeType(e){
        if(e==1){
          this.isShow=true;
        }else{
          this.isShow=false;
          this.form.setFieldsValue({start2Date:null,end2Date:null})
        }
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        if(record.shiftType!=null&&record.shiftType==1){
          this.isShow=true;
        }
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'name','delFlag','shiftType'))
		  //时间格式化
          this.form.setFieldsValue({startDate:this.model.startDate?moment(this.model.startDate):null})
          this.form.setFieldsValue({endDate:this.model.endDate?moment(this.model.endDate):null})
          this.form.setFieldsValue({start2Date:this.model.start2Date?moment(this.model.start2Date):null})
          this.form.setFieldsValue({end2Date:this.model.end2Date?moment(this.model.end2Date):null})
          // this.form.setFieldsValue({createDate:this.model.createDate?moment(this.model.createDate):null})
          // this.form.setFieldsValue({updateDate:this.model.updateDate?moment(this.model.updateDate):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.isShow=false;
      },
      handleOk () {
        const that = this;
        // if(this.model.name==null||this.model.name==""){
        //   that.$message.warning("请输入班次名称!");
        //   return;
        // }if(this.model.startDate==null||this.model.startDate==""){
        //   that.$message.warning("请选择上班时间!");
        //   return;
        // }if(this.model.endDate==null||this.model.endDate==""){
        //   that.$message.warning("请选择下班班时间!");
        //   return;
        // }
        // 触发表单验证
        this.form.validateFields((err, values) => {
          console.log(err)
          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')+":00":null;
            formData.endDate = formData.endDate?formData.endDate.format('YYYY-MM-DD HH:mm')+":00":null;
            formData.start2Date = formData.start2Date?formData.start2Date.format('YYYY-MM-DD HH:mm')+":00":null;
            formData.end2Date = formData.end2Date?formData.end2Date.format('YYYY-MM-DD HH:mm')+":00":null;
            // formData.createDate = formData.createDate?formData.createDate.format('YYYY-MM-DD HH:mm:ss'):null;
            // formData.updateDate = formData.updateDate?formData.updateDate.format('YYYY-MM-DD HH:mm:ss'):null;
            
            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>

.ls{
  
  list-style-type: none;  margin: 0;    padding: 0;
}

.ls li{
  float: left;
}
</style>