<template>
  <div class="container">
    <!-- Add Toast component -->
    <Toast ref="toast" />
    <!-- 导航栏 -->
    <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="header-text">
        <div class="sub-text">绑定银行卡</div>
        <div class="sub-text">为了保证您的账户资金安全</div>
        <div class="sub-text">请先完成银行卡绑定</div>
      </div>

      <div class="form-container">
        <div class="form-item">
          <div class="form-label">姓名</div>
          <input 
            type="text" 
            class="form-input"
            placeholder="输入姓名"
            v-model="formData.lastname"
          />
        </div>

        <div class="form-item">
          <div class="form-label">银行名称</div>
          <input 
            type="text" 
            class="form-input"
            placeholder="输入银行名称"
            v-model="formData.banktype"
          />
        </div>

        <div class="form-item">
          <div class="form-label">开户行</div>
          <input 
            type="text" 
            class="form-input"
            placeholder="输入银行卡开户行"
            v-model="formData.address"
          />
        </div>

        <div class="form-item">
          <div class="form-label">银行卡号</div>
          <input 
            type="text" 
            class="form-input"
            placeholder="输入银行卡号"
            v-model="banknumInput"
            @input="handleBanknumInput"
          />
        </div>

        <div style="display: flex;justify-content: center;align-items: center;">
          <button 
            class="submit-btn"
            @click="handleSubmit"
            :disabled="bankInfo.status"
          >
            {{ bankInfo.status ? '已绑定' : '立即绑定' }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getBankInfo, setBankInfo } from '@/api/profile';
import Toast from '@/components/Toast.vue';

export default {
  name: 'BankCard',
  components: {
    Toast
  },
  data() {
    return {
      bankInfo: {},
      formData: {
        banktype: '',
        address: '',
        lastname: '',
        banknum: ''
      },
      banknumInput: ''
    }
  },
  created() {
    this.getBankInfo();
  },
  methods: {
    handleBanknumInput(event) {
      // Remove any non-digit characters
      const value = event.target.value.replace(/\D/g, '');
      this.banknumInput = value;
      this.formData.banknum = value; // 始终用字符串
    },
    async getBankInfo() {
      try {
        const res = await getBankInfo();
        if (res && res.data) {
          this.bankInfo = res.data;
          if (this.bankInfo && this.bankInfo.status) {
            // 填充表单数据，允许修改
            this.formData = {
              banktype: this.bankInfo.banktype || '',
              address: this.bankInfo.address || '',
              lastname: this.bankInfo.lastname || '',
              banknum: this.bankInfo.banknum || ''
            };
            this.banknumInput = this.bankInfo.banknum ? String(this.bankInfo.banknum) : '';
          }
        } else {
          this.bankInfo = {
            status: false
          };
        }
      } catch (error) {
        this.bankInfo = {
          status: false
        };
        console.error('获取银行卡信息失败:', error);
      }
    },
    async handleSubmit() {
      if (!this.formData.banktype) {
        this.$refs.toast.show('请输入开户银行', 'warning');
        return;
      }
      if (!this.formData.address) {
        this.$refs.toast.show('请输入开户地址', 'warning');
        return;
      }
      if (!this.formData.lastname) {
        this.$refs.toast.show('请输入卡号姓名', 'warning');
        return;
      }
      if (!this.formData.banknum) {
        this.$refs.toast.show('请输入银行账号', 'warning');
        return;
      }

      try {
        // Ensure all values are strings when sending to FormData
        const submitData = {
          banktype: this.formData.banktype.trim(),
          address: this.formData.address.trim(),
          lastname: this.formData.lastname.trim(),
          banknum: String(this.formData.banknum).trim()
        };

        const res = await setBankInfo(submitData);
        if (res.code === 1) {
          this.$refs.toast.show(this.bankInfo.status ? '修改成功' : '绑定成功', 'success');
          await this.getBankInfo();
        } else {
          this.$refs.toast.show(res.msg || '操作失败，请重试', 'error');
        }
      } catch (error) {
        console.error('操作失败:', error);
        this.$refs.toast.show('操作失败，请重试', 'error');
      }
    }
  }
}
</script>

<style scoped>
.container {
  min-height: 100vh;
  background: url('../assets/dabag.png') no-repeat;
  background-size: cover;
  background-position: center;
}

.nav-bar {
  height: 44px;
  background-color: transparent;
  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;
}

.header-text {
  padding: 40px 20px;
  color: #fff;
}

.title-text {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 15px;
}

.sub-text {
  font-size: 14px;
  line-height: 1.5;
  font-weight: bold;
}

.form-container {
  background: #fff;
  border-radius: 8px;
  margin: 0 20px;
  padding: 20px;
}

.form-item {
  /* margin-bottom: 20px; */
}

.form-label {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  margin-bottom: 8px;
  
  
}

.form-input {
  width: 100%;
  height: 44px;
  border: none;
  background: #fff;
  font-size: 14px;
  padding: 0;
  color: #333;
}

.form-input::placeholder {
  color: #999;
}

.form-input:focus {
  outline: none;
}

.form-input:disabled {
  color: #666;
  background: #f5f5f5;
}

.submit-btn {
  width: 60%;
  height: 44px;
  background: #FF6B6B;
  border-radius: 22px;
  border: none;
  color: #fff;
  font-size: 16px;
  font-weight: 500;
  margin-top: 10px;
  cursor: pointer;
}

.submit-btn:active {
  opacity: 0.9;
}

.submit-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}
</style> 