<template>
  <div class="vh100">
    <!-- 导航栏 -->
    <div class="nav-bar">
      <div class="back-btn" @click="$router.back()">
        <i class="arrow-left"></i>
      </div>
      <div class="title">提现</div>
    </div>

    <!-- 温馨提示 -->
    <div class="warning-section">
      <div class="warning-title">温馨提示！</div>
      <div class="warning-content">
        提现资金是国务院国资委批准由国家四大基金打款到账您的银行卡，所有提现的资金均由财政部门，银监会监管，零费用，不冻结，不风控，最快速到账您的银行卡
      </div>
    </div>

    <!-- 提现表单 -->
    <div class="withdraw-form">
      <div class="form-item1">
        <div class="label">提现金额</div>
        <div class="form-content">
          <div class="amount-input">
            <span class="currency">¥</span>
            <input 
              type="number" 
              class="input-field" 
              v-model="amount"
              pattern="[0-9]*"
            >
          </div>
        </div>
      </div>

      <div class="available-amount">
        <div class="amount-row">
          <span>最多可提现金额：{{ availableBalance }}</span>
          <span class="withdraw-all" @click="setMaxAmount">全部提现</span>
        </div>
      </div>

      <div class="form-item">
        <div class="label">提现至</div>
        <div class="form-content">
          <div class="bank-detail" @click="handleBankClick">
            <template v-if="bankInfo && bankInfo.status">
              <div class="bank-name">{{ bankInfo.banktype }}</div>
              <div class="bank-number">{{ formatBankNumber(bankInfo.banknum) }}</div>
            </template>
            <template v-else>
              <div class="bank-name no-bank">未绑定银行卡</div>
              <div class="bank-number">点击绑定银行卡</div>
            </template>
          </div>
        </div>
      </div>

      <div class="form-item">
        <div class="label">登录密码</div>
        <div class="form-content">
          <input 
            :type="showPassword ? 'text' : 'password'"
            class="input-field"
            placeholder="请输入登录密码"
            v-model="password"
          >
        </div>
      </div>

      <div style="display: flex;justify-content: center;align-items: center;">
        <button 
          class="submit-btn"
          @click="handleWithdraw"
        >
        立即提现
      </button>
      </div>
    </div>

    <!-- 实名认证提示弹窗 -->
    <div v-if="showAuthDialog" class="custom-dialog-mask">
      <div class="custom-dialog auth-dialog">
        <div class="dialog-close" @click="showAuthDialog = false">
          <i class="close-icon"></i>
        </div>
        <div class="dialog-icon">
          <svg t="1710401275044" class="auth-icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6356">
            <path d="M512 512m-448 0a448 448 0 1 0 896 0 448 448 0 1 0-896 0Z" fill="#1890FF" p-id="6357"></path>
            <path d="M512 320c-17.7 0-32 14.3-32 32v256c0 17.7 14.3 32 32 32s32-14.3 32-32V352c0-17.7-14.3-32-32-32z" fill="#FFFFFF" p-id="6358"></path>
            <path d="M512 720m-48 0a48 48 0 1 0 96 0 48 48 0 1 0-96 0Z" fill="#FFFFFF" p-id="6359"></path>
          </svg>
        </div>
        <div class="dialog-title auth-title">温馨提示</div>
        <div class="dialog-content">您还未完成实名认证<br/>请先进行实名认证</div>
        <button class="dialog-btn auth-btn" @click="goToAuth">去认证</button>
      </div>
    </div>

    <!-- 通用提示弹窗 -->
    <div v-if="showTipsDialog" class="custom-dialog-mask">
      <div class="custom-dialog tips-dialog">
        <div class="dialog-close" @click="showTipsDialog = false">
          <i class="close-icon"></i>
        </div>
        <div class="dialog-title">温馨提示</div>
        <div class="dialog-content">{{ tipsMessage }}</div>
        <button class="dialog-btn tips-btn" @click="showTipsDialog = false">确定</button>
      </div>
    </div>
  </div>
</template>

<script>
import { getUserInfo } from '@/api/home';
import { withdrawCoin, getBankInfo } from '@/api/profile';

