<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="报销日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择入库日期" v-decorator="[ 'reimbursementDate', validatorRules.reimbursementDate]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="支付凭证编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'paymentNo', validatorRules.paymentNo]" placeholder="请输入支付凭证编号"></a-input>
        </a-form-item>
        <a-form-item label="车牌号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'carNo', validatorRules.carNo]" placeholder="请输入车牌号"></a-input>
        </a-form-item>
        <a-form-item label="报销人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-popup
            v-decorator="['submitter', validatorRules.submitter]"
            placeholder="请选择报销人"
            :trigger-change="true"
            org-fields="realname,depart_name"
            dest-fields="submitter,dept"
            code="user_dept"
            @callback="popupCallback"/>
        </a-form-item>
        <a-form-item label="部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'dept', validatorRules.dept]" placeholder="请输入部门" disabled></a-input>
        </a-form-item>
        <a-form-item label="报销内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'content', validatorRules.content]" placeholder="请输入报销内容"></a-input>
        </a-form-item>
        <a-form-item label="金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'amount', validatorRules.amount]" placeholder="请输入金额" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload v-decorator="['attachment', validatorRules.attachment]" :trigger-change="true"></j-upload>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入备注"></a-input>
        </a-form-item>
        <a-form-item label="登记人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'registrant', validatorRules.registrant]" placeholder="请输入登记人" disabled></a-input>
        </a-form-item>
        <a-form-item label="登记部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'registrantDept', validatorRules.registrantDept]" placeholder="请输入登记部门" disabled></a-input>
        </a-form-item>
        <a-form-item label="财务经办人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'financeOperator', validatorRules.financeOperator]" placeholder="请输入财务经办人" disabled></a-input>
        </a-form-item>
        <a-form-item label="财务经办部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'financeDept', validatorRules.financeDept]" placeholder="请输入财务经办部门" disabled></a-input>
        </a-form-item>
        <a-form-item label="财务负责人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'financeHead', validatorRules.financeHead]" placeholder="请输入财务负责人" disabled></a-input>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JUpload from '@/components/jeecg/JUpload'
  import JDate from '@/components/jeecg/JDate'  

  export default {
    name: "CarfareDetailModal",
    components: { 
      JUpload,
      JDate
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          reimbursementDate: {rules: [
          ]},
          paymentNo: {rules: [
          ]},
          carNo: {rules: [
          ]},
          submitter: {rules: [
          ]},
          dept: {rules: [
          ]},
          content: {rules: [
          ]},
          amount: {rules: [
          ]},
          attachment: {rules: [
          ]},
          remark: {rules: [
          ]},
          registrant: {rules: [
          ]},
          registrantDept: {rules: [
          ]},
          financeOperator: {rules: [
          ]},
          financeDept: {rules: [
          ]},
          financeHead: {rules: [
          ]},
        },
        url: {
          add: "/oa/carfareDetail/add",
          edit: "/oa/carfareDetail/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,'reimbursementDate','paymentNo','carNo','submitter','dept','content','amount','attachment','remark','registrant','registrantDept','financeOperator','financeDept','financeHead'))
        })
      },
      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);
            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()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'reimbursementDate','paymentNo','carNo','submitter','dept','content','amount','attachment','remark','registrant','registrantDept','financeOperator','financeDept','financeHead'))
      },

      
    }
  }
</script>