<?php include 'header.php'; ?>

<!-- Page Wrapper -->
<div class="px-0 py-3 md:p-2 lg:p-6 min-h-screen">

    <!-- Header -->
    <div
        class="flex flex-col md:flex-row md:items-center md:justify-between gap-4 mb-5 bg-white p-4 rounded-lg border border-gray-200">

        <!-- Title -->
        <div>
            <h1 class="text-2xl font-bold text-gray-800">
                Complaint List
            </h1>

            <p class="text-sm text-gray-500">
                Track, manage and monitor all LockOra tickets
            </p>
        </div>

        <!-- Actions -->
        <div class="flex flex-col sm:flex-row gap-3">

            <!-- Search -->
            <div class="relative">

                <input type="text" placeholder="Search here..." id="searchInput"
                    class="w-full sm:w-[220px] pl-10 pr-4 py-2.5 rounded-xl border border-gray-200 focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 outline-none text-sm">

                <i class="ri-search-line absolute left-3 top-1/2 -translate-y-1/2 text-gray-400"></i>

            </div>
            
                        
            <a href="add_lockora_ticket.php"
                class="px-5 py-2.5 rounded-xl bg-gradient-to-r from-blue-500 to-indigo-600 text-white font-medium text-sm flex items-center gap-2 shadow-lg hover:from-blue-600 hover:to-indigo-700 transition-all duration-300">
                <i class="ri-add-line"></i>
                Add Complaint
            </a>


        </div>

    </div>

<!-- Complaint Type Filter Buttons -->
<div class="bg-white rounded-2xl border border-gray-200 p-4 mb-5">
    <div class="flex flex-wrap gap-3">

        <button onclick="setTypeFilter('all')" id="typeBtn-all"
            class="type-btn px-5 h-11 rounded-xl text-sm font-medium border transition-all duration-200 border-slate-200 text-slate-700 bg-slate-100 hover:bg-slate-200 flex items-center gap-2">
            All Types
        </button>

        <button onclick="setTypeFilter('dealer')" id="typeBtn-dealer"
            class="type-btn px-5 h-11 rounded-xl text-sm font-medium border transition-all duration-200 border-blue-200 text-blue-700 bg-blue-50 hover:bg-blue-100 flex items-center gap-2">
            <i class="ri-store-2-line text-lg"></i> Dealer
        </button>

        <button onclick="setTypeFilter('customer')" id="typeBtn-customer"
            class="type-btn px-5 h-11 rounded-xl text-sm font-medium border transition-all duration-200 border-cyan-200 text-cyan-700 bg-cyan-50 hover:bg-cyan-100 flex items-center gap-2">
            <i class="ri-user-line text-lg"></i> Customer
        </button>

        <button onclick="setTypeFilter('sales partner')" id="typeBtn-sales partner"
            class="type-btn px-5 h-11 rounded-xl text-sm font-medium border transition-all duration-200 border-orange-200 text-orange-700 bg-orange-50 hover:bg-orange-100 flex items-center gap-2">
            <i class="ri-handshake-line text-lg"></i> Sales Partner
        </button>

    </div>
</div>

<!-- Status Filter Buttons -->
<div class="bg-white rounded-2xl border border-gray-200 p-4 mb-5">
    <div class="flex flex-wrap gap-3">

        <!-- Pending -->
        <button id="statusBtn-pending" onclick="filterComplaints('pending')"
            class="status-btn flex items-center gap-2 px-5 h-11 rounded-xl bg-red-50 text-red-700 border border-red-200 hover:bg-red-100 transition text-sm font-medium">
            <i class="ri-time-line text-lg"></i>
            Pending <span class="px-2 py-0.5 text-xs rounded-full bg-red-600 text-white">2</span>
        </button>

        <!-- Completed -->
        <button id="statusBtn-completed" onclick="filterComplaints('completed')"
            class="status-btn flex items-center gap-2 px-5 h-11 rounded-xl bg-green-50 text-green-700 border border-green-200 hover:bg-green-100 transition text-sm font-medium">
            <i class="ri-checkbox-circle-line text-lg"></i>
            Completed <span class="px-2 py-0.5 text-xs rounded-full bg-green-600 text-white">1</span>
        </button>
        
        
        <!-- All -->
        <button id="statusBtn-all" onclick="filterComplaints('all')"
            class="status-btn flex items-center gap-2 px-5 h-11 rounded-xl bg-slate-100 text-slate-700 border border-slate-200 hover:bg-slate-200 transition text-sm font-medium">
            <i class="ri-file-list-3-line text-lg"></i>
            All Complaints <span class="px-2 py-0.5 text-xs rounded-full bg-blue-600 text-white">3</span>
        </button>

    </div>
