<?php 
$colors = $this->config->item("package_selected_colors");

?>

<style>
	.top-header h1 {
		height: 90px;
		padding-top: 25px;
	}

	@media only screen and (max-width: 600px) {
		.top-header h1 {
			height: auto;
			padding-top: 10px;
			padding-bottom: 10px; 
		}
		.top-header .QR-code {
			display: none;
		}

	}


	 .order-list-detail tbody tr th, .order-list-detail tbody tr td {
        border-top: none;
        padding: 0px 8px;
        vertical-align: middle;
        
    }
    .order-list-detail tbody tr th{
        font-size: 17px ;
    }
    .order-list-detail tbody tr th span {
        font-size: 13px ;
        font-weight: normal;
    }
    .order-data {
        cursor: pointer;
    }
    .order-list-detail.table {
        margin-top: 22px;
        margin-bottom: 0px;
        
        float: left;
        width: 50%;
    }
    label{
        position: relative;
        cursor: pointer;
        color: #666;
        font-size: 26px;
    }
    .label-text {
        font-size: 16px;
    }
    .label-text:before{
        font-size: 26px;
    }
    input[type="checkbox"], input[type="radio"]{
        position: absolute;
        right: 9000px;
    }

    /*Check box*/
    input[type="checkbox"] + .label-text:before{
        content: "\f096";
        font-family: "FontAwesome";
        speak: none;
        font-style: normal;
        font-weight: normal;
        font-variant: normal;
        text-transform: none;
        line-height: 1;
        -webkit-font-smoothing:antialiased;
        width: 1em;
        display: inline-block;
        margin-right: 5px;
    }

    input[type="checkbox"]:checked + .label-text:before{
        content: "\f14a";
        color: #2980b9;
        animation: effect 250ms ease-in;
    }

    input[type="checkbox"]:disabled + .label-text{
        color: #aaa;
    }

    input[type="checkbox"]:disabled + .label-text:before{
        content: "\f0c8";
        color: #ccc;
    }

    /*Radio box*/

    input[type="radio"] + .label-text:before{
        content: "\f10c";
        font-family: "FontAwesome";
        speak: none;
        font-style: normal;
        font-weight: normal;
        font-variant: normal;
        text-transform: none;
        line-height: 1;
        -webkit-font-smoothing:antialiased;
        width: 1em;
        display: inline-block;
        margin-right: 5px;
    }

    input[type="radio"]:checked + .label-text:before{
        content: "\f192";
        color: #8e44ad;
        animation: effect 250ms ease-in;
    }

    input[type="radio"]:disabled + .label-text{
        color: #aaa;
    }

    input[type="radio"]:disabled + .label-text:before{
        content: "\f111";
        color: #ccc;
    }

    /*Radio Toggle*/

    .toggle input[type="radio"] + .label-text:before{
        content: "\f204";
        font-family: "FontAwesome";
        speak: none;
        font-style: normal;
        font-weight: normal;
        font-variant: normal;
        text-transform: none;
        line-height: 1;
        -webkit-font-smoothing:antialiased;
        width: 1em;
        display: inline-block;
        margin-right: 10px;
    }

    .toggle input[type="radio"]:checked + .label-text:before{
        content: "\f205";
        color: #16a085;
        animation: effect 250ms ease-in;
    }

    .toggle input[type="radio"]:disabled + .label-text{
        color: #aaa;
    }

    .toggle input[type="radio"]:disabled + .label-text:before{
        content: "\f204";
        color: #ccc;
    }


    @keyframes effect{
        0%{transform: scale(0);}
        25%{transform: scale(1.3);}
        75%{transform: scale(1.4);}
        100%{transform: scale(1);}
    }
    .direct-link {
        cursor: pointer;
        text-align: right;
        vertical-align: middle;
        font-size: 38px;
        height: 84px;
        border-left: 1px solid lightgrey;
        width: 5%;
        color: lightgrey;
    }
    .direct-link:hover {
        background-color: lightgrey;
        color: white;
    }
    .direct-link i {
        padding-top: 22px;
    }
    .order-data {
        width: 95%;
    }
    .package-item.tile-stats:hover {
        background-color: whitesmoke;
    }
    .package-item.tile-stats {
        padding-bottom: 0px;
    }
    .check-all {
        margin-left: 8px;
    }
    .order-list-detail.table1 th{
        width: 45%;
    }
    .order-list-detail.table1 th:first-child{
        width: 10%;
    }
    .order-list-detail.table2 th{
        width: 45%;
    }
    .empty-orders {
        text-align: center;
        font-weight: bolder;
        font-size: 35px;
        color: lightgrey;
        margin-top: 150px;
    }
    .dismis-popover {
        float: right;
        cursor: pointer;
        font-size: 18px;
        color: lightgrey;
    }
    .dismis-popover:hover {
        color: grey;
    }
    .picking-item {
        padding-top: 5px;
        padding-bottom: 5px
    }
    .picking-item div {
    	font-size: 20px;
    	font-weight: bold;

    }

    .picking-item div:last-child {
        font-weight: normal;
    }

    .product_amount {
        padding-top: 15px;
    }
    .product_orders {
        padding-top: 10px;
    }

    #picking-header div{
        font-size: 13px !important;
        text-align: left;
        padding-top: 0px;
    }
    #picking-header .product_image,#picking-header .product_amount,#picking-header .product_orders{
        padding-top: 20px;
    }
    .tile-stats {
        overflow: visible;
        z-index: 10;
    }
    .popover {
        max-width: none;
        width: max-content;
    }
    .order-popover {
        font-size: 15px;
    }
    .order-popover a{
        font-weight: bold;
    }
    .order-popover span{
        padding-left: 10px;
        padding-right: 5px;
    }
    .zoom {
        cursor: pointer;
    }
    .zoom:hover {
        transform: scale(5);
        transform-origin: bottom;
        transition-delay:0.3s;
    }
    .picking-item .product_numbers {
        padding-top: 18px !important;
        text-align: center !important;
    }
    .product_numbers_item {
       
        margin-left: 30px;
    }

    .product_numbers_item div {
        padding-top: 5px;

    }
    .product_numbers_item button .fa-minus {
        margin-top: 5px;
    }
     .product_numbers_item button {
        width: 25px;
        height: 25px;
        margin: 2px ;
        background-color: whitesmoke;
        border-radius: 3px;
        border: solid 1px grey;
        font-size: 13px;
    }
     .product_numbers_item button:hover {
        background-color:  #758ea3;
        color:white;
    }
    .btn-item-all {
        color:rgb(115, 135, 156);
    }
    .btn-item-all:hover{
        color: white;
    }
    .small-list-items .picking-item:first-child {
      display: none;
    }
    

    @media only screen and (max-width: 600px) {
        .order-list-detail.table {
            width: 100%;
        }
        .order-data table:last-child {
            margin-left: 18%;
            width: 82%;
            margin-top: 0px;
        }
        .order-data {
            width: 90%; 
        }
        .direct-link {
            width: 10%;
            height: 155px;
        }

        .direct-link i {
            padding-top: 56px;
        }

        .picking-item .product_codes p {
            width: 50%;
            float:left;
        }
        .mobile-hidden {
            display: none;
        }

    }
