<style>
    
    .package-menu, .package-filter {
        padding: 10px;
        margin: 2px;
        
    }
    .package-menu .btn {
        width: 100%;
    }
    .filter-item-table th {
        text-align: center;
    }
    .filter-item-table table{
        width: 50%;
        float: left;
    }
    .filter-item-table th, .filter-item-table td {
        width: 50%;
    }
    .package-filter-button {
        text-align: right;
    }
    
    
    .package-filter-button button.btn-success {
        margin-top: 1.5pc;
        width: 100%;
        
    }
    .package-filter-button button.btn  {
        width: 100%;
        
    }
    .package-filter-button hr {
        margin-top: 5px;
        margin-bottom: 8px;
    }
    .package-filter-button a.btn-danger {
        width: 100%;
        
    }
    .package-search-input {
        margin-top: 20px;
    }
    .package-search-input input {
        width: 100%;
        font-size: 25px;
        border-radius: 20px;
        padding: 5px 10px;
        border: 1px solid grey;
        text-align: center;
    }
    .list_items {
        margin-top: 20px;
    }
    .filter-hidden {
        display: none;
    }
    .filter-show-more {
        text-align: center;

    }
    .filter-show-more .show-hidden {
        display: none;
    }
    .filter-button-line {
        text-align: center;
    }
    .filter-button-line div span{
        color: lightgrey;
        font-size: 19px;
        font-weight: bolder;
    }
    .filter-button-line hr, .filter-main hr {
        margin-bottom: 5px;
        margin-top: 5px;
    }
    @media only screen and (max-width: 600px) {
        .filter-item-table table {
            margin-top: 0px;
            margin-bottom: 0px;
        }
        .package-filter-button {
            width: 100%;
        }
        .package-filter-button button {
            margin-top: 0px;
            width: 100%;
        }
        .package_start_button {
            width: 100%;
        }
    }
    .package_start_button {
        text-align: center;
        margin-top: 20px;
    }
    
    .package_start_button a {
      
        border-radius: 20px;
        font-size: 25px;
    }

    .select2-container .select2-selection--multiple .select2-selection__choice {
        max-width: 100%;
        box-sizing: border-box;
        white-space: normal;
        word-wrap: break-word;
        line-height: normal;
        padding-bottom: 5px;
    }
    #load_more_button {
        margin-top: 10px;
        margin-bottom: 40px;
    }
    #load_more_button:disabled {
        background-color: grey;
        border-color: grey;
    }
    #cancel-order-reader {
        position: absolute;
        right: 30px;
        top: -3px;
        font-size: 35px;
    }
    #cancel-order-reader i {
        -webkit-text-stroke: 3px white;
        color: lightgrey;
    }
    #cancel-order-reader i:hover {
        color: grey;
    }
</style>

<div class="row">
    <div class="col-xs-12 col-md-10">

        <div class="package-filter tile-stats row">
            <?= $this->load->view('tools/package/list_filter', ["settings" => $settings], true)?>
        </div>
       
    </div>
    <div class="col-md-2 col-xs-12 ">
        <div class="package-menu tile-stats">
            <?=anchor('package/settings/eshop_id/'.$this->uri['eshop_id'],'Nastavení' , 'class="btn btn-lg btn-primary"')?>
            
            
        </div>
    </div>
</div>
<div class="row">
    <div class="col-md-12 package_start_button">
        <?=anchor('package/packaging/eshop_id/'.$this->uri['eshop_id']."/cps/1",'Zabalit '.$selected_counter.' objednávek' , 'class="btn btn-lg btn-info" id="packaging-main-button"')?>
        <?=anchor('package/picking_list/eshop_id/'.$this->uri['eshop_id'],'Zobrazit produkty' , 'class="btn btn-lg btn-default" data-html="true" data-placement="top" data-trigger="hover" data-toggle="popover" data-content=\''.get_qrcode(base_url("package/picking_list/h/".$settings["hash"]."/c/".$this->client_model->current["client_id"])).'\'')?>
    </div>
</div>
<div class="row">
    <div class="col-xs-12 col-md-8 col-md-offset-2 package-search-input">
        <input id="order-reader" type="text" placeholder="naskenuj číslo balíku/objednávky"><a id="cancel-order-reader" href="javascript:void(0)" onclick="$('#order-reader').val(''); scan_orders(); $('#order-reader').blur();"><i class="fa fa-times" aria-hidden="true"></i></a>

    </div>
</div>

