<?php
require_once '../app/Controllers/sessions.php';
require_once '../app/Controllers/security_access.php';
require_once '../app/models/user.php';
require '../app/Controllers/layouts.php';
require '../app/Controllers/updateProfile.php';
require_once '../app/Controllers/checkInController.php';
require_once '../app/Controllers/controller_investments.php';

$toastMessage = $_SESSION['toastMessage'] ?? null;
$toastType = $_SESSION['toastType'] ?? null;
unset($_SESSION['toastMessage'], $_SESSION['toastType']);
?>

<?php if ($stmtThemesPlatformResult['theme_actual'] === 'brasileira') : ?>
  <script>window.location.href = '?theme=gringa2';</script>
<?php elseif ($stmtThemesPlatformResult['theme_actual'] === 'gringa') : ?>
  <script>window.location.href = '?theme=gringa2';</script>
<?php elseif ($stmtThemesPlatformResult['theme_actual'] === 'gringa2' || true) : ?>
  <!DOCTYPE html>
  <html lang="pt-BR">

  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Meu Perfil 我的资料 - <?= htmlspecialchars($stmtConfigsPlatformResult["name_platform"]) ?></title>

    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Oswald:wght@400;500;700&family=Inter:wght@300;400;600;700;800&family=Share+Tech+Mono&family=Noto+Sans+SC:wght@400;700&display=swap" rel="stylesheet">
    <script src="https://unpkg.com/lucide@latest"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">

    <script>
      tailwind.config = {
        theme: {
          extend: {
            colors: {
              vip: {
                bg: '#F8F9FA',      // Fundo claro
                card: '#FFFFFF',    // Card branco
                text: '#333333',    // Texto escuro
                border: '#E5E7EB',  // Borda cinza claro
                salmon: '#FF7E67',  // Cor Principal (Salmão)
                salmonlight: '#FFA090',
                red: '#E53935',
                green: '#43A047',
                blue: '#2980B9',
                gray: '#9CA3AF'
              }
            },
            fontFamily: {
              sans: ['Inter', 'Noto Sans SC', 'sans-serif'],
              mono: ['Oswald', 'sans-serif'],
              tech: ['"Share Tech Mono"', 'monospace']
            },
            boxShadow: {
              'soft': '0 4px 20px rgba(0, 0, 0, 0.05)',
              'glow-salmon': '0 0 15px rgba(255, 126, 103, 0.4)',
              'card-float': '0 15px 35px -5px rgba(255, 126, 103, 0.5)'
            }
          }
        }
      };
    </script>

    <style>
      body {
        background-color: #F8F9FA;
        /* Grade cinza sutil */
        background-image: 
            linear-gradient(#E5E7EB 1px, transparent 1px),
            linear-gradient(90deg, #E5E7EB 1px, transparent 1px);
        background-size: 40px 40px;
        color: #333;
        -webkit-font-smoothing: antialiased;
        padding-bottom: 90px;
      }

      /* Header Clean */
      .vip-header {
        background: rgba(255, 255, 255, 0.95);
        backdrop-filter: blur(10px);
        border-bottom: 1px solid #E5E7EB;
        padding: 15px 20px;
        position: sticky;
        top: 0;
        z-index: 50;
        display: flex;
        align-items: center;
        justify-content: space-between;
        box-shadow: 0 2px 10px rgba(0,0,0,0.03);
      }

      /* Avatar Section */
      .profile-section {
        background: #FFF;
        padding: 30px 20px 50px 20px; /* Padding bottom extra para o card */
        text-align: center;
        position: relative;
        border-bottom: 1px solid #E5E7EB;
      }
      
      .avatar-ring {
        width: 80px; height: 80px;
        margin: 0 auto 10px;
        border-radius: 50%;
        padding: 3px;
        background: linear-gradient(135deg, #FF7E67, #FFA090);
        position: relative;
        box-shadow: 0 0 20px rgba(255, 126, 103, 0.2);
      }
      
      .avatar-img {
        width: 100%; height: 100%;
        border-radius: 50%;
        object-fit: cover;
        border: 2px solid #FFF;
        background: #F3F4F6;
      }

      .btn-camera {
        position: absolute; bottom: 0; right: 0;
        background: #FF7E67; color: #FFF;
        width: 24px; height: 24px;
        border-radius: 50%;
        display: flex; align-items: center; justify-content: center;
        border: 2px solid #FFF;
        cursor: pointer;
        transition: transform 0.2s;
        box-shadow: 0 2px 5px rgba(0,0,0,0.1);
      }
      .btn-camera:active { transform: scale(0.9); }

      /* CARTÃO PREMIUM (SALDO) */
      .premium-card-container {
        perspective: 1000px;
        margin: -40px 20px 20px 20px; /* Sobe sobre a profile-section */
        position: relative; z-index: 10;
      }

      .premium-card {
        background: linear-gradient(135deg, #FF7E67 0%, #FF6B50 100%);
        border-radius: 20px;
        padding: 20px;
        position: relative;
        overflow: hidden;
        box-shadow: var(--card-float);
        min-height: 180px;
        display: flex; flex-direction: column; justify-content: space-between;
        color: white;
      }

      .premium-card::after {
        content: ''; position: absolute; top: -50%; right: -50%; width: 100%; height: 100%;
        background: radial-gradient(circle, rgba(255,255,255,0.2) 0%, transparent 60%);
        pointer-events: none;
      }

      .card-chip {
        width: 40px; height: 30px;
        background: rgba(255,255,255,0.2);
        border-radius: 4px;
        margin-bottom: 10px;
        border: 1px solid rgba(255,255,255,0.3);
      }

      .card-label {
        font-size: 9px; color: rgba(255,255,255,0.9); text-transform: uppercase; letter-spacing: 1px;
        font-family: 'Inter', sans-serif; margin-bottom: 2px;
      }

      .card-balance {
        font-family: 'Share Tech Mono', monospace;
        font-size: 28px; color: #fff;
        text-shadow: 0 2px 4px rgba(0,0,0,0.1); letter-spacing: 1px; font-weight: bold;
      }

      .card-vip-tag {
        position: absolute; top: 20px; right: 20px;
        background: rgba(255,255,255,0.2);
        padding: 4px 10px; border-radius: 20px;
        font-weight: bold; font-size: 12px;
        border: 1px solid rgba(255,255,255,0.4);
      }

      .card-info-row {
        display: flex; justify-content: space-between; align-items: flex-end; margin-top: 15px;
      }
      
      .invested-display {
        color: #FFF; font-family: 'Oswald', sans-serif; font-size: 14px; letter-spacing: 1px; font-weight: bold;
      }

      /* Quick Actions */
      .quick-actions {
        display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px;
        padding: 0 20px 20px 20px;
      }
      .quick-btn-profile {
        background: #FFF; border: 1px solid #E5E7EB;
        padding: 12px 5px;
        display: flex; flex-direction: column; align-items: center; gap: 6px;
        transition: all 0.2s; border-radius: 12px;
        box-shadow: 0 2px 5px rgba(0,0,0,0.02);
      }
      .quick-btn-profile:hover { border-color: #FF7E67; transform: translateY(-2px); }
      .quick-btn-profile i { color: #9CA3AF; transition: color 0.2s; }
      .quick-btn-profile:hover i { color: #FF7E67; }

      /* Menu List */
      .menu-group { margin-top: 20px; padding: 0 20px; }
      .menu-label {
        font-size: 10px; color: #9CA3AF; text-transform: uppercase;
        letter-spacing: 1px; margin-bottom: 10px; margin-left: 5px; font-weight: bold;
      }

      .menu-item {
        background: #FFF; border: 1px solid #E5E7EB;
        margin-bottom: 8px; padding: 14px;
        display: flex; align-items: center; justify-content: space-between;
        transition: all 0.2s; cursor: pointer; border-radius: 8px;
        box-shadow: 0 2px 5px rgba(0,0,0,0.02);
      }
      .menu-item:hover { border-color: #FF7E67; box-shadow: 0 4px 10px rgba(255, 126, 103, 0.1); }
      .menu-item:active { transform: scale(0.99); }

      .menu-icon {
        width: 32px; height: 32px; background: #F9FAFB;
        border: 1px solid #E5E7EB;
        display: flex; align-items: center; justify-content: center;
        margin-right: 12px; color: #9CA3AF; border-radius: 6px;
      }
      
      .menu-item:hover .menu-icon { border-color: #FF7E67; color: #FF7E67; background: #FFF; }

      /* Form Senha */
      .form-overlay {
        background: #FFF; border: 1px solid #E5E7EB;
        padding: 20px; margin: 0 20px 20px 20px;
        display: none; border-radius: 12px;
        box-shadow: 0 10px 30px rgba(0,0,0,0.05);
      }
      .form-overlay.active { display: block; animation: fadeIn 0.3s; }

      .input-custom {
        background: #F9FAFB; border: 1px solid #E5E7EB;
        color: #333; padding: 12px; width: 100%;
        font-size: 13px; margin-bottom: 10px;
        outline: none; border-radius: 6px; transition: all 0.3s;
      }
      .input-custom:focus { border-color: #FF7E67; background: #FFF; }

      .btn-save-custom {
        background: #FF7E67; color: #FFF;
        font-family: 'Oswald', sans-serif; font-weight: bold;
        text-transform: uppercase; padding: 12px; width: 100%;
        border: none; border-radius: 6px;
        box-shadow: 0 4px 10px rgba(255, 126, 103, 0.3);
      }

      /* Toast Light */
      .toast-custom {
        background: #FFF; border: 1px solid #E5E7EB; border-left: 3px solid #FF7E67;
        color: #333; padding: 12px 20px; box-shadow: 0 10px 40px rgba(0,0,0,0.1);
        display: flex; align-items: center; gap: 10px;
        animation: slideInTop 0.4s forwards;
      }
      @keyframes slideInTop { from { transform: translateY(-100%); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
      @keyframes fadeIn { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: translateY(0); } }
    </style>
  </head>

  <body class="font-sans text-gray-800">

    <div id="toast-container" class="fixed top-4 left-0 right-0 z-[100] flex justify-center pointer-events-none"></div>

    <div class="max-w-[480px] mx-auto min-h-screen relative border-x border-gray-100 shadow-xl">

      <div class="vip-header">
        <h1 class="text-sm font-bold text-vip-salmon uppercase tracking-[2px]">Centro de Membros 个人中心</h1>
        <a href="logout.php" class="text-gray-500 hover:text-vip-red transition text-[10px] uppercase font-bold border border-gray-200 px-3 py-1 bg-white rounded hover:border-vip-red">
          Sair 退出
        </a>
      </div>

      <div class="profile-section">
       

        <h2 class="text-lg font-bold text-gray-800 font-mono tracking-widest"><?= htmlspecialchars($user['phone']) ?></h2>
        
        <div class="flex justify-center items-center gap-2 mt-1">
            <span class="text-gray-400 text-[10px] font-mono bg-gray-100 px-2 py-0.5 rounded">
               ID: <?= htmlspecialchars($user['id']) ?>
            </span>
        </div>
      </div>

      <div class="premium-card-container">
         <div class="premium-card">
            <div class="card-vip-tag">VIP <?= htmlspecialchars($user['vip_level'] ?? '0') ?></div>
            
            <div>
               <div class="card-chip"></div>
               <p class="card-label">Saldo Atual 余额</p>
               <p class="card-balance">R$ <?= number_format($user['balance'], 2, ',', '.') ?></p>
            </div>
            
            <div class="card-info-row">
               <div>
                  <p class="card-label">Investido 投入</p>
                  <p class="invested-display">R$ <?= number_format($totalInvested ?? 0, 2, ',', '.') ?></p>
               </div>
               <div>
                  <p class="card-label text-right">Status 状态</p>
                  <p class="text-[10px] text-white font-bold uppercase flex items-center gap-1 justify-end">
                     Ativo <div class="w-1.5 h-1.5 bg-green-400 rounded-full animate-pulse shadow-lg"></div>
                  </p>
               </div>
            </div>
         </div>
      </div>

      <div class="quick-actions">
         <a href="payment.php" class="quick-btn-profile">
            <i data-lucide="wallet" class="w-5 h-5"></i>
            <span class="text-[10px] text-gray-500 uppercase font-bold">Depósito</span>
         </a>
         <a href="withdrawal.php" class="quick-btn-profile">
            <i data-lucide="arrow-up-right" class="w-5 h-5"></i>
            <span class="text-[10px] text-gray-500 uppercase font-bold">Saque</span>
         </a>
         <a href="team.php" class="quick-btn-profile">
            <i data-lucide="users" class="w-5 h-5"></i>
            <span class="text-[10px] text-gray-500 uppercase font-bold">Equipe</span>
         </a>
         <a href="<?= htmlspecialchars($stmtConfigsPlatformResult['support_link']) ?>" target="_blank" class="quick-btn-profile">
            <i data-lucide="headset" class="w-5 h-5"></i>
            <span class="text-[10px] text-gray-500 uppercase font-bold">Suporte</span>
         </a>
      </div>

      <div class="menu-group">
         <p class="menu-label">Configurações 设置</p>
         
         <button onclick="togglePasswordForm()" class="menu-item w-full">
            <div class="flex items-center">
               <div class="menu-icon"><i data-lucide="lock" class="w-4 h-4"></i></div>
               <span class="text-xs font-bold text-gray-700 uppercase">Alterar Senha 修改密码</span>
            </div>
            <i data-lucide="chevron-right" class="w-4 h-4 text-gray-400"></i>
         </button>

         <div id="password-form" class="form-overlay">
            <h3 class="text-xs font-bold text-vip-salmon uppercase mb-3">Segurança 安全</h3>
            <form method="POST">
              <input required placeholder="Senha Atual 当前密码" minlength="6" name="actual_pass" type="password" class="input-custom" />
              <input required placeholder="Nova Senha 新密码" minlength="6" name="new_pass" type="password" class="input-custom" />
              <input required placeholder="Confirmar Senha 确认密码" minlength="6" name="confirm_new_pass" type="password" class="input-custom" />
              <button name="alter_pass" type="submit" class="btn-save-custom">
                ATUALIZAR SENHA 更新
              </button>
            </form>
         </div>

         <a href="cupom_rescue.php" class="menu-item">
            <div class="flex items-center">
               <div class="menu-icon"><i data-lucide="ticket" class="w-4 h-4"></i></div>
               <span class="text-xs font-bold text-gray-700 uppercase">Resgatar Cupom 兑换券</span>
            </div>
            <i data-lucide="chevron-right" class="w-4 h-4 text-gray-400"></i>
         </a>

         <a href="envelope.php" class="menu-item">
            <div class="flex items-center">
               <div class="menu-icon"><i data-lucide="mail-open" class="w-4 h-4"></i></div>
               <span class="text-xs font-bold text-gray-700 uppercase">Baú 红包</span>
            </div>
            <span class="text-[9px] bg-vip-salmon text-white px-2 py-0.5 font-bold uppercase rounded shadow-sm">HOT</span>
         </a>
      </div>

      <div class="menu-group mb-8">
         <p class="menu-label">Histórico 记录</p>

         <a href="history_dep.php" class="menu-item">
            <div class="flex items-center">
               <div class="menu-icon"><i data-lucide="arrow-down-left" class="w-4 h-4"></i></div>
               <span class="text-xs font-bold text-gray-700 uppercase">Logs de Depósito 充值记录</span>
            </div>
            <i data-lucide="chevron-right" class="w-4 h-4 text-gray-400"></i>
         </a>

         <a href="history_with.php" class="menu-item">
            <div class="flex items-center">
               <div class="menu-icon"><i data-lucide="arrow-up-right" class="w-4 h-4"></i></div>
               <span class="text-xs font-bold text-gray-700 uppercase">Logs de Saque 提现记录</span>
            </div>
            <i data-lucide="chevron-right" class="w-4 h-4 text-gray-400"></i>
         </a>

         <a href="history_colet.php" class="menu-item">
            <div class="flex items-center">
               <div class="menu-icon"><i data-lucide="coins" class="w-4 h-4"></i></div>
               <span class="text-xs font-bold text-gray-700 uppercase">Logs de Lucro 收益记录</span>
            </div>
            <i data-lucide="chevron-right" class="w-4 h-4 text-gray-400"></i>
         </a>

         <?php if (isset($_SESSION['admin_session']) && $_SESSION['admin_session']) : ?>
            <div class="mt-4">
              <a href="dash" class="menu-item bg-gray-50 border-vip-red">
                <div class="flex items-center">
                   <div class="menu-icon" style="color:#E53935; border-color:#E53935; background:#FFF;"><i data-lucide="shield-alert" class="w-4 h-4"></i></div>
                   <span class="text-xs font-bold text-vip-red uppercase">Painel Admin 管理员</span>
                </div>
                <i data-lucide="chevron-right" class="w-4 h-4 text-gray-400"></i>
              </a>
            </div>
         <?php endif; ?>
      </div>

      <?php include 'partials/menu-bottom.php'; ?>

    </div>

    <script>
       lucide.createIcons();

       // Toggle Form
       function togglePasswordForm() {
         const form = document.getElementById('password-form');
         form.classList.toggle('active');
       }

       // Upload Automático
       function enviarAutomaticamente(input) {
         if (input.files[0]) {
           document.getElementById('form-upload').submit();
         }
       }

       // Toast Light
       function showToast(msg, type) {
         const container = document.getElementById('toast-container');
         const toast = document.createElement('div');
         const isError = type === 'error';
         const iconColor = isError ? 'text-vip-red' : 'text-vip-green';
         const borderColor = isError ? 'border-l-vip-red' : 'border-l-vip-salmon';

         toast.className = `toast-custom pointer-events-auto ${borderColor}`;
         toast.innerHTML = `
           <i data-lucide="${isError ? 'alert-triangle' : 'check-circle'}" class="w-4 h-4 ${iconColor}"></i>
           <span class="font-mono text-xs uppercase tracking-wide font-bold">${msg}</span>
         `;

         container.appendChild(toast);
         lucide.createIcons();

         setTimeout(() => {
           toast.style.opacity = '0';
           toast.style.transform = 'translateY(-20px)';
           toast.style.transition = 'all 0.3s';
           setTimeout(() => toast.remove(), 400);
         }, 3000);
       }

       <?php if ($toastMessage) : ?>
         window.addEventListener('load', () => showToast('<?= htmlspecialchars($toastMessage) ?>', '<?= $toastType ?>'));
       <?php endif; ?>
    </script>
  </body>
  </html>
<?php endif; ?>