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

        <a-form-item label="账户姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'userName', validatorRules.userName]" placeholder="请输入账户姓名"></a-input>
        </a-form-item>

        <a-form-item label="手机号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'phone', validatorRules.phone]" placeholder="请输入手机号"></a-input>
        </a-form-item>

        <a-form-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'idcardNumber', validatorRules.idcardNumber]" placeholder="请输入身份证号"></a-input>
        </a-form-item>

        <a-form-item label="银行卡号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'cardNumber', validatorRules.cardNumber]" placeholder="请输入银行卡号"></a-input>
        </a-form-item>

        <a-form-item label="是否招商银行" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="radio"
                             v-decorator="['bnkflg',validatorRules.bnkflg]" :trigger-change="true"
                             dictCode="bnkflg" placeholder="请选择是否招行卡" style="width: 100%"/>
        </a-form-item>

        <a-form-item v-show="is_show" label="他行开户行" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'eacbnk', { rules: [{ required: is_show, message: '请输入他行开户行!' }] }]"
                   placeholder="请输入他行开户行"></a-input>
        </a-form-item>

        <a-form-item v-show="is_show" label="他行开户地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'rcveaa', { rules: [{ required: is_show, message: '请输入他行开户地址!' }] }]"
                   placeholder="请输入他行开户地址"></a-input>
        </a-form-item>

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

<script>

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

  export default {
    name: 'HlgPaymentReissueModal',
    components: {
      JDate
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        is_show: true,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },

        confirmLoading: false,
        validatorRules: {
          userName: { rules: [{ required: true, message: '请输入账户姓名!' }] },
          phone: { rules: [{ required: true, message: '请输入手机号!' },{ validator: this.validatePhone }] },
          idcardNumber: { rules: [{ required: true, message: '请输入身份证号!' },{validator: this.validateIdCardNumber}] },
          cardNumber: { rules: [{ required: true, message: '请输入银行卡号!' },{validator: this.validateCardNumber}] },
          bnkflg: { rules: [{ required: true, validator: this.checkBnkflg }] }
          // eacbnk: { rules: [{ required: is_show, message: '请输入他行开户行!' }] },
          // rcveaa: { rules: [{ required: is_show, message: '请输入他行开户地址!' }] }
        },
        url: {
          supplementPayment: '/hlgpayment/hlgPayment/handSupplementPayment'
        }

      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit({})
      },
      edit(record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.visible = true
        this.is_show = record.bnkflg === 'N'
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'userName', 'phone','idcardNumber', 'cardNumber', 'bnkflg', 'eacbnk', 'rcveaa'))
        })
      },
      close() {
        this.$emit('close')
        this.$destroyAll()
        this.visible = false
      },
      handleOk() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            if (values.userName !== this.model.userName || values.idcardNumber !== this.model.idcardNumber) {
              const modal = this.$confirm({
                title: '提示',
                content: '姓名和身份证信息发生变动，是否确定补发?',
                okText: '确定',
                cancelText: '取消',
                centered: true,
                onOk: () => {
                  this.Reissue(values)
                },
                onCancel: () => {
                  modal.destroy()
                }
              })
            } else {
              this.Reissue(values)
            }
          }

        })
      },
      //补发
      Reissue(values) {
        let that = this;
        that.confirmLoading = true
        let httpurl = ''
        let method = ''
        httpurl = this.url.supplementPayment
        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,
              onOk: () => {

              }
            })
            that.$emit('ok')
            that.close()
          } else {
            that.$error({
              title: '错误提示',
              content: res.message,
              okText: '确定',
              centered:true
            })
          }
        }).finally(() => {
          that.confirmLoading = false
        })
      },
      checkBnkflg(rule, value, callback) {
        this.is_show = value === 'N'
        callback()
      },
      handleCancel() {
        this.close()
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'userName', 'phone','idcardNumber', 'cardNumber', 'bnkflg', 'eacbnk', 'rcveaa'))
      },
      //验证银行卡号
      validateCardNumber(rule, value, callback){
        if(!value){
          callback()
        }else{
          if(new RegExp(/^[0-9]+$/).test(value)){
            callback()
          }else{
            callback("请输入数字")
          }
        }
      },
      //校验手机号
      validatePhone(rule, value, callback) {
        if (!value) {
          callback()
        } else {
          if (new RegExp(/^1[0-9][0-9]\d{8}$/).test(value)) {
            callback()
          } else {
            callback('请输入正确格式的手机号码!')
          }
        }
      },
      //验证身份证号
      validateIdCardNumber(rule, value, callback){
        if (!value || new RegExp(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/).test(value)) {
          callback();
        } else {
          callback("请输入有效的身份证号!");
        }
      },

    }
  }
</script>