<?php
/**
 * Created by PhpStorm.
 * User: jasmanto
 * Date: 17/09/2018
 * Time: 19.35
 */

switch ($mode) {
    case "view_":
        // mati_disini();
        $strContent = "";
        if (isset($items) && sizeof($items) > 0) {
            if (is_array($items) && (sizeof($items) > 0)) {
                $strContent .= "<ul class='list-group text-left' style='line-height:13px;'>";
                if($cCode=="_TR_461"){
                    $strContent .= "<li class='list-group-item text-center text-muted' style='background: lightgray;' >
                                    <div class='text-right'><div onclick='selectorCheckAll()' class='btn btn-xs btn-warning'>pilih semua</div></div>
                                </li>";
                }
                foreach ($items as $iSpec) {
                    if(strlen($iSpec['nama'])>0){
                        if (isset($iSpec['minValue'])) {
                            $defaultValue = $iSpec['minValue'];
                            $defVal = "&minValue=$defaultValue";
                        }
                        else {
                            $defVal = "";
                        }
                        $strContent .= "<li class='list-group-item'>";
                        if ($socketURL[$iSpec['id']] != "") {
                            if (sizeof($socketParams[$iSpec['id']]) > 0) {
                                foreach ($socketParams[$iSpec['id']] as $key => $src) {
                                    if (isset($iSpec[$src])) {
                                        $iVal = addslashes($iSpec[$src]);
                                    }
                                    else {
                                        $iVal = 0;
                                    }
                                    $socketURL[$iSpec['id']] = str_replace("{" . $src . "}", $iVal, $socketURL[$iSpec['id']]);
                                }
                            }
                        }
                        if (isset($socketURL[$iSpec['id']]) && strlen($socketURL[$iSpec['id']]) > 3) {
                            $actionTarget = "top.BootstrapDialog.show({
                                                title: '" . $iSpec['nama'] . "',
                                                message: " . '$' . "('<div></div>').load('" . $socketURL[$iSpec['id']] . "&url=" . blobEncode("" . $iSpec['target'] . "?id=" . $iSpec['id'] . "$defVal") . "'),
                                                draggable: false,                                        
                                                type: top.BootstrapDialog.TYPE_DEFAULT,
                                                size: top.BootstrapDialog.SIZE_SMALL,
                                                closable: true,
                                            });";
                            $strContent .= "<a href='JavaScript:void(0);' onclick=\"$actionTarget\">";
                        }
                        else {

                            $mb_js = "";
                            if(isset($_GET['mb'])){
                                $mb_js = "setTimeout( function(){ top.load_shoppingcart() },500)";
                            }

                            switch ($cCode) {
                                case "_TR_3683":
                                    if (strlen($defVal) > 3) {
                                        $strContent .= "<a href='JavaScript:void(0);'
                                        onclick=\"document.getElementById('result').src='" . $iSpec['target'] . "?id=" . $iSpec['id'] . "$defVal'\">";
                                    }
                                    else {
                                        $strContent .= "<a href='JavaScript:void(0);' 
                                                        onclick=\" BootstrapDialog.show({
                                                            title: 'Attention !',
                                                            message: 'Out of <b class=\'font-size-1-5 text-red\'>" . $iSpec['produk_kode'] . " " . $iSpec['nama'] . "</b> stock '
                                                        });
                                                    \">";
                                    }
                                    break;
                                case "_TR_461":
                                    $strContent .= "<a href='javascript:void(0);' onclick=\"document.getElementById('result').src='" . $iSpec['target'] . "?selector&id=" . $iSpec['id'] . "$defVal';$mb_js\">";
                                    break;
                                default:
                                    $strContent .= "<a href='javascript:void(0);' onclick=\"document.getElementById('result').src='" . $iSpec['target'] . "?selector&id=" . $iSpec['id'] . "$defVal';$mb_js\">";
                                    break;
                            }
                        }
                        $strContent .= isset($iSpec['nama']) ? $iSpec['nama'] : "";
                        if(isset($_SESSION[$cCode]['items'][$iSpec['id']])){
                            $strContent .= "<span pid='".$iSpec['id']."' id='itmCheck_".$iSpec['id']."' class='itemCheck pull-right'> <i class='fa fa-check-square text-success text-bold'></i> </span>";
                        }
                        else{
                            $strContent .= "<span pid='".$iSpec['id']."' id='itmCheck_".$iSpec['id']."' class='itemCheck pull-right'></span>";
                        }
                        $strContent .= "</a>";
                        $strContent .= "</li>";
                    }
                }
                $strContent .= "<li class='list-group-item text-center text-muted' style='background:#e5e5c5;'><small>... type more keywords<br>for more specific results ...</small>";
                $strContent .= "</li>";
                $strContent .= "</ul class='list-group'>";
            }
            else {
                $strContent .= "<div class='form-control text-center'>";
                $strContent .= "- no matched item -";
                $strContent .= "</div>";
            }
        }
        else {
            $strContent .= "<div class='form-control text-center'>";
            $strContent .= "- no matched item -";
            $strContent .= "</div>";
        }
        if($cCode=="_TR_461"){
                $strContent .= "
            <script>
                var arrItemCheck = $('.itemCheck')
                var arrChecked = {};
                if(countObj(arrItemCheck)>0){
                    jQuery.each(arrItemCheck, function(i, b){
                        var pid = $(b).attr('pid');
                        if(arrChecked[pid]==undefined){
                            arrChecked[pid] = {}
                        }
                        arrChecked[pid] = 0
                    })
                    localStorage.sesCartBahan = JSON.stringify(arrChecked)
                }
                function selectorCheckAll(){
                    var sesCartBahan = localStorage.sesCartBahan != undefined ? JSON.parse(localStorage.sesCartBahan) : 0
                    var baseUrl = $('#baseURL').html()
                    if(sesCartBahan!=0&&countObj(sesCartBahan)>0){
                        $.ajax({
                            type: 'POST',
                            url: baseUrl + 'Selectors/_processSelectSupplies/gerbangBi/461?items=',
                            data: {items: JSON.stringify(sesCartBahan)},
                            success: function(data) {
                                $.ajax({
                                    type: 'GET',
                                    async: false,
                                    url: baseUrl + 'ValueGate/buildValues/461',
                                    success: function(data) {
                                        if($('#shopping_cart').length>0){
                                            $('#shopping_cart').load(baseUrl + '_shoppingCart/viewCart/461')
                                            $('input[name=text]').trigger('click')
                                        }
                                        else{
                                            top.load_shoppingcart();
                                            $('input[name=text]').trigger('click')
                                        }
                                        
                                    }
                                })
                            }
                        })
                    }
                    else{
                        swal('silahkan pilih bahan yang akan diinput stok / dibeli')
                    }
                }
            </script>
            ";
        }


        echo $strContent;
    break;
    case "view":
        //         arrprint($items);
        //        arrprint($socketURL);

        $strContent = "";
        if (isset($items) && sizeof($items) > 0) {
            if (is_array($items) && (sizeof($items) > 0)) {
                //$strContent .="<div>". json_encode($items) ."</div>";
                $strContent .= "<ul class='list-group text-left' style='line-height:13px;'>";
                foreach ($items as $iSpec) {
                    // arrPrint($iSpec);
                    $produk_id = $iSpec['id'];
                    if (isset($iSpec['minValue'])) {
                        $defaultValue = $iSpec['minValue'];
                        // cekBiru($defaultValue);
                        $defVal = "&minValue=$defaultValue";
                    }
                    else {
                        $defVal = "";
                    }
                    //                    $strContent .= "<div style='margin:1px;' class='panel no-padding text-bold text-left'>";
                    $bg_list = isset($iSpec['bg']) ? $iSpec['bg'] : "";
                    $strContent .= "<li class='list-group-item $bg_list'>";
                    if ($socketURL[$iSpec['id']] != "") {
                        if (sizeof($socketParams[$iSpec['id']]) > 0) {
                            foreach ($socketParams[$iSpec['id']] as $key => $src) {
                                if (isset($iSpec[$src])) {
                                    $iVal = addslashes($iSpec[$src]);
                                }
                                else {
                                    $iVal = 0;
                                }
                                $socketURL[$iSpec['id']] = str_replace("{" . $src . "}", $iVal, $socketURL[$iSpec['id']]);
                            }
                        }
                    }


                    //                    die($socketURL);

                    if (isset($socketURL[$iSpec['id']]) && strlen($socketURL[$iSpec['id']]) > 3) {
                        $actionTarget = "top.BootstrapDialog.show(                                   {
                                       title:'" . $iSpec['nama'] . "',
                                        message: " . 'top.$' . "('<div></div>').load('" . $socketURL[$iSpec['id']] . "&url=" . blobEncode("" . $iSpec['target'] . "?id=" . $iSpec['id'] . "$defVal") . "'),
                                        draggable:false,                                        
                                        type:top.BootstrapDialog.TYPE_DEFAULT,
                                        size:top.BootstrapDialog.SIZE_SMALL,
                                        closable:true,
                                        }
                                        );";

                        if (strlen($iSpec['target']) > 10) {
                            $strContent .= "<a href='JavaScript:void(0);'
                                        onclick=\"$actionTarget\" title='$produk_id'>";
                        }
                        else {
                            $strContent .= "";
                        }

                    }
                    else {
                        // cekBiru();
                        switch ($cCode) {
                            case "_TR_3683":
                                //                                cekHere(":: YES :: $defVal ::");
                                //                                if ($defVal > 0) {
                                if (strlen($defVal) > 3) {

                                    $strContent .= "<a href='JavaScript:void(0);'
                                        onclick=\"document.getElementById('result').src='" . $iSpec['target'] . "?id=" . $iSpec['id'] . "$defVal'\">";
                                }
                                else {

                                    $strContent .= "<a href='JavaScript:void(0);' 
                                    onclick=\" BootstrapDialog.show({
                                                    title: 'Attention !',
                                                    message: 'Out of <b class=\'font-size-1-5 text-red\'>" . $iSpec['produk_kode'] . " " . $iSpec['nama'] . "</b> stock '
                                                });
                                            \">";
                                }
                                break;
                            default:
                                if (strlen($iSpec['target']) > 10) {
                                    $strContent .= "<a href='JavaScript:void(0);'
                                        onclick=\"document.getElementById('result').src='" . $iSpec['target'] . "?id=" . $iSpec['id'] . "$defVal'\" title='$produk_id'>";
                                }
                                else {
                                    $strContent .= "";
                                }

                                break;
                        }

                    }

                    /* ------------------------------------------
                     * nama yg tampil diatur dr controler
                     * ---------------------------------------*/
                    // $strContent .= isset($iSpec['nama']) ? $iSpec['nama'] : "";
                    $label = strlen($iSpec['label']) > 0 ? $iSpec['label'] . " " : "";
                    $nama = strlen($iSpec['nama']) > 0 ? $iSpec['nama'] . " " : "";
                    $part = strlen($iSpec['no_part']) > 0 ? "<b class='text-green'>".$iSpec['no_part']."</b>" . " " : "";
                    $merek_nama = strlen($iSpec['merek_nama']) > 0 ? $iSpec['merek_nama'] . " " : "";
                    $kode = isset($iSpec['kode']) && strlen($iSpec['kode']) > 0 ? "<b class='text-red'>".$iSpec['kode']."</b>" . " " : "";

                    $strContent .= $kode. $nama . $label . $part . $merek_nama;
                    // $strContent .= $cCode;
                    //                    $strContent .= json_encode($iSpec);
                    if (strlen($iSpec['target']) > 10) {

                        $strContent .= "</a>";
                        if (isset($prod_suppliers[$produk_id])) {
                            $hpp_prod_supplier = $hpp_prod_suppliers[$produk_id];
                            // arrPrintWebs($hpp_prod_supplier);

                            foreach ($prod_suppliers[$produk_id] as $itemProdSupp) {
                                $supp_id = $itemProdSupp['supplier_id'];
                                $last_hpp = isset($hpp_prod_supplier[$supp_id]['nilai']) ? $hpp_prod_supplier[$supp_id]['nilai'] * 1 : "";
                                $last_hpp_f = $last_hpp > 0 ? "<b class='text-blue'>Rp." . formatField_he_format('angka_polos',$last_hpp)."</b>" : $last_hpp;
                                // $last_hpp_f = formatAngka($last_hpp);
                                $supp_kode = strlen($itemProdSupp['supplier_kode']) > 2 ? "<b class='text-blue'>" . $itemProdSupp['supplier_kode'] . "</b>" : "";
                                $supp_nama = strlen($itemProdSupp['supplier_nama']) > 2 ? "" . $itemProdSupp['supplier_nama'] . "" : "--";

                                $strContent .= isset($itemProdSupp['supplier_nama']) ? "<br><i title='$supp_id' class='text-orange'>$supp_kode $supp_nama $last_hpp_f</i>" : "-";
                            }
                        }
                        // }

                        if(isset($_SESSION[$cCode]['items'][$iSpec['id']])){
                            $strContent .= "<span class='pull-right text-bold text-green'><i class='fa fa-check'></i></span>";
                        }
                        else{
                            $strContent .= "<span class='pull-right' id='".$iSpec['id']."'></span>";
                        }

                    }
                    else {
                        $strContent .= "";
                    }
                    //                    $strContent .= "</div>";
                    $strContent .= "</li>";
                }
                $strContent .= "<li class='list-group-item text-center text-muted' style='background:#e5e5c5;'><small>... type more keywords<br>for more specific results ...</small>";
                $strContent .= "</li>";
                $strContent .= "</ul class='list-group'>";
            }
            else {
                $strContent .= "<div class='form-control text-center'>";
                $strContent .= "- no matched item -";
                $strContent .= "</div>";
            }
        }
        else {
            $strContent .= "<div class='form-control text-center'>";
            $strContent .= "- no matched item -";
            $strContent .= "</div>";
        }

        echo $strContent;

        break;
    default:
        die('mode tidak ditentukan');
    break;
}