<?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 {
    max-width: 100px!important;
    vertical-align: middle;
    }
</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>
                <div class="row">
                    <div class="col-sm-6 text-left"><?php echo $pagination; ?></div>
                    <div class="col-sm-6 text-right">
                        <b style="color: red;">Sayfada Gösterilen Ürün Sayısı: <?=$order_total_product;?></b><br>
                        <b style="color: red;">Toplam Sipariş Sayısı: <?=$order_total;?></b><br>

                        <b style="color: red;" class="complated_count"></b><br>

                    </div>
                </div>
                <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>Oda</td>
                                <td class="text-left">
                                    Resim
                                </td>
                                <td class="text-left">
                                    Generate
                                </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-left">
                                    <?php echo $column_status; ?>
                                </td>
                                <td>İşlem</td>
                                <td>İşlem</td>
                                <td>İşlem</td>
                                <td>Tekrar Basılacak</td>

                            </tr>
                            </thead>
                            <tbody>
                            <?php if ($products) { ?>
                            <?php foreach ($products as $product) { ?>


                            <tr class="Order" data-orderid="<?= $product ['order_id']; ?>"
                                data-totalcustomerorders="<?= $product ['total_customer_orders']; ?>"
                                data-status="<?= $product ['status']; ?>">
                                <td class="text-center"><input type="checkbox" name="selected_order_ids[]"
                                                               value="<?php echo $product['order_id']; ?>"/> </td>
                                <td class="text-left">
                                    <? if ($product ['order_status'] == "İptal Edildi" OR $product ['order_status'] == "İade Edildi"): ?>
                                    <span class="label label-danger"
                                          style="font-size: 18px;"><?= $product ['order_status']; ?></span>
                                    <? else: ?>
                                    <?= $product ['order_status']; ?>
                                    <? endif; ?>
                                </td>
                                <td>
                                    <select class="room_no<?= $product['order_product_id']; ?>" onchange="changeRoomNumber(<?= $product['order_product_id']; ?>);">
                                        <option value="0" <?= $product['room_no'] == 0? 'selected': ''; ?>> Seçiniz </option>
                                        <?php for($i = 1; $i <= 60; $i++){ ?>
                                        <?php if($i == $product['room_no']){ ?>
                                        <option value="<?=$i;?>" selected> <?=$i;?></option>
                                        <?php }else{ ?>
                                        <option value="<?=$i;?>"> <?=$i;?> </option>
                                        <?php } ?>
                                        <?php } ?>
                                    </select>
                                </td>
                                <td class="text-center">
                                    <?php if ($product['customcase_image'] != '') { ?>
                                    <img width="50" height="80" data-src="<?php echo $product['case_image']; ?>"
                                         src="<?=HTTP_CATALOG?>catalog/view/theme/deercase/image/placeholder250.png"
                                         alt="Frame" style="position:absolute;">
                                    <img width="49" height="80" data-src="<?php echo $product['customcase_image']; ?>"
                                         src="<?=HTTP_CATALOG?>catalog/view/theme/deercase/image/placeholder250.png"
                                         alt="">
                                    <?php }elseif ($product['case_order_image'] != '') { ?>
                                    <img data-src="<?php echo $product['case_order_image']; ?>" width="70"
                                         src="<?=HTTP_CATALOG?>catalog/view/theme/deercase/image/placeholder250.png"/></a>
                                    <?php } else { ?>
                                    -
                                    <?php  } ?>

                                </td>
                                <td class="text-left <?php if($product['reprint_download_count'] > 0) { ?>red-frame <?php } ?>">
                                    <?php echo $product['product_model']; ?> <br>
                                    <?php if ($product['customer_pattern_data'] == '') { ?>
                                    <?php if ($product['customcase_image'] != '') {
                                    
                                     $pathinfo = pathinfo(str_replace('cropped','',$product['customcase_image']));
                                        if(isset($product['model2']) && $product['model2']!=''){
                                        
                                       
                                        $file_name=$product['model2'].'-'.$product['order_id'].'.'.$pathinfo['extension'];
                                        
                                        }else{
                                        
                                        $file_name=$pathinfo['filename'].'-'.$product['order_id'].'.'.$pathinfo['extension'];
                                        
                                        }
                                    
                                    ?>
                                    <a download="<?=$file_name?>"
                                       href="<?php echo str_replace('cropped','',$product['customcase_image']) ?>"
                                       style="cursor:pointer;<?php if($product['reprint_download_count'] > 0) { ?> color:red; <?php } ?>"
                                       onclick="IncreaseReprintDownloadCount(<?= $product['order_product_id']; ?>)"><i class="fa fa-cloud-download" aria-hidden="true"></i>
                                        Customcase Download</a>
                                    <?php } else { ?>
                                    -
                                    <?php   } ?>
                                    <?php }else if($product['product_type']==14){  ?>
                                    <a download href="<?php echo $product['case_order_image'] ?>"
                                       style="cursor:pointer;<?php if($product['reprint_download_count'] > 0) { ?> color:red; <?php } ?>" 
                                       onclick="IncreaseReprintDownloadCount(<?= $product['order_product_id']; ?>)"><i class="fa fa-cloud-download" aria-hidden="true"></i> 4
                                        Download</a>
                                    <?php } else { if ($product['admin_pattern_image'] == '') { ?>
                                    <a id="btn_generate_<?php echo $product['order_product_id'] ?>"
                                       class="generate_button" style="cursor:pointer;"
                                       onclick="GenerateImage('<?php echo $product['model'] ?>',<?php echo $product['order_product_id'] ?>, <?php echo $product['order_id'] ?>,<?=$product['product_type'];?>, <?=$product['product_id'];?> )">Generate</a>
                                    <?php } ?>

                                    <input type="hidden"
                                           id="customer_pattern_data_<?php echo $product['order_product_id'] ?>"
                                           value="<?php echo @htmlspecialchars($product['customer_pattern_data']) ?>">
                                    <a download href="<?php echo $product['admin_pattern_image'] ?>"
                                       style="cursor:pointer;<?php if($product['reprint_download_count'] > 0) { ?> color:red; <?php } ?>display:<?php echo ($product['admin_pattern_image'] == '' ? 'none' : '') ?>"
                                       id="img_download_<?php echo $product['order_product_id'] ?>" onclick="IncreaseReprintDownloadCount(<?= $product['order_product_id']; ?>)"><i
                                                class="fa fa-cloud-download" aria-hidden="true"></i> Download</a>

                                    <?php if($product['admin_gradient_pattern_image']){ ?>
                                    <a download href="<?php echo $product['admin_gradient_pattern_image'] ?>"
                                       style=" margin-top: 5px; cursor:pointer;display:<?php echo ($product['admin_gradient_pattern_image'] == '' ? 'none' : '') ?>"
                                       id="gradient_img_download_<?php echo $product['order_product_id'] ?>">Gradient
                                        Download</a>
                                    <?php } ?>
                                    <?php if ($product['customer_pattern_data'] != '') { ?>
                                    <a onclick="deleteGenerateImage(<?= $product['order_product_id']; ?>)">Delete</a>
                                    <?php } ?>
                                    <?php if($product['reprint_download_count'] > 0) { ?><b style="color:red;display:block;">(Daha önce indirildi)</b><?php } ?>
                                    <i id="loading_icon_<?php echo $product['order_product_id'] ?>" style="display:none"
                                       class="fa fa-spinner" aria-hidden="true"> Loading</i>

                                    <?php  }  ?>
                                </td>
                                <td class="text-left"><?= $product['case_text']; ?></td>
                                <td class="text-left">
                                    <?= $product['name']; ?> <br>
                                    <?php if($product ['options']){ ?>
                                    <?php foreach($product ['options'] as $option){ ?>
                                    <b style="color: red;">(<?=$option['value'];?>)</b>
                                    <?php } ?>
                                    <?php } ?>
                                    <br>
                                    <?php if($product['product_type']==3){ ?>
                                    <b style="color: red; font-size: 20px">Tutacaklı</b>
                                    <?php } ?>
                                </td>
                                <td class="text-left">
                                    <?php if($product['return_status'] == 1){ ?>
                                    <b style="color: red">İade</b>
                                    <?php }else if($product['return_status'] == 2){ ?>
                                    <b style="color: red">İptal</b>
                                    <?php }else if($product['return_status'] == 3){ ?>
                                    <b style="color: red">Değişim</b>
                                    <?php } ?>
                                </td>


                                <td class="text-center">

                                    <?php
                                    $idle_stock_text = '';
                                    if (isset($product_idle_stock[$product['product_id']]) && $product_idle_stock[$product['product_id']] > 0
                                        && $product['case_text']=='' && $product['customcase_image']=='') {

                                        $product_idle_stock[$product['product_id']]--;
                                        $idle_stock_text = 'Atıl Stok Mevcut';
                                    }

                                    if ($product['show_idle_stock'] == 1) {
                                        $idle_stock_text = 'Atıl Stok Mevcut';
                                    }
                                    ?>

                                    <span id="decrease_product_idle_stock_button_container_<?=$product['product_id']?>_<?=$product['order_id']?>">
                                        <!--<a href="javascript:" class="btn btn-primary"
                                           onclick="decreaseIdleStock(<?=$product['product_id']?>,<?=$product['order_id']?>)">
                                            Stok Aldım
                                        </a><br>-->
                                        <b style="color:red" id="idle-stock-text-<?=$product['product_id']?>"><?=$idle_stock_text?></b>
                                    </span>
                                    <div>
                                        <a class="btn btn-default" id="idle-stock-button-<?=$product['product_id']?>_<?=$product['order_id']?>" onclick="increaseIdleStock(<?=$product['product_id']?>,<?=$product['order_id']?>)" style="margin-top:1rem;">Atıl Stok Ekle</a>
                                    </div>
                                </td>

                                <td class="text-left"><?= $product ['comment']; ?></td>
                                <td class="text-left"><?php echo $product['customer']; ?> - <a href="<?=$product['order_href'];?>"
                                                          target="_blank"><?php echo $product['order_id']; ?></a> - <?php echo $product['date_added']; ?></td>
                                
                                <td class="text-center"><?php if ($product ['status']) { ?>
                                    <input type="checkbox" name="selected[]"
                                           value="<?php echo $product['order_product_id']; ?>"
                                           data-order-id="<?= $product['order_id']; ?>" checked="checked"/>
                                    <?php } else { ?>
                                    <input type="checkbox" name="selected[]"
                                           value="<?php echo $product['order_product_id']; ?>"
                                           data-order-id="<?= $product['order_id']; ?>"/>
                                    <?php } ?>
                                    <input type="hidden" name="shipping_code[]"
                                           value="<?php echo $product['shipping_code']; ?>"/></td>

                                <td>
                                    <?php if ($product['order_status'] != "İptal Edildi" && $product['order_status'] != "İade Edildi") { ?>

                                    <?php if(!$product['kargo_talepno']){ ?>
                                    <a style="max-width: 100px;" class="button-shipping btn btn-info" data-toggle="tooltip"
                                       data-value="<?=$product['order_id'];?>" title="Kargoya Gönder"> <i
                                                class="fa fa-truck"></i> Kargoya Gönder </a>
                                    <?php }else{ ?>
                                    <a href="javascript:;"
                                       onclick="window.open ('<?=$product['kargo_barcode'];?>', '_blank', 'location=yes,height=570,width=520,scrollbars=yes,status=yes');"
                                       class="btn" data-toggle="tooltip" title="Kargo Barkodu Bastır"> <i
                                                style="font-size:40px;" class="fa fa-print"></i></a> <br>
                                    <a title="Kargo Barkodu Bastır" href="<?=$product['kargo_barcode'];?>" target="_blank"> Hızlı Barcode</a>
                                    <br>
                                    <?php if($product['kargo_talepno']['barcode_print_count']){ ?>
                                    <b><?=$product['kargo_talepno']['barcode_print_count']; ?></b> kere yazdırılmıştır.
                                    <?php } ?>
                                    <?php } ?>

                                    <?php } ?>

                                </td>

                                <td>

                                    <?php if ($product['order_status'] != "İptal Edildi" && $product['order_status'] != "İade Edildi") { ?>

                                    <?php if(!$product['byexpresskargo_barcode']){ ?>
                                    <a style="max-width: 100px;" class="button-shipping-byexpress btn btn-warning" data-toggle="tooltip" data-byexpress-value="<?=$product['order_id'];?>" title="ByExpress Kargoya Gönder"> <i class="fa fa-truck"></i> ByExpress </a>
                                    <?php }else{ ?>

                                        <a href="javascript:;"
                                           onclick="window.open ('<?=$product['by_kargo_barcode_default'];?>', '_blank', 'location=yes,height=570,width=520,scrollbars=yes,status=yes');"
                                           class="btn" data-toggle="tooltip" title="Kargo Barkodu Bastır"> <i
                                                    style="font-size:40px;" class="fa fa-print"></i></a> <br>
                                        <a title="Kargo Barkodu Bastır" href="<?=$product['by_kargo_barcode_default'];?>" target="_blank"> Hızlı Barcode</a>


                                    <a href="javascript:;"
                                       onclick="window.open ('<?=$product['by_kargo_barcode'];?>', '_blank', 'location=yes,height=570,width=520,scrollbars=yes,status=yes');"
                                       class="btn" data-toggle="tooltip" title="Kargo Barkodu Bastır"> <i
                                                style="font-size:40px;" class="fa fa-print"></i> BY</a> <br>
                                    <a title="Kargo Barkodu Bastır" href="<?=$product['by_kargo_barcode'];?>" target="_blank"> BY Hızlı Barcode</a>


                                        <br>
                                        <?php if($product['byexpresskargo_barcode']['barcode_print_count']){ ?>
                                        <b><?=$product['byexpresskargo_barcode']['barcode_print_count']; ?></b> kere yazdırılmıştır.
                                        <?php } ?>
                                    <?php }
                                     }
                                    ?>


                                </td>

                                <td>
                                    <?php if ($product['order_status'] != "İptal Edildi" && $product['order_status'] != "İade Edildi") { ?>
                                    <?php if(!$product['scottycargo_info']){ ?>
                                    <a style="max-width: 100px;  <?php if($product['disable_scotty']!==1 && $product['shipping_zone'] == 'İstanbul') { ?> margin-top:1.5rem; <?php } ?>"  class="button-shipping-scottycargo btn btn-danger" data-toggle="tooltip" data-scottycargo-value="<?=$product['order_id'];?>" title="Scotty Kargoya Gönder" <?php if($product['disable_scotty']==1) { ?> disabled <?php } ?>>
                                        <i class="fa fa-truck"></i> Scotty
                                    </a>
                                    <?php 
                                    if($product['disable_scotty']!==1 && $product['shipping_zone'] == 'İstanbul') { ?> <br/> 
                                        <span style="font-size:11px;">İstanbul</span>
                                        <?php if($product['payment_code'] == 'iyzico_checkout_form') { ?> <span style="font-size:10px;">(Kredi Kartı)</span>  <?php } ?>  

                                    <? } ?>
                                    <?php } else{ ?>

                                        <a href="javascript:;"
                                           onclick="window.open ('<?=$product['scottycargo_barcode'];?>', '_blank', 'location=yes,height=570,width=520,scrollbars=yes,status=yes');"
                                           class="btn" data-toggle="tooltip" title="Kargo Barkodu Bastır"> <i
                                                    style="font-size:40px;" class="fa fa-print"></i></a> <br>
                                        <a title="Kargo Barkodu Bastır" href="<?=$product['scottycargo_barcode'];?>" target="_blank"> Scotty Barcode</a>
                                        <br>
                                        <?php if($product['scottycargo_info']['barcode_print_count']){ ?>
                                        <b><?=$product['scottycargo_info']['barcode_print_count']; ?></b> kere yazdırılmıştır.
                                        <?php } ?>
                                    <?php }
                                     }
                                    ?>
                                </td>

                                <td class="text-center">
                                    <div style="<?= $product['reprint_reason_id']? '': 'display: none'; ?>">
                                        <?php if ($product ['reprint']) { ?>
                                        <input type="checkbox" name="reprinted[]" value="<?php echo $product['reprint']; ?>" data-order-product-id="<?= $product['order_product_id']; ?>" checked="checked" />
                                        <?php } else { ?>
                                        <input type="checkbox" name="reprinted[]" value="<?php echo $product['reprint']; ?>" data-order-product-id="<?= $product['order_product_id']; ?>" />
                                        <?php } ?>
                                        <b style="color: red; margin-top: 5px; float: left; <?= ($product ['reprint']) ? '' : 'display: none;'; ?>">TEKRAR BASILACAK!</b>
                                        <br>
                                    </div>
                                    <select style="max-width: 85px;" class="reprint_reason<?= $product['order_product_id']; ?>" onchange="changeReprintReason(<?= $product['order_product_id']; ?>)">
                                        <option value="0"> Seçiniz </option>
                                        <?php foreach($reprint_reasons as $reason){ ?>
                                        <?php if($reason['reprint_reason_id'] == $product['reprint_reason_id']){ ?>
                                        <option value="<?=$reason['reprint_reason_id'];?>" selected> <?=$reason['name'];?> </option>
                                        <?php }else{ ?>
                                        <option value="<?=$reason['reprint_reason_id'];?>"> <?=$reason['name'];?> </option>
                                        <?php } ?>
                                        <?php } ?>
                                    </select>
                                </td>


                                <style>.mukerrer{padding: 8px 28px; background: red; color: #fff; border-radius: 10px;}</style>

                            </tr>
                            <?php } ?>
                            <?php } else { ?>
                            <tr>
                                <td class="text-center" colspan="8"><?php echo $text_no_results; ?></td>
                            </tr>
                            <?php } ?>
                            </tbody>
                        </table>
                    </div>
                </form>
                <div class="row">
                    <div class="col-sm-6 text-left"><?php echo $pagination; ?></div>
                    <div class="col-sm-6 text-right"><?php echo $results; ?></div>
                </div>
            </div>
        </div>
    </div>

    <style>
        .red-frame {
            border: 3px solid red!important;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>
    <script type="text/javascript"><!--

        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_dev/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;
                }
            }
        });
    //--></script>


        <script type="text/javascript"><!--
        $('#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 () {

            $('.bg-success').each(function (Index, Item) {

                var orderid = $(Item).attr('data-orderid');

                $.ajax({
                    url: '/ps-panel/index.php?route=sale/prepared_orders_dev/hideCompleted&token=<?= $token; ?>&order_id=' + orderid,
                    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_dev/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.");

        };


        jQuery(function ($) {
            var CheckTRs = [];

            var complated_count = 0;
            $('.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'));
                }
            });

            $('.bg-success').each(function (Index, Item){
                complated_count++;
            });


            $('.complated_count').html('Tamamlandıya Dönen Ürün Sayısı: ' + complated_count);


            $('[name="selected[]"]').change(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_dev/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 - @toturkmen - Tolga
            $('[name="reprinted[]"]').change(function () {
                var OrderProductID = $(this).attr('data-order-product-id');
                $.ajax({
                    url: '/ps-panel/index.php?route=sale/prepared_orders_dev/toggleReprint&token=<?= $token; ?>',
                    method: 'POST',
                    data: {order_product_id: OrderProductID},
                    success: function () {}
                });
                $(this).siblings('b').toggle();
            });
            // 18 Ağustos 2020 - @toturkmen - Tolga
        });
    </script>

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

        function printBox(kargo_barcode, location, order_id) {

            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!")

        }

        $('.button-shipping').click(function (e) {

            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>");
                }
            });

        });


        //ByExpress Kargo #Bilal

        $('.button-shipping-byexpress').click(function (e) {
            var parent = $(this).parents('tr');
            var order_id = $(this).attr('data-byexpress-value');

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

                    } else if(json['error']){

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

        });

        //ByExpress Kargo #Bilal

        //Scotty Kargo -Can

        $('.button-shipping-scottycargo').click(function (e) {
            var parent = $(this).parents('tr');
            var order_id = $(this).attr('data-scottycargo-value');

            $.ajax({
                url: 'index.php?route=sale/order/scottycargo&order_id=' + order_id + '&token=<?=$token?>',
                dataType: 'json',
                type: 'POST',
                data: {},
                success: function (json) {
                    if (json['status'] == 1) {
                        var html = "<a onclick=\"printBox ('" + json['scottycargo_barcode'] + "','location=yes,height=570,width=520,scrollbars=yes,status=yes', '" + order_id + "');\" data-byexpress-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-scottycargo-value="' + order_id + '"]').after(html);
                        $('[data-scottycargo-value="' + order_id + '"]').hide();
                        addHistoryForScottyCargo(order_id);

                    } else if(json['error']){
                        $('body [data-scottycargo-value="' + order_id + '"]').after(json['error']);
                    }
                }, beforeSend: function () {
                    $('[data-scottycargo-value="' + order_id + '"]').html("<b>İşlem Yapılıyor..</b>");
                }
            });
        });

        //Scotty Kargo -Can

        function addHistoryForScottyCargo(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) {
                    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=' + '<?=$scottycargo_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);
                }
            });
        }

        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);

        $(function () {
            // Custom fadeIn Duration
            $('img').loadScroll(500);
        });


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

            //if (confirm('Excel dosyasını onaylıyor musunuz?')) {
            //} else {
            //	return false;
            //}
        });

        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_dev/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_dev/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_dev/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();
                        }*/
                    }
                });
            }
        }

        function increaseIdleStock(productId, orderId) {
            if (productId) {
                $.ajax({
                    url: "index.php?route=sale/prepared_orders_dev/increaseIdleStock&token=<?=$token?>&product_id=" + productId,
                    type: "GET",
                    beforeSend: function () {
                        $('#idle-stock-button-'+productId+'_'+orderId).html('Ekleniyor..');
                    },
                    success: function (result) {
                        Swal.fire({
                            icon: 'success',
                            title: result['message'],
                            text: 'Güncel atıl stok adeti: ' + result.data,
                            confirmButtonText: 'Tamam',
                        });
                        $('#idle-stock-text-'+productId).html('Atıl Stok Mevcut');
                    },
                    complete: function() {
                        $('#idle-stock-button-'+productId+'_'+orderId).html('Atıl Stok Ekle');
                    }
                });
            }
        }
        function IncreaseReprintDownloadCount(orderProductId) {
            $.ajax({
                url: '/ps-panel/index.php?route=sale/prepared_orders_dev/IncreaseReprintDownloadCount&token=<?= $token; ?>',
                method: 'POST',
                data: {order_product_id: orderProductId},
                success: function (result) {
                    if(result==1 && !$('#img_download_'+orderProductId).parent().hasClass("red-frame")) {
                        $('#img_download_'+orderProductId).css('color','red');
                        $('#img_download_'+orderProductId).parent().addClass('red-frame');
                        $('#img_download_'+orderProductId).parent().append('<b style="color:red;display:block;">(Daha önce indirildi)</b>');
                    }
                },
            });
        }

    </script>

