<template>
  <div class="profile-page">
    <!-- Add Toast component -->
    <Toast ref="toast" />
    
    <!-- 用户信息卡片 -->
    <div class="user-card">
      <div class="user-header">
        <div class="user-info">
          <div class="avatar">
            <img src="@/assets/hongqi.png" alt="红旗">
          </div>
          <div class="user-details">
            <div class="info-item">
              <span class="label">账号:</span>
              <span class="value">{{ userInfo.mobile }}</span>
            </div>
            <div class="info-item">
              <span class="label">邀请码:</span>
              <span class="value">{{ userInfo.salt }}</span>
            </div>
          </div>
        </div>
        <div class="sign-action" @click="goToSignin">
          <img src="@/assets/sign.png" alt="签到" />
        </div>
      </div>
    </div>

    <!-- 资产概览 -->
    <div class="assets-section">
      <div class="section-title">资产概览</div>
      <div class="assets-grid">
        <div class="asset-card">
          <div class="asset-header">
            <span class="asset-label">红旗资产</span>
            <button class="withdraw-btn" @click="handleRedFlagWithdraw">提现</button>
          </div>
          <div class="asset-amount">￥{{ userInfo.coins }}</div>
        </div>
        <div class="asset-card">
          <div class="asset-header">
            <span class="asset-label">退休补贴</span>
            <button class="withdraw-btn" @click="handleRetirementWithdraw">提现</button>
          </div>
          <div class="asset-amount">￥{{ userInfo.txbt ? userInfo.txbt : 0 }}</div>
        </div>
        <div class="asset-card">
          <div class="asset-header">
            <span class="asset-label">党员薪资</span>
            <button class="withdraw-btn" @click="handlePartySalaryWithdraw">提现</button>
          </div>
          <div class="asset-amount">￥{{ userInfo.dyxz ? userInfo.dyxz : 0 }}</div>
        </div>
        <div class="asset-card">
          <div class="asset-header">
            <span class="asset-label">每日现金</span>
            <button class="withdraw-btn" @click="handleDailyCashWithdraw">提现</button>
          </div>
          <div class="asset-amount">￥{{ userInfo.dayred }}</div>
        </div>
        <div class="asset-card">
          <div class="asset-header">
            <span class="asset-label">原始股权</span>
            <button class="withdraw-btn" @click="handleOriginalSharesWithdraw">兑换</button>
          </div>
          <div class="asset-amount">￥{{ userInfo.guquan }}</div>
        </div>
      </div>
    </div>

    <!-- 常用工具 -->
    <div class="tools-section">
      <div class="section-title">常用工具</div>
      <div class="tools-grid">
        <div class="tool-item" @click="$router.push('/user-center')">
          <div class="tool-icon">
            <img src="@/assets/221.png" alt="实名认证" />
          </div>
          <span class="tool-name">实名认证</span>
        </div>
        <div class="tool-item" @click="$router.push('/bank-card')">
          <div class="tool-icon">
            <img src="@/assets/222.png" alt="银行卡绑定" />
          </div>
          <span class="tool-name">银行卡绑定</span>
        </div>
        <div class="tool-item" @click="$router.push('/member')">
          <div class="tool-icon">
            <img src="@/assets/223.png" alt="我的团队" />
          </div>
          <span class="tool-name">我的团队</span>
        </div>
        <div class="tool-item" @click="$router.push('/order-list')">
          <div class="tool-icon">
            <img src="@/assets/224.png" alt="提现记录" />
          </div>
          <span class="tool-name">提现记录</span>
        </div>
        <div class="tool-item" @click="$router.push('/asset-center')">
          <div class="tool-icon">
            <img src="@/assets/225.png" alt="资金明细" />
          </div>
          <span class="tool-name">资金明细</span>
        </div>
        <div class="tool-item" @click="$router.push('/order-management')">
          <div class="tool-icon">
            <img src="@/assets/226.png" alt="股权管理" />
          </div>
          <span class="tool-name">股权管理</span>
        </div>
        <div class="tool-item" @click="$router.push('/retirement-subsidy')">
          <div class="tool-icon">
            <img src="@/assets/227.png" alt="退休补贴" />
          </div>
          <span class="tool-name">退休补贴</span>
        </div>
      </div>
    </div>

    <!-- 安全退出 -->
    <div class="logout-section">
      <button class="logout-btn" @click="handleLogout">安全退出</button>
    </div>

    <!-- 退出确认弹窗 -->
    <div v-if="showConfirm" class="dialog-overlay">
      <div class="gov-dialog confirm-dialog">
        <div class="dialog-header">
          <h3 class="dialog-title">系统提示</h3>
        </div>
        <div class="dialog-body">
          <div class="dialog-content">
            <p>确认退出登录吗？</p>
          </div>
          <div class="dialog-actions">
            <button class="gov-btn secondary" @click="cancelLogout">取消</button>
            <button class="gov-btn primary" @click="confirmLogout">确认</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getUserInfo } from '@/api/home';
import { logout, cashOut, withdrawPension } from '@/api/profile';
import Toast from '@/components/Toast.vue';