</div>



    <!-- Card -->
    <div class="bg-white rounded-lg border border-gray-200 shadow-sm overflow-x-auto overflow-y-hidden">

        <!-- Table -->
        <div class="overflow-x-auto">

            <table class="w-full min-w-[1200px]">

                <!-- Table Head -->
                <thead class="bg-gray-50 border-b border-gray-200">

                    <tr class="text-left">

                        <th
                            class="px-6 py-4 text-xs font-semibold text-gray-500 uppercase tracking-wider whitespace-nowrap">
                            Sr.No
                        </th>

                        <th
                            class="px-6 py-4 text-xs font-semibold text-gray-500 uppercase tracking-wider whitespace-nowrap">
                            Complaint ID
                        </th>

                        <th
                            class="px-6 py-4 text-xs font-semibold text-gray-500 uppercase tracking-wider whitespace-nowrap">
                            Customer Name
                        </th>


                        <th
                            class="px-6 py-4 text-xs font-semibold text-gray-500 uppercase tracking-wider whitespace-nowrap">
                            Product Model
                        </th>

                        <th
                            class="px-6 py-4 text-xs font-semibold text-gray-500 uppercase tracking-wider whitespace-nowrap">
                            Customer Type
                        </th>

                        <th
                            class="px-6 py-4 text-xs font-semibold text-gray-500 uppercase tracking-wider whitespace-nowrap">
                            Complaint
                        </th>

                        <th
                            class="px-6 py-4 text-xs font-semibold text-gray-500 uppercase tracking-wider whitespace-nowrap">
                            Date
                        </th>

                        <th
                            class="px-6 py-4 text-xs font-semibold text-gray-500 uppercase tracking-wider whitespace-nowrap">
                            Status
                        </th>

                        <th
                            class="px-6 py-4 text-xs font-semibold text-gray-500 uppercase tracking-wider whitespace-nowrap">
                            Action
                        </th>

                    </tr>

                </thead>

                <tbody class="divide-y divide-gray-100 bg-white">

    <!-- Pending Complaint -->
    <tr class="hover:bg-gray-50 transition" data-status="pending">
        <td class="px-6 py-4 font-semibold text-gray-700 whitespace-nowrap">1</td>
        <td class="px-6 py-4 font-semibold text-black-600 whitespace-nowrap">CMP-1000</td>
        <!-- Customer Name -->
<td class="px-6 py-4 whitespace-nowrap">
    <div class="text-sm font-medium text-gray-800">
        Dev Mistry
    </div>
    <div class="text-xs text-gray-500 mt-1">
       9876543210
    </div>
</td>
        <td class="px-6 py-4">
            <div class="text-sm font-semibold text-indigo-600">LockOra L1 Core</div>
            <div class="text-xs font-semibold text-sky-700 mt-1">Serial No: LO-001</div>
        </td>
        <td class="px-6 py-4 whitespace-nowrap">
            <span class="px-3 py-1 rounded-full bg-blue-100 text-blue-700 text-xs font-medium">Dealer Customer</span>
        </td>
        <td class="px-6 py-4 whitespace-nowrap">
            <span class="px-3 py-1 rounded-full text-xs font-medium bg-orange-100 text-orange-700">
                Fingerprint Issue
            </span>
        </td>
        <td class="px-6 py-4 whitespace-nowrap">07-06-2026</td>
        <td class="px-6 py-4 whitespace-nowrap">
            <span class="px-3 py-1 rounded-full bg-red-100 text-red-700 text-xs font-medium">Pending</span>
        </td>
        <td class="px-6 py-4 whitespace-nowrap">
            <div class="flex items-center gap-2">
                <button onclick="markComplete(this)" title="Mark Complete"
                    class="w-9 h-9 rounded-lg border border-gray-200 text-green-600 hover:bg-green-50 transition">
                    <i class="ri-checkbox-circle-line"></i>
                </button>
                <a href="view_lockora_ticket.php">
                <button class="w-9 h-9 rounded-lg border border-gray-200 text-blue-600 hover:bg-blue-50 transition">
                    <i class="ri-eye-line"></i>
                </button>
                </a>
                <button class="w-9 h-9 rounded-lg border border-gray-200 text-amber-600 hover:bg-amber-50 transition">
                    <i class="ri-edit-line"></i>
                </button>
                <button onclick="deleteRow(this)" class="w-9 h-9 rounded-lg border border-gray-200 text-red-600 hover:bg-red-50 transition">
                    <i class="ri-delete-bin-line"></i>
                </button>
            </div>
        </td>
    </tr>

    <!-- Completed Complaint -->
    <tr class="hover:bg-gray-50 transition" data-status="completed">
        <td class="px-6 py-4 font-semibold text-gray-700 whitespace-nowrap">2</td>
        <td class="px-6 py-4 font-semibold text-black-600 whitespace-nowrap">CMP-1001</td>
        <!-- Customer Name -->
