<?php
require_once 'auth_check.php';

if ($_SESSION['user_level'] !== 'agency') {
    header("Location: login.php");
    exit();
}

$agency_id = $_SESSION['user_id'] ?? null;
$message = '';
$message_type = '';
$show_status_modal = false;

// Handle Suspend Client
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['suspend_client'])) {
    $user_id = (int)$_POST['user_id'];
    if ($conn->query("UPDATE users SET status = 'suspended' WHERE id = $user_id")) {
        $message = "Client suspended successfully.";
        $message_type = "success";
        $show_status_modal = true;
    } else {
        $message = "Error suspending client: " . $conn->error;
        $message_type = "error";
        $show_status_modal = true;
    }
}

// Handle Restore Client
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['restore_client'])) {
    $user_id = (int)$_POST['user_id'];
    if ($conn->query("UPDATE users SET status = 'active' WHERE id = $user_id")) {
        $message = "Client restored successfully.";
        $message_type = "success";
        $show_status_modal = true;
    } else {
        $message = "Error restoring client: " . $conn->error;
        $message_type = "error";
        $show_status_modal = true;
    }
}

// Handle Remove Client Entirely
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['remove_client_full'])) {
    $user_id = (int)$_POST['user_id'];
    $conn->begin_transaction();
    try {
        $mapping_res = $conn->query("SELECT id FROM agency_clients WHERE client_id = $user_id AND agency_id = $agency_id");
        $mapping_ids = [];
        if ($mapping_res) {
            while ($m = $mapping_res->fetch_assoc()) $mapping_ids[] = $m['id'];
        }
        if (!empty($mapping_ids)) {
            $m_in = implode(',', $mapping_ids);
            $conn->query("DELETE FROM guard_assignments WHERE agency_client_id IN ($m_in)");
            $conn->query("DELETE FROM inspector_assignments WHERE agency_client_id IN ($m_in)");
            $conn->query("DELETE FROM tour_assignments WHERE agency_client_id IN ($m_in)");
            $conn->query("DELETE FROM shifts WHERE agency_client_id IN ($m_in)");
            $conn->query("DELETE FROM checkpoints WHERE agency_client_id IN ($m_in)");
            $conn->query("DELETE FROM agency_clients WHERE id IN ($m_in)");
        }
        if (!$conn->query("DELETE FROM users WHERE id = $user_id")) {
            throw new Exception("Error deleting user: " . $conn->error);
        }
        $conn->commit();
        $message = "Client permanently removed. Assigned limits/guards are now freed.";
        $message_type = "success";
        $show_status_modal = true;
    } catch (Exception $e) {
        $conn->rollback();
        $message = "Error removing client: " . $e->getMessage();
        $message_type = "error";
        $show_status_modal = true;
    }
}


// Handle Company Details Update
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['update_details'])) {
    $mapping_id = (int)$_POST['mapping_id'];
    $company_name = $conn->real_escape_string($_POST['company_name']);
    $company_address = $conn->real_escape_string($_POST['company_address']);
    $contact_no = $conn->real_escape_string($_POST['contact_no']);
    $email_address = $conn->real_escape_string($_POST['email_address']);
    $website_link = $conn->real_escape_string($_POST['website_link']);
    $contact_person = $conn->real_escape_string($_POST['contact_person']);
    $contact_person_position = $conn->real_escape_string($_POST['contact_person_position']);
    $contact_person_no = $conn->real_escape_string($_POST['contact_person_no']);
    $qr_limit = 0;
    $guard_limit = 0;
    $inspector_limit = 0;
    $supervisor_limit = 0;
    
    // Handle File Upload
    $logo_path = null;
    if (isset($_FILES['company_logo']) && $_FILES['company_logo']['error'] == 0) {
        $target_dir = "uploads/logos/";
        if (!file_exists($target_dir)) mkdir($target_dir, 0777, true);
        $file_ext = strtolower(pathinfo($_FILES["company_logo"]["name"], PATHINFO_EXTENSION));
        $new_filename = "logo_" . $mapping_id . "_" . time() . "." . $file_ext;
        $target_file = $target_dir . $new_filename;
        
        $valid_extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
        if (in_array($file_ext, $valid_extensions) && $_FILES["company_logo"]["size"] < 2000000) {
            if (move_uploaded_file($_FILES["company_logo"]["tmp_name"], $target_file)) {
                $logo_path = $target_file;
                $old_logo_res = $conn->query("SELECT company_logo FROM agency_clients WHERE id = $mapping_id");
                if ($old_logo_res && $old_res = $old_logo_res->fetch_assoc()) {
                    if ($old_res['company_logo'] && file_exists($old_res['company_logo'])) {
                        unlink($old_res['company_logo']);
                    }
                }
            }
        }
    }
    
    $update_logo_sql = $logo_path ? ", company_logo = '$logo_path'" : "";
    $sql = "UPDATE agency_clients SET 
            company_name = '$company_name',
            company_address = '$company_address',
            contact_no = '$contact_no',
            email_address = '$email_address',
            website_link = '$website_link',
            contact_person = '$contact_person',
            contact_person_position = '$contact_person_position',
            contact_person_no = '$contact_person_no',
            qr_limit = $qr_limit,
            guard_limit = $guard_limit,
            inspector_limit = $inspector_limit,
            supervisor_limit = $supervisor_limit
            $update_logo_sql 
            WHERE id = $mapping_id AND agency_id = $agency_id";
    
    if ($conn->query($sql)) {
        $message = "Client details updated successfully!";
        $message_type = "success";
        $show_status_modal = true;
    } else {
        $message = "Error updating details: " . $conn->error;
        $message_type = "error";
        $show_status_modal = true;
    }
}

