<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="员工姓名">
          <j-select-user-by-dep  v-decorator="['userId', validatorRules.realname]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="部门名称">
          <a-input placeholder="请输入部门名称" :disabled="true" v-decorator="['deptNames', validatorRules.deptname ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="类型">
          <j-dict-select-tag v-decorator="['type', validatorRules.type]" :triggerChange="true" placeholder="请选择类型"
            dictCode="work_type" />
    
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="日期">
          <a-date-picker showTime format='YYYY-MM-DD' v-decorator="[ 'date', validatorRules.date]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注">
          <a-textarea :maxLength="255" 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"
import JSelectUserByDep from '@/components/jeecgbiz/JSelectUserByDep'
  export default {
    name: "UserPrizeModal",
    components: {JSelectUserByDep},
    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: '请选择类型' }]},
        date:{rules: [{ required: true, message: '请选择日期' }]},
        },
        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.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'userId','deptNames','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.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();
            })



          }
        })
      },
      queryDeptName(e){
        getAction(this.url.dept,{id:e}).then((res)=>{
          if(res.success){
            if(res.result.length>0){
              var list=res.result;
              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({
                deptNames:deptname.join(",")
              })
            }else{
              this.deptid="";
              this.form.setFieldsValue({
                deptNames:""
              })
            }
          }
        })
      },
      validateRealname(rule, value, callback) {
        if (!value) {
          callback()
          this.deptid="";
          this.form.setFieldsValue({
            deptNames:""
          })
        } else {
          this.queryDeptName(value);
          callback()
        }
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>