<?= $this->extend('layouts/owner') ?>

<?= $this->section('content') ?>

<style>
    /* ===== ENTERPRISE SAAS DESIGN SYSTEM TOKENS ===== */
    :root {
        --bg-body: #f8fafc;
        --bg-card: #ffffff;
        --bg-accent: #f1f5f9;
        --bg-input: #ffffff;
        --border-color: #e2e8f0;
        --border-hover: #cbd5e1;
        --text-main: #0f172a;
        --text-sub: #64748b;
        --primary: #4f46e5;
        --primary-hover: #4338ca;
        --primary-light: rgba(79, 70, 229, 0.08);
        --success: #10b981;
        --success-light: rgba(16, 185, 129, 0.1);
        --warning: #f59e0b;
        --warning-light: rgba(245, 158, 11, 0.12);
        --danger: #ef4444;
        --danger-light: rgba(239, 68, 68, 0.12);
        --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
        --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.07), 0 2px 4px -2px rgba(0, 0, 0, 0.05);
        --radius-lg: 16px;
        --radius-md: 12px;
        --radius-sm: 8px;
        --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    }

    /* DARK MODE COLOR VARIABLES */
    @media (prefers-color-scheme: dark) {
        :root {
            --bg-body: #0b0f19;
            --bg-card: #151e2e;
            --bg-accent: #1e293b;
            --bg-input: #0f172a;
            --border-color: #243044;
            --border-hover: #334155;
            --text-main: #f8fafc; /* Putih terang */
            --text-sub: #94a3b8;  /* Abu-abu terang */
            --primary-light: rgba(79, 70, 229, 0.2);
            --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
            --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
        }
    }

    [data-bs-theme="dark"] {
        --bg-body: #0b0f19;
        --bg-card: #151e2e;
        --bg-accent: #1e293b;
        --bg-input: #0f172a;
        --border-color: #243044;
        --border-hover: #334155;
        --text-main: #f8fafc; /* Putih terang */
        --text-sub: #94a3b8;  /* Abu-abu terang */
        --primary-light: rgba(79, 70, 229, 0.2);
        --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
        --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
    }

    /* GLOBAL WORKSPACE OVERRIDES FOR DARK MODE TEXT */
    body, .content-wrapper, .main-content, .content, .page-content, main, .category-enterprise-workspace {
        background-color: var(--bg-body) !important;
        color: var(--text-main) !important;
        transition: var(--transition);
    }

    .text-main {
        color: var(--text-main) !important;
    }

    .text-sub {
        color: var(--text-sub) !important;
    }

    /* OVERRIDE BOOTSTRAP DARK TEXT IN DARK MODE */
    @media (prefers-color-scheme: dark) {
        .text-dark, .text-body, .table, h1, h2, h3, h4, h5, h6, p, span, td, th, label, option {
            color: var(--text-main) !important;
        }
        .text-muted {
            color: var(--text-sub) !important;
        }
    }

    [data-bs-theme="dark"] .text-dark,
    [data-bs-theme="dark"] .text-body,
    [data-bs-theme="dark"] .table,
    [data-bs-theme="dark"] h1, [data-bs-theme="dark"] h2, [data-bs-theme="dark"] h3,
    [data-bs-theme="dark"] h4, [data-bs-theme="dark"] h5, [data-bs-theme="dark"] h6,
    [data-bs-theme="dark"] p, [data-bs-theme="dark"] span, [data-bs-theme="dark"] td,
    [data-bs-theme="dark"] th, [data-bs-theme="dark"] label, [data-bs-theme="dark"] option {
        color: var(--text-main) !important;
    }

    [data-bs-theme="dark"] .text-muted {
        color: var(--text-sub) !important;
    }

    /* Card Premium */
    .card-modern {
        background: var(--bg-card);
        border: 1px solid var(--border-color);
        border-radius: var(--radius-lg);
        box-shadow: var(--shadow-sm);
        transition: var(--transition);
    }

    /* Metric KPI Cards */
    .kpi-card {
        background: var(--bg-card);
        border: 1px solid var(--border-color);
        border-radius: var(--radius-md);
        padding: 1.25rem;
        transition: var(--transition);
    }

    .kpi-card:hover {
        border-color: var(--border-hover);
        transform: translateY(-2px);
        box-shadow: var(--shadow-md);
    }

    .kpi-icon-box {
        width: 44px;
        height: 44px;
        border-radius: var(--radius-md);
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.25rem;
        flex-shrink: 0;
    }

    /* Table Styling */
    .table-modern {
        margin-bottom: 0;
        color: var(--text-main) !important;
    }

    .table-modern th {
        background-color: var(--bg-accent) !important;
        color: var(--text-sub) !important;
        font-weight: 600;
        font-size: 0.75rem;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        padding: 1rem 1.25rem;
        border-bottom: 1px solid var(--border-color);
    }

    .table-modern td {
        padding: 1rem 1.25rem;
        border-bottom: 1px solid var(--border-color);
        background-color: transparent !important;
        vertical-align: middle;
        font-size: 0.875rem;
        color: var(--text-main) !important;
    }

    .table-modern tbody tr {
        transition: var(--transition);
    }

    .table-modern tbody tr:hover {
        background-color: var(--primary-light) !important;
    }

    /* Form & Input Controls */
    .form-control-modern, .form-select-modern {
        background-color: var(--bg-input) !important;
        border: 1px solid var(--border-color) !important;
        color: var(--text-main) !important;
        border-radius: var(--radius-sm);
        padding: 0.55rem 0.9rem;
        font-size: 0.875rem;
        transition: var(--transition);
    }

    .form-control-modern {
        padding-left: 2.4rem;
    }

    .form-control-modern:focus, .form-select-modern:focus {
        background-color: var(--bg-input) !important;
        color: var(--text-main) !important;
        border-color: var(--primary) !important;
        box-shadow: 0 0 0 3px var(--primary-light);
        outline: none;
    }

    /* Fix Dropdown Options Text Color */
    .form-select-modern option {
        background-color: var(--bg-card) !important;
        color: var(--text-main) !important;
    }

    .search-wrapper {
        position: relative;
    }

    .search-icon {
        position: absolute;
        left: 0.85rem;
        top: 50%;
        transform: translateY(-50%);
        color: var(--text-sub);
        pointer-events: none;
    }

    /* Category Icon Avatar */
    .category-avatar {
        width: 40px;
        height: 40px;
        border-radius: var(--radius-md);
        background-color: var(--primary-light);
        color: var(--primary);
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.15rem;
        flex-shrink: 0;
    }

    /* Buttons Styling */
    .btn-primary-modern {
        background-color: var(--primary);
        color: #ffffff !important;
        border: 1px solid var(--primary);
        border-radius: var(--radius-sm);
        padding: 0.55rem 1.15rem;
        font-weight: 600;
        font-size: 0.875rem;
        transition: var(--transition);
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
    }

    .btn-primary-modern:hover {
        background-color: var(--primary-hover);
        border-color: var(--primary-hover);
        color: #ffffff !important;
        box-shadow: var(--shadow-sm);
    }

    .btn-outline-modern {
        background-color: transparent;
        color: var(--text-main) !important;
        border: 1px solid var(--border-color);
        border-radius: var(--radius-sm);
        transition: var(--transition);
    }

    .btn-outline-modern:hover {
        background-color: var(--bg-accent);
        border-color: var(--border-hover);
        color: var(--text-main) !important;
    }

    .btn-action-warning {
        width: 34px;
        height: 34px;
        padding: 0;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        border-radius: var(--radius-sm);
        background-color: var(--warning-light);
        color: var(--warning) !important;
        border: 1px solid transparent;
        transition: var(--transition);
    }

    .btn-action-warning:hover {
        background-color: var(--warning);
        color: #ffffff !important;
    }

    .btn-action-danger {
        width: 34px;
        height: 34px;
        padding: 0;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        border-radius: var(--radius-sm);
        background-color: var(--danger-light);
        color: var(--danger) !important;
        border: 1px solid transparent;
        transition: var(--transition);
    }

    .btn-action-danger:hover {
        background-color: var(--danger);
        color: #ffffff !important;
    }

    /* Modern Pagination Styles */
    .pagination-modern .page-link {
        background-color: var(--bg-card) !important;
        border: 1px solid var(--border-color) !important;
        color: var(--text-main) !important;
        padding: 0.45rem 0.85rem;
        font-size: 0.85rem;
        border-radius: var(--radius-sm);
        margin: 0 2px;
        transition: var(--transition);
    }

    .pagination-modern .page-link:hover {
        background-color: var(--bg-accent) !important;
        border-color: var(--border-hover) !important;
        color: var(--primary) !important;
    }

    .pagination-modern .page-item.active .page-link {
        background-color: var(--primary) !important;
        border-color: var(--primary) !important;
        color: #ffffff !important;
        font-weight: 600;
    }

    .pagination-modern .page-item.disabled .page-link {
        background-color: var(--bg-accent) !important;
        color: var(--text-sub) !important;
        opacity: 0.5;
    }
