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


switch ($mode) {

    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);
                    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\">";
                        }
                        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'\">";
                                }
                                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 .= $label;
                    // $strContent .= $cCode;
                    //                    $strContent .= json_encode($iSpec);
                    if (strlen($iSpec['target']) > 10) {

                        $strContent .= "</a>";
                        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;


}