// Handle Add Client
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_client'])) {
    $client_username = $conn->real_escape_string($_POST['client_username']);
    $password = password_hash($_POST['client_password'], PASSWORD_DEFAULT);
    $company_name = $conn->real_escape_string($_POST['company_name']);
    $company_address = $conn->real_escape_string($_POST['company_address']);
    $contact_no = $conn->real_escape_string($_POST['contact_no']);
    $email_address = $conn->real_escape_string($_POST['email_address']);
    $website_link = $conn->real_escape_string($_POST['website_link']);
    $contact_person = $conn->real_escape_string($_POST['contact_person']);
    $contact_person_position = $conn->real_escape_string($_POST['contact_person_position']);
    $contact_person_no = $conn->real_escape_string($_POST['contact_person_no']);
    $qr_limit = 1;
    $site_limit = 1;
    $guard_limit = 0;
    $inspector_limit = 0;
    $supervisor_limit = 0;

    $conn->begin_transaction();
    try {
        // 1. Check if username exists
        $user_check = $conn->query("SELECT id FROM users WHERE username = '$client_username'");
        if ($user_check && $user_check->num_rows > 0) {
            throw new Exception("Username '$client_username' is already taken.");
        }

        // 2. Check Agency Limit
        $limit_res = $conn->query("SELECT client_limit FROM users WHERE id = $agency_id");
        $client_limit = 0;
        if ($limit_res && $limit_res->num_rows > 0) {
            $client_limit = (int)$limit_res->fetch_assoc()['client_limit'];
        }

        $count_res = $conn->query("SELECT COUNT(DISTINCT client_id) as current_clients FROM agency_clients WHERE agency_id = $agency_id");
        $current_clients = (int)$count_res->fetch_assoc()['current_clients'];

        if ($client_limit === 0) {
            throw new Exception("Adding clients is disabled. Contact the Admin to set a client limit first.");
        }
        if ($client_limit > 0 && $current_clients >= $client_limit) {
            throw new Exception("You have reached your maximum client limit ($client_limit). Contact Admin to increase it.");
        }

        // 3. Create User Account
        if (!$conn->query("INSERT INTO users (username, password, user_level) VALUES ('$client_username', '$password', 'client')")) {
            throw new Exception("Error creating user: " . $conn->error);
        }
        $new_client_id = $conn->insert_id;

        // 4. Create Agency-Client Assignment and Profile
        if (!$conn->query("INSERT INTO agency_clients (agency_id, client_id, company_name, company_address, contact_no, email_address, website_link, contact_person, contact_person_position, contact_person_no, qr_limit, client_limit, guard_limit, inspector_limit, supervisor_limit) 
                           VALUES ($agency_id, $new_client_id, '$company_name', '$company_address', '$contact_no', '$email_address', '$website_link', '$contact_person', '$contact_person_position', '$contact_person_no', $qr_limit, $site_limit, $guard_limit, $inspector_limit, $supervisor_limit)")) {
            throw new Exception("Error creating client profile: " . $conn->error);
        }
        $mapping_id = $conn->insert_id;

        // 5. Handle Optional Supervisor Creation
        if (isset($_POST['create_supervisor'])) {
            $sup_name = $conn->real_escape_string($_POST['supervisor_name']);
            $sup_username = $conn->real_escape_string($_POST['supervisor_username']);
            $sup_password = password_hash($_POST['supervisor_password'], PASSWORD_DEFAULT);
            $sup_contact = $conn->real_escape_string($_POST['supervisor_contact'] ?? '');

            // Check supervisor username
            $sup_check = $conn->query("SELECT id FROM users WHERE username = '$sup_username'");
            if ($sup_check && $sup_check->num_rows > 0) {
                throw new Exception("Supervisor username '$sup_username' is already taken.");
            }

            // Check client's supervisor limit
            if ($supervisor_limit <= 0) {
                throw new Exception("Cannot create supervisor: This client's supervisor limit is set to 0.");
            }

            // Create Supervisor User
            if (!$conn->query("INSERT INTO users (username, password, user_level) VALUES ('$sup_username', '$sup_password', 'supervisor')")) {
                throw new Exception("Error creating supervisor user: " . $conn->error);
            }
            $sup_user_id = $conn->insert_id;

            // Create Supervisor Profile
            if (!$conn->query("INSERT INTO supervisors (user_id, agency_id, name, contact_no) VALUES ($sup_user_id, $agency_id, '$sup_name', '$sup_contact')")) {
                throw new Exception("Error creating supervisor profile: " . $conn->error);
            }
            $new_sup_id = $conn->insert_id;

            // Link Supervisor to Client
            if (!$conn->query("UPDATE agency_clients SET supervisor_id = $new_sup_id WHERE id = $mapping_id")) {
                throw new Exception("Error linking supervisor to client: " . $conn->error);
            }
        }

        // 6. Handle Logo Upload
        if (isset($_FILES['company_logo']) && $_FILES['company_logo']['error'] == 0) {
            $target_dir = "uploads/logos/";
            if (!file_exists($target_dir)) mkdir($target_dir, 0777, true);
            $file_ext = strtolower(pathinfo($_FILES["company_logo"]["name"], PATHINFO_EXTENSION));
            $new_filename = "logo_" . $mapping_id . "_" . time() . "." . $file_ext;
            $target_file = $target_dir . $new_filename;
            
            $valid_extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
            if (in_array($file_ext, $valid_extensions) && $_FILES["company_logo"]["size"] < 2000000) {
                if (move_uploaded_file($_FILES["company_logo"]["tmp_name"], $target_file)) {
                    $conn->query("UPDATE agency_clients SET company_logo = '$target_file' WHERE id = $mapping_id");
                }
            }
        }

        $conn->commit();
        $message = "Client account and profile created successfully!";
        $message_type = "success";
        $show_status_modal = true;
    } catch (Exception $e) {
        $conn->rollback();
        $message = $e->getMessage();
        $message_type = "error";
        $show_status_modal = true;
    }
}

