<!DOCTYPE html>
<html lang="en">
<head> 
  <meta charset="UTF-8"> 
  <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, viewport-fit=cover"> 
  <title>{{ config('app.name') }} | Team</title> 
  
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/layui@2.8.17/dist/css/layui.css">
  <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">

  <style>
    :root {
      --accent-black: #1a1a1a;
      --accent-gradient: linear-gradient(135deg, #1a1a1a 0%, #3a3a3a 100%);
      --bg-light: #eef7ff;
    }

    * {
      box-sizing: border-box;
    }

    body {
      font-family: 'Roboto', sans-serif;
      background-color: var(--bg-light);
      margin: 0; padding: 0;
      padding-bottom: 80px;
      animation: fadeInBody 0.5s ease;
    }

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

    @keyframes fadeInUp {
      from { opacity: 0; transform: translateY(15px); }
      to { opacity: 1; transform: translateY(0); }
    }

    @keyframes popIn {
      from { opacity: 0; transform: scale(0.94); }
      to { opacity: 1; transform: scale(1); }
    }

    @keyframes slideDown {
      from { opacity: 0; transform: translateY(-12px); }
      to { opacity: 1; transform: translateY(0); }
    }

    /* 🔹 Header */
    .top-header {
      padding: 15px 20px;
      background: var(--bg-light);
      text-align: center;
      animation: slideDown 0.4s ease both;
    }
    .top-header h2 { font-size: 20px; font-weight: 700; color: #1e293b; margin: 0; }

    /* 🔹 Team Summary Card */
    .team-card {
      margin: 10px 15px;
      padding: 20px;
      background: var(--accent-gradient);
      border-radius: 15px;
      color: white;
      display: flex;
      justify-content: space-around;
      align-items: center;
      position: relative;
      box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
      animation: popIn 0.5s ease 0.05s both;
      transition: transform 0.3s ease, box-shadow 0.3s ease;
    }

    .team-card:hover {
      transform: translateY(-3px);
      box-shadow: 0 8px 22px rgba(0, 0, 0, 0.28);
    }

    .team-card .stat-item { text-align: center; flex: 1; }
    .team-card .stat-item:not(:last-child) { border-right: 1px solid rgba(255,255,255,0.3); }
    .team-card h3 { font-size: 22px; margin: 0; font-weight: 800; }
    .team-card p { font-size: 13px; opacity: 0.9; margin: 5px 0 0; }

    /* 🔹 Invite Section */
    .invite-box {
      background: #fff; margin: 15px; padding: 20px; border-radius: 12px; text-align: center;
      animation: fadeInUp 0.4s ease 0.15s both;
      transition: box-shadow 0.3s ease;
    }
    .invite-box:hover { box-shadow: 0 8px 20px rgba(0,0,0,0.06); }
    .invite-code { color: #cbd5e1; font-size: 24px; font-weight: 700; letter-spacing: 1px; margin-bottom: 5px; }
    .invite-label { font-size: 12px; color: #94a3b8; margin-bottom: 15px; }
    .link-container {
      background: var(--bg-light); padding: 12px; border-radius: 30px;
      font-size: 13px; color: #64748b; word-break: break-all; margin-bottom: 15px; border: 1px dashed #cbd5e1;
      transition: border-color 0.25s ease, background 0.25s ease;
    }
    .link-container:hover { border-color: var(--accent-black); }
    .btn-copy {
      background: var(--accent-gradient); color: white; border: none; width: 100%;
      padding: 12px; border-radius: 30px; font-weight: 600; font-size: 16px;
      cursor: pointer;
      transition: transform 0.2s ease, box-shadow 0.25s ease;
    }
    .btn-copy:hover { box-shadow: 0 6px 14px rgba(0,0,0,0.2); }
    .btn-copy:active { transform: scale(0.97); }

    /* 🔹 Team Levels */
    .level-container { padding: 0 15px; }
    .level-container h4 { animation: fadeInUp 0.4s ease 0.2s both; }
    .level-card {
      background: #fff; border-radius: 12px; padding: 15px; margin-bottom: 15px;
      display: flex; justify-content: space-between; align-items: center; position: relative;
      animation: fadeInUp 0.4s ease both;
      transition: transform 0.25s ease, box-shadow 0.25s ease;
    }

    .level-card:nth-of-type(1) { animation-delay: 0.25s; }
    .level-card:nth-of-type(2) { animation-delay: 0.3s; }
    .level-card:nth-of-type(3) { animation-delay: 0.35s; }

    .level-card:hover {
      transform: translateY(-3px);
      box-shadow: 0 10px 22px rgba(0,0,0,0.07);
    }

    .level-info { flex: 1; }
    .level-info .grid { display: grid; grid-template-columns: 1fr 1fr; margin-bottom: 10px; }
    .level-info .grid div p:first-child { font-size: 12px; color: #94a3b8; margin: 0; }
    .level-info .grid div p:last-child { font-size: 16px; font-weight: 700; color: #1e293b; margin: 2px 0; }
    .level-reward { font-size: 13px; color: #3b82f6; font-weight: 500; }
    
    .btn-view {
      border: 1px solid var(--accent-black); color: var(--accent-black); background: transparent;
      padding: 5px 15px; border-radius: 6px; font-size: 12px; margin-top: 10px;
      cursor: pointer;
      transition: background 0.25s ease, color 0.25s ease, transform 0.15s ease;
    }

    .btn-view:hover { background: var(--accent-black); color: #fff; }
    .btn-view:active { transform: scale(0.96); }

    .level-badge {
      width: 50px;
      transition: transform 0.3s ease;
    }
    .level-card:hover .level-badge { transform: scale(1.08) rotate(-3deg); }
    .level-tag { position: absolute; bottom: 15px; right: 15px; font-weight: 800; font-style: italic; color: #1e293b; font-size: 18px; }

    /* 🔹 Modal Content */
    #modal-content { display: none; padding: 15px; }
  </style>
</head> 
<body> 

  <div class="top-header">
    <h2>Team Center</h2>
  </div>

  <div class="team-card">
    <div class="stat-item">
      <h3>{{ $team_size }}</h3>
      <p>Team Size</p>
    </div>
    <div class="stat-item">
      <h3>
        @php
            $total_comm = \App\Models\UserLedger::where('user_id', auth()->id())->where('reason', 'commission')->sum('amount');
        @endphp
        ₱{{ ($total_comm) }}
      </h3>
      <p>Total Commission</p>
    </div>
  </div>

  <div class="invite-box">
    <div class="invite-code">{{ auth()->user()->ref_id }}</div>
    <div class="invite-label">Invitation code</div>
    <div class="link-container" id="invite-link">
      {{ route('register', ['inviteCode' => auth()->user()->ref_id]) }}
    </div>
    <button class="btn-copy" onclick="copyText()">Copy link</button>
  </div>

 <div class="level-container">
    <h4 style="margin-bottom: 15px; color: #475569;">Team Division</h4>

    <div class="level-card">
      <div class="level-info">
        <div class="grid">
          <div><p>Size</p><p>{{ $first_level_users->count() }}</p></div>
          <div><p>Commission</p><p style="color:var(--accent-black)">₱{{ ($comm1) }}</p></div>
        </div>
        <div class="level-reward">32% team member reward</div>
        <button class="btn-view" onclick="showTeam('Level 1 Members', 'lvl1')">View now</button>
      </div>
      <img src="rocal/ic_level1.cde7cd4a.png" class="level-badge">
      <div class="level-tag">Level <span style="color:var(--accent-black)">1</span> Team</div>
    </div>

    <div class="level-card">
      <div class="level-info">
        <div class="grid">
          <div><p>Size</p><p>{{ $second_level_users->count() }}</p></div>
          <div><p>Commission</p><p style="color:var(--accent-black)">₱{{ ($comm2) }}</p></div>
        </div>
        <div class="level-reward">1% team member reward</div>
        <button class="btn-view" onclick="showTeam('Level 2 Members', 'lvl2')">View now</button>
      </div>
      <img src="rocal/ic_level2.c312183b.png" class="level-badge">
      <div class="level-tag">Level <span style="color:var(--accent-black)">2</span> Team</div>
    </div>

    <div class="level-card">
      <div class="level-info">
        <div class="grid">
          <div><p>Size</p><p>{{ $third_level_users->count() }}</p></div>
          <div><p>Commission</p><p style="color:var(--accent-black)">₱{{ ($comm3) }}</p></div>
        </div>
        <div class="level-reward">1% team member reward</div>
        <button class="btn-view" onclick="showTeam('Level 3 Members', 'lvl3')">View now</button>
      </div>
      <img src="rocal/ic_level3.de64c7b2.png" class="level-badge"> 
      <div class="level-tag">Level <span style="color:var(--accent-black)">3</span> Team</div>
    </div>
</div>
  <div id="modal-content">
    <table class="layui-table" lay-skin="line">
      <thead>
        <tr>
          <th>Account</th>
          <th>Income</th>
          <th>Status</th>
        </tr>
      </thead>
      <tbody id="member-list"></tbody>
    </table>
  </div>

<style>
  :root {
    --accent-black: #1a1a1a;
    --inactive-color: #94a3b8;
    --nav-bg: rgba(255, 255, 255, 0.95);
  }

  .foot {
    position: fixed;
    bottom: 0;
    width: 100%;
    background: var(--nav-bg);
    backdrop-filter: blur(10px);
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding: 8px 0;
    border-top: 1px solid rgba(0,0,0,0.05);
    z-index: 9999;
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
    box-shadow: 0 -5px 20px rgba(0, 0, 0, 0.03);
  }

  .foot ul {
    width: 100%;
    display: flex;
    margin: 0;
    padding: 0;
    list-style: none;
    align-items: flex-end;
  }

  .foot li {
    flex: 1;
    text-align: center;
    position: relative;
  }

  .foot li a {
    text-decoration: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    color: var(--inactive-color);
    transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  }

  .foot li img {
    width: 28px;
    height: 28px;
    margin-bottom: 4px;
    filter: grayscale(1) opacity(0.4);
    transition: all 0.4s ease;
  }

  .foot li p {
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin: 0;
  }

  .foot li.cur a { color: var(--accent-black); }
  .foot li.cur img {
    filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.25));
    transform: translateY(-5px) scale(1.2);
  }
  .foot li.cur::after {
    content: '';
    position: absolute;
    bottom: -5px;
    left: 50%;
    transform: translateX(-50%);
    width: 4px;
    height: 4px;
    background: var(--accent-black);
    border-radius: 50%;
  }
</style>

<div class="foot">
<ul>
  <li><a href="/dashboard"><img src="https://cdn-icons-png.flaticon.com/512/1946/1946436.png" alt="Home"><p>Home</p></a></li>
  <li><a href="/ordered"><img src="https://cdn-icons-png.flaticon.com/512/1170/1170678.png" alt="Order"><p>Order</p></a></li>
  <li class="cur"><a href="/my-team"><img src="https://cdn-icons-png.flaticon.com/512/912/912318.png" alt="Team"><p>Team</p></a></li>
  <li><a href="/my-profile"><img src="https://cdn-icons-png.flaticon.com/512/1077/1077063.png" alt="Mine"><p>Mine</p></a></li>
</ul>
</div>

  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/layui@2.8.17/dist/layui.js"></script>

  <script>
    const teamData = {
        lvl1: [
            @foreach($first_level_users as $u)
            { phone: '{{ substr($u->phone, 0, 3) }}****{{ substr($u->phone, -3) }}', income: '{{ ($u->total_income ?? 0) }}', status: 'Active' },
            @endforeach
        ],
        lvl2: [
            @foreach($second_level_users as $u)
            { phone: '{{ substr($u->phone, 0, 3) }}****{{ substr($u->phone, -3) }}', income: '{{ ($u->total_income ?? 0) }}', status: 'Active' },
            @endforeach
        ],
        lvl3: [
            @foreach($third_level_users as $u)
            { phone: '{{ substr($u->phone, 0, 3) }}****{{ substr($u->phone, -3) }}', income: '{{ ($u->total_income ?? 0) }}', status: 'Active' },
            @endforeach
        ]
    };

    function showTeam(title, level) {
      let html = '';
      const list = teamData[level];
      if(list.length === 0) {
          html = '<tr><td colspan="3" style="text-align:center;">No members found</td></tr>';
      } else {
          list.forEach(item => {
              html += `<tr>
                  <td>${item.phone}</td>
                  <td>${item.income}</td>
                  <td><span class="layui-badge-dot layui-bg-green"></span> ${item.status}</td>
              </tr>`;
          });
      }
      $('#member-list').html(html);
      layui.use('layer', function(){
        layui.layer.open({
          type: 1, title: title, area: ['90%', '60%'],
          content: $('#modal-content'), shadeClose: true, skin: 'layui-layer-rounded'
        });
      });
    }

    function copyText() {
      var text = $("#invite-link").text().trim();
      navigator.clipboard.writeText(text).then(function() {
        layui.layer.msg('Link Copied Successfully!');
      });
    }
  </script>
</body>
</html>
