<template>
  <div class="container">
    <!-- 导航栏 -->
    <div class="nav-bar">
      <div class="back-btn" @click="$router.back()">
        <i class="arrow-left"></i>
      </div>
      <div class="title">绑定银行卡</div>
    </div>

    <div class="main">
      <div class="income-card funs">
        <div class="u-field u-border-bottom">
          <div class="u-field-inner u-label-postion-left">
            <div class="u-label">
              <span class="u-label-text">开户银行</span>
            </div>
            <div class="fild-body">
              <div class="u-flex-1 u-flex">
                <input 
                  type="text" 
                  class="u-field__input-wrap"
                  placeholder="输入银行名称"
                  v-model="formData.banktype"
                  :disabled="!!bankInfo.status"
                />
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="income-card funs">
        <div class="u-field u-border-bottom">
          <div class="u-field-inner u-label-postion-left">
            <div class="u-label">
              <span class="u-label-text">开户地址</span>
            </div>
            <div class="fild-body">
              <div class="u-flex-1 u-flex">
                <input 
                  type="text" 
                  class="u-field__input-wrap"
                  placeholder="输入银行开户地址"
                  v-model="formData.address"
                  :disabled="!!bankInfo.status"
                />
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="income-card funs">
        <div class="u-field u-border-bottom">
          <div class="u-field-inner u-label-postion-left">
            <div class="u-label">
              <span class="u-label-text">卡号姓名</span>
            </div>
            <div class="fild-body">
              <div class="u-flex-1 u-flex">
                <input 
                  type="text" 
                  class="u-field__input-wrap"
                  placeholder="输入银行卡绑定姓名"
                  v-model="formData.lastname"
                  :disabled="!!bankInfo.status"
                />
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="income-card funs">
        <div class="u-field u-border-bottom">
          <div class="u-field-inner u-label-postion-left">
            <div class="u-label">
              <span class="u-label-text">银行账号</span>
            </div>
            <div class="fild-body">
              <div class="u-flex-1 u-flex">
                <input 
                  type="text" 
                  class="u-field__input-wrap"
                  placeholder="输入银行卡账号"
                  v-model="formData.banknum"
                  :disabled="!!bankInfo.status"
                />
              </div>
            </div>
          </div>
        </div>
      </div>

      <div 
        class="mt30 h40 lh40 tc white bgBlue radius28 ft14 w90 mauto"
        @click="handleSubmit"
      >
        {{ bankInfo.status ? '已绑定' : '确认绑定' }}
      </div>
    </div>
  </div>
</template>

<script>
import { getBankInfo } from '@/api/profile';

export default {
  name: 'BankCard',
  data() {
    return {
      bankInfo: {},
      formData: {
        banktype: '',
        address: '',
        lastname: '',
        banknum: ''
      }
    }
  },
  created() {
    this.getBankInfo();
  },
  methods: {
    async getBankInfo() {
      try {
        const res = await getBankInfo();
        this.bankInfo = res.data;
        if (this.bankInfo.status) {
          this.formData = {
            banktype: this.bankInfo.banktype,
            address: this.bankInfo.address,
            lastname: this.bankInfo.lastname,
            banknum: this.bankInfo.banknum
          };
        }
      } catch (error) {
        console.error('获取银行卡信息失败:', error);
      }
    },
    async handleSubmit() {
      if (this.bankInfo.status) return;
      
      if (!this.formData.banktype) {
        alert('请输入开户银行');
        return;
      }
      if (!this.formData.address) {
        alert('请输入开户地址');
        return;
      }
      if (!this.formData.lastname) {
        alert('请输入卡号姓名');
        return;
      }
      if (!this.formData.banknum) {
        alert('请输入银行账号');
        return;
      }
      // TODO: 实现银行卡绑定逻辑
      alert('银行卡绑定成功');
      await this.getBankInfo();
    }
  }
}
</script>

<style scoped>
.container {
  min-height: 100vh;
  background: #f5f5f5;
}

.nav-bar {
  height: 44px;
  background-color: #ed4b39;
  color: #fff;
  display: flex;
  align-items: center;
  position: relative;
  padding: 0 15px;
}

.back-btn {
  width: 24px;
  height: 44px;
  display: flex;
  align-items: center;
  cursor: pointer;
}

.arrow-left {
  width: 12px;
  height: 12px;
  border-left: 2px solid #fff;
  border-bottom: 2px solid #fff;
  transform: rotate(45deg);
}

.title {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  font-size: 16px;
  font-weight: 500;
}

.main {
  padding: 15px;
}

.income-card {
  background: #fff;
  border-radius: 8px;
  margin-bottom: 10px;
}

.u-field {
  padding: 10px 15px;
}

.u-field-inner {
  display: flex;
  align-items: center;
}

.u-label {
  flex: 0 0 65px;
  font-size: 14px;
  color: #333;
}

.fild-body {
  flex: 1;
}

.u-flex-1 {
  flex: 1;
}

.u-flex {
  display: flex;
}

.u-field__input-wrap {
  width: 100%;
  height: 24px;
  line-height: 24px;
  border: none;
  outline: none;
  font-size: 14px;
  color: #333;
  background: transparent;
}

.u-field__input-wrap::placeholder {
  color: #999;
}

.u-field__input-wrap:disabled {
  color: #666;
}

.u-border-bottom {
  position: relative;
}

.u-border-bottom::after {
  content: '';
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 1px;
  background: rgba(85, 89, 105, 0.12);
  transform: scaleY(0.5);
}

.mt30 { margin-top: 30px; }
.h40 { height: 40px; }
.lh40 { line-height: 40px; }
.tc { text-align: center; }
.white { color: #fff; }
.ft14 { font-size: 14px; }
.w90 { width: 90%; }
.mauto { margin: 0 auto; }
.radius28 { border-radius: 28px; }

.bgBlue {
  background: #ed4b39;
  cursor: pointer;
}

.bgBlue:active {
  opacity: 0.9;
}
</style> 