@extends('layouts.master')
@section('content')
    <style>
        select option {
            background-color: #007bff !important;
            color: white !important;
        }


        table {
            transform: scale(1);
            transform-origin: top left;
        }


        table.dataTable thead .sorting,
        table.dataTable thead .sorting_asc,
        table.dataTable thead .sorting_desc,
        table.dataTable thead .sorting_asc_disabled,
        table.dataTable thead .sorting_desc_disabled {
            background-image: url('https://cdn.datatables.net/1.10.24/images/sort_both.png');
            background-repeat: no-repeat;
            background-position: center right;
            cursor: pointer;
        }

        table.dataTable thead .sorting_asc {
            background-image: url('https://cdn.datatables.net/1.10.24/images/sort_asc.png');
        }

        table.dataTable thead .sorting_desc {
            background-image: url('https://cdn.datatables.net/1.10.24/images/sort_desc.png');
        }

        /* Remove hover-only display */
        table.dataTable thead>tr>th.sorting:before,
        table.dataTable thead>tr>th.sorting:after,
        table.dataTable thead>tr>th.sorting_asc:before,
        table.dataTable thead>tr>th.sorting_asc:after,
        table.dataTable thead>tr>th.sorting_desc:before,
        table.dataTable thead>tr>th.sorting_desc:after,
        table.dataTable thead>tr>th.sorting_asc_disabled:before,
        table.dataTable thead>tr>th.sorting_asc_disabled:after,
        table.dataTable thead>tr>th.sorting_desc_disabled:before,
        table.dataTable thead>tr>th.sorting_desc_disabled:after {
            opacity: 1 !important;
        }
    </style>

    <div class="container-xxl flex-grow-1 container-p-y">

        @if (isset($region))
            <div class="row" style="display: flow-root">
                <h5 class="card-title">Camera List</h5>
                <a href="{{ route('streamWithCamera.add', $region) }}"
                    style="width: fit-content;float: right;margin-bottom: 12px;margin-right:17px;"
                    class="btn btn-primary btn-xs">
                    <i class="menu-icon tf-icons bx bx-message-square-add"></i>New Camera
                </a>
            </div>
        @endif
        <div class="card" style="display: grid;height: 100%;">
            <div class="card-header">
                <h4 class="card-title">Attendance System
                </h4>
            </div>


            <div class="row container d-flex">
                <div class="col-md-2 d-flex align-items-center">
                    <h3 class="">Filters:</h3>
                </div>
                <div class="col-md-3 d-flex align-items-center">
                    <input type="text" class="form-control" id="searchCamera" placeholder="Search Camera">
                    {{-- <select class="form-control" id="searchCamera">
                        <option value="" selected>Select Camera</option>

                        @foreach ($cameras as $index => $list)
                            <option value="{{ $list['cameraname'] }}">{{ $list['cameraname'] }}</option>
                        @endforeach

                    </select> --}}
                </div>
                <div class="col-md-3 d-flex align-items-center">
                    <input type="text" class="form-control" id="searchRegion" placeholder="Search Region">
                    {{-- <select class="form-control" id="searchRegion">
                        <option value="" selected>Select Region</option>

                        @foreach ($cameras as $index => $list)
                            <option value="{{ $list['region']['region'] }}">{{ $list['region']['region'] }}</option>
                        @endforeach

                    </select> --}}
                </div>
                <div class="col-md-3 d-flex align-items-center">
                    <input type="text" class="form-control" id="searchUrl" placeholder="Search Stream URL">
                </div>
            </div>

            <div class="table-responsive container px-4">
                <table class="display table " id="myDataTable" style="height: 100%;">
                    <thead>
                        <tr class="text-nowrap">
                            <th>#</th>
                            <th>Camera Name</th>
                            <th>Region Name</th>
                            <th>Streaming URl</th>
                            <th>Add Attendee</th>
                            <th>Date Time</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        @if ($cameras)
                            @foreach ($cameras as $index => $list)
                                <tr>
                                    <th scope="row" style="vertical-align: middle;">{{ ++$index }}</th>
                                    <td>{{ $list['cameraname'] }}</td>
                                    <td>{{ $list['region']['region'] }}</td>
                                    <td>

                                        <span style="display: none">
                                            {{ $list['stream_url'] }}
                                        </span>

                                        <a href="javascript:void()" tabindex="0" class="" role="button"
                                            data-bs-toggle="popover" data-bs-trigger="focus" data-bs-title="Stream URL"
                                            data-bs-content=" {{ $list['stream_url'] }}">View URL</a>
                                    </td>
                                    <td>
                                        {{ \Carbon\Carbon::createFromTimestampMs($list['created_at'])->format('Y-m-d H:i:s') }}
                                    </td>
                                    <td>
                                        <a class="btn btn-secondary btn-sm"
                                            onclick="event.preventDefault(); window.location.href = this.href;"
                                            href="{{ route('faces.add', $list['_id']) }}">
                                            Add Attendee
                                        </a>
                                    </td>
                                    <td>

                                        <?php $camera = $list['_id']; ?>
                                        <div class="dropdown">
                                            <a role="button" data-bs-toggle="dropdown" aria-expanded="false">
                                                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20"
                                                    fill="#ffffff" viewBox="0 0 256 256">
                                                    <path
                                                        d="M156,128a28,28,0,1,1-28-28A28,28,0,0,1,156,128ZM128,76a28,28,0,1,0-28-28A28,28,0,0,0,128,76Zm0,104a28,28,0,1,0,28,28A28,28,0,0,0,128,180Z">
                                                    </path>
                                                </svg>
                                            </a>
                                            <ul class="dropdown-menu">


                                                {{-- <li>
                                                    <a class="dropdown-item"
                                                        onclick="event.preventDefault(); window.location.href = this.href;"
                                                        href="{{ route('faces.add', $camera) }}">
                                                        Add
                                                        attendee (for attendance system)
                                                    </a>
                                                </li> --}}
                                                <li>
                                                    <a class="dropdown-item"
                                                        onclick="event.preventDefault(); window.location.href = this.href;"
                                                        href="{{ route('faces.all', $camera) }}">
                                                        View Attendees
                                                    </a>
                                                </li>
                                                {{-- <li>
                                                    <a class="dropdown-item"
                                                        onclick="event.preventDefault(); window.location.href = this.href;"
                                                        href="{{ route('attendance.setup', $list['_id']) }}">
                                                        Attendance Setup
                                                    </a>
                                                </li> --}}
                                                <li>
                                                    <a class="dropdown-item"
                                                        onclick="event.preventDefault(); window.location.href = this.href;"
                                                        href="{{ route('attendance.report', $camera) }}">
                                                        Attendance Report
                                                    </a>
                                                </li>

                                                <li>
                                                    <a class="dropdown-item"
                                                        onclick="event.preventDefault(); window.location.href = this.href;"
                                                        href="{{ route('monitor.attendance', $camera) }}">
                                                        Real Time Attendee Monitoring
                                                    </a>
                                                </li>



                                            </ul>
                                        </div>









                                    </td>
                                </tr>
                            @endforeach
                        @endif
                    </tbody>
                </table>
            </div>
        </div>
        <div class="row" style=" text-align: center; display: block; margin-top: 100px; margin-bottom: 65px; ">
            {{-- <a href="" class="btn btn-secondary" style="width: 150px;margin-right: 50px;">Back</a> --}}
            {{-- <a href="" class="btn btn-primary" style="width: 150px;">Next</a> --}}
        </div>


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


    <script>
        function deleteCamera(id) {
            Swal.fire({
                title: "Are you sure?",
                text: "You won't be able to revert this!",
                icon: "warning",
                showCancelButton: true,
                confirmButtonColor: "#3085d6",
                cancelButtonColor: "#d33",
                confirmButtonText: "Yes, delete it!"
            }).then((result) => {
                if (result.isConfirmed) {
                    window.location.href = "{{ url('delete/camera/') }}/" + id;
                }
            });
        }




        // ~ datatable ~

        $(document).ready(function() {
            $('#myDataTable').DataTable({
                searching: true,
                ordering: true,
                info: false,
                lengthChange: false, // This line hides the entries dropdown
                pageLength: 10, // Optional: Set default number of entries per page
                "order": [], // Disable initial sorting
                "columnDefs": [{
                        "orderable": true,
                        "targets": "_all"
                    }, // Make all columns orderable
                    {
                        "type": "string",
                        "targets": "_all"
                    } // Treat all column data as strings for sorting
                ],
                language: {
                    paginate: {
                        previous: "<i class='fas fa-angle-left'></i>", // Icon for previous button
                        next: "<i class='fas fa-angle-right'></i>" // Icon for next button
                    }
                }
            });


            $("#searchCamera").on('keyup', function() {
                $('#myDataTable').DataTable().column(1).search(
                    $(this).val(),
                    false, // regex
                    true // smart search
                ).draw();
            });

            $("#searchRegion").on('keyup', function() {
                $('#myDataTable').DataTable().column(2).search(
                    $(this).val(),
                    false, // regex
                    true // smart search
                ).draw();
            });

            $("#searchUrl").on('keyup', function() {
                $('#myDataTable').DataTable().column(3).search(
                    $(this).val(),
                    false, // regex
                    true // smart search
                ).draw();
            });
        });




        document.querySelectorAll('.dropdown-submenu').forEach(function(submenu) {
            // Toggle the submenu when clicking on the parent item
            submenu.addEventListener('click', function(e) {
                e.preventDefault(); // Prevent default link behavior
                e.stopPropagation(); // Prevent the click from bubbling up

                let submenuList = submenu.querySelector('ul');

                // Close all other open submenus
                document.querySelectorAll('.dropdown-submenu ul').forEach(function(menu) {
                    if (menu !== submenuList) {
                        menu.style.display = 'none';
                    }
                });

                // Toggle the clicked submenu
                if (submenuList.style.display === 'block') {
                    submenuList.style.display = 'none';
                } else {
                    submenuList.style.display = 'block';
                }
            });

            // Close submenu when clicking outside
            document.addEventListener('click', function(e) {
                if (!submenu.contains(e.target)) {
                    let submenuList = submenu.querySelector('ul');
                    submenuList.style.display = 'none';
                }
            });
        });



    </script>


@endSection
