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

      <!-- 提现表单 -->
      <div class="withdraw-form">
        <div class="form-item">
          <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]*"
                placeholder="请输入提现金额"
              >
            </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 class="submit-section">
          <button 
            class="submit-btn"
            @click="handleWithdraw"
          >
            立即提现
          </button>
        </div>
      </div>
    </div>

    <!-- 实名认证提示弹窗 -->
    <div v-if="showAuthDialog" class="custom-dialog-mask">
      <div class="custom-dialog auth-dialog">
        <div class="dialog-header">
          <div class="dialog-title">温馨提示</div>
          <div class="dialog-close" @click="showAuthDialog = false">
            <i class="close-icon"></i>
          </div>
        </div>
        <div class="dialog-icon">
          <svg viewBox="0 0 1024 1024" width="60px" height="60px" fill="#ff4d4f" aria-hidden="true">
            <path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm0 736c-159.1 0-288-128.9-288-288S352.9 224 512 224s288 128.9 288 288-128.9 288-288 288z"></path>
            <path d="M464 688a48 48 0 1 0 96 0 48 48 0 1 0-96 0zm72-112c4.4 0 8-3.6 8-8V296c0-4.4-3.6-8-8-8h-48c-4.4 0-8 3.6-8 8v272c0 4.4 3.6 8 8 8h48z"></path>
          </svg>
        </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-header">
          <div class="dialog-title">温馨提示</div>
          <div class="dialog-close" @click="showTipsDialog = false">
            <i class="close-icon"></i>
          </div>
        </div>
        <div class="dialog-icon">
          <svg viewBox="0 0 1024 1024" width="60px" height="60px" fill="#2f9580" aria-hidden="true">
            <path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm0 736c-159.1 0-288-128.9-288-288S352.9 224 512 224s288 128.9 288 288-128.9 288-288 288z"></path>
            <path d="M464 688a48 48 0 1 0 96 0 48 48 0 1 0-96 0zm72-112c4.4 0 8-3.6 8-8V296c0-4.4-3.6-8-8-8h-48c-4.4 0-8 3.6-8 8v272c0 4.4 3.6 8 8 8h48z"></path>
          </svg>
        </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>
.mention-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);
}

.warning-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;
}

.warning-content {
  padding: 20px;
  font-size: 14px;
  line-height: 1.8;
  color: #374151;
}

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

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

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

.form-content {
  width: 100%;
}

.amount-input {
  display: flex;
  align-items: center;
  background: #f8f9fa;
  border: 2px solid #e5e7eb;
  border-radius: 12px;
  padding: 16px;
  transition: border-color 0.3s ease;
}

.amount-input:focus-within {
  border-color: #2f9580;
  background: #fff;
}

.currency {
  font-size: 18px;
  font-weight: 600;
  color: #2f9580;
  margin-right: 12px;
}

.input-field {
  flex: 1;
  border: none;
  font-size: 16px;
  outline: none;
  background: transparent;
  color: #374151;
}

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

.available-amount {
  margin-bottom: 24px;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 12px;
  border: 1px solid #e5e7eb;
}

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

.withdraw-all {
  color: #2f9580;
  font-weight: 600;
  cursor: pointer;
  padding: 6px 12px;
  border-radius: 8px;
  background: rgba(47, 149, 128, 0.1);
  transition: all 0.3s ease;
}

.withdraw-all:hover {
  background: rgba(47, 149, 128, 0.2);
}

.bank-detail {
  background: #f8f9fa;
  border: 2px solid #e5e7eb;
  border-radius: 12px;
  padding: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.bank-detail:hover {
  border-color: #2f9580;
  background: #fff;
}

.bank-name {
  font-size: 16px;
  color: #374151;
  font-weight: 500;
  margin-bottom: 4px;
}

.bank-number {
  font-size: 14px;
  color: #6b7280;
}

.no-bank {
  color: #ef4444;
}

.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);
}

.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: 320px;
  background: #fff;
  border-radius: 16px;
  overflow: hidden;
  animation: dialogFadeIn 0.3s ease;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
}

.dialog-header {
  background: linear-gradient(135deg, #2f9580 0%, #4fb3a1 100%);
  color: #fff;
  padding: 20px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dialog-title {
  font-size: 18px;
  font-weight: 600;
  text-align: center;
}

.dialog-close {
  position: absolute;
  top: 50%;
  right: 15px;
  transform: translateY(-50%);
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
}

.dialog-close:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: translateY(-50%) rotate(90deg);
}

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

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

.close-icon::before {
  transform: translateY(-50%) rotate(45deg);
}

.close-icon::after {
  transform: translateY(-50%) rotate(-45deg);
}

.dialog-icon {
  width: 80px;
  height: 80px;
  margin: 30px auto 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dialog-content {
  font-size: 15px;
  color: #374151;
  line-height: 1.6;
  margin: 0 20px 30px;
  text-align: center;
}

.dialog-btn {
  width: calc(100% - 40px);
  height: 48px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  color: #fff;
  cursor: pointer;
  transition: all 0.3s ease;
  margin: 0 20px 20px;
  display: block;
}

.auth-btn {
  background: #2f9580;
}

.tips-btn {
  background: #2f9580;
}

.dialog-btn:hover {
  opacity: 0.9;
  transform: translateY(-1px);
}

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

@keyframes dialogFadeIn {
  from {
    opacity: 0;
    transform: scale(0.95) translateY(-20px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

/* 移动端适配 */
@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;
  }
  
  .warning-content {
    padding: 16px;
    font-size: 13px;
  }
  
  .withdraw-form {
    padding: 20px 16px;
  }
  
  .form-item {
    margin-bottom: 20px;
  }
  
  .amount-input {
    padding: 14px;
  }
  
  .available-amount {
    padding: 14px;
  }
  
  .bank-detail {
    padding: 14px;
  }
  
  .submit-btn {
    height: 48px;
    font-size: 15px;
  }
  
  .custom-dialog {
    width: 90%;
    max-width: 300px;
  }
  
  .dialog-header {
    padding: 16px;
  }
  
  .dialog-title {
    font-size: 16px;
  }
  
  .dialog-content {
    margin: 0 16px 24px;
    font-size: 14px;
  }
  
  .dialog-btn {
    width: calc(100% - 32px);
    margin: 0 16px 16px;
    height: 44px;
    font-size: 15px;
  }
}
</style> 