<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 style="width: 95%; margin: 0 auto;">
      <div style="margin-top: 20px;">温馨提示</div>
      <div style="margin-top: 10px;">所有提现经国务院批准，由中央财政部联合拨款支出，用于帮扶人民群众救助贫困家庭，全程无需任何的费用</div>
    </div>

    <div class="ptb20 bgPart plr20">
      <div class="bd_input ptb10 plr10 tc flex1 radius4">
        <div class="flex between">
          <span>当前币种</span>
          <span>人民币</span>
        </div>
      </div>
      <div class="ft18"></div>
      <div class="mt10">可用 {{ availableBalance }}元</div>
      <div class="mt10">提现最小 {{ minWithdrawAmount }}元</div>
      <div class="mt10">预计到账 {{ amount || '0.00' }}元</div>
    </div>

    <div class="plr20">
      <div class="mb20">
        <div>提现金额</div>
        <div class="flex alcenter between bdb1f">
          <input 
            type="number" 
            class="h40 flex1 input-uni" 
            placeholder="最低提现金额20"
            v-model="amount"
            pattern="[0-9]*"
          >
          <div class="flex alcenter">
            <span class="blue ft14 pr10 bdr_white50">元</span>
            <div class="pl10" @click="setMaxAmount">全部</div>
          </div>
        </div>
      </div>

      <div class="mb20">
        <div>登录密码</div>
        <div class="flex alcenter between bdb1f h40">
          <input 
            :type="showPassword ? 'text' : 'password'"
            class="h40 flex1 input-uni" 
            placeholder="请输入登录密码"
            v-model="password"
          >
          <img 
            :src="require(`@/assets/${showPassword ? 'eye-open.png' : 'eye-close.png'}`)" 
            class="eye-icon"
            @click="togglePassword"
          >
        </div>
      </div>

      <div 
        class="mt40 bgBlue radius4 ptb10 white ft14 tc mb10 withdraw-btn"
        @click="handleWithdraw"
      >
        提现
      </div>
    </div>
  </div>
</template>

<script>
import { getUserInfo } from '@/api/home';
import { withdrawCoin } from '@/api/profile';
export default {
  name: 'Mention',
  data() {
    return {
      userInfo: {},
      amount: '',
      password: '',
      showPassword: false,
      type: ''
    }
  },
  computed: {
    minWithdrawAmount() {
      switch(this.type) {
        case '1':
          return 10000;
        case '2':
          return 10000;
        default:
          return 20;
      }
    },
    availableBalance() {
      switch(this.type) {
        case '1':
          return this.userInfo.coins || 0;
        case '2':
          return this.userInfo.money || 0;
        default:
          return this.userInfo.dayred || 0;
      }
    }
  },
  created() {
    this.type = this.$route.query.type || '';
    this.getUserInfo();
  },
  methods: {
    async getUserInfo() {
      try {
        const res = await getUserInfo();
        this.userInfo = res.data;
      } catch (error) {
        console.error('获取用户信息失败:', error);
      }
    },
    setMaxAmount() {
      this.amount = this.availableBalance;
    },
    togglePassword() {
      this.showPassword = !this.showPassword;
    },
    async handleWithdraw() {
      if (!this.amount) {
        alert('请输入提现金额');
        return;
      }
      if (this.amount < this.minWithdrawAmount) {
        alert(`提现金额不能小于${this.minWithdrawAmount}元`);
        return;
      }
      if (!this.password) {
        alert('请输入登录密码');
        return;
      }

      try {
        const params = {
          type: this.type || '3',
          price: this.amount,
          payPwd: this.password
        };
        
        let res = await withdrawCoin(params);
        
        if(res.code == 1){
        alert('提现申请已提交')
        }else{
          
        alert(res.msg)
          
        }
        // 刷新用户信息
        await this.getUserInfo();
      } catch (error) {
        console.error('提现失败:', error);
        alert('提现失败，请重试');
      }
    }
  }
}
</script>

<style scoped>
.vh100 {
  min-height: 100vh;
  background:#fff;
  background-size: 100% 100%;
  background-position: 0 0;
  background-repeat: no-repeat;
}

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

.ptb20 { padding: 20px 0; }
.plr20 { padding: 0 20px; }
.ptb10 { padding: 10px 0; }
.plr10 { padding: 0 10px; }
.mt10 { margin-top: 10px; }
.mt40 { margin-top: 40px; }
.mb20 { margin-bottom: 20px; }
.mb10 { margin-bottom: 10px; }
.pr10 { padding-right: 10px; }
.pl10 { padding-left: 10px; }

.bgPart {
  background: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  margin :15px 0 ;
}

.bd_input {
  background: #fff;
  border: 1px solid #eee;
}

.flex { display: flex; }
.between { justify-content: space-between;padding:  10px; }
.alcenter { align-items: center; }
.tc { text-align: center; }
.flex1 { flex: 1; }
.radius4 { border-radius: 4px; }
.ft18 { font-size: 18px; }
.ft14 { font-size: 14px; }
.blue { color: #ed4b39; }
.white { color: #fff; }

.bdb1f {
  border-bottom: 1px solid #f5f5f5;
}

.bdr_white50 {
  border-right: 1px solid rgba(255,255,255,0.5);
}

.h40 {
  height: 40px;
  line-height: 40px;
}

.input-uni {
  border: none;
  outline: none;
  background: transparent;
  width: 100%;
  padding: 0 10px;
}

.eye-icon {
  width: 20px;
  height: 20px;
  cursor: pointer;
}

.bgBlue {
  background: #ed4b39;
  cursor: pointer;
}

.withdraw-btn:active {
  opacity: 0.9;
}
</style> 