<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="form-containerr">
		<div class="bdyhk">
			<span class="bdyhkk">绑定银行卡</span>
			<svg t="1751273430369" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="30102" width="64" height="64"><path d="M481.8 767.8h-39.6c-7.5 0-13.1-6.7-11.8-14.1l18.3-103.8c1-5.7 6-9.9 11.8-9.9h39.6c7.5 0 13.1 6.7 11.8 14.1l-18.3 103.8c-1 5.7-6 9.9-11.8 9.9zM609.9 767.8h-39.6c-7.5 0-13.1-6.7-11.8-14.1l18.3-103.8c1-5.7 6-9.9 11.8-9.9h39.6c7.5 0 13.1 6.7 11.8 14.1l-18.3 103.8c-1 5.7-6 9.9-11.8 9.9zM801.8 767.8H698.4c-7.5 0-13.1-6.7-11.8-14.1l18.3-103.8c1-5.7 6-9.9 11.8-9.9h103.5c7.5 0 13.1 6.7 11.8 14.1l-18.3 103.8c-1 5.7-6 9.9-11.9 9.9z" fill="#a71fff" p-id="30103"></path><path d="M892.4 243.1c4.4 0 8 3.6 8 8v554.5c0 4.4-3.6 8-8 8h-761c-4.4 0-8-3.6-8-8V251.1c0-4.4 3.6-8 8-8h761m0-60h-761c-37.6 0-68 30.4-68 68v554.5c0 37.6 30.4 68 68 68h761c37.6 0 68-30.4 68-68V251.1c0-37.6-30.5-68-68-68z" fill="#a71fff" p-id="30104"></path><path d="M954.8 339.7H64.2v64.4h890.6v-64.4z" fill="#a71fff" p-id="30105"></path></svg>
		</div>
	</div>
      <div class="form-container">
        <div class="form-item">
			<div class="form-label" style="font-size: 20px;">填写银行卡信息</div>
          <div class="form-label">持卡姓名:<input 
            type="text" 
            class="form-input"
            placeholder="输入姓名"
            v-model="formData.lastname"
          /></div>
          
        </div>

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

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

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

        
      </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>
</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-color: #b43a39;
  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: 15px;
  font-weight: bold;
  color: #333;
  margin-bottom: 8px;
  
  
}

.form-input {
  width: 60%;
  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: #d89191;
  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;
}
.bdyhk{
	display: flex;
	justify-content: space-between;
	align-items: center;
}
.bdyhkk{
	font-size: 20px;
	font-weight: 500;
	color: #de3232;
}

.form-containerr {
    background: #f4c1c1;
        border-radius: 8px;
        margin: 0 20px;
        padding: 20px;
        margin-bottom: 50px;
        margin-top: 33px;
}
</style> 