<template>
  <view class="container-add">
    <navbar :config="config" backColor="#666"></navbar>
    <view class="tabs">
      <view class="tab" :class="{'activeTab': currentTab == index}" v-for="(item, index) in tabList" :key="item.value"
        @click="changeTab(index)">
        {{item.label}}
      </view>
    </view>
    <u-form :model="form" ref="formRef" :rules="rulesForm" label-position="top">
      <u-form-item label="银行卡号" prop="card_id" class="card-box">
        <view class="bank_name">
          <image class="bank-logo" v-if="bank_logo" :src="bank_logo" mode="widthFix"></image>
          <text class="txt">{{bankForm.local_bank_name}}</text>
        </view>
        <u-input v-model="form.card_id" type="number" :clearable="false" placeholder="请输入银行卡号" @blur="blurCard"
          @input="changeCard" />
      </u-form-item>
      <u-form-item label="银行预留手机号" prop="tel_no">
        <u-input v-model="form.tel_no" type="number" min="11" maxlength="11" :clearable="false"
          placeholder="请输入银行预留手机号" />
      </u-form-item>
      <!-- <u-form-item label="姓名">
        <u-input v-model="form.name" />
      </u-form-item>
      <u-form-item label="身份证号">
        <u-input v-model="form.intro" />
      </u-form-item> -->
      <!-- <template v-if="currentTab == 0">
        <u-form-item label="协议号" prop="protocol_no">
          <u-input v-model="form.protocol_no" :clearable="false" placeholder="请输入协议号，招行卡必填，其他卡非必填" />
        </u-form-item>
      </template> -->
      <template v-if="currentTab == 1">
        <u-form-item label="验证码（信用卡背面签名条末三位）" prop="vip_code">
          <u-input v-model="form.vip_code" :clearable="false" placeholder="请输入验证码" />
        </u-form-item>
        <u-form-item label="有效期" prop="expiration">
          <u-input v-model="form.expiration" :clearable="false" placeholder="请输入有效期" />
          <!-- <uni-datetime-picker type="date" :clear-icon="false" v-model="form.expiration" @maskClick="maskClick" /> -->
        </u-form-item>
      </template>
    </u-form>
    <view class="footer">
      <view class="btn" @click="$u.throttle(next, 500)">下一步</view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        config: {
          back: true,
          title: '新增银行卡',
          color: '#1a1a1a',
          backgroundColor: [1, "#fff"],
          statusBarFontColor: '#1A1A1A',
        },
        tabList: [{
            label: '借记卡',
            value: 0
          },
          {
            label: '信用卡',
            value: 1
          }
        ],
        currentTab: 0,
        form: {},
        bank_logo: '',
        bankForm: {},
        rulesForm: {
          card_id: [{
            required: true,
            message: '请输入银行卡号',
            // 可以单个或者同时写两个触发验证方式 
            trigger: ['blur'],
          }],
          tel_no: [{
            required: true,
            validator: (rule, value, callback) => {
              return this.$u.test.mobile(value);
            },
            message: '手机号码不正确',
            // 触发器可以同时用blur和change
            trigger: ['blur'],
          }, {
            required: true,
            min: 11,
            max: 11,
            message: '手机号必须是11位',
            // 可以单个或者同时写两个触发验证方式 
            trigger: ['blur'],
          }],
          // protocol_no: [{
          //   required: true,
          //   message: '请输入protocol_no',
          //   // 可以单个或者同时写两个触发验证方式 
          //   trigger: ['change', 'blur'],
          // }],
          vip_code: [{
            required: true,
            message: '请输入验证码',
            // 可以单个或者同时写两个触发验证方式 
            trigger: ['blur'],
          }],
          expiration: [{
            required: true,
            max: 4,
            message: '请输入有效期',
            // 可以单个或者同时写两个触发验证方式 
            trigger: ['blur'],
          }]
        },
      }
    },
    onReady() {
      this.$refs.formRef.setRules(this.rulesForm);
    },
    methods: {
      changeTab(i) {
        if (this.currentTab == i) {
          return false;
        }
        this.currentTab = i
        this.form = {}
        this.bankForm = {}
        this.bank_logo = ''
      },
      maskClick() {},
      blurCard(e) {
        if (e) {
          this.$http.post('/usercard/check', {
            card_id: e
          }).then(res => {
            if (res.code == 200 && res.data) {
              this.bankForm.local_bank_name = res.data.bank_name
              this.bankForm.local_bank_mark = res.data.bank_mark
              this.bankForm.local_bank_card_type = res.data.bank_card_type
              this.bankForm.local_bank_card_type_name = res.data.bank_card_type_name
              this.bank_logo = this.$mConfig.bankLink + res.data.bank_mark
              if (this.currentTab == 0) {
                if (res.data.bank_card_type != 'DC') {
                  this.$mUtil.toast("此卡号不是借记卡号，请输入对应借记卡卡号")
                }
                // if (res.data.bank_mark == 'CMB') {
                //   this.rulesForm.protocol_no = {
                //     required: true,
                //     message: '请输入协议号',
                //     trigger: 'blur',
                //   }
                // }
              } else if (this.currentTab == 1) {
                if (res.data.bank_card_type != 'CC') {
                  this.$mUtil.toast("此卡号不是信用卡号，请输入对应信用卡卡号")
                }
              }
            }
          })
        } else {
          this.bankForm = {}
          this.bank_logo = ''
        }
      },
      changeCard(e) {
        if (!e) {
          this.bankForm = {}
          this.bank_logo = ''
        }
      },
      next() {
        this.$refs.formRef.validate(valid => {
          if (valid) {
            console.log('验证通过');
            if (this.currentTab == 0) {
              this.form.credit = false
              this.form.protocol_no = '34463343'
            } else {
              this.form.credit = true
            }
            this.$http.post('/usercard/save', {
              ...this.form,
              ...this.bankForm
            }).then(res => {
              if (res.code == 200) {
                setTimeout(() => {
                  uni.redirectTo({
                    url: `/pages/order/banks/mobileCode?type=add&id=${res.data.id}`
                  })
                }, 500)
              }
            })
          } else {
            console.log('验证失败');
          }
        });
      }
    }
  }
</script>

<style lang="scss" scoped>
  .container-add {
    padding-bottom: 200rpx;
  }

  .tabs {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 40rpx 100rpx;
    border: 1rpx solid #1684FC;
    color: #1684FC;
    border-radius: 10rpx;

    .tab {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 50%;
      height: 60rpx;
    }

    .activeTab {
      color: #fff;
      background-color: #1684fc;
    }
  }

  .u-form {
    padding: 0 30rpx;

    .card-box {

      /deep/ .u-form-item--right__content__slot {
        display: flex;
        flex-direction: column;
        align-items: flex-start;

        .u-input {
          width: 100%;
        }
      }
    }

    .bank_name {
      display: flex;
      align-items: center;
      
      .txt {
        font-size: 30rpx;
        font-weight: bold;
      }
    }

    .bank-logo {
      width: 250rpx;
    }
  }

  .footer {
    width: 100%;
    padding: 30rpx 60rpx;
    position: fixed;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: #fff;
    z-index: 9;

    .btn {
      width: 100%;
      color: #fff;
      text-align: center;
      line-height: 85rpx;
      border: 1rpx solid #3775F6;
      border-radius: 44rpx;
      background-color: #FA6138;
      font-size: 28rpx;
      font-family: PingFang SC, PingFang SC-Regular;
      font-weight: 400;
      text-align: center;
    }
  }
</style>
