<?php $__env->startSection('content'); ?>
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/css/select2.min.css" rel="stylesheet" />
    <link href="<?php echo e(config('app.url')); ?>/public/public/vendors/datatables/dataTables.bootstrap4.css" rel="stylesheet"
        type="text/css" />
    <link href="<?php echo e(config('app.url')); ?>/public/public/vendors/datatables/datatables.mark.css" rel="stylesheet"
        type="text/css" />
    <link href="<?php echo e(config('app.url')); ?>/public/public/vendors/datatables/buttons.bootstrap4.css" rel="stylesheet"
        type="text/css" />
    <link href="<?php echo e(config('app.url')); ?>/public/public/vendors/datatables/responsive.bootstrap4.css" rel="stylesheet"
        type="text/css" />
    <link href="<?php echo e(config('app.url')); ?>/public/public/vendors/datatables/select.bootstrap4.css" rel="stylesheet"
        type="text/css" />
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/css/select2.min.css" rel="stylesheet" />
    <link href="<?php echo e(config('app.url')); ?>/public/public/assets/css/date-range-picker.css" rel="stylesheet"
    type="text/css" />
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />
    <div class="page-content-wrapper">
        <div class="page-content">
            <div class="row">
                <div class="col-lg-12 ">
                    <div class="card radius-10">
                        <div class="card-header">
                            <h4>Publisher Withdrawals</h4>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-lg-12 text-right  mt-2">
                                    <button class="btn btn-primary float-end" data-bs-toggle="modal" data-bs-target="#addModal">Create Withdraw Request</button>
                                </div>
                            </div>
                            <form id='form1' method="GET">
                                <div class="row">
                                    <div class="col-lg-3 ">
                                        <select type="text" name="affliatesearch" class="form-control" required="">
                                            <option value="">Select Publisher</option>
                                            <?php $__currentLoopData = $publishers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $q): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($q->id); ?>"><?php echo e($q->name); ?></option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    </div>
                                    <div class="col-lg-3">
                                        <select id="status" class="form-control mb-3" name="statussearch">
                                            <option value="">Select Status</option>
                                            <option value="Pending">Pending</option>
                                            <option value="Locked">Locked</option>
                                            <option value="Completed">Completed</option>
                                            <option value="Cancelled">Cancelled</option>
                                        </select>
                                    </div>
                                    <div class="col-lg-3">
                                        <input class="btn btn-dark" type="submit" id="search" value="Filter">
                                    </div>
                                </div>
                            </form>
                            <div class="row">
                                <div class="col-lg-12  table-responsive">
                                    <table id="cashout_table"
                                        class="table table-bordered table-striped dt-responsive nowrap"
                                        style="border-collapse: collapse; border-spacing: 0; width: 100%;">
                                        <thead>
                                            <tr>
                                                <td>Serial</td>
                                                <td>Date</td>
                                                <td>Publisher</td>
                                                <td>Method</td>
                                                <td>Amount</td>
                                                <td>Status</td>
                                                <td>Pay Terms</td>
                                                <td>Pay Period</td>
                                                <td>Action</td>
                                            </tr>
                                        </thead>
                                        <tbody id="showdata">

                                        </tbody>
                                    </table>
                                </div>
                                <div class="col-lg-12">
                                    <h4>Automate Withdrawl Request</h4>
                                    <a href="<?php echo e(url('admin/cron-payout-net-45')); ?>" class="btn btn-primary">Net 45</a>
                                    <a href="<?php echo e(url('admin/cron-payout-net-30')); ?>" class="btn btn-primary">Net 30</a>
                                    <a href="<?php echo e(url('admin/cron-payout-net-15')); ?>" class="btn btn-primary">Net 15</a>
                                    <a href="<?php echo e(url('admin/cron-payout-net-7')); ?>" class="btn btn-primary">Net 7</a>
                                </div>
                                <!-- ADD MODAL -->
                                <form action="<?php echo e(url('admin/insert-cashout')); ?>" id="add_cashout_request" method="post">
                                    <?php echo csrf_field(); ?>
                                    <div id="addModal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
                                        <div class="modal-dialog modal-lg" role="document">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <h5 class="modal-title">Add Withdraw Request</h5>
                                                    <div class="position-absolute top-0 end-0 mt-2 me-2 z-index-1">
                                                        <button
                                                            class="btn-close btn btn-sm btn-circle d-flex flex-center transition-base"
                                                            data-bs-dismiss="modal" aria-label="Close"></button>
                                                    </div>
                                                </div>
                                                <div class="modal-body">

                                                    <div class="row">
                                                        <div class="col-lg-3 ">
                                                            <label>Balance</label>
                                                            <input type="text" class="form-control" name="balance"
                                                                readonly="true">
                                                        </div>
                                                        <div class="col-lg-12 form-group">
                                                            <label>Select Publisher</label>
                                                            <select type="text" name="affliate_id" class="form-control"
                                                                id="publisher_id" required="">
                                                                <option value="">Select Publisher</option>

                                                                <?php $__currentLoopData = $publishers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $q): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                                    <option value="<?php echo e($q->id); ?>"
                                                                        data="<?php echo e($q->balance); ?>"><?php echo e($q->id); ?> | <?php echo e($q->name); ?>

                                                                    </option>
                                                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                            </select>
                                                        </div>
                                                        <div class="col-lg-12 form-group">
                                                            <label>Select Payment Terms</label>
                                                            <select type="text" name="payment_terms"
                                                                class="form-control">
                                                                <!-- <option value="netweekly">netweekly</option> -->
                                                                <option value="net30">net30</option>
                                                                <option value="net45">net45</option>
                                                                <option value="net15">net15</option>
                                                                <option value="net7">net7</option>

                                                                <option value="On Requested">On Requested</option>

                                                            </select>

                                                        </div>



                                                        <div class="form-group-column col-lg-4 mt-4">
                                                            <div class="daterange" id="reportrange" style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc; width: 100%">
                                                                <i class="fa fa-calendar"></i>&nbsp;
                                                                <input type="text" name="dateRangeInput" value="" readonly />
                                                                <i class="fa fa-caret-down"></i>
                                                            </div>
                                                        </div>

                                                        <div class="col-lg-12 form-group">
                                                            <label> Amount</label>
                                                            <input type="text" name="amount" id="amount"
                                                                class="form-control" required="">
                                                        </div>

                                                        <div class="col-lg-12 form-group">
                                                            <label>Note</label>
                                                            <textarea name="note" class="form-control"></textarea>
                                                        </div>
                                                    </div>

                                                </div>
                                                <div class="modal-footer">
                                                    <button type="submit" class="btn btn-primary"
                                                        id="savebtn">Request</button>
                                                    <button class="btn btn-secondary" type="button"
                                                        data-bs-dismiss="modal">Close</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </form>
                                <!-- End ADD MODAL -->



                                <!-- Edit MODAL -->
                                <form id="update_cashout" method="post" enctype="multipart/form-data">
                                    <div id="editModal" class="modal fade" tabindex="-1" role="dialog"
                                        aria-hidden="true">
                                        <div class="modal-dialog modal-lg" role="document">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <h5 class="modal-title"> Withdraw Request Number <span
                                                            id="withdraw_id"></span></h5>

                                                </div>
                                                <div class="modal-body">

                                                    <?php echo csrf_field(); ?>

                                                    <input type="hidden" name="id" id="id">
                                                    <input type="hidden" name="old_amount">
                                                    <input type="hidden" name="publisher_id">
                                                    <div class="row">
                                                        <div class="col-lg-3">
                                                            <p>Publisher</p>
                                                        </div>
                                                        <div class="col-lg-8">
                                                            <p id="affliate"></p>
                                                        </div>
                                                    </div>
                                                    <div class="row">
                                                        <div class="col-lg-3">
                                                            <p>Method</p>
                                                        </div>
                                                        <div class="col-lg-8">
                                                            <p id="methoddiv"></p>
                                                        </div>
                                                    </div>
                                                    <div class="row">
                                                        <div class="col-lg-3">
                                                            <p>Details</p>
                                                        </div>
                                                        <div class="col-lg-8">
                                                            <p id="detail"></p>
                                                        </div>
                                                    </div>
                                                    <div class="row">
                                                        <div class="col-lg-3">
                                                            <p>Note</p>
                                                        </div>
                                                        <div class="col-lg-8">
                                                            <p id="notediv"></p>
                                                        </div>
                                                    </div>
                                                    <div class="row">
                                                        <div class="col-lg-3">
                                                            <p>Payment Term</p>
                                                        </div>
                                                        <div class="col-lg-8">
                                                            <p id="paymentdiv"></p>
                                                        </div>
                                                    </div>
                                                    <div class="row">
                                                        <div class="col-lg-3">
                                                            <p>Period</p>
                                                        </div>
                                                        <div class="col-lg-8">
                                                            <p id="perioddiv"></p>
                                                        </div>
                                                    </div>
                                                    <div class="row mb-2">
                                                        <div class="col-lg-3 ">
                                                            <p>Amount</p>
                                                        </div>
                                                        <div class="col-lg-4"><input name="amount1" class="form-control">
                                                        </div>
                                                    </div>
                                                    <div class="row">
                                                        <div class="col-lg-3">
                                                            <p>Status</p>
                                                        </div>
                                                        <div class="col-lg-3">
                                                            <select id="status" class="form-control mb-3"
                                                                name="status">

                                                                <option value="Pending">Pending</option>
                                                                <option value="Locked">Locked</option>
                                                                <option value="Completed">Completed</option>
                                                                <option value="Cancelled">Cancelled</option>
                                                                <option value="Rejected">Rejected</option>
                                                            </select>
                                                        </div>
                                                    </div>
                                                    <div class="row">
                                                        <div class="col-lg-3">
                                                            <p>Upload File (pdf)</p>
                                                        </div>
                                                        <div class="col-lg-6">
                                                            <input class="form-control" type="file" id="upload_pdf" name="upload_pdf" accept="application/pdf">
                                                        </div>
                                                    </div>
                                                    
                                            
                                                    <div class="row">
                                                        <div class="col-lg-3">
                                                            <p>Date Of Withdraw Request</p>
                                                        </div>
                                                        <div class="col-lg-8">
                                                            <p id="datediv"></p>
                                                        </div>
                                                    </div>
                                                    <div class="row">

                                                    </div>


                                                </div>
                                                <div class="modal-footer">
                                                    <button type="submit" class="btn btn-primary"
                                                        id="btnupdate">Edit</button>
                                                    <button class="btn btn-secondary" type="button"
                                                        data-bs-dismiss="modal">Close</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </form>
                                <!-- End Edit MODAL -->

                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php
                $affliatesearch = isset($_GET['affliatesearch']) && !empty($_GET['affliatesearch']) ? $_GET['affliatesearch'] : '0';
                $statussearch = isset($_GET['statussearch']) && !empty($_GET['statussearch']) ? $_GET['statussearch'] : '0';
            ?>
        <?php $__env->stopSection(); ?>
        <?php $__env->startSection('js'); ?>
            <script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc="
                crossorigin="anonymous"></script>
            <script type="text/javascript" src="<?php echo e(config('app.url')); ?>/public/public/vendors/datatables/jquery.mark.min.js">
            </script>
            <script src="<?php echo e(config('app.url')); ?>/public/public/vendors/datatables/jquery.dataTables.min.js"></script>
            <script src="<?php echo e(config('app.url')); ?>/public/public/vendors/datatables/dataTables.bootstrap4.min.js"></script>
            <script src="<?php echo e(config('app.url')); ?>/public/public/vendors/datatables/dataTables.buttons.min.js"></script>
            <script src="<?php echo e(config('app.url')); ?>/public/public/vendors/datatables/buttons.bootstrap4.min.js"></script>
            <script src="<?php echo e(config('app.url')); ?>/public/public/vendors/datatables/dataTables.keyTable.min.js"></script>
            <script src="<?php echo e(config('app.url')); ?>/public/public/vendors/datatables/dataTables.select.min.js"></script>
            <script src="<?php echo e(config('app.url')); ?>/public/public/vendors/datatables/buttons.html5.min.js"></script>
            <script src="<?php echo e(config('app.url')); ?>/public/public/vendors/datatables/buttons.print.min.js"></script>
            <script src="<?php echo e(config('app.url')); ?>/public/public/vendors/datatables/dataTables.responsive.min.js"></script>
            <script src="<?php echo e(config('app.url')); ?>/public/public/vendors/datatables/responsive.bootstrap4.min.js"></script>
            <script src="<?php echo e(config('app.url')); ?>/public/public/vendors/datatables/datatables.mark.js"></script>
            <script src="<?php echo e(config('app.url')); ?>/public/public/assets/js/date-range-picker.js"></script>
            <script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
            <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>


            <script type="text/javascript">
                $(function() {
                    <?php if(Session::has('success')): ?>
                        Swal.fire({
                            title: '<?php echo e(Session::get('success')); ?>',
                            confirmButtonText: 'Ok'
                        })
                    <?php endif; ?>
                    let balance = 0;
                    $('#publisher_id').change(function() {

                        balance = $('option:selected', this).attr('data');
                        $('input[name=balance]').val(parseFloat(balance).toFixed(2));
                    })
                    var cashout_table = $('#cashout_table').DataTable({

                        "mark": true,

                        "order": [

                            [0, "desc"]

                        ],

                        "columnDefs": [

                            {

                                "targets": [0],

                                "render": function(data, type, row, meta) {

                                    return '#' + data;

                                }

                            },

                            {

                                "responsivePriority": 1,

                                "targets": 0

                            },

                            {

                                "responsivePriority": 2,

                                "targets": -1,

                                "orderable": false

                            }

                        ],

                        "processing": true,

                        "serverSide": true,

                        "ajax": {

                            url: "<?php echo e(route('admin.show-cashout')); ?>",
                            "data": function(d) {
                                d.affliatesearch = '<?php echo e($affliatesearch); ?>';
                                d.statussearch = '<?php echo e($statussearch); ?>';
                            },
                            error: function(xhr) {

                                session_error(xhr);

                                // console.log(xhr.status);

                            }

                        },

                        "columns": [

                            {
                                "data": "id"
                            },

                            {
                                "data": "date"
                            },
                            {
                                "data": "publisher.name"
                            },

                            {
                                "data": "method"
                            },

                            {
                                "data": "amount"
                            },
                            {
                                "data": "status"
                            },
                            {
                                "data": "payterm"
                            },
                            {
                                "data": "payperiod"
                            },

                            {
                                "data": "action"
                            }

                        ],

                        drawCallback: function(settings) {

                            $(document).find('[data-toggle="tooltip"]').tooltip();

                        },

                        initComplete: function(settings, json) {

                            // $(document).find('[data-toggle="tooltip"]').tooltip();

                        }

                    })

                    $(document).ready(function() {
                        $('#cashout_table').on('click', '.deleteData', function() {

                            let id = $(this).attr('data');
                            Swal.fire({
                                title: 'Are you sure?',
                                text: "You want to Delete this Withdraw Request ",
                                type: 'warning',
                                showCancelButton: true,
                                confirmButtonColor: '#3085d6',
                                cancelButtonColor: '#d33',
                                confirmButtonText: 'Yes, delete it!'
                            }).then((result) => {
                                if (result.value) {
                                    $.ajax({
                                        method: 'get',
                                        data: {
                                            id: id
                                        },
                                        url: "<?php echo e(route('admin.delete-cashout')); ?>",
                                        async: false,
                                        dataType: 'json',
                                        success: function(result) {

                                            // console.log(result);
                                            if (!result.status) {
                                                Swal.fire('Failed', result.message,
                                                    'error');
                                            } else {
                                                Swal.fire('Success', result.message,
                                                    'success');
                                                location.reload();
                                            }
                                        },
                                        error: function(xhr) {

                                            if (xhr.status == 422) {
                                                $.each(xhr.responseJSON.errors,
                                                    function(k, v) {
                                                        form.find('[name="' + k +
                                                            '"]').after(
                                                            '<div class="text-danger">' +
                                                            v[0] +
                                                            '</div>');
                                                    });
                                            } else if (xhr.status == 419) {
                                                window.location.href = "";
                                            }
                                            // console.log(xhr);
                                        }
                                    });
                                }
                            });
                        })
                        $('#savebtn').unbind().click(function(e) {
                            e.preventDefault();
                            let data = $('#add_cashout_request').serialize();
                            $.ajax({
                                url: "<?php echo e(route('admin.insert-cashout')); ?>",
                                type: 'POST',
                                dataType: 'JSON',
                                data: data,
                                success: function(result) {
                                    $('.loader').fadeOut();
                                    // console.log(result);
                                    if (!result.status) {
                                        Swal.fire('Failed', result.message, 'error');
                                    } else {
                                        Swal.fire('Success', result.message, 'success');
                                        location.reload();
                                    }
                                },
                                error: function(xhr) {
                                    $('.loader').fadeOut();
                                    if (xhr.status == 422) {
                                        $.each(xhr.responseJSON.errors, function(k, v) {
                                            $('#add_cashout_request').find('[name="' + k + '"]').after(
                                                '<div class="text-danger">' + v[0] +
                                                '</div>');
                                        });
                                    } else if (xhr.status == 419) {
                                        window.location.href = "";
                                    }
                                    // console.log(xhr);
                                }
                            });
                        })
                        $('#btnupdate').unbind().click(function(e) {
                            e.preventDefault();
                            
                            let files = $('#upload_pdf')[0].files;
                            var form = $("#update_cashout");
                            let data  = new FormData(form[0]);
                            if(files.length > 0){
                                data.append('upload_pdf',files[0]);
                            }
                            // let data = $('#update_cashout').serialize();
                            
                            console.log(data);
                            $.ajax({
                                url: "<?php echo e(route('admin.update-cashout')); ?>",
                                type: 'POST',
                                dataType: 'JSON',
                                processData: false,
                                contentType: false,
                                data: data,
                                success: function(result) {
                                    $('.loader').fadeOut();
                                    // console.log(result);
                                    if (!result.status) {
                                        Swal.fire('Failed', result.message, 'error');
                                    } else {
                                        Swal.fire('Success', result.message, 'success');
                                        location.reload();
                                    }
                                },
                                error: function(xhr) {
                                    $('.loader').fadeOut();
                                    if (xhr.status == 422) {
                                        $.each(xhr.responseJSON.errors, function(k, v) {
                                            $('#update_cashout').find('[name="' + k + '"]').after(
                                                '<div class="text-danger">' + v[0] +
                                                '</div>');
                                        });
                                    } else if (xhr.status == 419) {
                                        window.location.href = "";
                                    }
                                    // console.log(xhr);
                                }
                            });
                        })
                        $('#cashout_table').on('click', '.editData', function() {

                            let id = $(this).attr('data');

                            $.ajax({
                                method: 'get',
                                data: {
                                    id: id
                                },
                                url: "<?php echo e(route('admin.edit-cashout')); ?>",
                                async: false,
                                dataType: 'json',
                                success: function(res) {
                                    $('#editModal').modal('show');

                                    $('select[name=status]').val(res.status);

                                    $('#affliate').html(res.publisher.name);
                                    $('#methoddiv').html(res.method);
                                    $('#notediv').html(res.note);

                                    $('#detail').html(res.payment_details);
                                    $('#paymentdiv').html(res.payterm);
                                    $('#perioddiv').html(res.from_date + ' to ' + res.to_date);
                                    $('input[name=amount1]').val(parseFloat(res.amount).toFixed(
                                        2));
                                    $('input[name=old_amount]').val(parseFloat(res.amount)
                                        .toFixed(2));

                                    $('input[name=publisher_id]').val(res.affliate_id);

                                    $('#datediv').html(res.created_at);


                                    $('#withdraw_id').html(res.id);
                                    $('#id').val(res.id);

                                },



                            })
                        })


                    });


                    $(document).ready(function () {
                        $(function () {
                            var start = moment().subtract(29, 'days');
                            var end = moment();

                            function cb(start, end) {
                                var dateRangeText = start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY');
                                $('#reportrange input[name="dateRangeInput"]').val(dateRangeText);

                                // Send the selected date range to the server using Ajax
                                var startDate = start.format('YYYY-MM-DD');
                                var endDate = end.format('YYYY-MM-DD');

                                var publisherId = $('#publisher_id').val();

                                $.ajax({
                                    url: "<?php echo e(route('admin.filter-amount')); ?>", // route URL
                                    type: 'POST',
                                    data: {
                                        startDate: startDate,
                                        endDate: endDate,
                                        publisherId: publisherId,
                                        _token: $('meta[name="csrf-token"]').attr('content'), // CSRF token
                                    },
                                    success: function (data) {
                                        // Handle the server response here
                                        var totalEarnings = 0;
                                        data.offers.forEach(function (field) {
                                            var status = field.status;
                                            var publisherEarning = parseFloat(field.publisher_earned); // Convert to float if necessary
                                            
                                            if (status === 'Approved'){
                                                if (!isNaN(publisherEarning)) {
                                                    totalEarnings += publisherEarning;
                                                }
                                            }
                                        });

                                        var formattedTotalEarnings = totalEarnings.toFixed(2);

                                        $('#amount').val(formattedTotalEarnings);
                                    },
                                    error: function (xhr, textStatus, errorThrown) {
                                        // Handle any errors that occur during the request
                                        console.error(errorThrown);
                                    }
                                });
                            }

                            $('#reportrange').daterangepicker({
                                startDate: moment().subtract(29, 'days'),
                                endDate: moment(),
                                ranges: {
                                    'Today': [moment(), moment()],
                                    'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                                    'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                                    'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                                    'This Month': [moment().startOf('month'), moment().endOf('month')],
                                    'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
                                }
                            }, cb);

                            cb(moment().subtract(29, 'days'), moment());
                        });
                    });

                });
            </script>
        <?php $__env->stopSection(); ?>

<?php echo $__env->make('admin.layout.admin-dashboard', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/johir784/public_html/fiverr.bdbse.com/backend/resources/views/admin/cashout/Manage_Cashout.blade.php ENDPATH**/ ?>