</style>

<div class="row  top-header">
	<h1 class="col-md-10 tile-stats"> Produkty pro vybrané objednávky (<?=$selected_counter?>) </h1>
	<div class="col-md-2 QR-code" style="text-align: right;">
		<?=get_qrcode(base_url("package/picking_list/h/".$settings["hash"]."/c/".$client_id))?>
	</div>
</div>

<?php
$items_ean_to_code = [];
 $total_amounts = [];
 $item_code_id_pairs = [];
 $picked_items = [];

$full_picked_items = [];
foreach ($picking_items as $c => $i){
    $item_code_id_pairs[$i["shoptet_product"]["product_data"]["variants"][0]["code"]] = $c;
    if($i["shoptet_product"]["product_data"]["variants"][0]["ean"]){
        $items_ean_to_code[$i["shoptet_product"]["product_data"]["variants"][0]["ean"]] = $i["shoptet_product"]["product_data"]["variants"][0]["code"];
    }
    ksort($i["package_selected_numbers"]);
    $is_all_picked = True;
    foreach ($i["package_selected_numbers"] as $key => $value) {
        $total_amounts[$i["shoptet_product"]["product_data"]["variants"][0]["code"]] = $value;
        if(!isset($picked_items[$i["shoptet_product"]["product_data"]["variants"][0]["code"]])) $picked_items[$i["shoptet_product"]["product_data"]["variants"][0]["code"]] = [];
        $picked_items[$i["shoptet_product"]["product_data"]["variants"][0]["code"]][$key] = $package_selected_number[$i["shoptet_product"]["product_data"]["variants"][0]["code"]][$key];
        if($package_selected_number[$i["shoptet_product"]["product_data"]["variants"][0]["code"]][$key] < $value) $is_all_picked = False;
    }
    if($is_all_picked){
        $full_picked_items[$c] = $i;
        unset($picking_items[$c]);
    }
}
$full_picked_items = array_reverse($full_picked_items, True);



 

 if(count($picking_items) > 0):
    $ITEM_DATA["c"] = array_key_first($picking_items);
    
    $ITEM_DATA["i"] = $picking_items[$ITEM_DATA["c"]];
    ?>
    <div class="picking-item tile-stats row" id="picking-item-current" style="padding-top: 10px;" data-current-code="<?=$ITEM_DATA["c"]?>">
               <?php $this->load->view('tools/package/picking_list_active_item', $ITEM_DATA);?> 
                 
    </div>
        