<td class="px-6 py-4 whitespace-nowrap">
    <div class="text-sm font-medium text-gray-800">
        Priya Patel
    </div>
    <div class="text-xs text-gray-500 mt-1">
       9876543210
    </div>
</td>
        <td class="px-6 py-4">
            <div class="text-sm font-semibold text-indigo-600">LockOra L2+</div>
            <div class="text-xs font-semibold text-sky-700 mt-1">Serial No: LO-003</div>
        </td>
        <td class="px-6 py-4 whitespace-nowrap">
            <span class="px-3 py-1 rounded-full bg-cyan-100 text-cyan-700 text-xs font-medium">Website Customer</span>
        </td>
        <td class="px-6 py-4 whitespace-nowrap">
            <span class="px-3 py-1 rounded-full text-xs font-medium bg-purple-100 text-purple-700">
                App Connectivity
            </span>
        </td>        
        <td class="px-6 py-4 whitespace-nowrap">09-06-2026</td>
        <td class="px-6 py-4 whitespace-nowrap">
            <span class="px-3 py-1 rounded-full bg-green-100 text-green-700 text-xs font-medium">Completed</span>
        </td>
        <td class="px-6 py-4 whitespace-nowrap">
            <div class="flex items-center gap-2">
                <a href="view_lockora_ticket.php">
                <button class="w-9 h-9 rounded-lg border border-gray-200 text-blue-600 hover:bg-blue-50 transition">
                    <i class="ri-eye-line"></i>
                </button>
                </a>
                <button class="w-9 h-9 rounded-lg border border-gray-200 text-amber-600 hover:bg-amber-50 transition">
                    <i class="ri-edit-line"></i>
                </button>
                <button onclick="deleteRow(this)" class="w-9 h-9 rounded-lg border border-gray-200 text-red-600 hover:bg-red-50 transition">
                    <i class="ri-delete-bin-line"></i>
                </button>
            </div>
        </td>
    </tr>

    <!-- Pending Complaint -->
    <tr class="hover:bg-gray-50 transition" data-status="pending">
        <td class="px-6 py-4 font-semibold text-gray-700 whitespace-nowrap">3</td>
        <td class="px-6 py-4 font-semibold text-black-600 whitespace-nowrap">CMP-1002</td>
        <td class="px-6 py-4 whitespace-nowrap">
            <div class="text-sm font-medium text-gray-800">
               Amit Singh
            </div>
            <div class="text-xs text-gray-500 mt-1">
               9876543210
            </div>
        </td>
        <td class="px-6 py-4">
            <div class="text-sm font-semibold text-indigo-600">LockOra L4 Ultra</div>
            <div class="text-xs font-semibold text-sky-700 mt-1">Serial No: LO-004</div>
        </td>
        <td class="px-6 py-4 whitespace-nowrap">
            <span class="px-3 py-1 rounded-full bg-orange-100 text-orange-700 text-xs font-medium">Sales Partner Customer</span>
        </td>
        <td class="px-6 py-4 whitespace-nowrap">
            <span class="px-3 py-1 rounded-full text-xs font-medium bg-red-100 text-red-700">
                Lock Jam
            </span>
        </td>
        <td class="px-6 py-4 whitespace-nowrap">11-06-2026</td>
        <td class="px-6 py-4 whitespace-nowrap">
            <span class="px-3 py-1 rounded-full bg-red-100 text-red-700 text-xs font-medium">Pending</span>
        </td>
        <td class="px-6 py-4 whitespace-nowrap">
            <div class="flex items-center gap-2">
                <button onclick="markComplete(this)" title="Mark Complete"
                    class="w-9 h-9 rounded-lg border border-gray-200 text-green-600 hover:bg-green-50 transition">
                    <i class="ri-checkbox-circle-line"></i>
                </button>
                <a href="view_lockora_ticket.php">
                <button class="w-9 h-9 rounded-lg border border-gray-200 text-blue-600 hover:bg-blue-50 transition">
                    <i class="ri-eye-line"></i>
                </button>
                </a>
                <button class="w-9 h-9 rounded-lg border border-gray-200 text-amber-600 hover:bg-amber-50 transition">
                    <i class="ri-edit-line"></i>
                </button>
                <button onclick="deleteRow(this)" class="w-9 h-9 rounded-lg border border-gray-200 text-red-600 hover:bg-red-50 transition">
                    <i class="ri-delete-bin-line"></i>
                </button>
            </div>
        </td>
    </tr>

    <!-- Empty State -->
    <tr id="emptyStateRow" style="display:none;">
        <td colspan="9" class="px-6 py-16 text-center">
            <div class="flex flex-col items-center gap-3">
                <div class="w-16 h-16 rounded-2xl bg-gray-100 flex items-center justify-center">
                    <i class="ri-inbox-2-line text-3xl text-gray-400"></i>
                </div>
                <p class="text-base font-semibold text-gray-700">No Complaints Found</p>
                <p id="emptyStateMsg" class="text-sm text-gray-400">No complaints match the selected filters.</p>
            </div>
        </td>
    </tr>

