<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]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="上班时间">
         
          <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-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="下班时间">
          <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-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="创建时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'createDate', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="修改时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'updateDate', {}]" />
        </a-form-item> -->
       <!-- <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="删除标记">
          <a-input placeholder="请输入删除标记" v-decorator="['delFlag', {}]" />
        </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: "ShiftModal",
    data () {
      return {
        title:"操作",
        visible: false,
        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: "/shift/shift/add",
          edit: "/shift/shift/edit",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'name','delFlag'))
		  //时间格式化
          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({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;
      },
      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.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>