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

switch ($mode) {
    case "view":
        $strContent = "";
        $barcodesatuan = "";

//        arrPrint($items);
        if (isset($items) && sizeof($items) > 0) {
            if (is_array($items) && (sizeof($items) > 0)) {
                if( count($items) == 1 ){

                    if(isset($items[0]['barcodesatuan'])){
                        $arrBarcodeSatuan = array(
                            "barcodesatuan" => $items[0]['barcodesatuan'],
                            "satuan_nama"   => $items[0]['satuan_nama'],
                            "satuan_id"     => $items[0]['satuan_id'],
                            "satuan_qty"    => $items[0]['satuan_qty'],
                        );
                        $barcodesatuan = "&barcodesatuan=".blobEncode($arrBarcodeSatuan);
                    }

//                    $strContent .= "langsung aja select | " . $items[0]['nama'] . " ID: " . $items[0]['id'];
                    if (isset($items[0]['minValue'])) {
                        $defaultValue = $items[0]['minValue'];
                        $defVal = "&minValue=$defaultValue";
                    }
                    else {
                        $defVal = "";
                    }

                    if (isset($items[0]['setQty'])) {
                        $defaultValue = $items[0]['setQty'];
                        $setQty = "&qty_unit=$defaultValue";
                    }
                    else {
                        $setQty = "";
                    }
                    $result = array(
                        "status" => 1,
                        "js" => "top.document.getElementById('result').src='" . $items[0]['target'] . "?selector&id=" . $items[0]['id'] . "$barcodesatuan$setQty';",
                        "produk" => array_slice($items, 0, 5),
                        "handler" => "" . $items[0]['target'] . "?selector&id=",
                        "handler2" => "$barcodesatuan$setQty",
                        "src_diskon" => "$src_diskon",
                    );

//                    $strContent .= "";

                }
                else{

                    $result = array(
                        "status" => 2,
                        "js" => "",
                        "produk" => array_slice($items, 0, 10),
                        "handler" => "" . $items[0]['target'] . "?selector&id=",
                        "handler2" => "",
                        "src_diskon" => "$src_diskon",
                    );

                }
            }
            else {
//                $strContent .= "<div class='form-control text-center'>";
//                $strContent .= "- no matched item -";
//                $strContent .= "</div>";
                $result = array(
                    "status" => 0,
                    "js" => "top.swal('error','kode tidak ditemukan', 'error'),setTimeout( function(){ top.swal.close(),top.$('#addNewSKU').val(''),top.$('#addNewSKU').focus() }, 2500)"
                );
//                $strContent .= "";
            }
        }
        else {
//            $strContent .= "<div class='form-control text-center'>";
//            $strContent .= "- no matched item -";
//            $strContent .= "</div>";

            $result = array(
                "status" => 0,
                "js" => "top.swal('error','kode tidak ditemukan', 'error'),setTimeout( function(){ top.swal.close(),top.$('#addNewSKU').val(''),top.$('#addNewSKU').focus() }, 2500)"
            );

//            $strContent .= "";
        }

        // echo "<pre>";
        // print_r($result);
        // echo "</pre>";

        echo json_encode($result);

    break;
    case "viewOri":
        $strContent = "";
        $barcodesatuan = "";
        if (isset($items) && sizeof($items) > 0) {
            if (is_array($items) && (sizeof($items) > 0)) {

                $strContent .= "<ul class='list-group text-left' style='line-height:13px;'>";
                foreach ($items as $iSpec) {
                    if(isset($iSpec['barcodesatuan'])){
                        $arrBarcodeSatuan = array(
                            "barcodesatuan" => $iSpec['barcodesatuan'],
                            "satuan_nama"   => $iSpec['satuan_nama'],
                            "satuan_id"     => $iSpec['satuan_id'],
                            "satuan_qty"    => $iSpec['satuan_qty'],
                        );
                        $barcodesatuan = "&barcodesatuan=".blobEncode($arrBarcodeSatuan);
                    }
                    if(isset($iSpec['nama']) && strlen($iSpec['nama'])>0 || isset($iSpec['label']) && strlen($iSpec['label'])>0){
                        if (isset($iSpec['minValue'])) {
                            $defaultValue = $iSpec['minValue'];
                            $defVal = "&minValue=$defaultValue";
                        }
                        else {
                            $defVal = "";
                        }

                        $strContent .= "<li style='padding: 6px;' 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 && $use_numpad) {
                            $actionTarget = "top.BootstrapDialog.show({
                                                title: '" . (isset($iSpec['nama']) ? $iSpec['nama'] : $iSpec['label']) . "',
                                                message: " . '$' . "(`<div class='text-center'><img src='".base_url()."/public/images/sys/loader-100.gif'></div>`).load('" . $socketURL[$iSpec['id']] . "$barcodesatuan&url=" . blobEncode("" . $iSpec['target'] . "?id=" . $iSpec['id'] . "$defVal") . "'),
                                                draggable: false,
                                                type: top.BootstrapDialog.TYPE_DEFAULT,
                                                size: top.BootstrapDialog.SIZE_SMALL,
                                                closable: true,
                                                animate: false,
                                            });";
                            $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) {
                                default:
                                    $strContent .= "<a href='javascript:void(0);' onclick=\"document.getElementById('result').src='" . $iSpec['target'] . "?selector&id=" . $iSpec['id'] . "$defVal';$mb_js\">";
                                    break;
                            }

                        }

                        $label = strlen($iSpec['label']) > 0 ? $iSpec['label'] . "" : "";

                        $strContent .= isset($iSpec['nama']) ? strtoupper($iSpec['nama']) : "";
                        $strContent .= $label!="" ? $label : "";

                        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 .= "<span data-toggle='collapse' data-target='#collapse_".$iSpec['id']."' aria-expanded='false' aria-controls='collapse_".$iSpec['id']."' class='pull-right collapsible'><i class='fa fa-caret-down'></i></span>";

                        //dimatikan dulu
//                        $strContent .= "<div id='collapse_".$iSpec['id']."' class='panel panel-default no-margin collapse' data-parent='#accordion'>";
//                        $strContent .= "<i class='text-warning'>belum ada data tambahan....</i>";
//                        $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>";

                $strContent .= "<script>";
                $strContent .= " $('.collapsible').click(function(){
                                    console.log(this);
                                 }) ";
                $strContent .= "</script>";
            }
            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;
}