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

<!-- start page title -->
<!-- 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">User Statistics</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">User Statistics</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">User Statistics</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" 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">
                        <div class="col-lg-4 mb-3">
                            <label for="idGender">Gender</label>
                            <select class="form-select" data-choices data-choices-search-false name="gender" id="idGender">
                                <option value="all" selected>All gender</option>
                                <option value="Male">Male</option>
                                <option value="Female">Female</option>
                                <option value="Prefer Not to say">Prefer Not to say</option>
                            </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">
                            <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="UserReportTable" class="table table-bordered nowrap align-middle" style="width: 100%;">
                        <thead class="">
                            <tr class="text-uppercase">
                                <th>Sl No</th>
                                <th>Customer Name</th>
                                <th>Total Ads</th>
                                <th>Featured Ads</th>
                            </tr>
                        </thead>
                        <tbody class="list form-check-all">
                            
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <!--end col-->
</div>
<!--end row-->
<!--end row-->
@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>
    var table = $('#UserReportTable').DataTable({
            responsive: true, 
            paging: true,     
            searching: true,  
            ordering: true,   
            lengthChange: true, 
            serverSide: true,
            ajax: {
                url: "{{ route('admin.reports.userstatistics.list') }}",
                type:'GET',
                data: function(d) {
                    // Add the external search value to the request
                    d.status        = $('#idStatus').val();
                    d.gender        = $("#idGender").val();
                },
            },
            columns: [
                { data: 'id', name: 'id' },
                { data: 'name', name: 'name' },
                { data: 'total_ads', name: 'total_ads' },
                { data: 'featured_ads', name: 'featured_ads' },
            ],
            columnDefs: [
                {
                    orderable: false,
                    targets: [0, 3]
                },
                {
                    "targets": 0,
                    "render": function(data, type, row, meta) {
                    return meta.row + 1;
                    }
                }
            ],
        });

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

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

        $('#export-excel').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.userstatistics.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.userstatistics.exportPdf') }}?' + updatedData;
        });

    });
</script>

@endsection