export default {
  name: 'UsreMei',
  components: {
    Toast
  },
  data() {
    return {
      userInfo: {},
      showConfirm: false
    }
  },
  mounted() {
    this.getUserInfo();
  },
  methods: {
    async getUserInfo() {
      const res = await getUserInfo();
      this.userInfo = res.data;
    },
    goToCharges() {
      this.$router.push('/charges');
    },
    goToFinancial() {
      this.$router.push('/financial');
    },
    goToSignin() {
      this.$router.push('/signin');
    },
    async handleCashOut() {
      try {
        await cashOut();
        this.$refs.toast.show('套现成功', 'success');
        // 刷新用户信息
        await this.getUserInfo();
      } catch (error) {
        console.error('套现失败:', error);
        this.$refs.toast.show('套现失败，请重试', 'error');
      }
    },
    async handlePensionWithdraw() {
      try {
        await withdrawPension();
        this.$refs.toast.show('提现成功', 'success');
        // 刷新用户信息
        await this.getUserInfo();
      } catch (error) {
        console.error('提现失败:', error);
        this.$refs.toast.show('提现失败，请重试', 'error');
      }
    },
    handleLogout() {
      this.showConfirm = true;
    },
    cancelLogout() {
      this.showConfirm = false;
    },
    toAppDownload() {
      window.open(this.userInfo.link[1].value, '_blank');
    },
    async confirmLogout() {
      try {
        await logout();
        this.$refs.toast.show('退出成功', 'success');
        setTimeout(() => {
          
          // 清除本地存储的token等信息
          localStorage.removeItem('token');
          // 跳转到登录页
          this.$router.replace('/login');
        }, 2000);
      } catch (error) {
        this.$refs.toast.show('退出失败，请重试', 'error');
      } finally {
        this.showConfirm = false;
      }
    },
    // 红旗资产提现
    handleRedFlagWithdraw() {
      this.$router.push('/mention?type=1');
      // this.$refs.toast.show('暂未开启', 'info');
    },
    // 退休补贴提现
    handleRetirementWithdraw() {
      this.$router.push('/mention?type=2');
      // this.$refs.toast.show('中央资金筹备中', 'info');
    },
    // 党员薪资提现
    handlePartySalaryWithdraw() {
      this.$router.push('/mention?type=3');
      // this.$refs.toast.show('统一打款', 'info');
    },
    // 每日现金提现
    handleDailyCashWithdraw() {
      this.$router.push('/mention?type=4');
    },
    // 医疗补贴提现
    handleMedicalWithdraw() {
      this.$router.push('/mention?type=5');
    },
    // 原始股权提现
    handleOriginalSharesWithdraw() {
      // this.$router.push('/mention?type=6');
      this.$refs.toast.show('等待开启兑换','info');
    },
  }
}
</script>

