<?php $__env->startSection('maincontent'); ?>
<?php $__env->startSection('title'); ?>
    <?php echo e(env('APP_NAME')); ?>- Subcategory
<?php $__env->stopSection(); ?>
<style>
    div#roleinfo_length {
        color: red;
    }

    div#roleinfo_filter {
        color: red;
    }

    div#roleinfo_info {
        color: red;
    }
</style>
 <?php
    $admin=App\Models\Admin::where('id',Auth::guard('admin')->user()->id)->first();
    $balance = $admin->account_balance;
?>
<div class="container-fluid pt-4 px-4">
    <div class="row">
        <div class="col-sm-12 col-md-12 col-xl-12">
            <div class="h-100 bg-secondary rounded p-4 pb-0">
                <div class="d-flex align-items-center justify-content-between" style="width: 50%;float:left;">
                    <h6 class="mb-0">Withdraw History</h6>
                </div>
                <div class="" style="width: 50%;float:left;">
                    <a type="button" data-bs-toggle="modal" data-bs-target="#mainSubcategory"
                        class="btn btn-primary m-2" style="float: right"> + Withdraw Requst</a>
                </div>
            </div>
        </div>

        <div class="col-sm-12 col-md-12 col-xl-12">
            <div class="bg-secondary rounded h-100 p-4">
                <div class="data-tables">
                    <table class="table table-dark" id="withdrawinfo" width="100%" style="text-align: center;">
                        <thead class="thead-light">
                            <tr>
                                <th>Sl</th>
                                <th>Date</th>
                                <th>Amount</th>
                                <th>Status</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        
        <div class="modal fade" id="mainSubcategory" tabindex="-1">
            <div class="modal-dialog">
                <div class="modal-content bg-secondary rounded h-100">
                    <div class="modal-header">
                        <h5 class="modal-title" style="color: red;"> New Requst</h5>
                        <button type="button" class="btn-dark btn-close" data-bs-dismiss="modal"
                            aria-label="Close"></button>
                    </div>
                    <div class="modal-body">

                        <form name="form" id="AddWithdrawRequst" enctype="multipart/form-data" method="POST">
                            <?php echo csrf_field(); ?>
                             <div class="mb-3">
                                <label for="amount" class="form-label">Withdraw Amount</label>
                                <input type="number" class="form-control" id="amount" name="amount" placeholder="Enter Withdraw Amount" required style="border:1px solid #8d7c7c;">
                              </div>
                                <div class="mb-3">
                                    <label for="account_number" class="form-label">Receiver Account Number</label>
                                    <input type="number" class="form-control"  minlength="11" maxlength="11"  id="account_number" name="account_number" placeholder="Enter Receiver Account Number" required style="border:1px solid #8d7c7c;">
                                </div>
                                        
                                 <div class="mb-3">
                                    <label for="status" class="form-label">Payment Methods</label>
                                    <select name="payment_methods" id="payment_methods" class="form-select" required style="display: block !important;">
                                        <option value="Bkash">bKash</option>
                                        <option value="Nagad">Nagad</option>
                                    </select>
                                    <?php $__errorArgs = ['status'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                        <span class="text-danger small"><?php echo e($message); ?></span>
                                    <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                </div>
                    
                                <?php if($balance < 50): ?>
                                    <div class="alert alert-warning text-danger">
                                        You must have at least 100 TK to make a withdraw request.
                                    </div>
                                    <button type="submit" class="btn btn-secondary" disabled>Submit Request</button>
                                <?php else: ?>
                                    <button type="submit" class="btn btn-primary">Submit Request</button>
                                <?php endif; ?>
                        </form>

                    </div>

                </div>
            </div>
        </div><!-- End popup Modal-->

        
        <!-- Withdraw Details Modal -->
        <div class="modal fade" id="editmainSubcategory" tabindex="-1" aria-hidden="true">
          <div class="modal-dialog">
            <div class="modal-content bg-secondary rounded h-100">
              
              <div class="modal-header">
                <h5 class="modal-title" style="color: red;">Withdraw Details</h5>
                <button type="button" class="btn-dark btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
              </div>
        
              <div class="modal-body">
                <ul class="list-group">
                  <li class="list-group-item"><strong>Date:</strong> <span id="wd_date"></span></li>
                  <li class="list-group-item"><strong>Amount:</strong> ৳<span id="wd_amount"></span></li>
                  <li class="list-group-item"><strong><span id="wd_payment_method"></span> Number:</strong> <span id="wd_account_number"></span></li>
                  <li class="list-group-item" id="wd_note_row" style="display:none;"><strong>Admin Note:</strong> <span id="wd_note"></span></li>
                  <li class="list-group-item"><strong>Status:</strong> <span id="wd_status"></span></li>
                </ul>
              </div>
        
            </div>
          </div>
        </div>
        <!-- End Withdraw Details Modal -->

        
        
        
        <input type="hidden" name="_token" value="<?php echo e(csrf_token()); ?>" />
    </div>
</div>

<script>
    $(document).ready(function() {
        var token = $("input[name='_token']").val();

        var withdrawinfo = $('#withdrawinfo').DataTable({
            order: [
                [0, 'desc']
            ],
            processing: true,
            serverSide: true,
            ajax: '<?php echo route('vandor.withdraw.history_data'); ?>',
            columns: [{
                    data: 'id'
                }, {
                    data: 'created_at',
                    render: function (data, type, row) {
                        if (data) {
                            let date = new Date(data);
                            let day = ("0" + date.getDate()).slice(-2);
                            let month = ("0" + (date.getMonth() + 1)).slice(-2);
                            let year = date.getFullYear();
                            return day + '-' + month + '-' + year;
                        }
                        return '';
                    }
                },
                {
                    data: 'amount'
                },
                {
                  data: 'status',
                    render: function (data, type, row) {
                        if (data == 1) {
                            return '<span class="badge bg-warning text-dark">Pending</span>';
                        } else if (data == 2) {
                            return '<span class="badge bg-success">Received</span>';
                        } else if (data == 3) {
                            return '<span class="badge bg-danger">Declined</span>';
                        } else {
                            return '<span class="badge bg-secondary">Unknown</span>';
                        }
                    }
                },
                {
                    data: 'action',
                    name: 'action',
                    orderable: false,
                    searchable: false
                },

            ]
        });


        //add subcategory

        $('#AddWithdrawRequst').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: '<?php echo e(route('vandor.WithdrawRequst.store')); ?>', // এখানে uploadUrl না, শুধু url
            processData: false,
            contentType: false,
            data: new FormData(this),
            success: function(data) {
                $('#amount').val('');
                $('#account_number').val('');
    
                swal({
                    title: "Success!",
                    icon: "success",
                });
                withdrawinfo.ajax.reload();
            },
            error: function(error) {
                console.log(error.responseText);
            }
        });
    });

        //edit subcategory
        
      $(document).on('click', '#WithdrawDetailsBtn', function() {
        let withdrawId = $(this).data('id');
        //alert(withdrawId);
        $.ajax({
           type: 'GET',
           url:'vandor/withdraw/' + withdrawId + '/show',
            success: function(data) {
                let dateObj = new Date(data.created_at);
                let day = ("0" + dateObj.getDate()).slice(-2);
                let month = ("0" + (dateObj.getMonth() + 1)).slice(-2);
                let year = dateObj.getFullYear();
                let hours = dateObj.getHours();
                let minutes = ("0" + dateObj.getMinutes()).slice(-2);
                let ampm = hours >= 12 ? 'PM' : 'AM';
                hours = hours % 12;
                hours = hours ? hours : 12;
                let formattedDate = day + "-" + month + "-" + year + " " + hours + ":" + minutes + " " + ampm;
    
                $('#wd_date').text(formattedDate);
                $('#wd_amount').text(data.amount);
                $('#wd_payment_method').text(data.payment_methods);
                $('#wd_account_number').text(data.account_number);
    
                if (data.note) {
                    $('#wd_note_row').show();
                    $('#wd_note').text(data.note);
                } else {
                    $('#wd_note_row').hide();
                }
    
                let statusHtml = '';
                if (data.status == 1) {
                    statusHtml = '<span class="badge bg-warning text-dark">Pending</span>';
                } else if (data.status == 2) {
                    statusHtml = '<span class="badge bg-success">Received</span>';
                } else if (data.status == 3) {
                    statusHtml = '<span class="badge bg-danger">Declined</span>';
                } else {
                    statusHtml = '<span class="badge bg-secondary">Unknown</span>';
                }
                $('#wd_status').html(statusHtml);
            },
            error: function() {
                console.log('error loading withdraw details');
            }
        });
    });

       

        //update subcategory
        $('#EditSubcategory').submit(function(e) {
            e.preventDefault();
            let subcategoryId = $('#subcategory_id').val();
            $.ajax({
                type: 'POST',
                url: 'subcategory/' + subcategoryId,
                processData: false,
                contentType: false,
                data: new FormData(this),
                success: function(data) {
                    $('#EditSubcategory').find('#sub_category_name').val('');
                    $('#EditSubcategory').find('#category_id').val('');
                    swal({
                        title: "Subcategory update successfully !",
                        icon: "success",
                        showCancelButton: true,
                        focusConfirm: false,
                        confirmButtonColor: "#DD6B55",
                        confirmButtonText: "Yes",
                        cancelButtonText: "No",
                    });
                    withdrawinfo.ajax.reload();
                },
                error: function(error) {
                    console.log('error');
                }
            });
        });

        // delete subcategory

        $(document).on('click', '#deleteSubcategoryBtn', function() {
            let subcategoryId = $(this).data('id');
            swal({
                    title: "Are you sure?",
                    text: "Once deleted, you will not be able to recover this !",
                    icon: "warning",
                    buttons: true,
                    dangerMode: true,
                })
                .then((willDelete) => {
                    if (willDelete) {
                        $.ajax({
                            type: 'DELETE',
                            url: 'subcategorys/' + subcategoryId,
                            data: {
                                '_token': token
                            },
                            success: function(data) {
                                swal("Subcategory has been deleted!", {
                                    icon: "success",
                                });
                                withdrawinfo.ajax.reload();
                            },
                            error: function(error) {
                                console.log('error');
                            }
                        });

                    } else {
                        swal("Your data is safe!");
                    }
                });

        });

        // status update

        $(document).on('click', '#subcategorystatusBtn', function() {
            let subcategoryId = $(this).data('id');
            let subcategoryStatus = $(this).data('status');

            $.ajax({
                type: 'PUT',
                url: 'subcategory/status',
                data: {
                    subcategory_id: subcategoryId,
                    status: subcategoryStatus,
                    '_token': token
                },

                success: function(data) {
                    swal({
                        title: "Status updated !",
                        icon: "success",
                        showCancelButton: true,
                        focusConfirm: false,
                        confirmButtonColor: "#DD6B55",
                        confirmButtonText: "Yes",
                        cancelButtonText: "No",
                    });
                    withdrawinfo.ajax.reload();
                },
                error: function(error) {
                    console.log('error');
                }

            });
        });

    });
</script>

<?php $__env->stopSection(); ?>

<?php echo $__env->make('backend.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/falconen/multivendor.falconenterprisebd.com/resources/views/backend/content/withdraw/withdraw.blade.php ENDPATH**/ ?>