// Handle Guard Assignment
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['assign_guard'])) {
    $mapping_id = (int)$_POST['mapping_id'];
    $guard_id = (int)$_POST['guard_id'];
    
    // Check if already assigned
    $check = $conn->query("SELECT id FROM guard_assignments WHERE guard_id = $guard_id AND agency_client_id = $mapping_id");
    if ($check && $check->num_rows > 0) {
        $message = "Guard is already assigned to this client.";
        $message_type = "error";
        $show_status_modal = true;
    } else {
    // Check Agency's Pooled Guard Assignment Limit
    $limit_sql = "
        SELECT u.agency_guard_limit, 
               (SELECT COUNT(*) FROM guard_assignments ga 
                JOIN agency_clients ac_inner ON ga.agency_client_id = ac_inner.id 
                WHERE ac_inner.agency_id = u.id) as current_assigned
        FROM users u 
        WHERE u.id = $agency_id
    ";
    $limit_res = $conn->query($limit_sql);
    $can_assign = true;
    if ($limit_res && $row = $limit_res->fetch_assoc()) {
        $max_assigned = (int)$row['agency_guard_limit'];
        $current_assigned = (int)$row['current_assigned'];
        
        if ($max_assigned > 0 && $current_assigned >= $max_assigned) {
            $message = "Assignment failed: Your agency has reached its total assignment limit of $max_assigned guards.";
            $message_type = "error";
            $show_status_modal = true;
            $can_assign = false;
        }
    }

    if ($can_assign) {
        if ($conn->query("INSERT INTO guard_assignments (guard_id, agency_client_id) VALUES ($guard_id, $mapping_id)")) {
            $message = "Guard assigned successfully!";
            $message_type = "success";
            $show_status_modal = true;
        } else {
            $message = "Error assigning guard: " . $conn->error;
            $message_type = "error";
            $show_status_modal = true;
        }
    }
}

// Handle Inspector Assignment
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['assign_inspector'])) {
    $mapping_id = (int)$_POST['mapping_id'];
    $inspector_id = (int)$_POST['inspector_id'];
    
    // Check if already assigned
    $check = $conn->query("SELECT id FROM inspector_assignments WHERE inspector_id = $inspector_id AND agency_client_id = $mapping_id");
    if ($check && $check->num_rows > 0) {
        $message = "Inspector is already assigned to this client.";
        $message_type = "error";
        $show_status_modal = true;
    } else {
        // Enforce Client's Inspector Limit
        $limit_sql = "
            SELECT ac.inspector_limit, 
                   (SELECT COUNT(*) FROM inspector_assignments WHERE agency_client_id = ac.id) as current_inspectors
            FROM agency_clients ac 
            WHERE ac.id = $mapping_id
        ";
        $limit_res = $conn->query($limit_sql);
        $can_assign = true;
        if ($limit_res && $row = $limit_res->fetch_assoc()) {
            $max = (int)$row['inspector_limit'];
            $current = (int)$row['current_inspectors'];
            if ($max > 0 && $current >= $max) {
                $message = "Assignment failed: This client site has reached its limit of $max inspectors.";
                $message_type = "error";
                $show_status_modal = true;
                $can_assign = false;
            }
        }

        if ($can_assign) {
            if ($conn->query("INSERT INTO inspector_assignments (inspector_id, agency_client_id) VALUES ($inspector_id, $mapping_id)")) {
                $message = "Inspector assigned successfully!";
                $message_type = "success";
                $show_status_modal = true;
            } else {
                $message = "Error assigning inspector: " . $conn->error;
                $message_type = "error";
                $show_status_modal = true;
            }
        }
    }
}
}

// Fetch Client Limit and Current Count for Headers (after potential POST updates)
$limit_res = $conn->query("SELECT client_limit FROM users WHERE id = $agency_id");
$client_limit = 0;
if ($limit_res && $row = $limit_res->fetch_assoc()) {
    $client_limit = (int)$row['client_limit'];
}

$count_res = $conn->query("SELECT COUNT(DISTINCT client_id) as current_clients FROM agency_clients WHERE agency_id = $agency_id");
$current_clients = (int)$count_res->fetch_assoc()['current_clients'];

$display_title = "Assigned Clients";
$clients = [];

// Fetch Assigned Clients
$clients_sql = "
    SELECT ac.id as mapping_id, u.id as user_id, u.status, u.username as client_username, ac.company_name, ac.company_logo, 
           ac.qr_limit, ac.guard_limit, ac.inspector_limit, ac.supervisor_limit,
           ac.company_address, ac.contact_no, ac.email_address, ac.website_link,
           ac.contact_person, ac.contact_person_position, ac.contact_person_no,
           (SELECT COUNT(*) FROM guard_assignments WHERE agency_client_id = ac.id) as current_guards,
           (SELECT COUNT(*) FROM inspector_assignments WHERE agency_client_id = ac.id) as current_inspectors,
           (SELECT COUNT(*) FROM checkpoints WHERE agency_client_id = ac.id AND is_zero_checkpoint = 0) as qr_count,
           (
               SELECT GROUP_CONCAT(g.name SEPARATOR ' | ')
               FROM guard_assignments ga
               JOIN guards g ON ga.guard_id = g.id
               WHERE ga.agency_client_id = ac.id
           ) as guard_names
    FROM agency_clients ac
    JOIN users u ON ac.client_id = u.id
    JOIN users a ON ac.agency_id = a.id
    WHERE ac.agency_id = $agency_id
    ORDER BY ac.id ASC
";
$clients_res = $conn->query($clients_sql);
if ($clients_res) {
    while($row = $clients_res->fetch_assoc()) {
        $clients[] = $row;
    }
}

// Fetch All Guards for this agency for the modals
$guards_sql = "SELECT id, name FROM guards WHERE agency_id = $agency_id ORDER BY name ASC";
$guards_res = $conn->query($guards_sql);
$all_guards = [];
if ($guards_res) {
    while($g = $guards_res->fetch_assoc()) $all_guards[] = $g;
}

