@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">Payment 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">Payment 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">Payment 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="#" id="filterForm">
                    <div class="row g-3 align-items-end">
                        <div class="col-lg-4 mb-3">
                            <label for="userId" class="form-label">Customer Name</label>
                            <select name="user_id[]" class="form-select customer-dropdown-field" multiple>

                            </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="Pending">Pending</option>
                                <option value="approved">Approved</option>
                                <option value="Success">Success</option>
                            </select>
                        </div>
                        <div class="col-lg-4 mb-3">
                            <label for="start_date">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">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="UserReportTable" class="table table-bordered nowrap align-middle" style="width: 100%;">
                        <thead class="">
                            <tr class="text-uppercase">
                                <th>Sl No</th>
                                <th>User Details</th>
                                <th>Plan Name</th>
                                <th>Plan Duration</th>
                                <th>Amount</th>
                                <th>Payment Method</th>
                                <th>Status</th>
                                <th>Date Subscribed</th>
                            </tr>
                        </thead>
                        <tbody class="list form-check-all">
                            <!-- <tr>
                                <td>1</td>
                                <td>TXN123456789</td>
                                <td>Toyota Supra</td>
                                <td>John Doe</td>
                                <td>Premium</td>
                                <td>Credit Card</td>
                                <td>AED 100.00</td>
                                <td>Success</td>
                                <td>2023-10-01</td>
                            </tr>
                            <tr>
                                <td>2</td>
                                <td>TXN987654321</td>
                                <td>Ford Mustang</td>
                                <td>Jane Smith</td>
                                <td>Standard</td>
                                <td>PayPal</td>
                                <td>AED 50.00</td>
                                <td>Failed</td>
                                <td>2023-10-02</td>
                            </tr> -->
                        </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>
    $(document).ready(function () {
        var table = $('#UserReportTable').DataTable({
            responsive: true, 
            paging: true,     
            searching: true,  
            ordering: true,   
            lengthChange: true, 
            serverSide: true,
            ajax: {
                url: "{{ route('admin.reports.paymentreport.list') }}",
                type:'GET',
                data: function(d) {
                    d.status        = $('#idStatus').val();
                    d.user_id       = $('.customer-dropdown-field').val();
                    d.start_date    = $("#start_date").val();
                    d.end_date      = $("#end_date").val();
                },
            },
            columns: [
                { data: 'id', name: 'id' },
                { data: 'user_details', name: 'user_details' },
                { data: 'user_plan_name', name: 'user_plan_name' },
                { data: 'plan_type', name: 'plan_type' },
                { data: 'ad_amount', name: 'ad_amount' },
                { data: 'method_type', name: 'method_type' },
                { data: 'payment_status', name: 'payment_status' },
                { data: 'created_at', name: 'created_at' },
            ],
            columnDefs: [
                {
                    orderable: false,
                    targets: [0, 7]
                },
                {
                    "targets": 0, // Target the first column (index 0)
                    "render": function(data, type, row, meta) {
                        // Calculate the starting index of the current page
                        const pageInfo = $('#UserReportTable').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; 
        });

        $('#idStatus').change(function () {
            table.ajax.reload();
        });
        
        $(".customer-dropdown-field").select2({
            minimumInputLength: 2,
            minimumResultsForSearch: 10,
            multiple: true,
            ajax: {
                url: "{{ route('admin.getMinimalUserList') }}",
                dataType: "json",
                type: "GET",
                data: function (params) {
                    return {
                        term: params.term,
                        page: params.page,
                        limit: 10,
                        type: 'name'
                    };
                },
                processResults: function (data) {
                    return {
                        results: $.map(data, function (item) {
                            return {
                                text: item.text,
                                id: item.id
                            }
                        })
                    };
                },
            },
        });
        
        $(".customer-dropdown-field").on('change', function (e) {
            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();
        });

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

    });
</script>

@endsection
