<template>
  <div class="bankcard-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="content">
      <!-- 顶部提示文字 -->
      <div class="header-section">
        <div class="section-header">
          <div class="section-title">银行卡绑定</div>
        </div>
        <div class="header-content">
          <div class="sub-text">为了保证您的账户资金安全</div>
          <div class="sub-text">请先完成银行卡绑定</div>
        </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 class="submit-section">
          <button 
            class="submit-btn"
            @click="handleSubmit"
          >
            {{ 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 = Number(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>
.bankcard-container {
  min-height: 100vh;
  background: linear-gradient(to bottom, #2f9580 0%, #4fb3a1 100%);
}

.nav-bar {
  height: 56px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  color: #fff;
  display: flex;
  align-items: center;
  position: relative;
  padding: 0 15px;
}

.back-btn {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.back-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.05);
}

.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: 18px;
  font-weight: 600;
}

.content {
  padding: 20px;
  min-height: calc(100vh - 56px);
}

.header-section {
  background: #fff;
  border-radius: 16px;
  margin-bottom: 20px;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.section-header {
  background: linear-gradient(135deg, #2f9580 0%, #4fb3a1 100%);
  padding: 16px 20px;
}

.section-title {
  color: #fff;
  font-size: 16px;
  font-weight: 600;
}

.header-content {
  padding: 20px;
}

.sub-text {
  font-size: 14px;
  line-height: 1.8;
  color: #374151;
  font-weight: 500;
}

.form-container {
  background: #fff;
  border-radius: 16px;
  padding: 24px 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.form-item {
  margin-bottom: 24px;
}

.form-label {
  font-size: 15px;
  font-weight: 600;
  color: #374151;
  margin-bottom: 12px;
  display: block;
}

.form-input {
  width: 100%;
  height: 48px;
  border: 2px solid #e5e7eb;
  background: #f8f9fa;
  border-radius: 12px;
  font-size: 16px;
  padding: 0 16px;
  color: #374151;
  transition: all 0.3s ease;
  box-sizing: border-box;
}

.form-input::placeholder {
  color: #9ca3af;
}

.form-input:focus {
  outline: none;
  border-color: #2f9580;
  background: #fff;
  box-shadow: 0 0 0 3px rgba(47, 149, 128, 0.1);
}

.form-input:disabled {
  color: #6b7280;
  background: #f3f4f6;
  border-color: #d1d5db;
  cursor: not-allowed;
}

.submit-section {
  margin-top: 32px;
  text-align: center;
}

.submit-btn {
  width: 100%;
  height: 52px;
  background: #2f9580;
  border-radius: 12px;
  border: none;
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(47, 149, 128, 0.3);
}

.submit-btn:hover {
  background: #268371;
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(47, 149, 128, 0.4);
}

.submit-btn:active {
  transform: translateY(0);
}

.submit-btn:disabled {
  background: #9ca3af;
  cursor: not-allowed;
  box-shadow: none;
  transform: none;
}

.submit-btn:disabled:hover {
  background: #9ca3af;
  transform: none;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .nav-bar {
    height: 50px;
    padding: 0 12px;
  }
  
  .back-btn {
    width: 36px;
    height: 36px;
  }
  
  .title {
    font-size: 16px;
  }
  
  .content {
    padding: 16px;
  }
  
  .section-header {
    padding: 12px 16px;
  }
  
  .section-title {
    font-size: 15px;
  }
  
  .header-content {
    padding: 16px;
  }
  
  .sub-text {
    font-size: 13px;
  }
  
  .form-container {
    padding: 20px 16px;
  }
  
  .form-item {
    margin-bottom: 20px;
  }
  
  .form-label {
    font-size: 14px;
    margin-bottom: 10px;
  }
  
  .form-input {
    height: 44px;
    font-size: 15px;
    padding: 0 14px;
  }
  
  .submit-btn {
    height: 48px;
    font-size: 15px;
  }
}
</style> 