<?php endif; ?>

    <div class="picking-item mobile-hidden row" id="picking-header">
        <div class="col-md-2 col-xs-12 product_codes row">
            
            <div class="col-md-12 col-xs-6">Kód</div>
            <div class="col-md-12 col-xs-6">EAN</div>
         
        </div>
        <div class="col-md-4 col-xs-12 product_names row">
            <div class="col-md-12 col-xs-12">Název</div>
            <div class="col-md-12 col-xs-12">Název varianty</div>
        </div>
        <div class="col-md-1 col-xs-6 product_image">
            <div class="col-md-12 col-xs-6">Obrázek</div>
        </div>
        <div class="col-md-1 col-xs-6 product_location row">
            <div class="col-md-12 col-xs-12">Pozice skladu</div>
            <div class="col-md-12 col-xs-12">Skladovost</div>
        </div>
        <div class="col-md-1 col-xs-6 product_amount">
            <div>Počet kusů</div>
        </div>
        <div class="col-md-1 col-xs-12 product_orders row">
            Počet objednávek
        </div>
        <div class="col-md-2 col-xs-12 product_numbers row">
            Krabice
        </div>
</div>

 <div class="small-list-items">
<?php

foreach ($picking_items as $c => $i):
    //echo pr($i);
    $orders_popup = "";
    foreach ($i["for_orders"] as $o) {
        $orders_popup .= "<p class='order-popover'><a href='".$o["data"]["adminUrl"]."'>".$o["order_code"]."</a> <span>".floor($o["current_item"]["amount"])." ks</span></p>"; 
    }
    $orders_popup .= "";

    ?>
            <div class="picking-item tile-stats row" style="padding-top: 10px;" data-code="<?=$c?>">
            	<div class="col-md-2 col-xs-12 product_codes row">
                    
            		<div class="col-md-12 col-xs-6"><a href="<?=$i["shoptet_product"]["product_data"]["url"]?>" target="_blank"><?=$i["shoptet_product"]["product_data"]["variants"][0]["code"]?></a></div>
            		<div class="col-md-12 col-xs-6"><?=$i["shoptet_product"]["product_data"]["variants"][0]["ean"]?></div>
                 
            	</div>
            	<div class="col-md-4 col-xs-12 product_names row">
            		<div class="col-md-12 col-xs-12"><?=$i["shoptet_product"]["product_data"]["name"]?></div>
            		<div class="col-md-12 col-xs-12"><?=$i["shoptet_product"]["product_data"]["name"]?></div>
            	</div>
            	<div class="col-md-1 col-xs-6 product_image">
            		<img class="zoom" style='max-height: 65px; max-width: 80px;' src="<?php if($i["shoptet_product"]["product_data"]["images"][0]["name"]) echo $image_prefix.$i["shoptet_product"]["product_data"]["images"][0]["name"];?>">
            	</div>
            	<div class="col-md-1 col-xs-6 product_location">
                    <div class="col-md-12 col-xs-12"><?=$i["stock_location"]?></div>
                    <div class="col-md-12 col-xs-12"><?=$i["shoptet_product"]["product_data"]["variants"][0]["stock"]?> ks</div>
            	
            	</div>
            	<div class="col-md-1 col-xs-6 product_amount">
            		<div><?=$i["total_amount"]?> ks</div>
            	</div>
            	<div class="col-md-1 col-xs-12 product_orders row">
            		<button data-html="true" data-placement="top" data-trigger="manual" data-toggle="popover"
                        data-content="<?=$orders_popup?>"
                        class="btn btn-xs btn-default col-md-12 col-xs-6 col-xs-offset-3 col-md-offset-0">Objednávky <br class="mobile-hidden"> <?=count($i["for_orders"])?></button>
            	</div>
                <div class="col-md-2 col-xs-12 product_numbers_item row form-inline">
                    <?php ksort($i["package_selected_numbers"]);
                    foreach ($i["package_selected_numbers"] as $key => $value): ?>
                        <?php 
                            if(!isset($colors[$key-1])) $colors[$key-1] = "lightgrey";
                            if(hexdec(substr($colors[$key-1],1,2))+hexdec(substr($colors[$key-1],3,2))+hexdec(substr($colors[$key-1],5,2)) > 381) $text_color = "black"; else $text_color = "white"; 
                            if(!isset($package_selected_number[$i["shoptet_product"]["product_data"]["variants"][0]["code"]][$key])) $package_selected_number[$i["shoptet_product"]["product_data"]["variants"][0]["code"]][$key] = 0;
                           
                            ?> 
                            <div class="form-inline" style="display: flex;">
                                <h4><span class="label label-default" style="background-color: <?=$colors[$key-1]?>; color: <?=$text_color?>; margin-right: 10px;"><?=$key?></span>  <span id="<?="inum-".$c."-".$key?>"><?=$this->package_model->get_package_selected_number_amount_text($package_selected_number[$i["shoptet_product"]["product_data"]["variants"][0]["code"]][$key], $value)?></h4>
                                           
                                <button <?php if($package_selected_number[$i["shoptet_product"]["product_data"]["variants"][0]["code"]][$key] == $value)  echo "disabled"; ?> style="margin-left: 10px;" data-text-id="<?="inum-".$c."-".$key?>" data-total_amount="<?=$value?>" data-code="<?=$i["shoptet_product"]["product_data"]["variants"][0]["code"]?>" data-selected_number="<?=$key?>" onclick="add_selected_number(this);" class="btn-item-plus"><i class="fa fa-plus" aria-hidden="true"></i></button>
                                
                                <button   data-total_amount="<?=$value?>" data-text-id="<?="inum-".$c."-".$key?>" data-code="<?=$i["shoptet_product"]["product_data"]["variants"][0]["code"]?>" data-selected_number="<?=$key?>" onclick="add_selected_number(this,true);" class="btn-item-minus"><i class="fa fa-minus" aria-hidden="true"></i></button>

                                <button data-current_value='<?=$package_selected_number[$i["shoptet_product"]["product_data"]["variants"][0]["code"]][$key]?>' style="width: 50px; line-height:13px;" data-total_amount="<?=$value?>" data-text-id="<?="inum-".$c."-".$key?>" data-code="<?=$i["shoptet_product"]["product_data"]["variants"][0]["code"]?>" data-selected_number="<?=$key?>" onclick="add_all_selected_number(this);" class="btn btn-default btn-item-all">Vše</button>
                            </div>
                    <?php endforeach; ?>

                </div>
            	 
        	</div>
 <?php 
    endforeach; ?>
