<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="['realname', validatorRules.realname ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="部门名称">
          <a-input placeholder="请输入部门名称" v-decorator="['deptname', validatorRules.deptname ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="类型">
          <a-select v-decorator="[ 'type',{initialValue:'0'}]" 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-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="日期">
          <a-date-picker showTime format='YYYY-MM-DD' v-decorator="[ 'date', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注">
          <a-textarea  placeholder="请输入备注" v-decorator="['demo', {}]" />
        </a-form-item>
        
		
      </a-form>
    </a-spin>
  </j-modal>
</template>

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

  export default {
    name: "UserPrizeModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        userid:"",
        deptid:"",
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        realname:{rules: [{ required: true, message: '请输入员工名称!' },{
              validator: this.validateRealname
            }]},
        deptname:{rules: [{ required: true, message: '请输入部门名称!' }]},
        type:{rules: [{ required: true, message: '请输入类型(0奖励，1惩罚)!' }]},
        },
        url: {
          add: "/userPrize/userPrize/add",
          edit: "/userPrize/userPrize/edit",
          dept:"/sys/user/queryUsersByusername"
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.deptid=record.deptid;
        this.userid=record.userid;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'realname','deptname','type','demo'))
		  //时间格式化
          this.form.setFieldsValue({date:this.model.date?moment(this.model.date):null})
        });

      },
     
      close () {
        this.$emit('close');
        this.visible = false;
      },
      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.date = formData.date?formData.date.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.userId=this.userid;
            formData.deptId=this.deptid;
            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();
            })



          }
        })
      },
      queryDeptName(e){
        getAction(this.url.dept,{realname:e}).then((res)=>{
          if(res.success){
            if(res.result.length>0){
              var list=res.result;
              this.userid= list[0].id;
              var deptids=[];
              var deptname=[];
              for(var i=0;i<list.length;i++){
                deptids.push(list[i].deptid);
                deptname.push(list[i].deptname);
              }
              this.deptid=deptids.join(",");
              this.form.setFieldsValue({
                deptname:deptname.join(",")
              })
            }else{
              this.userid="";
              this.deptid="";
              this.form.setFieldsValue({
                deptname:""
              })
            }
          
          
          }
        }).finally(() => {
         
        })
      },
      validateRealname(rule, value, callback) {
        if (!value) {
          callback()
        } else {
          this.queryDeptName(value);
          callback()
        }
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>