</tbody>

            </table>

        </div>

        <!-- Footer -->
        <div class="px-4 py-4 border-t border-gray-100 flex flex-col md:flex-row items-center justify-between gap-4">

            <!-- Left -->
            <p class="text-sm text-gray-500">
                Showing 1 to 3 of 3 complaints
            </p>

            <!-- Pagination -->
            <div class="flex items-center gap-2">

                <button class="w-9 h-9 rounded-lg border border-gray-200 text-gray-500 hover:bg-gray-100 transition">
                    <i class="ri-arrow-left-s-line"></i>
                </button>

                <button
                    class="w-9 h-9 rounded-xl bg-gradient-to-r from-blue-500 to-indigo-600 text-white font-medium shadow-md">
                    1
                </button>

                <button class="w-9 h-9 rounded-lg border border-gray-200 text-gray-500 hover:bg-gray-100 transition">
                    <i class="ri-arrow-right-s-line"></i>
                </button>

            </div>

        </div>

    </div>

</div>


<script>
    function deleteRow(button) {

        Swal.fire({

            title: "Delete Record?",
            text: "This row will be deleted.",
            icon: "warning",

            width: "320px",
            padding: "1rem",

            showCancelButton: true,
            scrollbarPadding: false,
            confirmButtonText: "Delete",
            cancelButtonText: "Cancel",

            confirmButtonColor: "#2563eb",
            cancelButtonColor: "#ef4444",

            customClass: {

                popup: "rounded-2xl",
                title: "text-lg font-semibold",
                htmlContainer: "text-sm text-gray-500",
                confirmButton: "px-4 py-2 rounded-lg text-sm",
                cancelButton: "px-4 py-2 rounded-lg text-sm"

            }

        }).then((result) => {

            if (result.isConfirmed) {

                button.closest("tr").remove();

                Swal.fire({

                    icon: "success",
                    title: "Deleted!",
                    text: "Record removed successfully.",

                    width: "300px",
                    padding: "1rem",

                    showConfirmButton: false,
                    timer: 1400

                });

            }

        });

    }