</div>


    <div class="col-md-12 complete_items">
        <h3 style="text-align: center;">Zkompletované produkty</h3>
        <div class="complete_items_inner">
            <?php
            foreach ($full_picked_items as $c => $i):
            //echo pr($i);
            $orders_popup = "";
            foreach ($i["for_orders"] as $o) {
                $orders_popup .= "<p class='order-popover'><a href='".$o["data"]["adminUrl"]."'>".$o["order_code"]."</a> <span>".floor($o["current_item"]["amount"])." ks</span></p>"; 
            }
            $orders_popup .= "";

            ?>
                    <div class="picking-item tile-stats row" style="padding-top: 10px;" data-code="<?=$c?>">
                        <div class="col-md-2 col-xs-12 product_codes row">
                            
                            <div class="col-md-12 col-xs-6"><a href="<?=$i["shoptet_product"]["product_data"]["url"]?>" target="_blank"><?=$i["shoptet_product"]["product_data"]["variants"][0]["code"]?></a></div>
                            <div class="col-md-12 col-xs-6"><?=$i["shoptet_product"]["product_data"]["variants"][0]["ean"]?></div>
                         
                        </div>
                        <div class="col-md-4 col-xs-12 product_names row">
                            <div class="col-md-12 col-xs-12"><?=$i["shoptet_product"]["product_data"]["name"]?></div>
                            <div class="col-md-12 col-xs-12"><?=$i["shoptet_product"]["product_data"]["name"]?></div>
                        </div>
                        <div class="col-md-1 col-xs-6 product_image">
                            <img class="zoom" style='max-height: 65px; max-width: 80px;' src="<?php if($i["shoptet_product"]["product_data"]["images"][0]["name"]) echo $image_prefix.$i["shoptet_product"]["product_data"]["images"][0]["name"];?>">
                        </div>
                        <div class="col-md-1 col-xs-6 product_location">
                            <div class="col-md-12 col-xs-12"><?=($i["stock_location"])?$i["stock_location"]:"-"?></div>
                            <div class="col-md-12 col-xs-12"><?=$i["shoptet_product"]["product_data"]["variants"][0]["stock"]?> ks</div>
                        </div>
                        <div class="col-md-1 col-xs-6 product_amount">
                            <div><?=$i["total_amount"]?> ks</div>
                        </div>
                        <div class="col-md-1 col-xs-12 product_orders row">
                            <button data-html="true" data-placement="top" data-trigger="manual" data-toggle="popover"
                                data-content="<?=$orders_popup?>"
                                class="btn btn-xs btn-default col-md-12 col-xs-6 col-xs-offset-3 col-md-offset-0">Objednávky <br class="mobile-hidden"> <?=count($i["for_orders"])?></button>
                        </div>
                        <div class="col-md-2 col-xs-12 product_numbers_item row form-inline">
                            <?php ksort($i["package_selected_numbers"]);
                            foreach ($i["package_selected_numbers"] as $key => $value): ?>
                                <?php 
                                    if(!isset($colors[$key-1])) $colors[$key-1] = "lightgrey";
                                    if(hexdec(substr($colors[$key-1],1,2))+hexdec(substr($colors[$key-1],3,2))+hexdec(substr($colors[$key-1],5,2)) > 381) $text_color = "black"; else $text_color = "white"; 
                                    if(!isset($package_selected_number[$i["shoptet_product"]["product_data"]["variants"][0]["code"]][$key])) $package_selected_number[$i["shoptet_product"]["product_data"]["variants"][0]["code"]][$key] = 0;
                                   
                                    ?> 
                                    <div class="form-inline" style="display: flex;">
                                        <h4><span class="label label-default" style="background-color: <?=$colors[$key-1]?>; color: <?=$text_color?>; margin-right: 10px;"><?=$key?></span>  <span id="<?="inum-".$c."-".$key?>"><?=$this->package_model->get_package_selected_number_amount_text($package_selected_number[$i["shoptet_product"]["product_data"]["variants"][0]["code"]][$key], $value)?></h4>
                                                   
                                        <button <?php if($package_selected_number[$i["shoptet_product"]["product_data"]["variants"][0]["code"]][$key] == $value)  echo "disabled"; ?> style="margin-left: 10px;" data-text-id="<?="inum-".$c."-".$key?>" data-total_amount="<?=$value?>" data-code="<?=$i["shoptet_product"]["product_data"]["variants"][0]["code"]?>" data-selected_number="<?=$key?>" onclick="add_selected_number(this);" class="btn-item-plus"><i class="fa fa-plus" aria-hidden="true"></i></button>
                                        
                                        <button   data-total_amount="<?=$value?>" data-text-id="<?="inum-".$c."-".$key?>" data-code="<?=$i["shoptet_product"]["product_data"]["variants"][0]["code"]?>" data-selected_number="<?=$key?>" onclick="add_selected_number(this,true);" class="btn-item-minus"><i class="fa fa-minus" aria-hidden="true"></i></button>

                                        <button data-current_value='<?=$package_selected_number[$i["shoptet_product"]["product_data"]["variants"][0]["code"]][$key]?>' style="width: 50px; line-height:13px;" data-total_amount="<?=$value?>" data-text-id="<?="inum-".$c."-".$key?>" data-code="<?=$i["shoptet_product"]["product_data"]["variants"][0]["code"]?>" data-selected_number="<?=$key?>" onclick="add_all_selected_number(this);" class="btn btn-default btn-item-all">Vše</button>
                                    </div>
                            <?php endforeach; ?>

                        </div>
                         
                    </div>
         <?php 
            endforeach; ?>
        </div>




    </div>
 <div style="position: fixed;
    top: 50%;
    left: 50%;
    margin-right: -50%;
    transform: translate(-50%, -50%);">