<style scoped>
.profile-page {
  min-height: 100vh;
  background: linear-gradient(to bottom, #2f9580 0%, #4fb3a1 100%);
  padding: 20px 16px 80px;
  box-sizing: border-box;
}

/* 用户信息卡片 */
.user-card {
  background: #ffffff;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 24px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.user-card .user-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.user-card .user-header .user-info {
  display: flex;
  align-items: center;
  gap: 16px;
  flex: 1;
}

.user-card .user-header .user-info .avatar {
  width: 80px;
  height: 80px;
  background: linear-gradient(135deg, #f0fdfb 0%, #f8fffe 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 3px solid #e0f2f1;
}

.user-card .user-header .user-info .avatar img {
  width: 40px;
  height: 40px;
  object-fit: contain;
}

.user-card .user-header .user-info .user-details {
  flex: 1;
}

.user-card .user-header .user-info .user-details .info-item {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.user-card .user-header .user-info .user-details .info-item:last-child {
  margin-bottom: 0;
}

.user-card .user-header .user-info .user-details .info-item .label {
  color: #6b7280;
  font-size: 14px;
  margin-right: 8px;
  min-width: 60px;
}

.user-card .user-header .user-info .user-details .info-item .value {
  color: #374151;
  font-size: 15px;
  font-weight: 600;
}

.user-card .user-header .sign-action {
  cursor: pointer;
  padding: 8px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.user-card .user-header .sign-action:hover {
  background: rgba(47, 149, 128, 0.1);
}

.user-card .user-header .sign-action img {
  width: 60px;
  height: 50px;
  object-fit: contain;
}

/* 区域标题 */
.section-title {
  color: #ffffff;
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 16px;
  padding-left: 8px;
  position: relative;
}

.section-title::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 16px;
  background: #ffffff;
  border-radius: 2px;
}

/* 资产概览 */
.assets-section {
  margin-bottom: 24px;
}

.assets-section .assets-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.asset-card {
  background: #ffffff;
  border-radius: 8px;
  padding: 16px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.asset-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15);
}

.asset-card .asset-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.asset-card .asset-header .asset-label {
  color: #374151;
  font-size: 14px;
  font-weight: 600;
}

.asset-card .asset-header .withdraw-btn {
  padding: 4px 12px;
  background: #2f9580;
  color: #ffffff;
  border: none;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.asset-card .asset-header .withdraw-btn:hover {
  background: #268371;
}

.asset-card .asset-amount {
  font-size: 20px;
  font-weight: 700;
  color: #2f9580;
}

/* 常用工具 */
.tools-section {
  margin-bottom: 32px;
}

.tools-section .tools-grid {
  background: linear-gradient(135deg, #2f9580 0%, #4fb3a1 100%);
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

.tool-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  cursor: pointer;
  padding: 16px 8px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.tool-item:hover {
  background: rgba(255, 255, 255, 0.1);
  transform: translateY(-2px);
}

.tool-item .tool-icon {
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tool-item .tool-icon img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  filter: brightness(0) invert(1);
}

.tool-item .tool-name {
  font-size: 13px;
  color: #ffffff;
  text-align: center;
  font-weight: 500;
  line-height: 1.3;
}

/* 安全退出 */
.logout-section {
  display: flex;
  justify-content: center;
}

.logout-section .logout-btn {
  width: 200px;
  padding: 14px 0;
  background: #ffffff;
  color: #374151;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.logout-section .logout-btn:hover {
  background: #f9fafb;
  border-color: #d1d5db;
}

/* 弹窗样式 */
.dialog-overlay {
  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;
  animation: fadeIn 0.3s ease;
}

.gov-dialog {
  width: 320px;
  background: #ffffff;
  border-radius: 12px;
  overflow: hidden;
  animation: slideIn 0.3s ease;
}

.gov-dialog .dialog-header {
  background: linear-gradient(135deg, #f8fffe 0%, #f0fdfb 100%);
  padding: 20px 24px;
  border-bottom: 1px solid #e5e7eb;
}

.gov-dialog .dialog-header .dialog-title {
  font-size: 18px;
  font-weight: 600;
  color: #2f9580;
  margin: 0;
  text-align: center;
}

.gov-dialog .dialog-body {
  padding: 32px 24px;
  text-align: center;
}

.gov-dialog .dialog-body .dialog-content {
  margin-bottom: 32px;
}

.gov-dialog .dialog-body .dialog-content p {
  font-size: 15px;
  color: #6b7280;
  margin: 0;
  line-height: 1.5;
}

.gov-dialog .dialog-body .dialog-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.gov-btn {
  padding: 12px 24px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid transparent;
  min-width: 80px;
}

.gov-btn.primary {
  background: #2f9580;
  color: #ffffff;
  border-color: #2f9580;
}

.gov-btn.primary:hover {
  background: #268371;
  border-color: #268371;
}

.gov-btn.secondary {
  background: #ffffff;
  color: #6b7280;
  border-color: #e5e7eb;
}

.gov-btn.secondary:hover {
  background: #f9fafb;
  border-color: #d1d5db;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

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

/* 移动端适配 */
@media (max-width: 768px) {
  .profile-page {
    padding: 16px 12px 80px;
  }
  
  .user-card {
    padding: 16px;
  }
  
  .user-card .user-header .user-info {
    gap: 12px;
  }
  
  .user-card .user-header .user-info .avatar {
    width: 60px;
    height: 60px;
  }
  
  .user-card .user-header .user-info .avatar img {
    width: 30px;
    height: 30px;
  }
  
  .user-card .user-header .user-info .user-details .info-item .label {
    font-size: 13px;
    min-width: 50px;
  }
  
  .user-card .user-header .user-info .user-details .info-item .value {
    font-size: 14px;
  }
  
  .user-card .user-header .sign-action img {
    width: 50px;
    height: 40px;
  }
  
  .assets-grid {
    gap: 10px;
  }
  
  .asset-card {
    padding: 12px;
  }
  
  .asset-card .asset-header .asset-label {
    font-size: 13px;
  }
  
  .asset-card .asset-amount {
    font-size: 18px;
  }
  
  .asset-card .asset-header .withdraw-btn {
    padding: 2px 8px;
    font-size: 11px;
  }
  
  .tools-section .tools-grid {
    padding: 16px;
    gap: 16px;
  }
  
  .tool-item {
    padding: 12px 4px;
    gap: 8px;
  }
  
  .tool-item .tool-icon {
    width: 40px;
    height: 40px;
  }
  
  .tool-item .tool-name {
    font-size: 12px;
  }
  
  .logout-section .logout-btn {
    width: 100%;
    max-width: 200px;
  }
  
  .gov-dialog {
    width: 300px;
    margin: 0 12px;
  }
  
  .gov-dialog .dialog-header {
    padding: 16px 20px;
  }
  
  .gov-dialog .dialog-header .dialog-title {
    font-size: 16px;
  }
  
  .gov-dialog .dialog-body {
    padding: 24px 20px;
  }
  
  .gov-dialog .dialog-body .dialog-content p {
    font-size: 14px;
  }
  
  .gov-dialog .dialog-body .dialog-actions {
    gap: 8px;
  }
  
  .gov-dialog .dialog-body .dialog-actions .gov-btn {
    padding: 10px 20px;
    font-size: 13px;
    min-width: 70px;
  }
}
</style> 