<?php $__env->startSection('title', 'Shipped'); ?>

<?php $__env->startSection('content'); ?>

    <section>
        <div class="row">
           
            <div class="col-12">

                <div class="card">
                    <div class="card-header">
                        <h1>Shipped</h1>
                    </div>  
                    <div class="card-content" aria-expanded="true">
                        <div class="card-body">
                            <?php echo $__env->make('inc.messages', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>

                            <form id="shipped_form" class="form-inline mb-1 justify-content-center" novalidate="novalidate">

                                <div class="form-group">
                                    <input type="text" name="tracking_number" class="form-control tracking_number" placeholder="Tracking Number*" data-rule-required="true" data-msg-required="Tracking Number is required">

                                </div>

                                <div class="form-group ml-1">
                                    <button type="submit" name="add" class="btn btn-dark add" value="Add">Add</button>
                                </div>
                            </form>

                            <table class="table table-bordered datatable" id="datatable" style="z-index: 3;">
                                <thead>
                                <tr role="row" class="bg-dark white">
                                    <th class="border-dark border-darken-1">S. No.</th>
                                    <th class="border-dark border-darken-1">Tracking Number</th>
                                    <th class="border-dark border-darken-1">Card Number</th>
                                    <th class="border-dark border-darken-1">Customer Name</th>
                                    <th class="border-dark border-darken-1">Customer Address</th>
                                    <th class="border-dark border-darken-1">Customer Contact</th>
                                    <th class="border-dark border-darken-1">Card Category</th>
                                    <th class="border-dark border-darken-1">City</th>
                                    <th class="border-dark border-darken-1"></th>
                                </tr>
                                </thead>
                            </table>

                            <form id="shipped_status_form" class="form-horizontal text-center" method="POST" action="<?php echo e(route('shipped.card_store')); ?>" novalidate="novalidate">
                                <?php echo e(csrf_field()); ?>


                                <input type="hidden" name="card_ids" class="card_ids">
                                <input type="hidden" name="card_ids2" class="card_ids2">

                                <div class="form-group ml-1">
                                    <button type="submit" name="confirm" class="btn btn-dark confirm" value="Confirm" disabled="disabled">Submit</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('css'); ?>
    <link rel="stylesheet" type="text/css" href="<?php echo e(asset('app-assets/vendors/css/forms/toggle/bootstrap-switch.min.css')); ?>">
    <link rel="stylesheet" type="text/css" href="<?php echo e(asset('app-assets/vendors/css/extensions/toastr.css')); ?>">
<?php $__env->stopSection(); ?>

<?php $__env->startSection('js'); ?>
    <script src="<?php echo e(asset('app-assets/vendors/js/forms/toggle/bootstrap-checkbox.min.js')); ?>" type="text/javascript"></script>
    <script src="<?php echo e(asset('app-assets/vendors/js/forms/extended/inputmask/jquery.inputmask.bundle.min.js')); ?>" type="text/javascript"></script>
    <script src="<?php echo e(asset('app-assets/vendors/js/forms/validation/jquery.validate.min.js')); ?>" type="text/javascript"></script>
    <script src="<?php echo e(asset('app-assets/vendors/js/extensions/toastr.min.js')); ?>" type="text/javascript"></script>
    <script src="<?php echo e(asset('app-assets/vendors/js/quagga/quagga.min.js')); ?>" type="text/javascript"></script>
    <script src="<?php echo e(asset('js/custom.js')); ?>" type="text/javascript"></script>
    <script type="text/javascript">

        $(document).ready(function() {
            
            var check_batch = 0;
            var card_ids = [];
            var card_ids2 = [];
            var misscid = 0;
            
          
            $('#shipped_form input.tracking_number').focus();

            var table = $('#datatable').DataTable({
                dom: 'ltipr',
                scrollX: false,
                paging:false,
                columns: [
                    {orderable: false, searchable: false, name: 'serial_number', class: 'align-middle serial_number'},
                    {name: 'tracking_number', class: 'align-middle tracking_number', orderable: false, searchable: false},
                    {name: 'card_number', class: 'align-middle card_number', orderable: false, searchable: false},
                    {name: 'customer_name', class: 'align-middle customer_name', orderable: false, searchable: false},
                    {name: 'customer_address', class: 'align-middle customer_address', orderable: false, searchable: false},
                    {name: 'customer_contact', class: 'align-middle customer_contact', orderable: false, searchable: false},
                    {name: 'card_category', class: 'align-middle card_category', orderable: false, searchable: false},
                    {name: 'city', class: 'align-middle city', orderable: false, searchable: false},
                    {name: 'remove', class: 'align-middle remove', sortable: false, orderable: false, searchable: false}
                ],
                rowCallback: function(row, data, index) {
                    // var info = table.page.info();
                    //
                    // $('td:eq(0)', row).html(index + 1 + info.page * info.length);
                },
                initComplete: function() {
                    var search = $('<tr role="row" class="bg-primary bg-lighten-1 search"></tr>').appendTo(this.api().table().header());

                    var td = '<td style="padding:5px;" class="border-primary border-lighten-2"><fieldset class="form-group m-0 position-relative has-icon-right"></fieldset></td>';
                    var input = '<input type="text" class="form-control form-control-sm input-sm primary">';
                    var icon = '<div class="form-control-position primary"><i class="la la-search"></i></div>';

                    this.api().columns().every(function(column_id) {
                        var column = this;
                        var header = column.header();
                    });
                    this.api().table().columns.adjust();
                }
            });

            $('#shipped_form input.tracking_number').inputmask({
                'alias': 'integer',
                'allowMinus': false,
                'allowPlus': false
            });

            
            $('#shipped_form').validate({
                errorClass: 'danger',
                successClass: 'success',
                errorPlacement: function(error, element) {
                    error.addClass('w-100').appendTo(element.parents('form'));
                },
                submitHandler: function(form) {
                    $('#shipped_form button.add').prop('disabled', true);

                    var tracking_number = $(form).find('input.tracking_number').val();
                    if (table.columns('.tracking_number').data().eq(0).indexOf(parseInt(tracking_number)) === -1) {
                        
                        $('#shipped_status_form input.card_ids2').val(card_ids);

                        $.ajax({
                            url: '<?php echo route('shipped.card_details'); ?>',
                            method: 'POST',
                            data: {
                                'tracking_number': tracking_number,
                                'check_batch': check_batch,
                                'card_ids2': $('#shipped_status_form input.card_ids2').val(),
                                
                                '_token': '<?php echo e(csrf_token()); ?>'
                            },
                            timeout: 5000,
                            error: function (data) {
                                form.reset();

                                $('#shipped_form input.tracking_number').val('').focus();

                                $('#shipped_form button.add').prop('disabled', false);
                                scan_sound(2);
                                toastr.error('Couldn\'t connect to server, check internet connection and re-enter!', 'Error!', {positionClass: 'toast-top-center', containerId: 'toast-top-center'});
                            },
                            success: function(data) {
                                form.reset();

                                $('#shipped_form input.tracking_number').val('').focus();

                                remove_button = '<button type="button" class="btn btn-icon btn-danger"><i class="la la-close"></i></button>';
                                
                                if (data.status == 0) {
                                    id = data.details.id;

                                    var index = $.inArray(id, card_ids);

                                    if (index === -1) {
                                        var rowNo = table.rows().count();
                                        table.row.add([rowNo + 1, data.details.tracking_number, data.details.card_number, data.details.customer_name, data.details.customer_address, data.details.customer_contact, data.details.card_category, data.details.city, remove_button]).node().id = data.details.id;
                                        table.draw(false);
                                        table.order([0, 'desc']).draw();
                                        scan_sound(1);
                                        card_ids.push(data.details.id);
                                        card_ids2=card_ids;
                                        check_batch=data.batch_id;
                                        $('#shipped_form button.add').prop('disabled', false);

                                        $('#shipped_status_form button.confirm').prop('disabled', false);

                                        toastr.success(data.success, 'Success!', {positionClass: 'toast-bottom-center', containerId: 'toast-bottom-center'});
                                    }
                                    console.log('check missing cards');
                                console.log(data.check_missing_cards);
                                
                                
                                }
                                else {
                                    $('#shipped_form button.add').prop('disabled', false);
                                    scan_sound(2);
                                    toastr.error(data.error, 'Error!', {positionClass: 'toast-top-center', containerId: 'toast-top-center'});
                                }
                            }
                        });
                    }
                    else {
                        $('#shipped_form button.add').prop('disabled', false);

                        toastr.error('Shipment has been added already', 'Error!', {positionClass: 'toast-top-center', containerId: 'toast-top-center'});
                    }

                    return false;
                }
            });

            
            $('#shipped_status_form').bind('submit', function(e) {
                e.preventDefault();

                // $('#add_shipment_weight_form input.card_ids').val(card_ids);

                // $('#ShipmentWeightModal').modal('show');
            });

            $('#shipped_status_form').validate({
                errorClass: 'danger',
                successClass: 'success',
                errorPlacement: function (error, element) {
                    error.addClass('w-100').appendTo(element.parents('.form-group'));
                },
                submitHandler: function (form) {
                    $('#shipped_status_form input.card_ids').val(card_ids);
                    $.ajax({
                    url: '<?php echo route('shipped.check_missing_cards'); ?>',
                    method: 'POST',
                    data: {
                        'card_ids': $('#shipped_status_form input.card_ids').val(),
                        'check_batch': check_batch,
                        '_token': '<?php echo e(csrf_token()); ?>'
                    },
                    timeout: 5000,
                    error: function (data) {
                        toastr.error('Couldn\'t connect to server, check internet connection and re-enter!', 'Error!', {positionClass: 'toast-top-center', containerId: 'toast-top-center'});
                    },
                    success: function (data) {
                            var tracking_numbers = '';
                        if (data.status == 0) {
                            $.each(data.missing_cards, function( key, value ) {
                                console.log(value.tracking_number);
                                tracking_numbers+= value.tracking_number+'\n';
                            });
                             swal({
                                title: 'Missing Tracking Numbers',
                        text: tracking_numbers,
                        icon: 'warning',
                        buttons: {
                            cancel: {
                                text: 'No',
                                value: null,
                                visible: true,
                                closeModal: true,
                            },
                            confirm: {
                                text: 'Yes',
                                value: true,
                                visible: true,
                                closeModal: true
                            }
                        },
                        closeOnClickOutside: false,
                        closeOnEsc: false,
                        dangerMode: true
                    }).then(function(confirm) {
                        if (confirm) {
                            swal({
                                title: 'Please Wait!',
                                text: 'Cards are being marked Shipped!',
                                icon: 'info',
                                buttons: false,
                                closeOnClickOutside: false,
                                closeOnEsc: false
                            });
                            blockPagePermanently();
                            form.submit();
                        }
                    });
                        }
                        else {
                            swal({
                        text: 'Are you sure you want to proceed these cards',
                        icon: 'warning',
                        buttons: {
                            cancel: {
                                text: 'No',
                                value: null,
                                visible: true,
                                closeModal: true,
                            },
                            confirm: {
                                text: 'Yes',
                                value: true,
                                visible: true,
                                closeModal: true
                            }
                        },
                        closeOnClickOutside: false,
                        closeOnEsc: false,
                        dangerMode: true
                    }).then(function(confirm) {
                        if (confirm) {
                            swal({
                                title: 'Please Wait!',
                                text: 'Cards are being marked Shipped!',
                                icon: 'info',
                                buttons: false,
                                closeOnClickOutside: false,
                                closeOnEsc: false
                            });
                            blockPagePermanently();
                            form.submit();
                        }
                    });

                        }
                    }
                });
                   
                }
            });


            $('#datatable tbody').on('click', 'tr td.remove button', function() {
                var parent = $(this).parents('tr');
                var id = parseInt(parent.attr('id'));

                $.ajax({
                    url: '<?php echo route('shipped.card_remove'); ?>',
                    method: 'POST',
                    data: {
                        'id': id,
                        '_token': '<?php echo e(csrf_token()); ?>'
                    },
                    timeout: 5000,
                    error: function (data) {
                        toastr.error('Couldn\'t connect to server, check internet connection and re-enter!', 'Error!', {positionClass: 'toast-top-center', containerId: 'toast-top-center'});
                    },
                    success: function (data) {
                        if (data.status == 0) {
                            table.row(parent).remove();
                            table.draw(false);

                            var index = $.inArray(id, card_ids);

                            if (index !== -1) {
                                card_ids.splice(index, 1);
                                card_ids2=card_ids;
                                if (card_ids.length == 0) {
                                    $('#shipped_status_form button.confirm').prop('disabled', true);
                                }
                            }

                            toastr.success(data.success, 'Success!', {positionClass: 'toast-bottom-center', containerId: 'toast-bottom-center'});
                        }
                        else {
                            toastr.error(data.error, 'Error!', {positionClass: 'toast-top-center', containerId: 'toast-top-center'});
                        }
                    }
                });
            });
            
           
        });
  
     
        
     
    </script>

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

<?php echo $__env->make('layout.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\laragon\www\catalyst\resources\views/shipped/index.blade.php ENDPATH**/ ?>