<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :bodyStyle="bodyStyle"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    :footer="null">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="账户姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" style="margin-top: 10px;">
          <a-input v-decorator="[ 'userName', validatorRules.userName]" placeholder="请输入账户姓名" style="width: 100%"/>
        </a-form-item>
          
        <a-form-item label="银行卡号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'cardNumber', validatorRules.cardNumber]" placeholder="请输入银行卡号" style="width: 100%"/>
        </a-form-item>

        <a-form-item label="是否招行卡" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" @change="getcontractType"
                             v-decorator="['bnkflg',validatorRules.bnkflg]" :trigger-change="true"
                             dictCode="bnkflg" placeholder="请选择是否招行卡" style="width: 100%"/>
        </a-form-item>
        <template v-if="model.bnkflg=='N'">
          <a-form-item label="他行开户行" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input v-decorator="[ 'eacbnk', validatorRules.eacbnk]" placeholder="请输入他行开户行" style="width: 100%"/>
          </a-form-item>

          <a-form-item label="他行开户地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input v-decorator="[ 'rcveaa', validatorRules.rcveaa]" placeholder="请输入他行开户地址" style="width: 100%"/>
          </a-form-item>
        </template>

      </a-form>
    </a-spin>
    <div class="drawer-bootom-button" v-show="!disableSubmit">
      <a-button class="ant-btn"  @click="handleCancel">取消</a-button>
      <a-button class="ant-btn ant-btn-primary" style="margin-left: 8px"  @click="handleOk">确定补发</a-button>
    </div>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'  
  
  export default {
    name: "HwPaymentDetailModal",
    components: { 
      JDate,
    },
    data () {
      return {
        disableSubmit:false,
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        bodyStyle: {
          paddingTop: '20px',
          paddingBottom:'50px',
        },
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
          userName:{rules: [{ required: true, message: '请输入账户姓名!' }]},
          cardNumber:{rules: [{ required: true, message: '请输入银行卡号!' },{
              validator: this.validateCardNumber
            }]},
          bnkflg:{rules: [{ required: true, message: '请选择是否招行卡!' }]},
          eacbnk:{rules: [{ required: true, message: '请输入他行开户行!' }]},
          rcveaa:{rules: [{ required: true, message: '请输入他行开户地址!' }]},
        },
        url: {
          add: "/hwpayment/hwPaymentDetail/handSupplementPayment",
          edit: "/hwpayment/hwPaymentDetail/handSupplementPayment",
        },
     
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        console.log("ddd",record)
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'userName','cardNumber','bnkflg','eacbnk','rcveaa'))
        })
      },
      close () {
        this.$emit('ok');
        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.$success({
                  title: '提示',
                  content: res.message,
                  okText: '确定',
                  centered:true,
                })
                that.$emit('ok');
              }else{
                that.$error({
                  title: '错误提示',
                  content: res.message,
                  okText: '确定',
                  centered:true
                })
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'userName','cardNumber','bnkflg','eacbnk','rcveaa'))
      },
      getcontractType(val) {
        this.model.bnkflg = val
      },
      //验证银行卡号
      validateCardNumber(rule, value, callback){
        if(!value){
          callback()
        }else{
          if(new RegExp(/^[0-9]+$/).test(value)){
            callback()
          }else{
            callback("请输入数字")
          }
        }
      },
    }
  }
</script>
<style lang="less" scoped>

  .drawer-bootom-button {
    position: absolute;
    bottom: -8px;
    width: 100%;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: right;
    left: 0;
    background: #fff;
    border-radius: 0 0 2px 2px;
  }
</style>