<div class="row">
    <div class="list_items col-md-10 col-md-offset-1" id="ajax-list-items">
        
    </div>
    <div class="col-md-12" style="text-align: center;"><button class="btn btn-info btn-lg" id="load_more_button" onclick="load_next_page();">Načíst další objednávky</button></div>
</div>



<script>
    var PAGE = 0;
    // aktualizace cisla v tlacitku pro baleni
    function reaload_main_button() {
        
        $.ajax({
            url: "<?=base_url("package/list_main_button_text/eshop_id/" . $this->uri["eshop_id"])?>",
            async: false,
            type: "GET",
            dataType: "html",
            success: function (data) {
                $('#packaging-main-button').text(data);
            }
        });
    }

    //aktualizace objednavek
    function reload_orders(order_scan, action_settings) {
        if(order_scan == undefined) var order_scan_string = "";
        else var order_scan_string = "/order/"+order_scan;
        $.ajax({
            url: "<?=base_url("package/list_items_part/eshop_id/" . $this->uri["eshop_id"])?>"+order_scan_string+"/page/0",
            async: false,
            type: "GET",
            dataType: "html",
            success: function (data) {
                $('#ajax-list-items').html(data);
                if(order_scan != undefined){
                    switch (action_settings) {
                        case "select_order":
                            $(".package-item input").prop( "checked", true ).click();
                            break;
                        case "start_packaging":
                            var href = $("#ajax-list-items .package-item a.direct-link").attr("href");
                            if(href !== undefined){window.location.href = href;}
                            break;
                    }
                }
            }
        });
        $("#load_more_button").html("Načíst další objednávky");
        $("#load_more_button").prop("disabled", false);
        PAGE = 0;
    }
    // nacteni dalsich objednavek
    function load_next_page(){
        PAGE += 1
            $.ajax({
                url: "<?=base_url("package/list_items_part/eshop_id/" . $this->uri["eshop_id"])?>/page/"+PAGE,
                async: false,
                type: "GET",
                dataType: "html",
                success: function (data) {
                    if(data == ""){
                        $("#load_more_button").html("Žádné další objednávky");
                        $("#load_more_button").prop("disabled", true);
                    }else{
                        $('#ajax-list-items').append(data);
                    }
                    
                }
        });
    }
    // aplikace filtru
    function set_filter(save_filter, delete_filter){
        if(save_filter == true){
            var input = $("<input>").attr("type", "hidden").attr("name","filter_save[button_trigger]").val("1");
            $('#package-filter-form').append($(input));
        }
        if(delete_filter){
            var input = $("<input>").attr("type", "hidden").attr("name","filter_save[delete]").val(delete_filter);
            $('#package-filter-form').append($(input));
        }
        $("#package-filter-form").ajaxSubmit({
            success : function (response) {
                if(save_filter == true){
                    location.reload();
                }else{
                    reload_orders();
                }
                
            }
        });
    }
    
    reload_orders();
    reaload_main_button();
    $(document).on("click",".filter-button-line a", function(e){
        var href = $(this).data("href");
        $.ajax({
                url: href,
                async: false,
                type: "GET",
                dataType: "html",
                success: function (data) {
                    $(".package-filter").html(data);
                    $(".select2-tags").select2({
                        tags: true,
                        tokenSeparators: ['\r\n', '\n']
                    });
                    $(".select2-notags").select2({
                    });
                    reload_orders();
                }
        });
        e.preventDefault();

    });
    
    //Only for Mobile version
    if(window.outerWidth < 425) {
        $(".package-menu").insertBefore(".package-filter");
    }
    // Select2 init
    $(".select2-tags").select2({
        tags: true,
        tokenSeparators: ['\r\n', '\n']
    });
    $(".select2-notags").select2({
    });
    // naskenovani objednavky
    function scan_orders(){
        var action_settings = "<?=$settings["settings"]["scan_action"]?>";
        var value = $("#order-reader").val();
        if(value == "") reload_orders();
        else reload_orders(value, action_settings);
    }
    // zobrazeni filtru
    function show_filter(){
        $(".filter-show-more .show-more").addClass("show-hidden");
        $(".filter-show-more .show-less").removeClass("show-hidden");
        $(".filter-main").slideDown(500).removeClass("filter-hidden");
    }
    // skryti filtru
    function hide_filter(){
        $(".filter-show-more .show-more").removeClass("show-hidden");
        $(".filter-show-more .show-less").addClass("show-hidden");
        $(".filter-main").slideUp(500).removeClass("filter-hidden");
    }
    // scan a pokud neni focus na input, tak jej provest do skryteho pole
    $(document).keypress(function (e) {

        if(e.which == 13) {
            scan_orders();
            $("#order-reader").blur();
            return;
        }
        var focused = $('input:focus');
        if (typeof focused[0] == 'undefined') {
            if (e.which !== 0) {
                $("#order-reader").focus();
                $("#order-reader").select();
            }
        }
    });
   
    // sken v pripade vlozeni
    $(document).bind('paste', function (e){
        var focused = $('input:focus');
        if (typeof focused[0] == 'undefined') {

            if (e.which !== 0) {
                $("#order-reader").focus();
                $("#order-reader").select();
            }
        }
    });


