<?php echo $header; ?><?php echo $column_left; ?>
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
            <div class="pull-right">
                <a id="send-logo" onclick="showLogoModal()" class="btn btn-default">Logo'ya Aktar</a>
                <a href="<?php echo $add_product_stock; ?>" data-toggle="tooltip" title="Ürün ve Stok numarası" class="btn btn-warning"><i class="fa fa-plus"></i> Ürün ve Stok Numarası Ekleme</a>
                <a href="<?php echo $add; ?>" data-toggle="tooltip" title="<?php echo $button_add; ?>" class="btn btn-primary"><i class="fa fa-plus"></i></a>
                <button type="button" data-toggle="tooltip" title="<?php echo $button_delete; ?>" class="btn btn-danger" onclick="confirm('<?php echo $text_confirm; ?>') ? $('#form-option').submit() : false;"><i class="fa fa-trash-o"></i></button>
            </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="modal fade" id="send-logo-modal" role="dialog" data-backdrop="false">
    <div class="modal-dialog">
    
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Logo Materyal Aktarımı</h4>
        </div>
        <div class="modal-body">
          <div class="progress">
            <div id="progress-bar" class="progress-bar progress-bar-info progress-bar-striped" style="width: 0%">
            </div> 
        </div>
            <p id="logo-is-calculating" class="text-center">Aktarılabilir materyaller hesaplanıyor...</p>
            <p class="text-center hide" id="logo-send-details">
                <span id="logo-processed">0</span>
                <span id="logo-seperator">/</span>
                <span id="logo-total">? (Aktarılabilir Materyal Sayısı)</span>
            </p>

        </div>

        <div class="modal-footer">
          <button type="button" class="btn btn-primary" id="start-sending-to-logo">Aktarımı Başlat</button>
        </div>
      </div>
    </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" style='display:none;'>
                <form action="<?php echo $delete; ?>" method="post" enctype="multipart/form-data" id="form-option">
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover">
                            <thead>
                            <tr>
                                <td style="width: 1px;" class="text-center"><input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" /></td>
                                <td class="text-left"><?php if ($sort == 'od.name') { ?>
                                    <a href="<?php echo $sort_name; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_name; ?></a>
                                    <?php } else { ?>
                                    <a href="<?php echo $sort_name; ?>"><?php echo $column_name; ?></a>
                                    <?php } ?></td>

                                <td class="text-left">Barcode</td>
                               <td class="text-left"><?php echo $column_product_name; ?></td>
                                <td class="text-left"><?php echo $column_category; ?></td>
                                <td class="text-left"><?php echo $column_collection; ?></td>
                                <td class="text-left"><?php echo $column_color; ?></td>
                                <td class="text-left"><?php echo $column_price; ?></td>
                                <td class="text-left"><?php echo $column_price_cost; ?></td>
                                <td class="text-left"><?php echo $column_quantity; ?></td>
                                <td class="text-right"><?php echo $column_action; ?></td>
                            </tr>
                            </thead>
                            <tbody>
                            <?php if ($stocks) { ?>
                            <?php foreach ($stocks as $stock) { ?>
                            <tr>
                                <td class="text-center"><?php if (in_array($stock['stock_id'], $selected)) { ?>
                                        <input type="checkbox" name="selected[]" value="<?php echo $stock['stock_id']; ?>" checked="checked" />
                                    <?php } else { ?>
                                        <input type="checkbox" name="selected[]" value="<?php echo $stock['stock_id']; ?>" />
                                    <?php } ?></td>
                                <td class="text-left"><?php echo $stock['name']; ?></td>
                                <td class="text-left"><?php echo $stock['barcode']; ?></td>
                                <td class="text-left"><?php echo $stock['category_name']; ?></td>
                                <td class="text-left"><?php echo $stock['collection_name']; ?></td>
                                <td class="text-left"><?php echo $stock['color_name']; ?></td>
                                <td class="text-left"><?php echo $stock['price']; ?></td>
                                <td class="text-left"><?php echo $stock['price_cost']; ?></td>
                                <td class="text-left"><?php echo $stock['quantity']; ?></td>
                                <td class="text-right">
                                    <a href="<?php echo $stock['edit']; ?>" data-toggle="tooltip" title="<?php echo $button_edit; ?>" class="btn btn-primary"><i class="fa fa-pencil"></i></a>
                                    <a data-toggle="tooltip" data-id="<?=$stock['barcode'];?>" title="Stok Dağıt" class="btn btn-danger barcode_emit"><i class="fa fa-arrows-alt"></i></a>
                                </td>
                            </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 class="panel-body">

                <form action="{{ delete }}" enctype="multipart/form-data" method="post" id="form-information">
                    <table id="example" class="display" style="width:100%">
                        <thead>
                        <tr>
                            <th class="text-left"></th>
                            <th style="width:10px;"><input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);"></th>
                            <th class="text-left">Barcode</th>
                            <th class="text-left"><?php echo $column_name; ?></th>
                            <th class="text-left"><?php echo $column_category; ?></th>
                            <th class="text-left"><?php echo $column_collection; ?></th>
                            <th class="text-left"><?php echo $column_color; ?></th>
                            <th class="text-left"><?php echo $column_price; ?></th>
                            <th class="text-left"><?php echo $column_price_cost; ?></th>
                            <th class="text-left">Kritik Stok</th>
                            <th class="text-left"><?php echo $column_quantity; ?></th>
                            <th class="text-right"></th>
                            <th class="text-right"></th>
                        </tr>
                        </thead>
                    </table>
                </form >

                <br>
                <br>
                <br>

                <?php

if($smsatilacakliste){
foreach($smsatilacakliste as $smslist){

	echo '<div class="col-md-12"> Bu ürünün <b>'.$smslist['name'].' | ('.$smslist['barcode'].')</b>  stoğu ('.$smslist['quantity'].') kritik düzeyin altındadır, kontrol ediniz. </div>';

            }
            }


            ?>

            <br>
            <br>
            <br>

            <script src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js" type="text/javascript"></script>
            <link type="text/css" href="https://cdn.datatables.net/1.10.23/css/jquery.dataTables.min.css" rel="stylesheet" media="screen" />
            <script>



                function kadetdegistir (product_stock_id) {

                    var kquantity = $("#k"+product_stock_id+"").val();

                    $.ajax({
                        type: 'POST',
                        url: 'index.php?route=catalog/infinity_stock/kadetdegistir&token=<?php echo $token; ?>',
                        beforeSend: function() {
                            $(".sk"+product_stock_id+"").html('<i style="color:yellow; padding:10px;" class="fa fa-spinner" aria-hidden="true"></i>');
                        },
                        data: {
                            'product_stock_id': product_stock_id,
                            'kquantity': kquantity
                        },
                        success: function(response) {
                            if(response.success){

                                $(".sk"+product_stock_id+"").html('');
                                $(".sk"+product_stock_id+"").html('<i style="color:green; padding:10px;" class="fa fa-check-circle" aria-hidden="true"></i>');
                                setTimeout(function(){  $(".sk"+product_stock_id+"").html(''); }, 1000);

                            } else{
                                $(".sk"+product_stock_id+"").html('Hata!');
                                setTimeout(function(){  $(".sk"+product_stock_id+"").html(''); }, 1000);
                            }
                        }
                    });


                }


                function adetdegistir(product_stock_id) {

                    var quantity = $("#a"+product_stock_id+"").val();

                    $.ajax({
                        type: 'POST',
                        url: 'index.php?route=catalog/infinity_stock/adetdegistir&token=<?php echo $token; ?>',
                        beforeSend: function() {
                            $(".ak"+product_stock_id+"").html('<i style="color:yellow; padding:10px;" class="fa fa-spinner" aria-hidden="true"></i>');
                        },
                        data: {
                            'product_stock_id': product_stock_id,
                            'quantity': quantity
                        },
                        success: function(response) {
                            if(response.success){

                                $(".ak"+product_stock_id+"").html('');
                                $(".ak"+product_stock_id+"").html('<i style="color:green; padding:10px;" class="fa fa-check-circle" aria-hidden="true"></i>');
                                setTimeout(function(){  $(".ak"+product_stock_id+"").html(''); }, 1000);

                            } else{
                                $(".ak"+product_stock_id+"").html('Hata!');
                                setTimeout(function(){  $(".sk"+product_stock_id+"").html(''); }, 1000);
                            }
                        }
                    });

                }




                $(document).ready(function() {
                    $('#example').DataTable( {
                        "ajax": 'index.php?route=catalog/infinity_stock/infinity_stockdatatable&token=<?=$token;?>',
                        "pageLength": 20,
                        "columns": [
                            { "searchable": false },
                            null,
                            null,
                            null,
                            null,
                            null,
                            null,
                            null,
                            null,
                            { "searchable": false },
                            { "searchable": false },
                            { "searchable": false },
                            { "searchable": false },
                        ],
                        "language": {
                            "emptyTable": "Tabloda herhangi bir veri mevcut değil",
                            "info": "_TOTAL_ kayıttan _START_ - _END_ arasındaki kayıtlar gösteriliyor",
                            "infoEmpty": "Kayıt yok",
                            "infoFiltered": "(_MAX_ kayıt içerisinden bulunan)",
                            "infoThousands": ".",
                            "lengthMenu": "Sayfada _MENU_ kayıt göster",
                            "loadingRecords": "Yükleniyor...",
                            "processing": "İşleniyor...",
                            "search": "Ara:",
                            "zeroRecords": "Eşleşen kayıt bulunamadı",
                            "paginate": {
                                "first": "İlk",
                                "last": "Son",
                                "next": "Sonraki",
                                "previous": "Önceki"
                            },
                            "aria": {
                                "sortAscending": ": artan sütun sıralamasını aktifleştir",
                                "sortDescending": ": azalan sütun sıralamasını aktifleştir"
                            },
                            "select": {
                                "rows": {
                                    "_": "%d kayıt seçildi",
                                    "1": "1 kayıt seçildi",
                                    "0": "-"
                                },
                                "0": "-",
                                "1": "%d satır seçildi",
                                "2": "-",
                                "_": "%d satır seçildi",
                                "cells": {
                                    "1": "1 hücre seçildi",
                                    "_": "%d hücre seçildi"
                                },
                                "columns": {
                                    "1": "1 sütun seçildi",
                                    "_": "%d sütun seçildi"
                                }
                            },
                            "autoFill": {
                                "cancel": "İptal",
                                "fillHorizontal": "Hücreleri yatay olarak doldur",
                                "fillVertical": "Hücreleri dikey olarak doldur",
                                "info": "-",
                                "fill": "Bütün hücreleri <i>%d<\/i> ile doldur"
                            },
                            "buttons": {
                                "collection": "Koleksiyon <span class=\"ui-button-icon-primary ui-icon ui-icon-triangle-1-s\"><\/span>",
                                "colvis": "Sütun görünürlüğü",
                                "colvisRestore": "Görünürlüğü eski haline getir",
                                "copy": "Koyala",
                                "copyKeys": "Tablodaki sisteminize kopyalamak için CTRL veya u2318 + C tuşlarına basınız.",
                                "copySuccess": {
                                    "1": "1 satır panoya kopyalandı",
                                    "_": "%ds satır panoya kopyalandı"
                                },
                                "copyTitle": "Panoya kopyala",
                                "csv": "CSV",
                                "excel": "Excel",
                                "pageLength": {
                                    "-1": "Bütün satırları göster",
                                    "1": "-",
                                    "_": "%d satır göster"
                                },
                                "pdf": "PDF",
                                "print": "Yazdır"
                            },
                            "decimal": "-",
                            "infoPostFix": "-",
                            "searchBuilder": {
                                "add": "Koşul Ekle",
                                "button": {
                                    "0": "Arama Oluşturucu",
                                    "_": "Arama Oluşturucu (%d)"
                                },
                                "clearAll": "Hepsini Kaldır",
                                "condition": "Koşul",
                                "conditions": {
                                    "date": {
                                        "after": "Sonra",
                                        "before": "Önce",
                                        "between": "Arasında",
                                        "empty": "Boş",
                                        "equals": "Eşittir",
                                        "not": "Değildir",
                                        "notBetween": "Dışında",
                                        "notEmpty": "Dolu"
                                    },
                                    "number": {
                                        "between": "Arasında",
                                        "empty": "Boş",
                                        "equals": "Eşittir",
                                        "gt": "Büyüktür",
                                        "gte": "Büyük eşittir",
                                        "lt": "Küçüktür",
                                        "lte": "Küçük eşittir",
                                        "not": "Değildir",
                                        "notBetween": "Dışında",
                                        "notEmpty": "Dolu"
                                    },
                                    "string": {
                                        "contains": "İçerir",
                                        "empty": "Boş",
                                        "endsWith": "İle biter",
                                        "equals": "Eşittir",
                                        "not": "Değildir",
                                        "notEmpty": "Dolu",
                                        "startsWith": "İle başlar"
                                    }
                                },
                                "data": "Veri",
                                "deleteTitle": "Filtreleme kuralını silin",
                                "leftTitle": "Kriteri dışarı çıkart",
                                "logicAnd": "ve",
                                "logicOr": "veya",
                                "rightTitle": "Kriteri içeri al",
                                "title": {
                                    "0": "Arama Oluşturucu",
                                    "_": "Arama Oluşturucu (%d)"
                                },
                                "value": "Değer"
                            },
                            "searchPanes": {
                                "clearMessage": "Hepsini Temizle",
                                "collapse": {
                                    "0": "Arama Bölmesi",
                                    "_": "Arama Bölmesi (%d)"
                                },
                                "count": "{total}",
                                "countFiltered": "{shown}\/{total}",
                                "emptyPanes": "Arama Bölmesi yok",
                                "loadMessage": "Arama Bölmeleri yükleniyor ...",
                                "title": "Etkin filtreler - %d"
                            },
                            "searchPlaceholder": "Ara",
                            "thousands": "."
                        }

                    } );
                } );


            </script>

        </div>
    </div>
</div>
</div>
<?php echo $footer; ?>
<style>
    #send-logo-modal .modal-footer{
  display: flex;
  justify-content: center;
}
</style>
<script>
    var materials = [];
    var successMaterials = 0;
    var failedMaterials = 0;
    async function showLogoModal() {
        $('#send-logo-modal').modal('show');
        $.ajax({
            url: '/index.php?route=module/logo/get_unreferenced_materials',
            method: 'GET',
            success: function (data) {
                if(data.length>0) {
                    window.materials = data;
                    $('#logo-total').text(data.length);
                    $('#logo-send-details').removeClass('hide');
                    $('#logo-is-calculating').addClass('hide');                        
                } else {
                    $('#logo-is-calculating').text('Aktarılmamış materyal yok.');                        
                }
            },
        })          
    }

    $('#start-sending-to-logo').on('click', async function () {
            $('#start-sending-to-logo').attr('disabled','disabled');
            $('#start-sending-to-logo').text('İşlem yapılıyor..');
            $('#progress-bar').addClass('active');
            let progressStep = parseFloat(100/window.materials.length).toFixed(4);
            let progress = document.getElementById("progress-bar");
            let processedCount = $('#logo-processed');
            for (let i = 0; i < window.materials.length; i++) {
                await sendLogo(window.materials[i].product_stock_id);
                progress.style.width= parseFloat(progress.style.width.replace("%", ""))+parseFloat(progressStep)+'%';
                processedCount.html(parseInt(processedCount.text())+1);
            }
            $('#logo-send-details').addClass('hide');
            $('#logo-is-calculating').removeClass('hide');  
            $('#logo-is-calculating').text('Aktarım tamamlandı. ('+ window.successMaterials + ' Başarılı, ' + window.failedMaterials + ' Başarısız) \n'); 
            $('#start-sending-to-logo').text('Aktarımı Başlat');
            $('#progress-bar').removeClass('active');
        });

        async function sendLogo(product_stock_id) {
            return new Promise(function(resolve, reject) {
                $.ajax({
                    method: "POST",
                    url: "/index.php?route=tool/logo_transfer/send_material",
                    data: { product_stock_id: product_stock_id },
                    success: function(data){
                        if(data['status']=='success') {
                            window.successMaterials++;
                        }else if(data['status'] = 'failed'){
                            window.failedMaterials++;
                        }
                        resolve(data['status'])
                    },
                    error: function(err) {
                        reject(err)
                    }
                });
            }); 
        }
        
    $(function (){
        $('.barcode_emit').on('click', function (){
            var barcode = $(this).attr('data-id');

            $.ajax({
                method: "GET",
                url: "index.php?route=catalog/infinity_stock/barcode_emit&token=" + '<?=$token;?>',
                data: { barcode: barcode },
                success: function(json){

                    if(json['success']){
                        alert(json['success']);
                    }
                    if(json['error']){
                        alert(json['error']);
                    }
                }

            });
        });
    });

</script>