    <?php echo $header; ?><?php echo $column_left; ?>
<style>
    .lodaer_class{position:fixed;display:none;top:0;right:0;width:100%;height:100%;background-color:#FFF;background-repeat:no-repeat;background-position:center;z-index:10000000;filter: alpha(opacity=40);opacity:.8;transition:all .3s ease-out 0s}.loadinggif

    {position:fixed;top:50%;left:50%;z-index:101;width:110px;height:110px;transform:translate(-50%,-50%)}
    .table thead > tr > td, .table tbody > tr > td {min-width: 5px!important;} 
.btn {padding: 6px 6px;font-size: 11px;}
.table > thead > tr > th, .table > tbody > tr > th, .table > tfoot > tr > th, .table > thead > tr > td, .table > tbody > tr > td, .table > tfoot > tr > td {padding: 5px;}


</style>
<div id="img_loader" class="lodaer_class">
    <img src="<?= $catalog." image/loader.gif"?>" class="loadinggif" >
</div>
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
            <div class="pull-right">
                <a href="<?=$all_products ?>" class="btn btn-default"> Tüm ürünler</a>
                <a href="<?=$filter_by_personalized?>" class="btn btn-info"><i class="fa fa-filter"></i>
                    Kişiselleştirilmiş Ürünleri Filtrele</a>
                <a href="<?=$filter_by_reprinted?>" class="btn btn-info"><i class="fa fa-filter"></i> Yeniden Basılacak
                    Ürünleri Filtrele</a>
                <a href="javascript: hideSelected ();" class="btn btn-warning"><i class="fa fa-trash-o"></i> Seçilenleri
                    Sil</a>
                <a href="javascript: hideCompleted ();" class="btn btn-danger"><i class="fa fa-trash-o"></i>
                    Tamamlananları Sil</a>
            </div>
            <h1><?php echo $heading_title; ?></h1>
            <ul class="breadcrumb">
                <?php foreach ($breadcrumbs as $breadcrumb) { ?>
                <li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
                <?php } ?>
            </ul>
        </div>
    </div>
    <div class="container-fluid">
        <?php if ($error_warning) { ?>
        <div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
            <button type="button" class="close" data-dismiss="alert">&times;</button>
        </div>
        <?php } ?>
        <?php if ($success) { ?>
        <div class="alert alert-success"><i class="fa fa-check-circle"></i> <?php echo $success; ?>
            <button type="button" class="close" data-dismiss="alert">&times;</button>
        </div>
        <?php } ?>
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title"><i class="fa fa-list"></i> <?php echo $text_list; ?></h3>
            </div>
            <div class="panel-body">
                <div class="row">
                    <div class="col-sm-12">
                        <a class="btn btn-primary" form="form-order" formaction="<?php echo $bulkdownload; ?>"
                           formtarget="_blank" id="button-bulk-download"><i class="fa fa-cogs" aria-hidden="true"></i>
                            Seçtiklerini İndir</a> &nbsp;&nbsp;&nbsp;
                        Toplu Baskı Görseli İndirme <i class="fa fa-hand-o-right" aria-hidden="true"></i>
                        <div class="btn-group">
                            <a class="btn btn-default btn_print" onclick="downloadCollection('al')"><i
                                        class="fa fa-etsy" aria-hidden="true"></i> Alfabe</a>
                            <a class="btn btn-default btn_print" onclick="downloadCollection('ism')"><i
                                        class="fa fa-pencil" aria-hidden="true"></i> İsmini Yaz</a>
                            <a class="btn btn-default btn_print" onclick="downloadCollection('ps')"><i
                                        class="fa fa-circle" aria-hidden="true"></i> Popsoket</a>
                            <a class="btn btn-default btn_print" onclick="downloadCollection('DeerCustom')"><i
                                        class="fa fa-picture-o" aria-hidden="true"></i> DeerCustom</a>
                        </div>
                        <style type="text/css">
                            .btn_print

                            {margin: 0px 10px;}

                        </style>
                    </div>
                </div>
                <input type="hidden" id="pageno" name="pageno" value=""/>

                <form method="post" action="" enctype="multipart/form-data" id="form-order">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover" id="order_tb">
                            <thead>
                            <tr>
                                <td style="width: 1px;" class="text-center"><input type="checkbox"
                                                                                   onclick="$('input[name*=\'selected_order_ids\']').prop('checked', this.checked);"/>
                                </td>
                                <td class="text-left">
                                    Sipariş Durumu
                                </td>
                                <td class="text-right">
                                    <?php echo $column_product_model; ?>
                                </td>
                                <td>Oda</td>
                                <td class="text-left">
                                    Resim
                                </td>
                                <td class="text-left">
                                    <i class="fa fa-cloud-download" aria-hidden="true"></i>
                                </td>
                                <td class="text-left">
                                    Yazı
                                </td>
                                <td class="text-left"><?= $column_product_name; ?></td>
                                <td class="text-left">İade/İptal/Değişim</td>

                                <td>Atıl Stok</td>
                                <td class="text-left">
                                    <?= $column_comment; ?>
                                </td>
                                <td class="text-left">
                                    <?php echo $column_customer; ?>
                                </td>
                                <td class="text-right">
                                    <?php echo $column_order_id; ?>
                                </td>
                                <td class="text-left">
                                    <?php echo $column_date_added; ?>
                                </td>
                                <td class="text-left">
                                    <?php echo $column_status; ?>
                                </td>
                                <td>İşlem -</td>
                                <td>Tekrar Basılacak</td>
                            </tr>
                            </thead>
                            <tbody>


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

            </div>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>
    <script type="text/javascript"><!--
        var productIdleStockShown = {};
        var productIdleStock = {};

        function allGenerate() {
            $('.generate_button').each(function (index, item) {
                setTimeout(function () {
                    $(item).trigger('click');
                }, 1000 + (index * 1000));
            });
            alert("Sistemin Görselleri Hazırlaması Zaman Alacaktır. Lütfen Toplu Baskı Görseli İndirme İşlemini 15 Dakika Sonra Gerçekleştiriniz.");
        }

        function downloadCollection(model) {
            $.ajax({
                url: '/ps-panel/index.php?route=sale/prepared_orders/downloadGeneratedImages&token=<?= $token; ?>',
                type: 'post',
                dataType: 'json',
                data: {model: model},
                complete: function (json) {
                    window.location.href = json.responseJSON.link.toString();
                }
            });
        }

        function deleteGenerateImage(product_id) {

            if (confirm("Silmek istediğinizden Emin misiniz?")) {
                $.ajax({
                    url: '<?php echo HTTPS_CATALOG ?>index.php?route=piyerSoft/cases/deleteGenerateImage',
                    type: 'post',
                    dataType: 'json',
                    data: {order_product_id: product_id},
                    success: function (json) {
                        if (json['success'])
                            location.reload();
                        else
                            alert(json['error']);
                    }
                });
            }


        }

        function GenerateImage(pattern_code, id, order_id, product_type, product_id) {
            //alert(product_type);
            var customer_pattern_data = $('#customer_pattern_data_' + id).val();

            if (product_type == 4) {
                var url = '<?php echo HTTPS_CATALOG ?>index.php?route=piyerSoft/cases/GenerateDpiImageType4';
            } else {
                var url = '<?php echo HTTPS_CATALOG ?>index.php?route=piyerSoft/cases/GenerateDpiImage';
            }


            $("#btn_generate_" + id).hide();
            $("#loading_icon_" + id).show();
            $.ajax({
                url: url,
                type: 'post',
                dataType: 'json',
                data: {order_product_id: id, order_id: order_id, pattern_code: pattern_code, customer_pattern_data: customer_pattern_data, product_id: product_id},

                success: function (res) {

                    if (res.responce == 'success') {
                        $("#loading_icon_" + id).hide();
                        $("#img_download_" + id).attr("href", '<?php echo HTTPS_CATALOG ?>image/' + res.pattern_img);
                        $("#img_download_" + id).show();
                        if (res.gradient_img != '') {
                            $("#gradient_img_download_" + id).attr("href", '<?php echo HTTPS_CATALOG ?>image/' + res.gradient_img);
                            $("#gradient_img_download_" + id).show();
                        }
                    } else {
                        alert(res.responce);
                        $("#btn_generate_" + id).show();
                        $("#loading_icon_" + id).hide();
                    }

                }
            });

        }


        $('#button-filter').on('click', function () {
            url = 'index.php?route=sale/order&token=<?php echo $token; ?>';

            var filter_order_id = $('input[name=\'filter_order_id\']').val();

            if (filter_order_id) {
                url += '&filter_order_id=' + encodeURIComponent(filter_order_id);
            }

            var filter_email = $('input[name=\'filter_email\']').val();

            if (filter_email) {
                url += '&filter_email=' + encodeURIComponent(filter_email);
            }

            var filter_telephone = $('input[name=\'filter_telephone\']').val();

            if (filter_telephone) {
                url += '&filter_telephone=' + encodeURIComponent(filter_telephone);
            }

            var filter_customer = $('input[name=\'filter_customer\']').val();

            if (filter_customer) {
                url += '&filter_customer=' + encodeURIComponent(filter_customer);
            }

            var filter_order_status = $('select[name=\'filter_order_status\']').val();

            if (filter_order_status != '*') {
                url += '&filter_order_status=' + encodeURIComponent(filter_order_status);
            }

            var filter_order_zone = $('select[name=\'filter_order_zone\']').val();

            if (filter_order_zone != '*') {
                url += '&filter_order_zone=' + encodeURIComponent(filter_order_zone);
            }

            var filter_order_payment = $('select[name=\'filter_order_payment\']').val();

            if (filter_order_payment != '*') {
                url += '&filter_order_payment=' + encodeURIComponent(filter_order_payment);
            }

            var filter_total = $('input[name=\'filter_total\']').val();

            if (filter_total) {
                url += '&filter_total=' + encodeURIComponent(filter_total);
            }

            var filter_date_added = $('input[name=\'filter_date_added\']').val();

            if (filter_date_added) {
                url += '&filter_date_added=' + encodeURIComponent(filter_date_added);
            }

            var filter_date_modified = $('input[name=\'filter_date_modified\']').val();

            if (filter_date_modified) {
                url += '&filter_date_modified=' + encodeURIComponent(filter_date_modified);
            }

            location = url;
        });
        //--></script>
    <script type="text/javascript"><!--
        $('input[name=\'filter_customer\']').autocomplete({
            'source': function (request, response) {
                $.ajax({
                    url: 'index.php?route=customer/customer/autocomplete&token=<?php echo $token; ?>&filter_name=' + encodeURIComponent(request),
                    dataType: 'json',
                    success: function (json) {
                        response($.map(json, function (item) {
                            return {
                                label: item['name'],
                                value: item['customer_id']
                            }
                        }));
                    }
                });
            },
            'select': function (item) {
                $('input[name=\'filter_customer\']').val(item['label']);
            }
        });
        //--></script>
    <script type="text/javascript"><!--
        $('input[name^=\'selected\']').on('change', function () {
            $('#button-shipping, #button-invoice').prop('disabled', true);

            var selected = $('input[name^=\'selected\']:checked');

            if (selected.length) {
                $('#button-invoice').prop('disabled', false);
            }

            for (i = 0; i < selected.length; i++) {
                if ($(selected[i]).parent().find('input[name^=\'shipping_code\']').val()) {
                    $('#button-shipping').prop('disabled', false);

                    break;
                }
            }
        });


        $('#button-delete').on('click', function (e) {
            $('#form-order').attr('action', this.getAttribute('formAction'));

            if (confirm('<?php echo $text_confirm; ?>')) {
                $('#form-order').submit();
            } else {
                return false;
            }
        });
        //--></script>

    <style>
        .bg-warning {background-color: #f39c12 !important;} .bg-success

        {background-color: #27ae60 !important; }
    </style>
    <script>
        var hideCompleted = function () {
            $.ajax({
                url: '/ps-panel/index.php?route=sale/prepared_orders/hideCompleted&token=<?= $token; ?>',
                method: 'GET',
                success: function () {
                    $('.Order.bg-success').remove();
                    alert("Temizlendi!");
                }
            })
        };

        var hideSelected = function () {
            var OrderIDs = [];

            $('[name="selected_order_ids[]"]:checked').each(function () {
                OrderIDs.push($(this).val());
            });

            if (0 < OrderIDs.length) {
                $.ajax({
                    url: '/ps-panel/index.php?route=sale/prepared_orders/hideSelected&token=<?= $token; ?>',
                    method: 'GET',
                    data: {OrderIDs: OrderIDs},
                    success: function (ssss) {
                        $('[name="selected_order_ids[]"]:checked').each(function () {
                            $(this).closest('tr.Order').remove();
                        });
                        alert("Temizlendi!");
                    }
                })
            } else window.alert("Lütfen en az 1 ürün seçin.");

        };

        function add_tr_class() {
            var CheckTRs = [];
            $('.Order').each(function (Index, Item) {
                if (CheckTRs.indexOf($(Item).attr('data-orderid')) == -1) {
                    if ($('[data-orderid="' + $(Item).attr('data-orderid') + '"][data-status="1"]').length == $('[data-orderid="' + $(Item).attr('data-orderid') + '"]').length) $('[data-orderid="' + $(Item).attr('data-orderid') + '"][data-status="1"]').removeClass('bg-warning').addClass('bg-success');
                    else $('[data-orderid="' + $(Item).attr('data-orderid') + '"][data-status="1"]').removeClass('bg-success').addClass('bg-warning');
                    CheckTRs.push($(Item).attr('data-orderid'));
                }
            });
        }

        jQuery(function ($) {

            $('body').on('change', '[name="selected[]"]', function () {
                var SelectedElement = $(this);
                var SelectedElementTR = SelectedElement.closest('tr');

                var order_id = $(this).attr('data-order-id');
                //alert(order_id);


                $.ajax({
                    url: '/ps-panel/index.php?route=sale/prepared_orders/toggleStatus&token=<?= $token; ?>',
                    method: 'POST',
                    data: {order_product_id: SelectedElement.val ()},
                    success: function (jSON) {
                        if (SelectedElementTR.attr('data-status') == "0") {
                            SelectedElementTR.attr('data-status', "1");
                            if ($('[data-orderid="' + SelectedElementTR.attr('data-orderid') + '"][data-status="1"]').length == $('[data-orderid="' + SelectedElementTR.attr('data-orderid') + '"]').length) {
                                $('[data-orderid="' + SelectedElementTR.attr('data-orderid') + '"][data-status="1"]').removeClass('bg-warning').addClass('bg-success');
                                addHistroy(order_id);
                            } else {
                                SelectedElementTR.addClass('bg-warning');
                            }
                        } else {
                            SelectedElementTR.attr('data-status', "0");
                            $('[data-orderid="' + SelectedElementTR.attr('data-orderid') + '"][data-status="1"]').removeClass('bg-success').addClass('bg-warning');
                            SelectedElementTR.removeClass('bg-success bg-warning');
                        }
                    }
                })
            });

            // 18 Ağustos 2020 - 
            $('body').on('change', '[name="reprinted[]"]', function () {
                var OrderProductID = $(this).attr('data-order-product-id');
                $.ajax({
                    url: '/ps-panel/index.php?route=sale/prepared_orders/toggleReprint&token=<?= $token; ?>',
                    method: 'POST',
                    data: {order_product_id: OrderProductID},
                    success: function () {}
                });
                $(this).siblings('b').toggle();
            });
            // 18 Ağustos 2020 
        });
    </script>

    <?php if($yurticikargo_status){ ?>
    <script>

        function printBox(kargo_barcode, location, order_id) {

            //var html = "<a href=\"javascript:;\" onclick=\"window.open ('"+json['kargo_barcode'] + "', '_blank', 'location=yes,height=570,width=520,scrollbars=yes,status=yes');\" data-value=\'pr_"+ order_id +"\' class=\"btn button-print\" data-toggle=\"tooltip\" title=\"Kargo Barkodu Bastır\" > <i style=\"font-size:40px;\" class=\"fa fa-print\"></i></a><br>\n";

            window.open(kargo_barcode, '_blank', location)
            var count = $('[data-value="pr_' + order_id + '"]').next("b").text() + 1;
            $('[data-value="pr_' + order_id + '"]').after(count + " kere yadırılmıştır!")

        }


        $('body').on('click', '.button-shipping', function () {

            var parent = $(this).parents('tr');
            var order_id = $(this).attr('data-value');
            var firma = '<?= $kargo_firma; ?>';

            $.ajax({
                url: 'index.php?route=sale/order/kargola&firma=' + firma + '&order_id=' + order_id + '&token=<?=$token?>',
                dataType: 'json',
                type: 'POST',
                data: {},
                success: function (json) {
                    if (json['status'] == 1) {
                        var html = "<a onclick=\"printBox ('" + json['kargo_barcode'] + "','location=yes,height=570,width=520,scrollbars=yes,status=yes', '" + order_id + "');\" data-value=\'pr_" + order_id + "\' class=\"btn button-print\" data-toggle=\"tooltip\" title=\"Kargo Barkodu Bastır\" > <i style=\"font-size:40px;\" class=\"fa fa-print\"></i></a><br>\n";
                        $('body [data-value="' + order_id + '"]').after(html);
                        $('[data-value="' + order_id + '"]').hide();
                        //addHistroy(order_id);

                    } else {

                        $('body [data-value="' + order_id + '"]').after("Başarısız");
                    }
                }, beforeSend: function () {
                    $('[data-value="' + order_id + '"]').html("<b>İşlem Yapılıyor..</b>");
                }
            });


        });

        function addHistroy(order_id) {

            var token = '';

            // Login to the API
            $.ajax({
                url: '<?php echo $catalog; ?>index.php?route=api/login',
                type: 'post',
                dataType: 'json',
                data: 'key=<?php echo $api_key; ?>',
                crossDomain: true,
                success: function (json) {

                    console.log(json);

                    if (json['token']) {
                        token = json['token'];

                        $.ajax({
                            url: '<?php echo $catalog; ?>index.php?route=api/order/history&token=' + token + '&store_id=0&order_id=' + order_id,
                            type: 'post',
                            dataType: 'json',
                            data: 'order_status_id=' + '<?=$order_status_id;?>' + '&notify=1&override=1&append=1&comment=' + encodeURIComponent("Kargo Kaydı Açıldı"),
                            success: function (json) {
                                console.log(json);
                            },
                            error: function (xhr, ajaxOptions, thrownError) {
                                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                            }
                        });

                    }

                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            });


        }
    </script>
    <?php } ?>


    <?php echo $footer; ?>

    <script>
        (function ($) {

            $.fn.loadScroll = function (duration) {

                var $window = $(window),
                    images = this,
                    inview,
                    loaded;

                images.one('loadScroll', function () {

                    if (this.getAttribute('data-src')) {
                        this.setAttribute('src',
                            this.getAttribute('data-src'));
                        this.removeAttribute('data-src');

                        if (duration) {

                            $(this).hide()
                                .fadeIn(duration)
                                .add('img')
                                .removeAttr('style');

                        } else return false;
                    }

                });

                $window.scroll(function () {

                    inview = images.filter(function () {

                        var a = $window.scrollTop(),
                            b = $window.height(),
                            c = $(this).offset().top,
                            d = $(this).height();

                        return c + d >= a && c <= a + b;

                    });

                    loaded = inview.trigger('loadScroll');
                    images = images.not(loaded);

                });
            };

        })(jQuery);
    </script>

    <script>
        $(function () {
            // Custom fadeIn Duration
            $('img').loadScroll(500);

        });
    </script>
    <script>
        $('body').on('click', '#button-bulk-download', function () {

            $('#form-order').attr('action', this.getAttribute('formAction'));
            $('#form-order').submit();

            //if (confirm('Excel dosyasını onaylıyor musunuz?')) {
//	} else {
            //	return false;
            //}
        });
    </script>
    <script>
        var processing = false;

        function ajax_orders() {

            var windowScrollTop = $(window).scrollTop();
            var windowHeight = $(window).height();
            var documentHeight = $(document).height();
            var threshold = -1250;
            var lastorder = $('#order_tb tr').last();
            var lastOrderOffsetTop = lastorder.offset().top + threshold;

            if (windowScrollTop > lastOrderOffsetTop || (documentHeight - windowHeight) == windowScrollTop) {

                if (!processing) {

                    processing = true;

                    if ($('#pageno').val() == '') {
                        var page = 1;
                    } else {
                        var page = parseInt($('#pageno').val()) + 1;
                    }

                    var filter_by = "<?= (isset($_GET['filter_by']) ? $_GET['filter_by'] : '') ?>";

                    $.ajax({
                        url: "index.php?route=sale/prepared_orders/getajaxOrders&token=<?= $token; ?>&page=" + page + (filter_by != '' ? "&filter_by=" + filter_by : ''),
                        type: "GET",
                        dataType: 'json',
                        beforeSend: function () {
                            $('#img_loader').show();
                        },
                        success: function (json) {

                            if (json['orders']) {

                                if ($('#order_tb tr:last').length) {
                                    $('#order_tb tr:last').after(json['orders_data']);
                                } else {
                                    $('#order_tb tbody').html(json['orders_data']);
                                }

                                $('#pageno').val(json['pageno']);
                                add_tr_class();
                            } else {
                                $('#dcloading-icon').remove();
                                var str = '<tr id="dcloading-icon"><td colspan="20" class="text-center" >No Result Found</td></tr>';
                                $('#order_tb tr:last').after(str);
                            }

                            $('img').loadScroll(500);
                            processing = false;
                            $('#img_loader').hide();

                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            $('#img_loader').hide();
                            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                        }

                    });
                }
            }
        }

        $(window).on('scroll', function () {
            ajax_orders();
        });

        $(function () {
            ajax_orders();
        });

        $('.reprint_reason').on('change', function () {
            var reprint_reason_id = $(this).val();
            alert(reprint_reason_id);
        });

        function changeReprintReason(order_product_id) {
            var reprint_reason_id = $(".reprint_reason" + order_product_id).val();

            if (order_product_id) {
                $.ajax({
                    url: "index.php?route=sale/prepared_orders/editReprintReason&token=<?= $token; ?>&reprint_reason_id=" + reprint_reason_id + "&order_product_id=" + order_product_id,
                    type: "GET",
                    dataType: 'json',
                    beforeSend: function () {
                    },
                    success: function (json) {
                    }
                });
                if ($(".reprint_reason" + order_product_id).val() != "0") $(".reprint_reason" + order_product_id).siblings("div").show ();
                else $(".reprint_reason" + order_product_id).siblings("div").hide ();
            }
        }

        function changeRoomNumber(order_product_id) {
            var room_no = $(".room_no" + order_product_id).val();

            if (order_product_id) {
                $.ajax({
                    url: "index.php?route=sale/prepared_orders/editRoomNumber&token=<?= $token; ?>&room_no=" + room_no + "&order_product_id=" + order_product_id,
                    type: "GET",
                    dataType: 'json',
                    beforeSend: function () {
                    },
                    success: function (json) {
                    }
                });
            }
        }

        //22.09.20 ajax decrease product idle stock (ali.a)
        function decreaseIdleStock(productId, orderId) {
            if (productId) {
                $.ajax({
                    url: "index.php?route=sale/prepared_orders/decreaseProductIdleStock&token=<?=$token?>&productId=" + productId,
                    type: "GET",
                    success: function (result) {
                        let r = JSON.parse(result);
                        Swal.fire({
                            icon: 'success',
                            title: 'Atıl stok bir azaltıldı',
                            confirmButtonText: 'Tamam',
                        });

                        /* $("#decrease_product_idle_stock_button_container_" + productId + '_' + orderId).remove(); */

                        if (r.idleStock == 0) {
                            $("#decrease_product_idle_stock_button_container_" + productId + '_' + orderId).remove();
                        }
                    }
                });
            }
        }
    </script>