</script>

<script>
        function hide_popover(_this){
            $(_this).parents().eq(1).popover('hide');
        }
        var elem_touchstart = null;
        // vyber objednavky
        $(document).on("click", ".order-data", function () {

            var ch = $(this).find("input[type=checkbox]");
            if(ch.is(':disabled')){
                var packaging_client_name = ch.data("packaging-name");
                if(confirm("Opravdu chcete převzít balení? Objednávka se pak odstraní ze seznamu balení uživatele "+packaging_client_name+ " a přidá se na ten Váš.")){
                    ch.prop("disabled", false);
                    $(".order-number-th br").hide();
                    $(".order-number-th span").hide();
                }else{
                    return false;
                }
            }
            ch.prop("checked", !ch.prop("checked"));
            ch.change();
            return false;

        });
            
        // vyber vsechn objednavek
         $(document).on("click",'.check-all input[type=checkbox]', function() {

                if($(this).parent().data("max_value") !== undefined){
                    var settings_max_weight = "<?=$settings["settings"]["order_max_weight"]?>";
                    console.log(settings_max_weight);
                    if(settings_max_weight != ""){
                        
                        var total_weight = 0;
                        var num_of_orders = 0;
                        $(".order-list-detail input[type=checkbox]").each(function(){
                            total_weight += $(this).data("total-weight");
                            if(total_weight > settings_max_weight && num_of_orders != 0){
                                return false;
                            }else{
                                num_of_orders += 1;
                            }
                        });
                        $(".order-list-detail input[type=checkbox]").slice(0,num_of_orders).prop("checked", $(this).prop("checked"));
                    }else{
                        $(".order-list-detail input[type=checkbox]").slice(0,$(this).parent().data("max_value")).prop("checked", $(this).prop("checked"));
                    }
                    
                }else{
                    $(".order-list-detail input[type=checkbox]").prop("checked", $(this).prop("checked"));
                }
                
                //$(".order-list-detail input[type=checkbox]").change();
                var code_array = [];
                var package_numbers = {};
                if(num_of_orders == undefined) var num_of_orders = $(this).parent().data("max_value");
                $(".order-list-detail input[type=checkbox]" ).slice(0,num_of_orders).each(function( index ) {
                    var code = $(this).data("code"); 
                    var package_num = $(this).data("package_num"); 
                    if(!$(this).is(":disabled")){
                        code_array.push(code);
                        package_numbers[code] = package_num;
                    }
                    
                });
                
                var selected = $(this).prop("checked");
                if(selected){
                   selected = "<?=$this->client_model->current["client_id"]?>";
               }else{
                   selected = "0";
               }
                $.ajax({
                    type: "POST",
                    url: "<?=base_url("package/select_order")?>",
                    data: {"eshop_id": "<?=$this->uri["eshop_id"]?>", "order_codes": JSON.stringify(code_array), "selected": selected, "package_selected_number": JSON.stringify(package_numbers)},
                    success: function (d) {
                        reaload_main_button();
                    }
                });
            });
            // pri zmene vyberu odesilat ajaxem info
           $(document).on("change","input[type=checkbox]", function (e) {
               var order_code = $(this).data("code");
               var package_selected_number = $(this).data("package_num");
               if(order_code === undefined) return;
               var selected = $(this).prop("checked");
               var disabled = $(this).prop("disabled");
               if(disabled) {
                return true;
                }
               if(selected){
                   selected = "<?=$this->client_model->current["client_id"]?>";
               }else{
                   selected = "0";
               }

                $.ajax({
                    type: "POST",
                    url: "<?=base_url("package/select_order")?>",
                    data: {"eshop_id": "<?=$this->uri["eshop_id"]?>", "order_code": order_code, "selected": selected, "package_selected_number": package_selected_number},
                    success: function (d) {
                        reaload_main_button();
                    }
                });
            });


         
    </script>