@extends('layouts.admin')
@section('content')

 
<!-- start page title -->
<div class="row">
    <div class="col-12">
        <div class="page-title-box d-sm-flex align-items-center justify-content-between bg-galaxy-transparent">
            <h4 class="mb-sm-0">Male Registration Report</h4>

            <div class="page-title-right">
                <ol class="breadcrumb m-0">
                    <li class="breadcrumb-item"><a href="javascript: void(0);">Reports</a></li>
                    <li class="breadcrumb-item active">Male Registration Report</li>
                </ol>
            </div>
        </div>
    </div>
</div>
<!-- end page title -->

<div class="row">
    <div class="col-lg-12">
        <div class="card" id="orderList">
            <div class="card-header border-0">
                <div class="row align-items-center gy-3">
                    <div class="col-sm">
                        <h5 class="card-title mb-0">Male Registration Report</h5>
                    </div>
                    <div class="col-sm-auto">
                        <div class="d-flex gap-1 flex-wrap">
                            <button type="button" class="btn btn-info" id="export-excel"><i class="ri-file-download-line align-bottom me-1"></i> Export to Excel</button>
                            <button type="button" class="btn btn-info d-none" id="export-pdf"><i class="ri-file-download-line align-bottom me-1"></i> Export to PDF</button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="card-body border border-dashed border-end-0 border-start-0">
                <form method="GET" action="{{ route('admin.reports.customeregistration.list') }}" id="filterForm">
                    <div class="row g-3 align-items-end">
                        <input type="hidden" name="gender" value="Male" />
                        <div class="col-lg-4 mb-3">
                            <label for="idNationality">Nationality</label>
                            <select class="form-select" data-choices data-choices-search-false name="nationality_id" id="idNationality">
                                <option value="all" selected>All nationalities</option>
                                @foreach($nationalities as $nationality)
                                    <option value="{{ $nationality->id }}">{{ $nationality->name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-lg-4 mb-3">
                            <label for="idEmirate">Emirates</label>
                            <select class="form-select" data-choices data-choices-search-false name="emirates_id" id="idEmirate">
                                <option value="all" selected>All Emirates</option>
                                @foreach($emirates as $emirate)
                                    <option value="{{ $emirate->id }}">{{ $emirate->name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-lg-4 mb-3">
                            <label for="idSect">Sect</label>
                            <select class="form-select" data-choices data-choices-search-false name="sect_id" id="idSect">
                                <option value="all" selected>All sects</option>
                                @foreach($sects as $sect)
                                    <option value="{{ $sect->id }}">{{ $sect->name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-lg-4 mb-3">
                            <label for="idPlan">Subscription Plan</label>
                            <select class="form-select" data-choices data-choices-search-false name="plan_id" id="idPlan">
                                <option value="all" selected>All plans</option>
                                @foreach($plans as $plan)
                                    <option value="{{ $plan->id }}">{{ $plan->name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-lg-4 mb-3">
                            <label for="idStatus">Status</label>
                            <select class="form-select" data-choices data-choices-search-false name="status" id="idStatus">
                                <option value="all" selected>All status</option>
                                <option value="1">Active</option>
                                <option value="0">Inactive</option>
                            </select>
                        </div>
                        <div class="col-lg-4 mb-3">
                            <label for="start_date">Registration Starts From</label>
                            <input type="date" name="start_date" id="start_date" class="form-control" max="{{ $today }}" placeholder="Start Date From">

                        </div>
                        <div class="col-lg-4 mb-3">

                            <label for="end_date">Registration End Date</label>
                            <input type="date" id="end_date" name="end_date" class="form-control" placeholder="End Date" max="{{ $today }}">

                        </div>
                        
                        <div class="col-lg-4 mb-3">
                            <button type="submit" class="btn btn-primary">Filter</button>
                            <button type="submit" class="btn btn-soft-dark" id="clearButton">Clear</button>
                        </div>
                    </div>
                    <!--end row-->
                </form>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table id="UsersTable" class="table table-bordered nowrap align-middle" style="width: 100%;"  data-src="{{route('admin.getUserList')}}">
                        <thead class="">
                            <tr class="text-uppercase">
                                <th>Sl No</th>
                                <th>Profile Image</th>
                                <th>User ID</th>
                                <th>Name</th>
                                <th>Mobile Number</th>
                                <th>Nationality</th>
                                <th>Sect</th>
                                <th>Subscription Plan</th>
                                <th>Status</th>
                                <th>Created Date</th>
                                <th>Actions</th>
                            </tr>
                        </thead> 
                        <tbody class="list form-check-all">
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <!--end col-->
</div>
<!--end row-->

<div id="removeuser" class="modal fade zoomIn" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" id="NotificationModalbtn-close"></button>
            </div>
            <div class="modal-body">
                <div class="mt-2 text-center">
                    <lord-icon src="https://cdn.lordicon.com/gsqxdxog.json" trigger="loop" colors="primary:#f7b84b,secondary:#f06548" style="width: 100px; height: 100px;"></lord-icon>
                    <div class="mt-4 pt-2 fs-15 mx-4 mx-sm-5">
                        <h4>Are you sure ?</h4>
                        <p class="text-muted mx-4 mb-0">Are you sure you want to remove this plan.</p>
                    </div>
                </div>
                <div class="d-flex gap-2 justify-content-center mt-4 mb-2">
                    <button type="button" class="btn w-sm btn-light" data-bs-dismiss="modal">Close</button>
                    <button type="button" class="btn w-sm btn-danger" id="delete-notification">Yes, Delete It!</button>
                </div>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
<!-- /.modal -->
@endsection
@section('script')
<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/additional-methods.min.js"></script>
   <script>
    $(document).ready(function() {
        var table = $('#UsersTable').DataTable({
            responsive: true, 
            paging: true,     
            searching: true,  
            ordering: true,   
            lengthChange: true, 
            serverSide: true,
            ajax: {
                url: "{{ route('admin.reports.customeregistration.list') }}",
                type:'GET',
                data: function(d) {
                    // Add the external search value to the request
                    d.status        = $('#idStatus').val();
                    d.gender        = 'Male';
                    d.nationality_id = $('#idNationality').val();
                    d.emirates_id = $('#idEmirate').val();
                    d.sect_id       = $('#idSect').val();
                    d.plan_id       = $('#idPlan').val();
                    d.start_date    = $("#start_date").val();
                    d.end_date      = $("#end_date").val();
                },
            },
            columns: [
                { data: 'id', name: 'id' },
                { data: 'user_image', name: 'user_image' },
                { data: 'user_number', name: 'user_number' },
                { data: 'name', name: 'name' },
                { data: 'phone', name: 'phone' },
                { data: 'nationality_name', name: 'nationality_name' },
                { data: 'sect_name', name: 'sect_name' },
                { data: 'subscription_plan', name: 'subscription_plan' },
                { 
                    data: 'status', 
                    name: 'status',
                    render: function(data, type, row) {
                        return data;
                    }
                },
                { data: 'created_at', name: 'created_at' },
                { data: 'action', name: 'action' }
            ],
            columnDefs: [
                {
                    orderable: false,
                    targets: [0, 6]
                },
                {
                    "targets": 0, // Target the first column (index 0)
                    "render": function(data, type, row, meta) {
                        // Calculate the starting index of the current page
                        const pageInfo = $('#UsersTable').DataTable().page.info();
                        const startIndex = pageInfo.start;
                        // Calculate the serial number
                        return startIndex + meta.row + 1;
                    }
                }
            ],
        });

        $('#filterForm').on('submit', function(e) {
            e.preventDefault(); 
            table.ajax.reload();
        });

        $('#clearButton').on('click', function(e) {
            e.preventDefault(); 
            $('#filterForm')[0].reset(); 
            window.location.href = window.location.pathname; 
        });

        $('#idNationality').change(function () {
            table.ajax.reload();
        });

        $('#idEmirate').change(function () {
            table.ajax.reload();
        });

        $('#idSect').change(function () {
            table.ajax.reload();
        });

        $('#idPlan').change(function () {
            table.ajax.reload();
        });

        $('#idGender').change(function () {
            table.ajax.reload();
        });
        
        $('#idStatus').change(function () {
            table.ajax.reload();
        });

        $('#start_date').change(function() {
            var startDate = $(this).val();
            var endDate = $('#end_date').val();
            if (startDate) {
            $('#end_date').attr('min', startDate);
            if (endDate && new Date(endDate) < new Date(startDate)) {
                $('#end_date').val('');
            }
            } else {
            $('#end_date').removeAttr('min');
            $('#end_date').attr('max', '{{ $today }}');
            }
            table.ajax.reload();
        });

        $('#end_date').change(function() {
            var endDate = $(this).val();
            var startDate = $('#start_date').val();
            if (endDate) {
            $('#start_date').attr('max', endDate);
            if (startDate && new Date(startDate) > new Date(endDate)) {
                $('#start_date').val('');
            }
            } else {
            $('#start_date').removeAttr('max');
            $('#start_date').attr('max', '{{ $today }}');
            }
            table.ajax.reload();
        });
    });

    </script>

<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/additional-methods.min.js"></script>
  
<script>
    $(document).ready(function () {

        $('#export-excel').click(function () {
            var serializedData = $('#filterForm').serialize();
            console.log(serializedData);
            var newValue = $('.dt-container input[type="search"]').val();
            var updatedData = serializedData + '&' + encodeURIComponent('search') + '=' + encodeURIComponent(newValue);
            window.location.href = '{{ route('admin.reports.customeregistration.exportExcel') }}?' + updatedData;
        });

        $('#export-pdf').click(function () {
            var serializedData = $('#filterForm').serialize();
            var newValue = $('.dt-container input[type="search"]').val();
            var updatedData = serializedData + '&' + encodeURIComponent('search') + '=' + encodeURIComponent(newValue);
            window.location.href = '{{ route('admin.reports.customeregistration.exportPdf') }}?' + updatedData;
        });

    });
</script>

@endsection