function markComplete(button) {

    Swal.fire({

        title: "Mark as Completed?",
        text: "This complaint will be marked as completed.",
        icon: "question",

        width: "320px",
        padding: "1rem",

        showCancelButton: true,
        scrollbarPadding: false,

        confirmButtonText: "Mark Complete",
        cancelButtonText: "Cancel",

        confirmButtonColor: "#16a34a",
        cancelButtonColor: "#6b7280",

        customClass: {

            popup: "rounded-2xl",
            title: "text-lg font-semibold",
            htmlContainer: "text-sm text-gray-500",
            confirmButton: "px-4 py-2 rounded-lg text-sm",
            cancelButton: "px-4 py-2 rounded-lg text-sm"

        }

    }).then((result) => {

        if (result.isConfirmed) {

            const row = button.closest("tr");

            // Update row status
            row.dataset.status = "completed";

            // Update status badge
            const statusCell = row.querySelector("td:nth-child(9) span");

            if (statusCell) {

                statusCell.textContent = "Completed";

                statusCell.classList.remove(
                    "bg-red-100",
                    "text-red-700",
                    "bg-yellow-100",
                    "text-yellow-700"
                );

                statusCell.classList.add(
                    "bg-green-100",
                    "text-green-700"
                );
            }

            // Remove Mark Complete button
            button.remove();

            Swal.fire({

                title: "Completed!",
                text: "Complaint marked as completed successfully.",
                icon: "success",

                width: "320px",
                padding: "1rem",

                showConfirmButton: false,
                timer: 1400,

                customClass: {

                    popup: "rounded-2xl",
                    title: "text-lg font-semibold",
                    htmlContainer: "text-sm text-gray-500"

                }

            });

            // Re-apply filters
            if (typeof applyFilters === "function") {
                applyFilters();
            }
        }

    });

}
</script>

<script>
    let activeStatus = "all";
    let activeType   = "all";

    function applyFilters() {
        const rows = document.querySelectorAll("tbody tr:not(#emptyStateRow)");
        const emptyRow = document.getElementById("emptyStateRow");
        const emptyMsg = document.getElementById("emptyStateMsg");

        let visibleCount = 0;

        rows.forEach(row => {
            const rowStatus = row.dataset.status || "";
            const typeCell  = row.querySelector("td:nth-child(6) span");
            const rowType   = typeCell ? typeCell.textContent.trim().toLowerCase() : "";

            const statusMatch = activeStatus === "all" || rowStatus === activeStatus;
            const typeMatch   = activeType   === "all" || rowType   === activeType;

            const visible = statusMatch && typeMatch;
            row.style.display = visible ? "" : "none";
            if (visible) visibleCount++;
        });

        // Build empty state message
        const typeLabel = activeType === "all" ? "" :
            activeType.charAt(0).toUpperCase() + activeType.slice(1);

        const statusLabel = activeStatus === "all" ? "" :
            activeStatus.charAt(0).toUpperCase() + activeStatus.slice(1);

        if (visibleCount === 0) {
            let msg = "No";
            if (statusLabel) msg += ` ${statusLabel}`;
            msg += " complaints";
            if (typeLabel) msg += ` from ${typeLabel}`;
            msg += " found.";
            emptyMsg.textContent = msg;
            emptyRow.style.display = "";
        } else {
            emptyRow.style.display = "none";
        }
    }

    function filterComplaints(status) {
        activeStatus = status;

        // Update status button active highlight
        document.querySelectorAll(".status-btn").forEach(btn => {
            btn.classList.remove("ring-2", "ring-offset-1", "ring-indigo-400");
        });
        const activeBtn = document.getElementById("statusBtn-" + status);
        if (activeBtn) activeBtn.classList.add("ring-2", "ring-offset-1", "ring-indigo-400");

        applyFilters();
    }

    function setTypeFilter(type) {
        activeType = type;

        // Update button styles
        document.querySelectorAll(".type-btn").forEach(btn => {
            btn.classList.remove(
                "bg-gradient-to-r", "from-blue-500", "to-indigo-600",
                "from-cyan-500", "to-sky-600",
                "from-orange-400", "to-orange-500",
                "text-white", "border-transparent", "shadow-md"
            );
            btn.classList.add("bg-white");
        });

        const activeColors = {
            "all":          ["from-blue-500",   "to-indigo-600",  "text-white", "border-transparent"],
            "dealer":       ["from-blue-500",   "to-indigo-600",  "text-white", "border-transparent"],
            "customer":     ["from-cyan-500",   "to-sky-600",     "text-white", "border-transparent"],
            "sales partner":["from-orange-400", "to-orange-500",  "text-white", "border-transparent"],
        };

        const btn = document.getElementById("typeBtn-" + type);
        if (btn) {
            btn.classList.remove("bg-white");
            btn.classList.add("bg-gradient-to-r", "shadow-md", ...(activeColors[type] || []));
        }

        applyFilters();
    }

// Init
filterComplaints("pending");
setTypeFilter("all");
</script>

<?php include 'footer.php'; ?>