---
import Layout from '../../layouts/Layout.astro';
import Header from '../../components/layout/Header';
import Footer from '../../components/layout/Footer';

const token = Astro.cookies.get('session')?.value;
if (!token) return Astro.redirect('/login');

// Fetch user data
const userRes = await fetch(new URL('/api/auth/me', Astro.url).toString(), {
  headers: { 'Authorization': `Bearer ${token}` }
});
const { user } = await userRes.json();
---

<Layout title="บัญชีของฉัน">
  <Header />
  
  <div class="container mx-auto px-4 py-8">
    <h1 class="text-2xl font-bold mb-8">บัญชีของฉัน</h1>
    
    <div class="grid md:grid-cols-3 gap-8">
      <!-- Main Content -->
      <div class="md:col-span-2 space-y-6">
        <!-- Profile Info -->
        <div class="bg-white rounded-xl p-6 shadow-sm">
          <div class="flex items-center justify-between mb-6">
            <h2 class="font-bold text-lg">ข้อมูลส่วนตัว</h2>
            <button class="text-blue-600 hover:underline text-sm">แก้ไข</button>
          </div>
          
          <div class="flex items-center gap-4 mb-6">
            <div class="w-20 h-20 bg-blue-100 rounded-full flex items-center justify-center text-2xl font-bold text-blue-600">
              {user?.name?.charAt(0) || 'U'}
            </div>
            <div>
              <p class="font-medium text-lg">{user?.name || 'ผู้ใช้งาน'}</p>
              <p class="text-gray-500">{user?.email}</p>
            </div>
          </div>
          
          <dl class="space-y-3">
            <div class="flex border-b pb-3">
              <dt class="w-32 text-gray-500">ชื่อ-นามสกุล</dt>
              <dd class="font-medium">{user?.name || '-'}</dd>
            </div>
            <div class="flex border-b pb-3">
              <dt class="w-32 text-gray-500">อีเมล</dt>
              <dd class="font-medium">{user?.email || '-'}</dd>
            </div>
            <div class="flex border-b pb-3">
              <dt class="w-32 text-gray-500">เบอร์โทร</dt>
              <dd class="font-medium">{user?.phone || '-'}</dd>
            </div>
            <div class="flex">
              <dt class="w-32 text-gray-500">สถานะ</dt>
              <dd>
                <span class={`px-3 py-1 rounded-full text-sm ${
                  user?.role === 'admin' ? 'bg-purple-100 text-purple-700' :
                  user?.role === 'vendor' ? 'bg-blue-100 text-blue-700' :
                  'bg-gray-100 text-gray-700'
                }`}>
                  {user?.role === 'admin' ? 'ผู้ดูแล' : user?.role === 'vendor' ? 'ร้านค้า' : 'ลูกค้า'}
                </span>
              </dd>
            </div>
          </dl>
        </div>
        
        <!-- Vendor Access -->
        {user?.role === 'vendor' && (
          <div class="bg-gradient-to-r from-blue-50 to-purple-50 rounded-xl p-6 border border-blue-100">
            <h2 class="font-bold text-lg mb-2">ร้านค้าของฉัน</h2>
            <p class="text-gray-600 mb-4">เข้าถึงหน้าจัดการร้านค้าเพื่อเพิ่มสินค้าและติดตามคำสั่งซื้อ</p>
            <a href="/vendor/dashboard" class="inline-flex items-center gap-2 bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition-colors">
              <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2V6zM14 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2V6zM4 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2v-2zM14 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2v-2z" />
              </svg>
              เข้าสู่หน้าจัดการร้านค้า
            </a>
          </div>
        )}
        
        <!-- Admin Access -->
        {user?.role === 'admin' && (
          <div class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-xl p-6 border border-purple-100">
            <h2 class="font-bold text-lg mb-2">ผู้ดูแลระบบ</h2>
            <p class="text-gray-600 mb-4">จัดการร้านค้า ผู้ใช้งาน และคำสั่งซื้อทั้งหมด</p>
            <a href="/admin/dashboard" class="inline-flex items-center gap-2 bg-purple-600 text-white px-4 py-2 rounded-lg hover:bg-purple-700 transition-colors">
              <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
              </svg>
              เข้าสู่หน้าผู้ดูแล
            </a>
          </div>
        )}
      </div>
      
      <!-- Sidebar -->
      <div class="space-y-6">
        <div class="bg-white rounded-xl p-6 shadow-sm">
          <h3 class="font-bold mb-4">เมนูบัญชี</h3>
          <nav class="space-y-1">
            <a href="/account" class="flex items-center gap-3 py-2 px-3 rounded-lg bg-blue-50 text-blue-700 font-medium">
              <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
              </svg>
              ข้อมูลส่วนตัว
            </a>
            <a href="/account/orders" class="flex items-center gap-3 py-2 px-3 rounded-lg text-gray-600 hover:bg-gray-50 transition-colors">
              <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" />
              </svg>
              คำสั่งซื้อของฉัน
            </a>
            <a href="/wishlist" class="flex items-center gap-3 py-2 px-3 rounded-lg text-gray-600 hover:bg-gray-50 transition-colors">
              <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z" />
              </svg>
              สินค้าที่ชอบ
            </a>
            <a href="/account/addresses" class="flex items-center gap-3 py-2 px-3 rounded-lg text-gray-600 hover:bg-gray-50 transition-colors">
              <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z" />
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z" />
              </svg>
              ที่อยู่จัดส่ง
            </a>
          </nav>
          
          <hr class="my-4" />
          
          <a href="/api/auth/logout" class="flex items-center gap-3 py-2 px-3 rounded-lg text-red-600 hover:bg-red-50 transition-colors">
            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1" />
            </svg>
            ออกจากระบบ
          </a>
        </div>
      </div>
    </div>
  </div>
  
  <Footer />
</Layout>