</style>

<div class="container-fluid py-4 category-enterprise-workspace">
    
    <!-- PAGE HEADER -->
    <div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center mb-4 gap-3">
        <div>
            <div class="d-flex align-items-center gap-2 mb-1">
                <span class="badge bg-primary-subtle text-primary border border-primary-subtle rounded-pill px-2.5 py-1 fw-semibold fs-8">
                    <i class="bi bi-shield-check me-1"></i> Enterprise Inventory
                </span>
            </div>
            <h3 class="fw-bold mb-0 text-main">Kategori Barang</h3>
            <p class="text-sub small mb-0 mt-0.5">Kelola taksonomi, hierarki, dan pengelompokan produk dalam katalog inventaris.</p>
        </div>
        <div class="d-flex flex-wrap align-items-center gap-2">
            <button onclick="window.location.reload()" class="btn btn-outline-modern rounded-3 px-3 py-2 fw-semibold d-inline-flex align-items-center gap-2 fs-7">
                <i class="bi bi-arrow-clockwise"></i> Refresh
            </button>
            <a href="<?= base_url('owner/category/create') ?>" class="btn btn-primary-modern shadow-sm">
                <i class="bi bi-plus-lg fs-6"></i> Tambah Kategori
            </a>
        </div>
    </div>

    <!-- ENTERPRISE KPI METRICS -->
    <div class="row g-3 mb-4">
        <div class="col-12 col-sm-6 col-xl-3">
            <div class="kpi-card">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <span class="text-sub small fw-semibold d-block mb-1">Total Kategori</span>
                        <h4 class="fw-bold text-main mb-0"><?= (int) ($totalCategories ?? 0) ?></h4>
                    </div>
                    <div class="kpi-icon-box" style="background-color: var(--primary-light); color: var(--primary);">
                        <i class="bi bi-tags-fill"></i>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-12 col-sm-6 col-xl-3">
            <div class="kpi-card">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <span class="text-sub small fw-semibold d-block mb-1">Kategori dengan Produk</span>
                        <h4 class="fw-bold text-main mb-0"><?= count(array_filter($categories ?? [], static fn (array $category): bool => (int) ($category['total_products'] ?? 0) > 0)) ?></h4>
                    </div>
                    <div class="kpi-icon-box" style="background-color: var(--success-light); color: var(--success);">
                        <i class="bi bi-check-circle-fill"></i>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-12 col-sm-6 col-xl-3">
            <div class="kpi-card">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <span class="text-sub small fw-semibold d-block mb-1">Total Produk Terkait</span>
                        <h4 class="fw-bold text-main mb-0"><?= (int) ($totalProducts ?? 0) ?></h4>
                    </div>
                    <div class="kpi-icon-box" style="background-color: var(--warning-light); color: var(--warning);">
                        <i class="bi bi-box-seam-fill"></i>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-12 col-sm-6 col-xl-3">
            <div class="kpi-card">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <span class="text-sub small fw-semibold d-block mb-1">Rata-rata Produk/Kategori</span>
                        <h4 class="fw-bold text-main mb-0"><?= number_format((float) ($averageProducts ?? 0), 1, ',', '.') ?></h4>
                    </div>
                    <div class="kpi-icon-box" style="background-color: var(--primary-light); color: var(--primary);">
                        <i class="bi bi-diagram-3-fill"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- FLASH NOTIFICATIONS -->
    <?php if (session()->getFlashdata('success')) : ?>
        <div class="alert alert-success alert-dismissible fade show rounded-3 shadow-sm border-0 mb-4" role="alert">
            <div class="d-flex align-items-center gap-2">
                <i class="bi bi-check-circle-fill fs-5 text-success"></i>
                <div><?= session()->getFlashdata('success') ?></div>
            </div>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endif; ?>

    <?php if (session()->getFlashdata('error')) : ?>
        <div class="alert alert-danger alert-dismissible fade show rounded-3 shadow-sm border-0 mb-4" role="alert">
            <div class="d-flex align-items-center gap-2">
                <i class="bi bi-exclamation-triangle-fill fs-5 text-danger"></i>
                <div><?= session()->getFlashdata('error') ?></div>
            </div>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endif; ?>

    <!-- MAIN TABLE CARD WITH ENTERPRISE TOOLBAR -->
    <div class="card-modern">
        
        <!-- TOOLBAR: SEARCH & FILTERS -->
        <div class="p-3 border-bottom border-color">
            <form method="get" action="<?= base_url('owner/category') ?>">
                <div class="row g-2 align-items-center">
                    
                    <!-- Search Field -->
                    <div class="col-12 col-md-5 col-lg-4">
                        <div class="search-wrapper">
                            <i class="bi bi-search search-icon"></i>
                            <input type="text" 
                                   name="keyword" 
                                   class="form-control form-control-modern" 
                                   placeholder="Cari nama kategori..." 
                                   value="<?= esc($keyword ?? '') ?>">
                        </div>
                    </div>

                    <!-- Sort / Filter Options -->
                    <div class="col-6 col-md-3 col-lg-2">
                        <select name="sort" class="form-select form-select-modern fs-7">
                            <option value="latest" <?= ($sort ?? 'latest') === 'latest' ? 'selected' : '' ?>>Terbaru</option>
                            <option value="name_asc" <?= ($sort ?? '') === 'name_asc' ? 'selected' : '' ?>>Nama (A-Z)</option>
                            <option value="name_desc" <?= ($sort ?? '') === 'name_desc' ? 'selected' : '' ?>>Nama (Z-A)</option>
                            <option value="products_high" <?= ($sort ?? '') === 'products_high' ? 'selected' : '' ?>>Produk Terbanyak</option>
                        </select>
                    </div>

                    <div class="col-6 col-md-2 col-lg-2">
                        <button type="submit" class="btn btn-outline-modern w-100 py-2 fw-semibold fs-7 d-inline-flex align-items-center justify-content-center gap-1.5">
                            <i class="bi bi-funnel"></i> Filter
                        </button>
                    </div>

                    <?php if (!empty($keyword)) : ?>
                        <div class="col-12 col-md-2 col-lg-2">
                            <a href="<?= base_url('owner/category') ?>" class="btn btn-link text-danger text-decoration-none p-0 small">
                                <i class="bi bi-x-circle me-1"></i> Reset Cari
                            </a>
                        </div>
                    <?php endif; ?>

                </div>
            </form>
        </div>

        <!-- TABLE SECTION -->
        <div class="table-responsive">
            <table class="table table-modern align-middle">
                <thead>
                    <tr>
                        <th style="width: 60px;" class="text-center">No</th>
                        <th>Kategori Barang</th>
                        <th style="width: 160px;" class="text-center">Produk Terkait</th>
                        <th style="width: 120px;" class="text-end">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $displayCategories = $categories ?? []; ?>

                    <?php if (empty($displayCategories)): ?>
                        <tr>
                            <td colspan="4" class="text-center py-5">
                                <div class="py-4">
                                    <i class="bi bi-search fs-1 text-sub opacity-50 d-block mb-2"></i>
                                    <h6 class="fw-semibold text-main mb-1">Kategori Tidak Ditemukan</h6>
                                    <p class="text-sub small mb-0">Coba ubah kata kunci pencarian atau tambah kategori baru.</p>
                                </div>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $no = 1; ?>
                        <?php foreach($displayCategories as $row): ?>
                            <tr>
                                <td class="text-center text-sub font-monospace fs-7"><?= $no++ ?></td>
                                <td>
                                    <div class="d-flex align-items-center gap-3">
                                        <div class="category-avatar">
                                            <i class="bi bi-tag-fill"></i>
                                        </div>
                                        <div>
                                            <span class="fw-bold text-main d-block leading-tight">
                                                <?= esc($row['nama_kategori']) ?>
                                            </span>
                                            <span class="text-sub fs-7">
                                                ID Kategori: <?= (int) $row['id_category'] ?>
                                            </span>
                                        </div>
                                    </div>
                                </td>
                                <td class="text-center">
                                    <span class="badge bg-primary-subtle text-primary border border-primary-subtle rounded-pill px-3 py-1 fw-semibold font-monospace">
                                        <i class="bi bi-box-seam me-1"></i><?= (int) ($row['total_products'] ?? 0) ?> Produk
                                    </span>
                                </td>
                                <td>
                                    <div class="d-flex align-items-center justify-content-end gap-1.5">
                                        <a href="<?= base_url('owner/category/edit/'.$row['id_category']) ?>" 
                                           class="btn btn-action-warning" 
                                           title="Edit Kategori">
                                            <i class="bi bi-pencil-square fs-6"></i>
                                        </a>

                                        <form method="post" 
                                              action="<?= base_url('owner/category/delete/'.$row['id_category']) ?>" 
                                              class="d-inline" 
                                              onsubmit="return confirm('Hapus kategori? Kategori yang masih dipakai produk tidak dapat dihapus.')">
                                            <?= csrf_field() ?>
                                            <button type="submit" 
                                                    class="btn btn-action-danger" 
                                                    title="Hapus Kategori">
                                                <i class="bi bi-trash3 fs-6"></i>
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <!-- DATA SUMMARY FOOTER -->
        <div class="p-3 border-top border-color d-flex flex-column flex-sm-row justify-content-between align-items-center gap-3">
            <div class="text-sub fs-7">
                Menampilkan <span class="fw-semibold text-main"><?= count($displayCategories) ?></span> dari <span class="fw-semibold text-main"><?= (int) ($totalCategories ?? 0) ?></span> kategori
            </div>
            <div class="text-sub fs-7">Data ditampilkan langsung dari database.</div>
        </div>

    </div>

</div>

<?= $this->endSection() ?>
