<?php
/* ============================================================================
 *  company/approval_settings.php — configure two-stage approval routing.
 *  Stage 1: each department's Manager approves their own staff's requests.
 *  Stage 2: the company's final Manager gives final approval.
 *  Also lets you put staff into departments. Bilingual EN/Lao.
 * ==========================================================================*/
session_start();
if (!isset($_SESSION["user_id"]) || $_SESSION["role"] !== "company_admin") {
    header("Location: ../auth/login.php"); exit;
}
require_once '../config/database.php';
date_default_timezone_set('Asia/Vientiane');
$companyId   = (int)$_SESSION["company_id"];
$companyName = $_SESSION["company_name"] ?? "My Company";
$me          = (int)$_SESSION["user_id"];
$flash = null;

/* ensure tables/columns exist (graceful if migration not yet run) */
function colExists(PDO $pdo,$t,$c){ try{return (bool)$pdo->query("SHOW COLUMNS FROM `$t` LIKE ".$pdo->quote($c))->fetch();}catch(Throwable $e){return false;} }
$ready = colExists($pdo,'company_departments','manager_user_id');

/* ── POST: save managers + settings ── */
if ($_SERVER['REQUEST_METHOD']==='POST' && ($_POST['action']??'')==='save' && $ready) {
    try {
        // department managers
        foreach (($_POST['dept_mgr'] ?? []) as $deptId=>$uid) {
            $deptId=(int)$deptId; $uid=(int)$uid ?: null;
            $pdo->prepare("UPDATE company_departments SET manager_user_id=? WHERE id=? AND company_id=?")
                ->execute([$uid,$deptId,$companyId]);
        }
        // final manager + require_dept
        $finalId = (int)($_POST['final_manager_id'] ?? 0) ?: null;
        $reqDept = isset($_POST['require_dept']) ? 1 : 0;
        $pdo->prepare("INSERT INTO company_approval_settings (company_id, final_manager_id, require_dept, updated_at)
                       VALUES (?,?,?,NOW())
                       ON DUPLICATE KEY UPDATE final_manager_id=VALUES(final_manager_id), require_dept=VALUES(require_dept), updated_at=NOW()")
            ->execute([$companyId,$finalId,$reqDept]);
        // owner / founder (separate UPDATE so a missing column never breaks the save)
        $ownerId = (int)($_POST['owner_user_id'] ?? 0) ?: null;
        try { $pdo->prepare("UPDATE company_approval_settings SET owner_user_id=? WHERE company_id=?")->execute([$ownerId,$companyId]); } catch (Throwable $e) {}
        $flash=['ok','Approval settings saved.','ບັນທຶກການຕັ້ງຄ່າແລ້ວ.'];
    } catch (Throwable $e) { $flash=['err','Error: '.$e->getMessage(),'ຜິດພາດ: '.$e->getMessage()]; }
}

/* ── POST: set / clear my delegate (stand-in approver while I'm away) ── */
if ($_SERVER['REQUEST_METHOD']==='POST' && ($_POST['action']??'')==='set_delegate') {
    $to    = (int)($_POST['delegate_to'] ?? 0);
    $until = trim($_POST['delegate_until'] ?? '');
    $untilVal = preg_match('/^\d{4}-\d{2}-\d{2}$/',$until) ? $until : null;
    try {
        if ($to > 0 && $to !== $me) {
            $pdo->prepare("INSERT INTO approval_delegates (company_id, from_user_id, to_user_id, until_date, created_at)
                           VALUES (?,?,?,?,NOW())
                           ON DUPLICATE KEY UPDATE to_user_id=VALUES(to_user_id), until_date=VALUES(until_date), created_at=NOW()")
                ->execute([$companyId,$me,$to,$untilVal]);
            $flash=['ok','Delegate set — they can approve on your behalf.','ມອບໝາຍແລ້ວ.'];
        }
    } catch (Throwable $e) { $flash=['err','Error: '.$e->getMessage(),'ຜິດພາດ: '.$e->getMessage()]; }
}
if ($_SERVER['REQUEST_METHOD']==='POST' && ($_POST['action']??'')==='clear_delegate') {
    try { $pdo->prepare("DELETE FROM approval_delegates WHERE company_id=? AND from_user_id=?")->execute([$companyId,$me]); $flash=['ok','Delegation stopped.','ຍົກເລີກການມອບໝາຍແລ້ວ.']; } catch (Throwable $e) {}
}

/* ── POST: put a staff member into a department ── */
if ($_SERVER['REQUEST_METHOD']==='POST' && ($_POST['action']??'')==='assign_dept') {
    $uid=(int)$_POST['staff_user_id']; $dept=trim($_POST['department']??'');
    $pdo->prepare("UPDATE company_staff SET department=? WHERE user_id=? AND company_id=?")
        ->execute([$dept!==''?$dept:null,$uid,$companyId]);
    $flash=['ok','Staff assigned to department.','ກຳນົດພະແນກໃຫ້ພະນັກງານແລ້ວ.'];
}

/* ── data ── */
$departments = [];
if ($ready) {
    $d=$pdo->prepare("SELECT id,name,manager_user_id FROM company_departments WHERE company_id=? ORDER BY name");
    $d->execute([$companyId]); $departments=$d->fetchAll(PDO::FETCH_ASSOC);
}
$staff = $pdo->prepare("SELECT u.id,u.name,cs.staff_code,cs.staff_role,cs.department
                        FROM company_staff cs JOIN users u ON u.id=cs.user_id
                        WHERE cs.company_id=? AND u.status='active'
                        ORDER BY (cs.staff_code IS NULL OR cs.staff_code=''), cs.staff_code");
$staff->execute([$companyId]); $staff=$staff->fetchAll(PDO::FETCH_ASSOC);

$settings=['final_manager_id'=>null,'require_dept'=>1,'owner_user_id'=>null];
if ($ready) {
    try { $s=$pdo->prepare("SELECT final_manager_id,require_dept,owner_user_id FROM company_approval_settings WHERE company_id=?"); $s->execute([$companyId]); if($r=$s->fetch(PDO::FETCH_ASSOC))$settings=array_merge($settings,$r); }
    catch(Throwable $e){ try { $s=$pdo->prepare("SELECT final_manager_id,require_dept FROM company_approval_settings WHERE company_id=?"); $s->execute([$companyId]); if($r=$s->fetch(PDO::FETCH_ASSOC))$settings=array_merge($settings,$r); } catch(Throwable $e2){} }
}

/* staff lookup for names */
$byId=[]; foreach($staff as $s)$byId[(int)$s['id']]=$s;

/* my current delegate (stand-in) */
$myDelegate=['to_user_id'=>0,'until_date'=>null];
try { $d=$pdo->prepare("SELECT to_user_id,until_date FROM approval_delegates WHERE company_id=? AND from_user_id=? LIMIT 1"); $d->execute([$companyId,$me]); if($r=$d->fetch(PDO::FETCH_ASSOC))$myDelegate=$r; } catch(Throwable $e){}
$delegateName = ($myDelegate['to_user_id'] && isset($byId[(int)$myDelegate['to_user_id']])) ? $byId[(int)$myDelegate['to_user_id']] : null;
$deptNames=array_values(array_unique(array_filter(array_map(fn($d)=>$d['name'],$departments))));
$unassigned=array_values(array_filter($staff, fn($s)=>!$s['department']));

function nm($s){ return htmlspecialchars($s['name']).($s['staff_code']?' ('.htmlspecialchars($s['staff_code']).')':''); }
?>
<!DOCTYPE html>
<html lang="lo">
<head>
    <?php $pwaTheme='#0f766e'; include __DIR__.'/../config/pwa_head.php'; ?>
    <title><?= htmlspecialchars($companyName) ?> — Approval Settings</title>
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Lao:wght@400;500;600;700;800&family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <script src="../assets/lang.js" defer></script>
    <style>
        *,*::before,*::after{box-sizing:border-box;margin:0;padding:0;}
        :root{--primary:#0f766e;--surface:#fff;--bg:#f0f4f8;--border:#e2e8f0;--text:#0f172a;--muted:#64748b;--green:#16a34a;--green-lt:#dcfce7;--red:#dc2626;--red-lt:#fee2e2;--amber:#d97706;--amber-lt:#fef3c7;--radius:14px;--shadow:0 2px 14px rgba(15,23,42,.07);}
        body{font-family:"Noto Sans Lao","Inter",Arial,sans-serif;background:var(--bg);color:var(--text);min-height:100vh;}
        .top-bar{background:var(--surface);border-bottom:1px solid var(--border);padding:calc(12px + env(safe-area-inset-top, 0px)) 24px 12px;display:flex;justify-content:space-between;align-items:center;gap:14px;position:sticky;top:0;z-index:100;box-shadow:0 1px 6px rgba(15,23,42,.06);flex-wrap:wrap;}
        .back-link{display:inline-flex;align-items:center;gap:6px;background:#f1f5f9;color:var(--text);border:1.5px solid var(--border);padding:7px 13px;border-radius:8px;text-decoration:none;font-size:13px;font-weight:700;}
        .lang-bar{display:flex;gap:7px;}
        .lang-btn{padding:4px 11px;border-radius:999px;border:1.5px solid var(--border);background:var(--surface);font-size:11px;font-weight:700;color:var(--muted);cursor:pointer;font-family:inherit;}
        .lang-btn.active{background:var(--primary);border-color:var(--primary);color:#fff;}
        .page{max-width:880px;margin:auto;padding:22px 18px;}
        h1{font-size:21px;font-weight:800;margin-bottom:4px;}
        .sub{color:var(--muted);font-size:13px;margin-bottom:18px;}
        .flash{padding:11px 15px;border-radius:10px;margin-bottom:16px;font-size:14px;font-weight:600;}
        .flash.ok{background:var(--green-lt);color:#15803d;}.flash.err{background:var(--red-lt);color:var(--red);}
        .card{background:var(--surface);border:1px solid var(--border);border-radius:var(--radius);box-shadow:var(--shadow);padding:18px 20px;margin-bottom:16px;}
        .card h2{font-size:16px;font-weight:800;margin-bottom:4px;}
        .card .hint{color:var(--muted);font-size:12px;margin-bottom:14px;}
        label.fld{display:block;font-size:13px;font-weight:700;color:#374151;margin:12px 0 5px;}
        select,input[type=text]{width:100%;max-width:420px;padding:9px 11px;border:1.5px solid var(--border);border-radius:9px;font-size:14px;font-family:inherit;background:#fff;}
        .flow{display:flex;align-items:center;gap:8px;flex-wrap:wrap;background:#f0fdfa;border:1px solid #99f6e4;border-radius:10px;padding:10px 14px;font-size:13px;font-weight:700;color:#0f766e;margin-bottom:14px;}
        .flow .step{background:#fff;border:1.5px solid #99f6e4;border-radius:8px;padding:4px 10px;}
        table{width:100%;border-collapse:collapse;font-size:14px;}
        th{text-align:left;font-size:11px;text-transform:uppercase;letter-spacing:.3px;color:var(--muted);padding:8px;border-bottom:2px solid var(--border);}
        td{padding:9px 8px;border-bottom:1px solid var(--border);vertical-align:middle;}
        .pill{display:inline-block;font-size:11px;font-weight:800;padding:2px 9px;border-radius:999px;background:#f1f5f9;color:#475569;}
        .pill.adm{background:#ede9fe;color:#6d28d9;}
        .chk{display:flex;align-items:center;gap:8px;font-size:13px;font-weight:600;margin-top:10px;}
        .btn{background:var(--primary);color:#fff;border:none;padding:11px 20px;border-radius:9px;font-weight:800;font-size:14px;cursor:pointer;font-family:inherit;}
        .small{padding:6px 10px;font-size:12px;border-radius:8px;}
        .warn{background:var(--amber-lt);color:#92400e;border-radius:10px;padding:10px 14px;font-size:13px;font-weight:600;margin-bottom:12px;}
        .mini-form{display:flex;gap:6px;align-items:center;}
        .mini-form select{max-width:200px;padding:6px 9px;font-size:13px;}
    </style>
</head>
<body>

<div class="top-bar">
    <a class="back-link" href="dashboard.php">← <span data-lo="ກັບຄືນ" data-en="Back">Back</span></a>
    <div class="lang-bar">
        <button class="lang-btn" id="btn-lo" onclick="setLang('lo')">🇱🇦 ລາວ</button>
        <button class="lang-btn" id="btn-en" onclick="setLang('en')">🇬🇧 EN</button>
    </div>
</div>

<div class="page">
    <h1 data-lo="ການຕັ້ງຄ່າການອະນຸມັດ" data-en="Approval Settings">Approval Settings</h1>
    <div class="sub" data-lo="ກຳນົດວ່າຄຳຂໍ (ລາພັກ, ກູ້ຢືມ, OT) ຈະສົ່ງໄປຫາໃຜກ່ອນ." data-en="Control who staff requests (leave, loans, OT) go to for approval.">Control who staff requests (leave, loans, OT) go to for approval.</div>

    <div class="flow">
        <span data-lo="ພະນັກງານສົ່ງຄຳຂໍ" data-en="Staff submits">Staff submits</span> →
        <span class="step" data-lo="ຫົວໜ້າພະແນກ" data-en="Department Manager">Department Manager</span> →
        <span class="step" data-lo="ຜູ້ຈັດການຝ່າຍປະຕິບັດການ" data-en="Operation Manager">Operation Manager</span> →
        <span data-lo="ອະນຸມັດ" data-en="Approved">Approved</span>
    </div>

    <?php if ($flash): ?>
        <div class="flash <?= $flash[0]==='ok'?'ok':'err' ?>" data-en="<?= htmlspecialchars($flash[1]) ?>" data-lo="<?= htmlspecialchars($flash[2]) ?>"><?= htmlspecialchars($flash[2]) ?></div>
    <?php endif; ?>

    <!-- Delegate my approvals (any manager, self-service) -->
    <div class="card" style="border:1.5px solid #fde68a;background:#fffbeb;">
        <h2 data-lo="ມອບໝາຍການອະນຸມັດ (ເມື່ອບໍ່ຢູ່)" data-en="Delegate my approvals (when away)">Delegate my approvals (when away)</h2>
        <div class="hint" data-lo="ມອບໝາຍໃຫ້ພະນັກງານຄົນອື່ນອະນຸມັດແທນທ່ານ. ຄຳຂໍຈະແຈ້ງເຕືອນທັງສອງຄົນ." data-en="Hand your approval power to another staff member while you're away. New requests alert both of you.">Hand your approval power to another staff member while you're away. New requests alert both of you.</div>
        <?php if ($delegateName): ?>
            <div style="background:#dbeafe;color:#1e40af;border-radius:10px;padding:10px 14px;font-weight:700;font-size:13px;margin-bottom:12px;">
                ✅ <span data-lo="ກຳລັງມອບໝາຍໃຫ້:" data-en="Currently delegated to:">Currently delegated to:</span>
                <?= nm($delegateName) ?><?php if(!empty($myDelegate['until_date'])): ?> · <span data-lo="ຫາ" data-en="until">until</span> <?= date('d/m/Y',strtotime($myDelegate['until_date'])) ?><?php endif; ?>
            </div>
            <form method="POST" style="display:inline" onsubmit="return confirm('Stop delegating your approvals?')">
                <input type="hidden" name="action" value="clear_delegate">
                <button type="submit" style="background:#fee2e2;color:#b91c1c;border:1.5px solid #fecaca;padding:9px 16px;border-radius:9px;font-weight:800;cursor:pointer;font-family:inherit;" data-lo="🛑 ຢຸດການມອບໝາຍ" data-en="🛑 Stop delegation">🛑 Stop delegation</button>
            </form>
        <?php else: ?>
            <form method="POST">
                <input type="hidden" name="action" value="set_delegate">
                <div class="row" style="display:flex;gap:12px;flex-wrap:wrap;align-items:flex-end;">
                    <div style="flex:1;min-width:200px;">
                        <label class="fld" data-lo="ມອບໝາຍໃຫ້" data-en="Delegate to">Delegate to</label>
                        <select name="delegate_to" required>
                            <option value="0" data-lo="— ເລືອກພະນັກງານ —" data-en="— choose staff —">— choose staff —</option>
                            <?php foreach ($staff as $s): if((int)$s['id']===$me) continue; ?>
                                <option value="<?= (int)$s['id'] ?>"><?= nm($s) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div style="min-width:160px;">
                        <label class="fld" data-lo="ຫາວັນທີ (ບໍ່ບັງຄັບ)" data-en="Until (optional)">Until (optional)</label>
                        <input type="date" name="delegate_until">
                    </div>
                </div>
                <button class="btn" type="submit" style="margin-top:12px;">🤝 <span data-lo="ມອບໝາຍ" data-en="Delegate">Delegate</span></button>
            </form>
        <?php endif; ?>
    </div>

    <?php if (!$ready): ?>
        <div class="warn">⚠️ <span data-lo="ກະລຸນາແລ່ນ admin/setup_approvals.php ກ່ອນ ເພື່ອສ້າງຖານຂໍ້ມູນ." data-en="Run admin/setup_approvals.php once first to create the database columns, then reload this page.">Run <b>admin/setup_approvals.php</b> once first to create the database columns, then reload this page.</span></div>
    <?php else: ?>

    <form method="POST">
        <input type="hidden" name="action" value="save">

        <!-- Owner / Founder -->
        <div class="card">
            <h2 data-lo="ເຈົ້າຂອງ / ຜູ້ກໍ່ຕັ້ງ" data-en="Owner / Founder">Owner / Founder</h2>
            <div class="hint" data-lo="ເຈົ້າຂອງສາມາດອະນຸມັດໄດ້ທຸກຢ່າງ (ບໍ່ຖືກລັອກ) ແລະ ຄຳຂໍຂອງເຈົ້າຂອງເອງຈະຖືກອະນຸມັດອັດຕະໂນມັດ." data-en="The owner can approve anything (never locked out), and the owner's own requests are auto-approved.">The owner can approve anything (never locked out), and the owner's own requests are auto-approved.</div>
            <label class="fld" data-lo="ເລືອກເຈົ້າຂອງ" data-en="Select Owner">Select Owner</label>
            <select name="owner_user_id">
                <option value="0" data-lo="— ບໍ່ໄດ້ກຳນົດ —" data-en="— not set —">— not set —</option>
                <?php foreach ($staff as $s): ?>
                    <option value="<?= (int)$s['id'] ?>" <?= (int)($settings['owner_user_id']??0)===(int)$s['id']?'selected':'' ?>><?= nm($s) ?></option>
                <?php endforeach; ?>
            </select>
        </div>

        <!-- Stage 2: final manager -->
        <div class="card">
            <h2 data-lo="ຜູ້ຈັດການຝ່າຍປະຕິບັດການ (ອະນຸມັດສຸດທ້າຍ)" data-en="Operation Manager (final approval)">Operation Manager (final approval)</h2>
            <div class="hint" data-lo="ຄົນນີ້ຢູ່ເໜືອຫົວໜ້າພະແນກທັງໝົດ ແລະ ອະນຸມັດເປັນຂັ້ນສຸດທ້າຍ." data-en="This person sits above all department managers and gives the final approval.">This person sits above all department managers and gives the final approval.</div>
            <label class="fld" data-lo="ເລືອກຜູ້ຈັດການຝ່າຍປະຕິບັດການ" data-en="Select Operation Manager">Select Operation Manager</label>
            <select name="final_manager_id">
                <option value="0" data-lo="— ບໍ່ໄດ້ກຳນົດ —" data-en="— not set —">— not set —</option>
                <?php foreach ($staff as $s): ?>
                    <option value="<?= (int)$s['id'] ?>" <?= (int)$settings['final_manager_id']===(int)$s['id']?'selected':'' ?>><?= nm($s) ?><?= $s['staff_role']==='admin'?' · Admin':'' ?></option>
                <?php endforeach; ?>
            </select>
            <label class="chk"><input type="checkbox" name="require_dept" <?= (int)$settings['require_dept']?'checked':'' ?>>
                <span data-lo="ຕ້ອງຜ່ານຫົວໜ້າພະແນກກ່ອນ (ຖ້າບໍ່ຕິກ ຈະໄປຫາຜູ້ຈັດການຝ່າຍປະຕິບັດການໂດຍກົງ)" data-en="Require Department Manager first (uncheck to send straight to the Operation Manager)">Require Department Manager first (uncheck to send straight to the Operation Manager)</span></label>
        </div>

        <!-- Stage 1: department managers -->
        <div class="card">
            <h2 data-lo="ຫົວໜ້າພະແນກ" data-en="Department Managers">Department Managers</h2>
            <div class="hint" data-lo="ກຳນົດຫົວໜ້າຂອງແຕ່ລະພະແນກ — ເຂົາຈະອະນຸມັດຄຳຂໍຂອງພະນັກງານໃນພະແນກນັ້ນກ່ອນ." data-en="Set the head of each department — they approve their own department's requests first.">Set the head of each department — they approve their own department's requests first.</div>
            <?php if (!$departments): ?>
                <div class="warn" data-lo="ຍັງບໍ່ມີພະແນກ. ເພີ່ມພະແນກໃນ ຟອມພະນັກງານ." data-en="No departments yet. Add departments in the staff form first.">No departments yet. Add departments in the staff form first.</div>
            <?php else: ?>
                <table>
                    <thead><tr>
                        <th data-lo="ພະແນກ" data-en="Department">Department</th>
                        <th data-lo="ຫົວໜ້າພະແນກ" data-en="Manager">Manager</th>
                        <th data-lo="ຈຳນວນ" data-en="Staff">Staff</th>
                    </tr></thead>
                    <tbody>
                    <?php foreach ($departments as $d):
                        $cnt=count(array_filter($staff, fn($s)=>$s['department']===$d['name'])); ?>
                        <tr>
                            <td><strong><?= htmlspecialchars($d['name']) ?></strong></td>
                            <td>
                                <select name="dept_mgr[<?= (int)$d['id'] ?>]">
                                    <option value="0" data-lo="— ບໍ່ໄດ້ກຳນົດ —" data-en="— not set —">— not set —</option>
                                    <?php foreach ($staff as $s): ?>
                                        <option value="<?= (int)$s['id'] ?>" <?= (int)$d['manager_user_id']===(int)$s['id']?'selected':'' ?>><?= nm($s) ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </td>
                            <td><span class="pill"><?= $cnt ?></span></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>

        <button class="btn" type="submit">💾 <span data-lo="ບັນທຶກການຕັ້ງຄ່າ" data-en="Save settings">Save settings</span></button>
    </form>

    <!-- staff -> department -->
    <div class="card" style="margin-top:18px;">
        <h2 data-lo="ຈັດພະນັກງານເຂົ້າພະແນກ" data-en="Put staff into departments">Put staff into departments</h2>
        <div class="hint" data-lo="ພະນັກງານທີ່ບໍ່ມີພະແນກ ຈະຂ້າມຂັ້ນຫົວໜ້າພະແນກ ແລະ ໄປຫາຜູ້ຈັດການໂດຍກົງ." data-en="Staff with no department skip stage 1 and go straight to the Manager.">Staff with no department skip stage 1 and go straight to the Manager.</div>
        <?php if (!$deptNames): ?>
            <div class="warn" data-lo="ຍັງບໍ່ມີພະແນກໃຫ້ເລືອກ." data-en="No departments to choose from yet.">No departments to choose from yet.</div>
        <?php endif; ?>
        <table>
            <thead><tr>
                <th data-lo="ພະນັກງານ" data-en="Staff">Staff</th>
                <th data-lo="ປະເພດ" data-en="Role">Role</th>
                <th data-lo="ພະແນກ" data-en="Department">Department</th>
            </tr></thead>
            <tbody>
            <?php foreach ($staff as $s): ?>
                <tr>
                    <td><strong><?= nm($s) ?></strong></td>
                    <td><span class="pill <?= $s['staff_role']==='admin'?'adm':'' ?>"><?= $s['staff_role']==='admin'?'Admin':'Staff' ?></span></td>
                    <td>
                        <form method="POST" class="mini-form">
                            <input type="hidden" name="action" value="assign_dept">
                            <input type="hidden" name="staff_user_id" value="<?= (int)$s['id'] ?>">
                            <select name="department" onchange="this.form.submit()">
                                <option value="" <?= !$s['department']?'selected':'' ?> data-lo="— ບໍ່ມີ —" data-en="— none —">— none —</option>
                                <?php foreach ($deptNames as $dn): ?>
                                    <option value="<?= htmlspecialchars($dn) ?>" <?= $s['department']===$dn?'selected':'' ?>><?= htmlspecialchars($dn) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </form>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <?php endif; ?>
</div>

<?php require __DIR__ . '/../config/auto_update.php'; ?>
</body>
</html>