<?php
// Connect to the database
require_once '../../../dripend/includes/db.php';

// Fetch Summary Statistics for the Top Cards
$stats_query = "SELECT COUNT(code) as total_codes FROM icd10_dictionary";
$stats_result = $conn->query($stats_query);
$stats = $stats_result ? $stats_result->fetch_assoc() : ['total_codes' => 0];
?>

<style>
/* Enterprise SPA Styles */
.catalog-container { font-family: "Segoe UI", system-ui, sans-serif; animation: fadeIn 0.3s ease; }
.header-flex { display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; }
.header-flex h2 { color: #1e3a8a; font-size: 24px; font-weight: 800; display: flex; align-items: center; gap: 10px; text-transform: uppercase; margin: 0; }

.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px; margin-bottom: 24px; }
.stat-card { background: #fff; border: 1px solid #e2e8f0; border-radius: 12px; padding: 20px; display: flex; justify-content: space-between; align-items: center; border-left: 4px solid #1e3a8a; box-shadow: 0 2px 4px rgba(0,0,0,0.02); }
.stat-card.green { border-left-color: #10b981; }
.stat-info h4 { font-size: 11px; color: #64748b; text-transform: uppercase; font-weight: 700; margin: 0 0 8px 0; letter-spacing: 0.5px; }
.stat-info h3 { font-size: 28px; color: #0f172a; margin: 0; font-weight: 800; }
.stat-icon { font-size: 32px; color: #f1f5f9; }

.advanced-filters { background: #fff; border: 1px solid #e2e8f0; border-radius: 12px; padding: 20px; margin-bottom: 24px; display: flex; gap: 15px; align-items: flex-end; flex-wrap: wrap; box-shadow: 0 2px 4px rgba(0,0,0,0.02); }
.filter-group { display: flex; flex-direction: column; gap: 6px; flex: 1; min-width: 180px; }
.filter-group label { font-size: 11px; font-weight: 700; color: #64748b; text-transform: uppercase; }
.search-wrapper { position: relative; width: 100%; }
.search-wrapper i { position: absolute; left: 14px; top: 14px; color: #94a3b8; font-size: 14px; }
.filter-input { width: 100%; padding: 10px 14px; border: 1px solid #cbd5e1; border-radius: 8px; font-size: 14px; color: #334155; outline: none; box-sizing: border-box; background: #fff; }
.search-input { padding-left: 36px !important; }
.filter-input:focus { border-color: #1e3a8a; }

.btn-apply { background: #16a34a; color: #fff; border: none; padding: 10px 24px; border-radius: 8px; font-weight: 700; font-size: 14px; cursor: pointer; display: flex; align-items: center; gap: 8px; height: 42px; transition: 0.2s; white-space: nowrap; }
.btn-apply:hover { background: #15803d; }

/* Table Styles */
.table-container { background: #fff; border: 1px solid #e2e8f0; border-radius: 12px; overflow: hidden; box-shadow: 0 2px 4px rgba(0,0,0,0.02); margin-bottom: 16px; }
.data-table { width: 100%; border-collapse: collapse; text-align: left; }
.data-table th { padding: 16px 20px; font-size: 11px; font-weight: 800; color: #64748b; text-transform: uppercase; border-bottom: 1px solid #e2e8f0; background: #fafaf9; }
.data-table td { padding: 16px 20px; font-size: 14px; border-bottom: 1px solid #f1f5f9; vertical-align: middle; color: #334155; }
.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover { background: #f8fafc; }

.code-badge { background: #e0e7ff; color: #1e40af; padding: 4px 10px; border-radius: 6px; font-weight: 700; font-size: 13px; border: 1px solid #bfdbfe; display: inline-block; }
.desc-text { font-weight: 600; color: #0f172a; }

/* Action Buttons */
.actions-flex { display: flex; gap: 6px; justify-content: flex-end; }
.btn-action { width: 32px; height: 32px; border-radius: 6px; border: 1px solid #e2e8f0; background: #fff; display: flex; align-items: center; justify-content: center; cursor: pointer; transition: 0.2s; color: #64748b; }
.btn-action:hover { background: #f1f5f9; color: #0f172a; }
.btn-action.view:hover { color: #3b82f6; border-color: #bfdbfe; background: #eff6ff; }
.btn-action.edit:hover { color: #f59e0b; border-color: #fde68a; background: #fffbeb; }
.btn-action.delete:hover { color: #ef4444; border-color: #fecaca; background: #fef2f2; }
.btn-action.toggle.active { color: #10b981; }
.btn-action.toggle.inactive { color: #94a3b8; opacity: 0.6; }
.btn-action.toggle.active:hover { background: #dcfce7; border-color: #bbf7d0; }

/* Pagination */
.pagination-container { display: flex; justify-content: space-between; align-items: center; padding: 20px 0; }
.page-btn { padding: 8px 16px; border: 1px solid #cbd5e1; background: #fff; border-radius: 6px; font-weight: 600; color: #334155; cursor: pointer; }
.page-btn:disabled { opacity: 0.5; cursor: not-allowed; }
</style>

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onload="if(typeof fetchIcdDictionary === 'function') { fetchIcdDictionary(1); } this.remove();" style="display:none;">

<div class="catalog-container">
    <div class="header-flex">
        <h2><i class="fa-solid fa-book-medical"></i> ICD-10 Dictionary</h2>
        <button class="btn-apply" style="background:#1e3a8a;" onclick="if(typeof loadContent === 'function') { loadContent('Add ICD-10', 'gdrg/addIcd.php', 'ICD-10 Dictionary', 'facility', null, true); }">
            <i class="fa-solid fa-plus"></i> Add New ICD-10 Code
        </button>
    </div>

    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-info">
                <h4>Total ICD-10 Codes</h4>
                <h3><?php echo number_format($stats['total_codes']); ?></h3>
            </div>
            <div class="stat-icon"><i class="fa-solid fa-barcode"></i></div>
        </div>
        <div class="stat-card green">
            <div class="stat-info">
                <h4>Status</h4>
                <h3 style="font-size: 20px; padding-top: 5px;">Active & Verified</h3>
            </div>
            <div class="stat-icon"><i class="fa-solid fa-check-circle"></i></div>
        </div>
    </div>

    <div class="advanced-filters">
        <div class="filter-group" style="flex: 3;">
            <label>Search ICD-10 Dictionary</label>
            <div class="search-wrapper">
                <i class="fa-solid fa-magnifying-glass"></i>
                <input type="text" id="icdSearch" class="filter-input search-input" placeholder="Search by Code (e.g. B50.9) or Description (e.g. Malaria)..." onkeyup="if(typeof debounceFetchIcd === 'function') debounceFetchIcd();">
            </div>
        </div>

        <div class="filter-group" style="flex: 1; min-width: 100px;">
            <label>Records</label>
            <select id="icdLimit" class="filter-input" onchange="if(typeof fetchIcdDictionary === 'function') fetchIcdDictionary(1);">
                <option value="25">25</option>
                <option value="50" selected>50</option>
                <option value="100">100</option>
                <option value="500">500</option>
            </select>
        </div>

        <button type="button" class="btn-apply" onclick="if(typeof fetchIcdDictionary === 'function') fetchIcdDictionary(1);"><i class="fa-solid fa-filter"></i> Search</button>
    </div>

    <div class="table-container">
        <table class="data-table">
            <thead>
                <tr>
                    <th style="width: 40px;">#</th>
                    <th style="width: 150px;">ICD-10 Code</th>
                    <th>Diagnosis Description</th>
                    <th>Status</th>
                    <th style="text-align:right;">Actions</th>
                </tr>
            </thead>
            <tbody id="icdTableBody">
                <tr>
                    <td colspan="5" style="text-align:center; padding: 40px; color:#64748b;">
                        <i class="fa-solid fa-spinner fa-spin" style="font-size: 24px; margin-bottom:10px;"></i><br>Loading Dictionary Data...
                    </td>
                </tr>
            </tbody>
        </table>
    </div>

    <div class="pagination-container">
        <div id="icdPageInfo" style="font-size:13px; font-weight:600; color:#64748b;">Showing 0 to 0 of 0 entries</div>
        <div style="display:flex; gap:8px;">
            <button id="icdPrev" class="page-btn" onclick="if(typeof changeIcdPage === 'function') changeIcdPage(-1);" disabled>Previous</button>
            <button id="icdNext" class="page-btn" onclick="if(typeof changeIcdPage === 'function') changeIcdPage(1);" disabled>Next</button>
        </div>
    </div>
</div>