@extends('Admin.layout')
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css">
@section('content')
<meta name="csrf-token" content="{{ csrf_token() }}">

<div class="row align-items-center justify-content-between mb-3 mt-3 m-2">
    <div class="col-auto">
        <h5>Diamond Payout Report</h5>
    </div>
  
</div>
<div class="card p-0 m-2">
   
    <div class="row mb-3 align-items-center p-3 g-2">
        <div class="col-1">
            <select id="pageLength" class="form-select">
                <option value="10"> 10</option>
                <option value="25"> 25</option>
                <option value="50"> 50</option>
                <option value="100"> 100</option>
            </select>
        </div>

        <div class="col-md-3 ms-auto">
            <input type="text" id="toDate" class="form-control" placeholder="Search">
            
        </div>
         <div class="col-lg-1 col-md-6">
                <button id="exportExcel" class="btn btn-label-secondary w-100">
                    <i class="ti tabler-download me-1"></i> Export
                </button>
            </div>
    </div>


    <div class="table-responsive pt-0 pb-2">
        <div class="row card-header flex-column flex-md-row border-bottom mx-0 p-0">
            <div class="justify-content-between dt-layout-table">
                <div class=" justify-content-between align-items-center dt-layout-full table-responsive">
                    <table class="table" id="usersTable">
                        <thead>
                            <tr>
                                <th>Sno</th>
                                <th>From  Date</th>
                                <th>To Date</th>
                                <th>User ID</th>
                                <th>User Name</th>
                                <th>Type</th>
                                <!-- <th>Amount</th> -->
                                <!-- <th>Tds charge</th>
                                <th>Admin charge</th> -->
                                <th>Payable Amount</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>

                </div>
            </div>
        </div>
    </div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script>
    $(document).ready(function() {

        const table = $('#usersTable').DataTable({
            ajax:"{{ route('admin.view_diamond_payout_data', $user->user_id) }}",
               
            processing: true,
            serverSide: true,
            // data: users,
            searching   : true,
            paging      : true,
            info        : true,
            lengthChange: false,
            pageLength  : 10,
            ordering    : false,

             buttons: [{
                extend: 'excelHtml5',
                 title: 'Diamond Payout Report - {{ $user->name }} ({{ $user->user_id }})',
                className: 'd-none',

                exportOptions: {
                    columns: ':visible'
                },

                action: function(e, dt, button, config) {

                    $.ajax({

                        url: "{{ route('admin.view_diamond_payout_data', $user->user_id) }}",
                        type: "GET",

                        data: {
                            start: 0,
                            length: 1000000
                        },

                        success: function(json) {

                            let oldData = dt.rows().data().toArray();

                            dt.clear();

                            for (let i = 0; i < json.data.length; i++) {

                                dt.row.add(json.data[i]);
                            }

                            dt.draw();

                            $.fn.dataTable.ext.buttons.excelHtml5.action.call(
                                this,
                                e,
                                dt,
                                button,
                                config
                            );

                            dt.clear();

                            for (let i = 0; i < oldData.length; i++) {

                                dt.row.add(oldData[i]);
                            }

                            dt.draw();
                        }
                    });
                }
            }],

             columns: [

            {
                data: 'DT_RowIndex',
                name: 'DT_RowIndex',
                searchable: false
            },
              {
                data: 'from_date',
                name: 'from_date'
            },
            {
                data: 'to_date',
                name: 'to_date'
            },
            {
                data: 'user_id',
                name: 'user_id'
            },
            {
                data: 'user_name',
                name: 'user_name'
            },
            {
                data: 'type',
                name: 'type'
            },
            // {
            //     data: 'amount',
            //     name: 'amount'
            // },
            // {
            //     data: 'tds',
            //     name: 'tds'
            // },
            // {
            //     data: 'admin_charge',
            //     name: 'admin_charge'
            // },
            {
                data: 'payable_amount',
                name: 'payable_amount'
            }
        ]

        });

        // CUSTOM SEARCH
        $('#toDate').on('keyup', function() {
            table.ajax.reload();
        });

        
        $('#exportExcel').click(function() {

            table.button('.buttons-excel').trigger();

        });

       

    });
</script>




@endsection