export default {
  name: 'Mention',
  data() {
    return {
      userInfo: {},
      bankInfo: null,
      amount: '',
      password: '',
      showPassword: false,
      type: '',
      showAuthDialog: false,
      showTipsDialog: false,
      tipsMessage: ''
    }
  },
  computed: {
    minWithdrawAmount() {
      switch(this.type) {
        case '1':  // coins
          return 10000;
        case '2':  // money
          return 10000;
        case '3':  // txbt
          return 20;
        case '4':  // dyxz
          return 20;
        case '5':  // dayred
          return 20;
        case '6':  // guquan
          return 20;
        default:
          return 20;
      }
    },
    availableBalance() {
      switch(this.type) {
        case '1':  
          return this.userInfo.coins || 0;
        case '2':
          return this.userInfo.txbt|| 0;
        case '3': 
          return this.userInfo.dyxz || 0;
        case '4':  
          return this.userInfo.dayred || 0;
        case '5':  
          return this.userInfo.money || 0;
        case '6':  
          return this.userInfo.guquan || 0;
        default:
          return 0;
      }
    }
  },
  created() {
    this.type = this.$route.query.type || '';
    this.getUserInfo();
    this.getBankInfo();
  },
  methods: {
    async getUserInfo() {
      try {
        const res = await getUserInfo();
        this.userInfo = res.data;
      } catch (error) {
        console.error('获取用户信息失败:', error);
      }
    },
    async getBankInfo() {
      try {
        const res = await getBankInfo();
        if (res.code === 1 && res.data) {
          this.bankInfo = res.data;
        }
      } catch (error) {
        console.error('获取银行卡信息失败:', error);
      }
    },
    formatBankNumber(number) {
      if (!number) return '';
      const str = String(number);
      return `(${str.slice(0, 4)}****${str.slice(-4)})`;
    },
    handleBankClick() {
      if (!this.bankInfo || !this.bankInfo.status) {
        this.$router.push('/bank-card');
      }
    },
    setMaxAmount() {
      this.amount = this.availableBalance;
    },
    togglePassword() {
      this.showPassword = !this.showPassword;
    },
    showTips(message) {
      this.tipsMessage = message;
      this.showTipsDialog = true;
    },
    async handleWithdraw() {
      // 检查是否绑定银行卡
      if (!this.bankInfo || !this.bankInfo.status) {
        this.showTips('请先绑定银行卡');
        return;
      }

      // 检查实名认证
      // if (this.userInfo.if_real === 0) {
      //   this.showAuthDialog = true;
      //   return;
      // }

      if (!this.amount) {
        this.showTips('请输入提现金额');
        return;
      }
      // 取消前端金额校验
      // if (this.amount < this.minWithdrawAmount) {
      //   this.showTips(`提现金额不能小于${this.minWithdrawAmount}元`);
      //   return;
      // }
      if (!this.password) {
        this.showTips('请输入登录密码');
        return;
      }

      try {
        const params = {
          type: this.type || '3',
          price: this.amount,
          payPwd: this.password
        };
        
        let res = await withdrawCoin(params);
        
        // 只提示后端返回内容
        this.showTips(res.msg);
        await this.getUserInfo();
      } catch (error) {
        console.error('提现失败:', error);
        this.showTips('提现失败，请重试');
      }
    },
    goToAuth() {
      this.$router.push('/user-center');
    }
  }
}
</script>

<style scoped>
.vh100 {
  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;
}

.warning-section {
  padding: 20px;
  color: #fff;
}

.warning-title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 15px;
}

.warning-content {
  font-size: 14px;
  line-height: 1.6;
}

.withdraw-form {
  background: #fff;
  border-radius: 8px;
  margin: 20px;
  padding: 20px;
  margin-bottom: 40px;
}

.form-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 15px;
  
}
.form-item1 {
  margin-bottom: 15px;
  
}
.label {
  font-size: 14px;
  color: #333;
  font-weight: bold;
  width: 70px;
  flex-shrink: 0;
  padding-top: 10px;
}

.form-content {
  flex: 1;
  min-width: 0;
}

.amount-input {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #eee;
  padding-bottom: 10px;
}

.currency {
  font-size: 16px;
  font-weight: bold;
  margin-right: 10px;
}

.input-field {
  width: 100%;
  height: 40px;
  border: none;
  font-size: 14px;
  outline: none;
  background: transparent;
}

.available-amount {
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}

.amount-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #666;
  font-size: 14px;
}

.withdraw-all {
  color: #FF6B6B;
  text-decoration: underline;
  font-weight: bold;
}

.bank-detail {
  padding: 12px 0;
  border-bottom: 1px solid #eee;
  cursor: pointer;
}

.bank-name {
  font-size: 16px;
  color: #333;
  line-height: 1.5;
  font-weight: 500;
}

.bank-number {
  font-size: 14px;
  color: #666;
  line-height: 1.5;
  margin-top: 4px;
}

.no-bank {
  color: #FF6B6B;
}

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

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

.custom-dialog-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.custom-dialog {
  width: 280px;
  background: #fff;
  border-radius: 12px;
  padding: 20px;
  text-align: center;
  animation: dialogFadeIn 0.3s ease;
  position: relative;
}

.dialog-icon {
  width: 60px;
  height: 60px;
  margin: 0 auto 16px;
}

.auth-icon {
  width: 100%;
  height: 100%;
}

.dialog-title {
  font-size: 18px;
  font-weight: 500;
  margin-bottom: 12px;
}

.auth-title {
  color: #1890FF;
}

.dialog-content {
  font-size: 14px;
  color: #666;
  line-height: 1.8;
  margin: 16px 0 24px;
}

.dialog-btn {
  width: 100%;
  height: 40px;
  border: none;
  border-radius: 20px;
  font-size: 16px;
  color: #fff;
  cursor: pointer;
  transition: all 0.3s ease;
}

.auth-btn {
  background: #1890FF;
}

.auth-btn:hover {
  background: #40a9ff;
}

.auth-dialog {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

@keyframes dialogFadeIn {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.dialog-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.dialog-close:hover {
  transform: rotate(90deg);
}

.dialog-close .close-icon {
  position: relative;
  width: 16px;
  height: 16px;
}

.dialog-close .close-icon::before,
.dialog-close .close-icon::after {
  content: '';
  position: absolute;
  width: 100%;
  height: 2px;
  background-color: #999;
  top: 50%;
  left: 0;
}

.dialog-close .close-icon::before {
  transform: rotate(45deg);
}

.dialog-close .close-icon::after {
  transform: rotate(-45deg);
}

.tips-dialog {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.tips-btn {
  background: #ed4b39;
}

.tips-btn:hover {
  background: #d43b2a;
}

.dialog-title {
  font-size: 18px;
  font-weight: 500;
  margin-bottom: 12px;
  color: #333;
}
</style> 