// Fetch All Inspectors for this agency
$inspectors_sql = "SELECT id, name FROM inspectors WHERE agency_id = $agency_id ORDER BY name ASC";
$inspectors_res = $conn->query($inspectors_sql);
$all_inspectors = [];
if ($inspectors_res) {
    while($i = $inspectors_res->fetch_assoc()) $all_inspectors[] = $i;
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <script src="js/modal_system.js"></script>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Inter', sans-serif; }
        body { display: flex; height: 100vh; background-color: #f3f4f6; color: #1f2937; padding: 0 16px 0 0; gap: 16px; }

        .sidebar { width: 250px; background-color: #111827; color: #fff; display: flex; flex-direction: column; transition: all 0.3s ease; box-shadow: 2px 0 10px rgba(0,0,0,0.1); overflow: hidden; }
        .sidebar-header { padding: 24px 20px; font-size: 1.5rem; font-weight: 700; text-align: center; border-bottom: 1px solid #374151; letter-spacing: 0.5px; color: #f9fafb; }
        .nav-links { list-style: none; flex: 1; padding-top: 15px; }
        .nav-link { padding: 15px 24px; display: flex; align-items: center; color: #9ca3af; text-decoration: none; font-weight: 500; transition: background 0.2s, color 0.2s, border-color 0.2s; border-left: 4px solid transparent; }
        .nav-link:hover, .nav-link.active { background-color: #1f2937; color: #fff; border-left-color: #10b981; }
        .sidebar-footer { padding: 20px; border-top: 1px solid #374151; }
        .logout-btn { display: block; text-align: center; padding: 12px; background-color: #ef4444; color: white; text-decoration: none; border-radius: 8px; font-weight: 600; transition: background 0.3s; }
        .logout-btn:hover { background-color: #dc2626; }

        .main-content { flex: 1; display: flex; flex-direction: column; overflow-y: auto; background: white; border-radius: 16px; border: 1px solid #e5e7eb; }
        .topbar { background: white; padding: 20px 32px; display: flex; justify-content: space-between; align-items: center; box-shadow: 0 1px 3px rgba(0,0,0,0.05); position: sticky; top: 0; z-index: 10; }
        .topbar h2 { font-size: 1.25rem; font-weight: 600; color: #111827; }

        .content-area { padding: 32px; max-width: 1220px; margin: 0 auto; width: 100%; }
        
        @keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

        .alert { padding: 16px; border-radius: 8px; margin-bottom: 24px; font-weight: 500; }
        .alert-success { background-color: #d1fae5; color: #065f46; border: 1px solid #34d399; }
        .alert-error { background-color: #fee2e2; color: #991b1b; border: 1px solid #f87171; }

        .card { background: white; padding: 28px; border-radius: 12px; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05); }
        .card-header { font-size: 1.125rem; font-weight: 600; margin-bottom: 20px; border-bottom: 1px solid #e5e7eb; padding-bottom: 12px; display: flex; justify-content: space-between; align-items: center; }

        table { width: 100%; border-collapse: collapse; margin-top: 10px; }
        th, td { padding: 12px 16px; text-align: left; border-bottom: 1px solid #e5e7eb; }
        tbody tr { cursor: pointer; transition: background 0.2s; }
        tbody tr:hover { background-color: #f8fafc; }
        th { background-color: #f9fafb; font-weight: 600; color: #4b5563; font-size: 0.875rem; }
        
        .btn-sm { height: 32px; padding: 0 12px; font-size: 0.75rem; border-radius: 6px; cursor: pointer; border: 1px solid transparent; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; white-space: nowrap; transition: all 0.2s; }
        .btn-primary { background: #10b981; color: white; border: none; }
        .btn-outline { background: white; border: 1.5px solid #e2e8f0; color: #475569; }
        .btn-outline:hover { background: #f9fafb; }

        .modal { display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(17, 24, 39, 0.7); z-index: 1000; align-items: center; justify-content: center; backdrop-filter: blur(4px); }
        .modal-content { background: white; padding: 32px; border-radius: 12px; width: 100%; max-width: 500px; position: relative; text-align: center; box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04); max-height: 90vh; overflow-y: auto; }
        .modal.show { display: flex; }
        
        .form-group { margin-bottom: 16px; }
        .form-label { display: block; font-size: 0.875rem; font-weight: 500; color: #374151; margin-bottom: 8px; }
        .form-control { width: 100%; padding: 10px 14px; border: 1px solid #d1d5db; border-radius: 6px; font-size: 0.95rem; }
    </style>
</head>
<body>

    <aside class="sidebar">
        <div class="sidebar-header">Agency Portal</div>
        <ul class="nav-links">
            <li><a href="agency_dashboard.php" class="nav-link">Dashboard</a></li>
            <li><a href="agency_client_management.php" class="nav-link active">Client Management</a></li>

            <li><a href="manage_guards.php" class="nav-link">Manage Guards</a></li>
            <li><a href="manage_inspectors.php" class="nav-link">Manage Inspectors</a></li>
            <li><a href="agency_patrol_management.php" class="nav-link">Patrol Management</a></li>
            <li><a href="agency_patrol_history.php" class="nav-link">Patrol History</a></li>
            <li><a href="agency_inspector_history.php" class="nav-link">Inspector Visits</a></li>
            <li><a href="agency_incidents.php" class="nav-link">Incident Reports</a></li>
            <li><a href="agency_reports.php" class="nav-link">Reports</a></li>

        </ul>
        <div class="sidebar-footer">
            <a href="#" class="logout-btn" onclick="document.getElementById('logoutModal').classList.add('show'); return false;">Logout</a>
        </div>
    </aside>

    <main class="main-content">
        <header class="topbar">
            <h2>Client Profile Management</h2>
        </header>

        <div class="content-area">

            <!-- Registered Clients Section -->
            <div class="card">
                <div class="card-header">
                    <h3 style="margin: 0; border: none;"><?php echo $display_title; ?></h3>
                    <div style="display: flex; align-items: center; gap: 12px;">
                        <?php
                            $limit_color = ($client_limit > 0 && $current_clients >= $client_limit) ? '#ef4444' : '#10b981';
                            $limit_display = $client_limit;
                        ?>
                        <div style="display: flex; align-items: center; gap: 8px; background: #f0fdf4; border: 1px solid #bbf7d0; padding: 6px 14px; border-radius: 99px;">
                            <span style="font-size: 0.75rem; color: #6b7280; font-weight: 500;">Client Slots:</span>
                            <span style="font-size: 0.95rem; font-weight: 700; color: <?php echo $limit_color; ?>;">
                                <?php echo $current_clients; ?> / <?php echo $limit_display; ?>
                            </span>
                        </div>
                        <?php if ($client_limit > 0): ?>
                        <button class="btn-sm btn-primary" onclick="openAddClientModal()" style="padding: 8px 16px;">+ Add Client</button>
                        <?php else: ?>
                        <button class="btn-sm" style="padding: 8px 16px; background:#e2e8f0; color:#94a3b8; cursor:not-allowed; border:none;" disabled title="Client limit is 0. Contact Admin to enable.">+ Add Client</button>
                        <?php endif; ?>
                    </div>
                </div>
                    <table>
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Company Details</th>
                                <th>QR Checkpoints</th>
                                <th>Guards Assigned</th>
                                <th>Inspectors</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                    <tbody>
                         <?php 
                            for ($i = 1; $i <= $client_limit; $i++): 
                                $row = isset($clients[$i-1]) ? $clients[$i-1] : null;
                                if ($row):
                         ?>
                                <tr onclick='openSummaryModal(<?php echo htmlspecialchars(json_encode($row)); ?>)'>
                                    <td><?php echo $i; ?></td>
                                    <td>
                                        <div style="display: flex; align-items: center; gap: 12px;">
                                            <?php if ($row['company_logo']): ?>
                                                <img src="<?php echo htmlspecialchars($row['company_logo']); ?>" alt="Logo" style="width: 40px; height: 40px; object-fit: contain; border-radius: 4px; border: 1px solid #e5e7eb;">
                                            <?php else: ?>
                                                <div style="width: 40px; height: 40px; background: #f1f5f9; display: flex; align-items: center; justify-content: center; border-radius: 4px; color: #9ca3af; font-size: 0.75rem;">No Logo</div>
                                            <?php endif; ?>
                                            <div>
                                                <div style="font-size: 0.9rem; font-weight: 600; display: flex; align-items: center; gap: 6px;">
                                                    <?php echo $row['company_name'] ?: 'Client ' . $i; ?>
                                                    <?php if (($row['status'] ?? 'active') === 'suspended'): ?>
                                                        <span style="font-size: 0.65rem; background: #fee2e2; color: #ef4444; padding: 2px 6px; border-radius: 4px; font-weight: 700; text-transform: uppercase;">Suspended</span>
                                                    <?php endif; ?>
                                                </div>
                                                <div style="font-size: 0.75rem; color: #6b7280; margin-top: 2px;">Account: <strong><?php echo htmlspecialchars($row['client_username']); ?></strong></div>
                                            </div>
                                        </div>
                                    </td>
                                    <td style="white-space: nowrap;">
                                        <span style="font-weight: 600; color: #10b981;">
                                            Active: <?php echo $row['qr_count']; ?> / <?php echo $row['qr_limit']; ?>
                                        </span>
                                    </td>
                                    <td style="white-space: nowrap;">
                                        <span style="font-weight: 600; color: #10b981;">
                                            Active: <?php echo $row['current_guards']; ?> / <?php echo $row['guard_limit']; ?>
                                        </span>
                                    </td>
                                    <td style="white-space: nowrap;">
                                        <span style="font-weight: 600; color: #10b981;">
                                            Active: <?php echo $row['current_inspectors']; ?> / <?php echo $row['inspector_limit']; ?>
                                        </span>
                                    </td>
                                     <td>
                                        <div style="display: flex; gap: 8px; justify-content: flex-start; align-items: center;" onclick="event.stopPropagation()">
                                            <button class="btn-sm btn-primary" onclick="openGuardModal(<?php echo $row['mapping_id']; ?>, '<?php echo addslashes($row['client_username']); ?>')">Assign Guard</button>
                                            <button class="btn-sm" style="background:#4f46e5; color:white; border:none;" onclick="openInspectorModal(<?php echo $row['mapping_id']; ?>, '<?php echo addslashes($row['client_username']); ?>')">Assign Inspector</button>
                                            <?php if (($row['status'] ?? 'active') === 'suspended'): ?>
                                                <form method="POST" action="" onsubmit="CustomModal.confirmForm(event, 'Restore this client\'s access?');" style="margin:0; display: contents;">
                                                    <input type="hidden" name="user_id" value="<?php echo $row['user_id']; ?>">
                                                    <button type="submit" name="restore_client" class="btn-sm" style="background: #10b981; color: white; border: none;">Restore</button>
                                                </form>
                                            <?php else: ?>
                                                <form method="POST" action="" onsubmit="CustomModal.confirmForm(event, 'Suspend this client\'s access?');" style="margin:0; display: contents;">
                                                    <input type="hidden" name="user_id" value="<?php echo $row['user_id']; ?>">
                                                    <button type="submit" name="suspend_client" class="btn-sm" style="background: #f59e0b; color: white; border: none;">Suspend</button>
                                                </form>
                                            <?php endif; ?>
                                            <form method="POST" action="" onsubmit="CustomModal.confirmForm(event, 'Are you completely sure? This will permanently delete the client and free up all their assigned slots. This cannot be undone.');" style="margin:0; display: contents;">
                                                <input type="hidden" name="user_id" value="<?php echo $row['user_id']; ?>">
                                                <button type="submit" name="remove_client_full" class="btn-sm" style="background: #ef4444; color: white; border: none;">Remove</button>
                                            </form>
                                        </div>
                                    </td>
                                </tr>
                            <?php else: ?>
                                <tr onclick="openAddClientModal()" style="cursor: pointer;" onmouseover="this.style.background='#f0fdf4'" onmouseout="this.style.background='transparent'">
                                    <td><?php echo $i; ?></td>
                                    <td colspan="5" style="color: #10b981; font-style: italic; font-weight: 500;">+ Available Client Slot &nbsp;<span style="font-size:0.75rem; color:#9ca3af; font-weight:400;">(click to add)</span></td>
                                </tr>
                            <?php endif; ?>
                        <?php endfor; ?>
                    </tbody>
                </table>
                </div>
            </div>
        </main>

    <!-- Modal: Add New Client -->
    <div id="addClientModal" class="modal">
        <div class="modal-content" style="max-width: 800px; text-align: left;">
            <div class="card-header" style="padding-top: 0;">
                <h3 style="margin: 0; border: none;">Create New Client Account</h3>
                <button type="button" class="btn-sm btn-outline" onclick="closeModal('addClientModal')" style="border:none; font-size: 1.5rem; line-height: 1;">&times;</button>
            </div>
                    <form action="agency_client_management.php" method="POST" enctype="multipart/form-data" autocomplete="off">
                        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 24px;">
                            <div class="form-group" style="grid-column: span 2;">
                                <label class="form-label">Client Username</label>
                                <input type="text" name="client_username" class="form-control" placeholder="Account login username" required autocomplete="none">
                            </div>

                            <div class="form-group" style="grid-column: span 2;">
                                <label class="form-label">Account Password</label>
                                <input type="password" name="client_password" class="form-control" placeholder="••••••••" required autocomplete="new-password">
                            </div>

                            <div class="form-group" style="grid-column: span 2; border-top: 2px solid #f3f4f6; padding-top: 24px; margin-top: 8px;">
                                <label class="form-label" style="font-weight: 700; color: #111827;">COMPANY PROFILE DETAILS</label>
                            </div>

                            <div class="form-group" style="grid-column: span 2;">
                                <label class="form-label">Company Name</label>
                                <input type="text" name="company_name" class="form-control" placeholder="e.g. Acme Corp" required>
                            </div>
                            
                            <div class="form-group" style="grid-column: span 2;">
                                <label class="form-label">Company Address</label>
                                <textarea name="company_address" class="form-control" placeholder="Full Business Address" rows="3"></textarea>
                            </div>

                            <div class="form-group">
                                <label class="form-label">Contact No</label>
                                <input type="text" name="contact_no" class="form-control" placeholder="Company Phone">
                            </div>

                            <div class="form-group">
                                <label class="form-label">Email Address</label>
                                <input type="email" name="email_address" class="form-control" placeholder="contact@company.com">
                            </div>

                            <div class="form-group" style="grid-column: span 2;">
                                <label class="form-label">Website/Social Link (Optional)</label>
                                <input type="text" name="website_link" class="form-control" placeholder="FB, Viber, or Website URL">
                            </div>

                            <div style="grid-column: span 2; margin-top: 16px; border-top: 1px solid #f3f4f6; padding-top: 24px; margin-bottom: 8px;">
                                <h4 style="font-size: 0.95rem; font-weight: 700; color: #111827; text-transform: uppercase;">Contact Person Details</h4>
                            </div>

                            <div class="form-group">
                                <label class="form-label">Contact Person</label>
                                <input type="text" name="contact_person" class="form-control" placeholder="Full Name">
                            </div>

                            <div class="form-group">
                                <label class="form-label">Position</label>
                                <input type="text" name="contact_person_position" class="form-control" placeholder="Job Title">
                            </div>

                            <div class="form-group" style="grid-column: span 2;">
                                <label class="form-label">Contact No (Contact Person)</label>
                                <input type="text" name="contact_person_no" class="form-control" placeholder="Personal or Office Phone">
                            </div>



                            <div class="form-group" style="grid-column: span 2; border-top: 2px solid #f3f4f6; padding-top: 24px; margin-top: 8px;">
                                <div style="display: flex; align-items: center; gap: 12px; margin-bottom: 4px;">
                                    <input type="checkbox" name="create_supervisor" id="create_supervisor_chk" style="width: 20px; height: 20px; cursor: pointer;" onchange="toggleSupervisorFields()">
                                    <label for="create_supervisor_chk" style="font-weight: 700; color: #111827; cursor: pointer; margin-bottom: 0;">Create Supervisor Account for this Client?</label>
                                </div>
                                <p style="font-size: 0.8rem; color: #6b7280; padding-left: 32px;">Register a dedicated supervisor and auto-assign them to this client site.</p>
                            </div>

                            <div id="supervisor_fields" style="grid-column: span 2; display: none; background: #f8fafc; padding: 20px; border-radius: 8px; border: 1px dashed #cbd5e1; margin-top: 8px;">
                                <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 16px;">
                                    <div class="form-group" style="grid-column: span 2;">
                                        <label class="form-label">Supervisor Full Name</label>
                                        <input type="text" name="supervisor_name" id="sup_fullname" class="form-control" placeholder="Complete Name">
                                    </div>
                                    <div class="form-group">
                                        <label class="form-label">Account Username</label>
                                        <input type="text" name="supervisor_username" id="sup_username" class="form-control" placeholder="Unique username">
                                    </div>
                                    <div class="form-group">
                                        <label class="form-label">Account Password</label>
                                        <input type="password" name="supervisor_password" id="sup_password" class="form-control" placeholder="••••••••">
                                    </div>
                                    <div class="form-group" style="grid-column: span 2;">
                                        <label class="form-label">Contact No (Optional)</label>
                                        <input type="text" name="supervisor_contact" id="sup_contact" class="form-control" placeholder="Mobile phone">
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div style="margin-top: 40px; border-top: 1px solid #e5e7eb; padding-top: 24px; display: flex; gap: 12px; justify-content: flex-end;">
                            <button type="button" class="btn-sm btn-outline" onclick="closeModal('addClientModal')" style="padding: 12px 24px;">Cancel</button>
                            <button type="submit" name="add_client" class="btn-sm btn-primary" style="padding: 12px 40px; font-size: 1rem; border-radius: 8px;">Create & Register Client</button>
                        </div>
                    </form>
        </div>
    </div>
        </div>
    </main>

    <!-- Site Summary Modal -->
    <div id="summaryModal" class="modal">
        <div class="modal-content" style="text-align: left;">
            <h3 id="summary_title" style="margin-bottom: 24px; border-bottom: 1px solid #e5e7eb; padding-bottom: 12px;">Site Summary</h3>
            
            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 24px;">
                <div style="background: #f0fdf4; padding: 16px; border-radius: 12px; border: 1px solid #bbf7d0;">
                    <div style="color: #166534; font-size: 0.75rem; font-weight: 700; text-transform: uppercase; margin-bottom: 4px;">Total QR Checkpoints</div>
                    <div id="summary_qr_count" style="font-size: 1.5rem; font-weight: 800; color: #14532d;">0</div>
                </div>
                <div style="background: #eff6ff; padding: 16px; border-radius: 12px; border: 1px solid #bfdbfe;">
                    <div style="color: #1e40af; font-size: 0.75rem; font-weight: 700; text-transform: uppercase; margin-bottom: 4px;">Personnel Count</div>
                    <div id="summary_guard_count" style="font-size: 1.5rem; font-weight: 800; color: #1e3a8a;">0</div>
                </div>
            </div>

            <div style="background: #f9fafb; padding: 20px; border-radius: 12px; border: 1px solid #e5e7eb; margin-bottom: 20px;">
                <div style="font-size: 0.8rem; font-weight: 700; color: #6b7280; text-transform: uppercase; margin-bottom: 12px; letter-spacing: 0.05em;">Client Details</div>
                <div style="display: grid; gap: 12px;">
                    <div style="display: flex; align-items: flex-start; gap: 10px;">
                        <span style="font-size: 1.1rem; filter: grayscale(1);">👤</span>
                        <div>
                            <div style="font-size: 0.75rem; color: #9ca3af;">Contact Person</div>
                            <div id="summary_contact_person" style="font-size: 0.95rem; font-weight: 600; color: #374151;">---</div>
                        </div>
                    </div>
                    <div style="display: flex; align-items: flex-start; gap: 10px;">
                        <span style="font-size: 1.1rem; filter: grayscale(1);">📞</span>
                        <div>
                            <div style="font-size: 0.75rem; color: #9ca3af;">Phone Number</div>
                            <div id="summary_contact_no" style="font-size: 0.95rem; font-weight: 600; color: #374151;">---</div>
                        </div>
                    </div>
                    <div style="display: flex; align-items: flex-start; gap: 10px;">
                        <span style="font-size: 1.1rem; filter: grayscale(1);">✉️</span>
                        <div>
                            <div style="font-size: 0.75rem; color: #9ca3af;">Email Address</div>
                            <div id="summary_email" style="font-size: 0.95rem; font-weight: 600; color: #374151;">---</div>
                        </div>
                    </div>
                    <div style="display: flex; align-items: flex-start; gap: 10px;">
                        <span style="font-size: 1.1rem; filter: grayscale(1);">📍</span>
                        <div>
                            <div style="font-size: 0.75rem; color: #9ca3af;">Company Address</div>
                            <div id="summary_address" style="font-size: 0.9rem; font-weight: 500; color: #4b5563; line-height: 1.4;">---</div>
                        </div>
                    </div>
                </div>
            </div>

            <div style="background: #ffffff; padding: 20px; border-radius: 12px; border: 1px solid #e5e7eb;">
                <div style="font-size: 0.8rem; font-weight: 700; color: #6b7280; text-transform: uppercase; margin-bottom: 12px; letter-spacing: 0.05em;">Assigned Personnel</div>
                <div id="summary_guards_list" style="font-size: 0.95rem; color: #4b5563; font-weight: 500; line-height: 1.7;">
                    <!-- Guards list injected here -->
                </div>
            </div>

            <div style="margin-top: 32px; display: flex; gap: 12px;">
                <button type="button" class="btn-sm btn-outline" style="flex:1; padding: 12px; background: #f3f4f6;" id="summary_edit_btn">Edit Details</button>
                <button type="button" class="btn-sm btn-outline" style="flex:1; padding: 12px;" onclick="closeModal('summaryModal')">Close</button>
            </div>
        </div>
    </div>

    <!-- Details Modal -->
    <div id="detailsModal" class="modal">
        <div class="modal-content" style="max-width: 600px; text-align: left;">
            <h3 style="margin-bottom: 20px; border-bottom: 1px solid #e5e7eb; padding-bottom: 12px;">Edit Client Details</h3>
            <form action="agency_client_management.php" method="POST" enctype="multipart/form-data">
                <input type="hidden" name="mapping_id" id="details_mapping_id">
                
                <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 16px;">
                    <div class="form-group" style="grid-column: span 2;">
                        <label class="form-label">Company Name</label>
                        <input type="text" name="company_name" id="details_company_name" class="form-control" placeholder="e.g. Acme Corp" required>
                    </div>
                    
                    <div class="form-group" style="grid-column: span 2;">
                        <label class="form-label">Company Address</label>
                        <textarea name="company_address" id="details_company_address" class="form-control" placeholder="Full Business Address" rows="2"></textarea>
                    </div>

                    <div class="form-group">
                        <label class="form-label">Contact No</label>
                        <input type="text" name="contact_no" id="details_contact_no" class="form-control" placeholder="Company Phone">
                    </div>

                    <div class="form-group">
                        <label class="form-label">Email Address</label>
                        <input type="email" name="email_address" id="details_email_address" class="form-control" placeholder="contact@company.com">
                    </div>

                    <div class="form-group" style="grid-column: span 2;">
                        <label class="form-label">Website/Social Link (Optional)</label>
                        <input type="text" name="website_link" id="details_website_link" class="form-control" placeholder="FB, Viber, or Website URL">
                    </div>

                    <div style="grid-column: span 2; margin-top: 8px; border-top: 1px solid #f3f4f6; padding-top: 16px; margin-bottom: 16px;">
                        <h4 style="font-size: 0.9rem; font-weight: 700; color: #4b5563; text-transform: uppercase; letter-spacing: 0.5px;">Contact Person Details</h4>
                    </div>

                    <div class="form-group">
                        <label class="form-label">Contact Person</label>
                        <input type="text" name="contact_person" id="details_contact_person" class="form-control" placeholder="Full Name">
                    </div>

                    <div class="form-group">
                        <label class="form-label">Position</label>
                        <input type="text" name="contact_person_position" id="details_contact_person_position" class="form-control" placeholder="Job Title">
                    </div>

                    <div class="form-group" style="grid-column: span 2;">
                        <label class="form-label">Contact No (Contact Person)</label>
                        <input type="text" name="contact_person_no" id="details_contact_person_no" class="form-control" placeholder="Personal or Office Phone">
                    </div>



                    <div class="form-group" style="grid-column: span 2; margin-top: 8px; border-top: 1px solid #f3f4f6; padding-top: 16px;">
                        <label class="form-label">Company Logo (Photo)</label>
                        <input type="file" name="company_logo" id="details_company_logo" class="form-control" accept="image/*">
                        <small style="color: #6b7280; font-size: 0.75rem;">Upload a photo of the client logo.</small>
                    </div>
                </div>

                <div style="display: flex; gap: 12px; margin-top: 32px;">
                    <button type="button" class="btn-sm btn-outline" style="flex:1; padding: 12px;" onclick="closeModal('detailsModal')">Cancel</button>
                    <button type="submit" name="update_details" class="btn-sm btn-primary" style="flex:1; padding: 12px;">Save Changes</button>
                </div>
            </form>
        </div>
    </div>

    <!-- Guard Modal -->
    <div id="guardModal" class="modal">
        <div class="modal-content">
            <h3 style="margin-bottom: 10px;">Assign Guard</h3>
            <p id="guard_client_name" style="color: #6b7280; font-size: 0.9rem; margin-bottom: 20px;"></p>
            <form action="agency_client_management.php" method="POST">
                <input type="hidden" name="mapping_id" id="guard_mapping_id">
                <div class="form-group">
                    <label class="form-label">Select Internal Guard</label>
                    <select name="guard_id" class="form-control" required>
                        <option value="" disabled selected>-- Select Guard --</option>
                        <?php foreach($all_guards as $g): ?>
                            <option value="<?php echo $g['id']; ?>"><?php echo htmlspecialchars($g['name']); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div style="display: flex; gap: 12px; margin-top: 24px;">
                    <button type="button" class="btn-sm btn-outline" style="flex:1; padding: 12px;" onclick="closeModal('guardModal')">Cancel</button>
                    <button type="submit" name="assign_guard" class="btn-sm btn-primary" style="flex:1; padding: 12px;">Assign Personnel</button>
                </div>
            </form>
        </div>
    </div>

    <!-- Inspector Modal -->
    <div id="inspectorModal" class="modal">
        <div class="modal-content">
            <h3 style="margin-bottom: 10px;">Assign Inspector</h3>
            <p id="inspector_client_name" style="color: #6b7280; font-size: 0.9rem; margin-bottom: 20px;"></p>
            <form action="agency_client_management.php" method="POST">
                <input type="hidden" name="mapping_id" id="inspector_mapping_id">
                <div class="form-group">
                    <label class="form-label">Select Inspector</label>
                    <select name="inspector_id" class="form-control" required>
                        <option value="" disabled selected>-- Select Inspector --</option>
                        <?php foreach($all_inspectors as $i): ?>
                            <option value="<?php echo $i['id']; ?>"><?php echo htmlspecialchars($i['name']); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div style="display: flex; gap: 12px; margin-top: 24px;">
                    <button type="button" class="btn-sm btn-outline" style="flex:1; padding: 12px;" onclick="closeModal('inspectorModal')">Cancel</button>
                    <button type="submit" name="assign_inspector" class="btn-sm btn-primary" style="flex:1; padding: 12px; background: #4f46e5; border:none;">Assign Inspector</button>
                </div>
            </form>
        </div>
    </div>

    <!-- Logout Modal -->
    <div class="modal" id="logoutModal">
        <div class="modal-content" style="max-width: 400px; text-align: center;">
            <h3 style="margin-bottom: 20px;">Ready to Leave?</h3>
            <div style="display: flex; gap: 12px;">
                <button class="btn-sm btn-outline" style="flex:1;" onclick="closeModal('logoutModal')">Cancel</button>
                <a href="logout.php" class="btn-sm btn-primary" style="flex:1; background: #ef4444; border:none; text-decoration:none; display:flex; align-items:center; justify-content:center;">Log Out</a>
            </div>
        </div>
    </div>

    <!-- Status Process Modal (Generic) -->
    <div id="statusModal" class="modal <?php echo $show_status_modal ? 'show' : ''; ?>">
        <div class="modal-content" style="max-width: 400px;">
            <div style="width: 60px; height: 60px; background: <?php echo $message_type === 'success' ? '#d1fae5' : '#fee2e2'; ?>; color: <?php echo $message_type === 'success' ? '#10b981' : '#ef4444'; ?>; border-radius: 50%; display: flex; align-items: center; justify-content: center; margin: 0 auto 20px; font-size: 1.5rem;">
                <?php echo $message_type === 'success' ? '✓' : '!'; ?>
            </div>
            <h3 style="margin-bottom: 10px;"><?php echo $message_type === 'success' ? 'Success!' : 'Notice'; ?></h3>
            <p style="color: #6b7280; margin-bottom: 24px;"><?php echo $message; ?></p>
            <button class="btn-sm btn-primary" style="width: 100%; padding: 10px;" onclick="closeModal('statusModal')">Done</button>
        </div>
    </div>

    <script>
        function openAddClientModal() {
            document.getElementById('addClientModal').classList.add('show');
        }

        function closeModal(id) {
            document.getElementById(id).classList.remove('show');
        }

        function openGuardModal(id, clientUname) {
            document.getElementById('guard_mapping_id').value = id;
            document.getElementById('guard_client_name').innerText = "Assigning Personnel to Site: " + clientUname;
            document.getElementById('guardModal').classList.add('show');
        }

        function openInspectorModal(id, clientUname) {
            document.getElementById('inspector_mapping_id').value = id;
            document.getElementById('inspector_client_name').innerText = "Assigning Inspector to Site: " + clientUname;
            document.getElementById('inspectorModal').classList.add('show');
        }

        function openSummaryModal(data) {
            const clientName = data.company_name || data.client_username;
            document.getElementById('summary_title').innerText = "Site Summary: " + clientName;
            document.getElementById('summary_qr_count').innerText = data.qr_count || 0;
            
            document.getElementById('summary_contact_person').innerText = data.contact_person || "Not Set";
            document.getElementById('summary_contact_no').innerText = data.contact_person_no || "Not Set";
            document.getElementById('summary_email').innerText = data.email_address || "Not Set";
            document.getElementById('summary_address').innerText = data.company_address || "Not Set";
            
            const listDiv = document.getElementById('summary_guards_list');
            if (data.guard_names && data.guard_names.trim() !== '') {
                const names = data.guard_names.split(' | ');
                document.getElementById('summary_guard_count').innerText = names.length;
                listDiv.innerHTML = names.map(n => `<div style="padding: 6px 0; border-bottom: 1px dotted #e5e7eb; display: flex; align-items: center; gap: 8px;"><span style="color: #10b981;">•</span> ${n}</div>`).join('');
            } else {
                document.getElementById('summary_guard_count').innerText = "0";
                listDiv.innerHTML = '<span style="color: #9ca3af; font-style: italic; font-size: 0.9rem;">No guards assigned to this site.</span>';
            }

            // Set up Edit button
            document.getElementById('summary_edit_btn').onclick = function() {
                closeModal('summaryModal');
                openDetailsModal(data);
            };

            document.getElementById('summaryModal').classList.add('show');
        }

        function openDetailsModal(data) {
            document.getElementById('details_mapping_id').value = data.mapping_id;
            document.getElementById('details_company_name').value = data.company_name || '';
            document.getElementById('details_company_address').value = data.company_address || '';
            document.getElementById('details_contact_no').value = data.contact_no || '';
            document.getElementById('details_email_address').value = data.email_address || '';
            document.getElementById('details_website_link').value = data.website_link || '';
            document.getElementById('details_contact_person').value = data.contact_person || '';
            document.getElementById('details_contact_person_position').value = data.contact_person_position || '';
            document.getElementById('details_contact_person_no').value = data.contact_person_no || '';
            document.getElementById('detailsModal').classList.add('show');
        }

        function toggleSupervisorFields() {
            const chk = document.getElementById('create_supervisor_chk');
            const fields = document.getElementById('supervisor_fields');
            const inputs = fields.querySelectorAll('input');
            
            if (chk.checked) {
                fields.style.display = 'block';
                inputs.forEach(input => {
                    if (input.id !== 'sup_contact') {
                        input.required = true;
                    }
                });
            } else {
                fields.style.display = 'none';
                inputs.forEach(input => {
                    input.required = false;
                });
            }
        }

        window.onclick = function(event) {
            if (event.target.classList.contains('modal')) {
                event.target.classList.remove('show');
            }
        }
    </script>
    <?php include_once 'includes/common_modals.php'; ?>
</body>
</html>