<input id="scanning-input" name="scan_code" style="opacity:0; width: 0px; overflow: hidden;">
</div>


<div id="scan_num_show"  
style="position: fixed;
    top: 50%;
    left: 50%;
    margin-right: -50%;
    transform: translate(-50%, -50%);
    z-index:200;
    font-size: 200px;
    display: none;">

</div>



 <script type="text/javascript">
    var MISSING_ITEM_MAP = [];
    load_missing_items_map();
    var ORDER_ITEMS_EAN_TO_CODE = JSON.parse('<?=json_encode($items_ean_to_code)?>');
    var TOTAL_AMOUNTS = JSON.parse('<?=json_encode($total_amounts)?>');
    var ITEM_CODE_ID_PAIRS = JSON.parse('<?=json_encode($item_code_id_pairs)?>');
    var PICKED_ITEMS = JSON.parse('<?=json_encode($picked_items)?>');
    var lock = false;
    // aktualizace chybejicich produtku
    function load_missing_items_map(){
        $.ajax({
                type: "GET",
                url: "<?=base_url("package/ajax_get_package_selected_number/eshop_id/".$this->uri["eshop_id"])?>",
                async: false,
                success: function (data) {
                    MISSING_ITEM_MAP = JSON.parse(data);
                    move_next_items(JSON.parse(data));
                    

                }
            });
    }
    // zobrazeni dalsiho produktu v poradi
    function move_next_items(missing_map){
        
        var current_code = $("#picking-item-current").data("current-code");
        if(current_code === undefined) return;
   
        if(missing_map[current_code].length == 0){
            
            $(".small-list-items .picking-item:first-child").prependTo(".complete_items_inner");
            if($(".small-list-items .picking-item:first-child").length == 0){
                console.log("here");
                 $("#picking-item-current").html("").hide();
                 return;
            }
            var next_code = $(".picking-item:first-child").data("code");
            $.ajax({
                type: "GET",
                url: "<?=base_url("package/picking_list/eshop_id/".$this->uri["eshop_id"]."/active_item/")?>"+next_code,
                async: false,
                success: function (data) {
                    $("#picking-item-current").html(data);
                    $("#picking-item-current").data("current-code", next_code);
                }
            });
        }
    }
    
    // naskenovani produktu a jeho kompletace
    function add_scan_number(value){
        if(value in ORDER_ITEMS_EAN_TO_CODE) value = ORDER_ITEMS_EAN_TO_CODE[value];
        if(value in MISSING_ITEM_MAP && MISSING_ITEM_MAP[value][0] !== undefined){

            var data = {};
            data["code"] = value;
            data["package_selected_number"] =  MISSING_ITEM_MAP[value][0];
            data["total_amount"] = TOTAL_AMOUNTS[value];
            data["add"] = "1";
            var text_id = "inum-"+ITEM_CODE_ID_PAIRS[value]+"-"+data["package_selected_number"];
            var num_html = $("#"+text_id).parent().parent().find(".label").clone();

            $("#scan_num_show").html(num_html).fadeIn(500);
            
            setTimeout(function(){
              $("#scan_num_show").fadeOut(500, function(){
                $("#scan_num_show").html("");
              });
            }, 2000);

            lock = true
            $.ajax({
                    type: "POST",
                    url: "<?=base_url("package/ajax_package_selected_number/eshop_id/".$this->uri["eshop_id"])?>",
                    data: data,
                    success: function (d) {
                        if (d.indexOf("#26B99A;") >= 0){
                            $("#"+text_id).parent().parent().find(".fa-plus").parent().prop( "disabled", true );
                            $("#"+text_id+"-active").parent().parent().find(".fa-plus").parent().prop( "disabled", true );
                        }else{
                            $("#"+text_id).parent().find(".fa-plus").parent().prop( "disabled", false );
                            $("#"+text_id+"-active").parent().find(".fa-plus").parent().prop( "disabled", false );
                        }
                        $("#"+text_id).html(d);
                        $("#"+text_id+"-active").html(d);

                        load_missing_items_map();
                        PICKED_ITEMS[data["code"]][data["package_selected_number"]] += 1;
                        lock = false
                        
                    }
                });
        }else{
             $("#scan_num_show").html('<span class="label label-default" style="border: 12px solid red; background-color: white; color: red; margin-right: 10px;">!</span>').fadeIn(500);

                setTimeout(function(){
                  $("#scan_num_show").fadeOut(500, function(){
                    $("#scan_num_show").html("");
                  });
                }, 2000);
        }
        
    }

    // odchytavani naskenovanych retezcu do skryteho pole
    $(document).ready(function() {
        $("[data-toggle=popover]").popover();
        $(window).click(function() {
            $("[data-toggle=popover]").popover("hide");
        });
        $('body').on('click', '.popover.fade', function(e) {
            e.stopPropagation();
        });
        $('body').on('click', '[data-toggle=popover]', function(event) {

            $("[data-toggle=popover]").not(this).popover("hide");
            $(this).popover("toggle");
            event.stopPropagation();
        });
    });

     $(document).keypress(function (e) {

        if(e.which == 13) {
            console.log("zde");
            $("#scanning-input").blur();
            $("#scanning-input").trigger("scan");
            return false;
        }
        var focused = $('input:focus');
        var focused2 = $('textarea:focus');
        if (typeof focused[0] == 'undefined' && typeof focused2[0] == 'undefined') {
            if (e.which !== 0) {
                $("#scanning-input").focus();
                $("#scanning-input").select();

                
            }
        }
    });
    $(document).bind('paste', function (e){
        var focused = $('input:focus');
        var focused2 = $('textarea:focus');
        if (typeof focused[0] == 'undefined' && typeof focused2[0] == 'undefined') {

            if (e.which !== 0) {
                $("#scanning-input").focus();
                $("#scanning-input").select();
            }
        }
    });
    
    $("#scanning-input").on( "scan", function(e) {
        var value = $("#scanning-input").val();
        $("#scanning-input").val('');
        add_scan_number(value);
        
    });
    function countElement(item,array) {
        var count = 0;
        $.each(array, function(i,v) { if (v === item) count++; });
        return count;
    }
    // kompletace hromadne vsechn zbyvajicich polozek
    function add_all_selected_number(t){
        
        var total_amount = $(t).data("total_amount");
        var current_val = PICKED_ITEMS[$(t).data("code")][$(t).data("selected_number")];
        
        var data = {};
        if(current_val >= total_amount){
            data["all_minus"] = current_val;
        }else{
            data["all_add"] = total_amount - current_val;

        }
        data["package_selected_number"] = $(t).data("selected_number");
        data["code"] = $(t).data("code");
        data["total_amount"] = $(t).data("total_amount");
        var text_id = $(t).data("text-id");
        lock = true
        $.ajax({
                type: "POST",
                url: "<?=base_url("package/ajax_package_selected_number/eshop_id/".$this->uri["eshop_id"])?>",
                data: data,
                success: function (data) {
                    if (data.indexOf("#26B99A;") >= 0){
                        $(t).parent().find(".fa-plus").parent().prop( "disabled", true );
                        $("#"+text_id).parent().find(".fa-plus").parent().prop( "disabled", true );
                    }else{
                        $(t).parent().find(".fa-plus").parent().prop( "disabled", false );
                        $("#"+text_id).parent().find(".fa-plus").parent().prop( "disabled", false );
                    }
                    $("#"+text_id).html(data);
                    $("#"+text_id+"-active").html(data);
                    load_missing_items_map();
                    
                    if(current_val >= total_amount) PICKED_ITEMS[$(t).data("code")][$(t).data("selected_number")] -= current_val;
                    else PICKED_ITEMS[$(t).data("code")][$(t).data("selected_number")] += total_amount-current_val;
                    lock = false;
                }
            });
    }

    // kompletace produktu
    function add_selected_number(t, minus){
        var start = new Date().getTime();
       
        var data = {};
        if(minus == undefined){
            data["add"] = "1";
        }else{
            data["minus"] = "1";
        }
        data["package_selected_number"] = $(t).data("selected_number");
        data["code"] = $(t).data("code");
        data["total_amount"] = $(t).data("total_amount");
        var text_id = $(t).data("text-id");
        lock = true
        $.ajax({
                type: "POST",
                url: "<?=base_url("package/ajax_package_selected_number/eshop_id/".$this->uri["eshop_id"])?>",
                data: data,
                success: function (data) {
                    if (data.indexOf("#26B99A;") >= 0){
                        $(t).parent().find(".fa-plus").parent().prop( "disabled", true );
                        $("#"+text_id+"-active").parent().find(".fa-plus").parent().prop( "disabled", true );
                    }else{
                        $(t).parent().find(".fa-plus").parent().prop( "disabled", false );
                        $("#"+text_id+"-active").parent().find(".fa-plus").parent().prop( "disabled", false );
                    }
                    console.log(text_id);
                    $("#"+text_id+"-active").html(data);
                    $("#"+text_id).html(data);

                    load_missing_items_map();
                    lock = false
                    if(minus == undefined) PICKED_ITEMS[$(t).data("code")][$(t).data("selected_number")] += 1;
                    else PICKED_ITEMS[$(t).data("code")][$(t).data("selected_number")] -= 1;
                }
            });
     }
 </script>