<?php

/**
 * Created by PhpStorm.
 * User: chepy
 * Date: 9/17/2022
 * Time: 2:51 PM
 */
require_once "Modul_Controller.php";

class _selectorItem extends Modul_Controller
{
    public function __construct()
    {
        parent::__construct();
        // arrPrint($this->uri->segment_array());
        $this->load->library("MobileDetect");
    }

    public function selectItem()
    {
//        matiHere(__LINE__);
        // arrPrint($this->uri->segment_array());
        $mb = New MobileDetect();
        $isMob = $mb->isMobile();
        $jenisTr = $this->jenisTr;
        $cCode = $this->cCode;

        $cekES = isset($_SESSION[$cCode]['main']['pihakMdlName']) ? $_SESSION[$cCode]['main']['pihakMdlName'] : "";
        $cID = isset($_SESSION[$cCode]['main']['placeID']) ? $_SESSION[$cCode]['main']['placeID'] : $this->session->login['cabang_id'];
        $gID = isset($_SESSION[$cCode]['main']['gudangID']) ? $_SESSION[$cCode]['main']['gudangID'] : $this->session->login['gudang_id'];
        $tkID = isset($_SESSION[$cCode]['main']['tokoID']) ? $_SESSION[$cCode]['main']['tokoID'] : (isset($this->session->login['toko_id'])? $this->session->login['toko_id'] : "");

        $mdlName = isset($_SESSION[$cCode]['main']['pihakMdlName']) ? $_SESSION[$cCode]['main']['pihakMdlName'] : $this->uri->segment(5);

        $fields = $this->configUi[$jenisTr]['selectorFields'];

        $modelFilter = isset($this->configUi[$jenisTr]['selectorFilters']) ? $this->configUi[$jenisTr]['selectorFilters'] : array();
        $modelFilterCustom = isset($this->configUi[$jenisTr]['selectorMainFilters']) ? $this->configUi[$jenisTr]['selectorMainFilters'] : array();
        $modelSrcFilter = isset($this->configUi[$jenisTr]['selectorSrcFilters']) ? $this->configUi[$jenisTr]['selectorSrcFilters'] : array();

        $selectorFields = isset($this->configUi[$jenisTr]['selectorViewedFields']) ? $this->configUi[$jenisTr]['selectorViewedFields'] : array();
        $selectorNaming = isset($this->configUi[$jenisTr]['selectorViewedNames']) ? $this->configUi[$jenisTr]['selectorViewedNames'] : array();
        $selectorParamFields = isset($this->configUi[$jenisTr]['selectorParamFields']) ? $this->configUi[$jenisTr]['selectorParamFields'] : array();
        $selectorSrcParamFields = isset($this->configUi[$jenisTr]['selectorSrcParamFields']) ? $this->configUi[$jenisTr]['selectorSrcParamFields'] : array();
        $selectorMainFields = isset($this->configUi[$jenisTr]['selectorMainViewedFields']) ? $this->configUi[$jenisTr]['selectorMainViewedFields'] : array();
        $selectorMainParamFields = isset($this->configUi[$jenisTr]['selectorMainParamFields']) ? $this->configUi[$jenisTr]['selectorMainParamFields'] : array();
        $selectorModel = isset($this->configUi[$jenisTr]['selectorModel']) ? $this->configUi[$jenisTr]['selectorModel'] : "MdlProduk";
        $selectorSrcModel = isset($this->configUi[$jenisTr]['selectorSrcModel']) ? $this->configUi[$jenisTr]['selectorSrcModel'] : "MdlProduk";
        $selectorView = isset($this->configUi[$jenisTr]['selectorView']) ? $this->configUi[$jenisTr]['selectorView'] : "_selector";
        // cekMerah($selectorView);
        $selectorDefaultMinValue = isset($this->configUi[$jenisTr]['selectorDefaultMinValue']) ? $this->configUi[$jenisTr]['selectorDefaultMinValue'] : "0";
        $key = isset($_GET['search']) ? $_GET['search'] : (isset($_POST['search']) ? $_POST['search'] : "");
        $setQty = isset($_GET['qty_produk']) ? $_GET['qty_produk'] : (isset($_POST['qty_produk']) ? $_POST['qty_produk'] : 1);
        $preLocker = isset($this->configUi[$jenisTr]['validLocker']) ? $this->configUi[$jenisTr]['validLocker'] : false;
        $selectorOrderBy = isset($this->configUi[$jenisTr]['selectorOrderBy']) ? $this->configUi[$jenisTr]['selectorOrderBy'] : NULL;
        $autoCreateOrderId = isset($this->configUi[$jenisTr]['autoCreateOrderId']) ? $this->configUi[$jenisTr]['autoCreateOrderId'] : false;

        if($autoCreateOrderId){
            if( !isset($_SESSION[$cCode]['main']['shoppingcart_order_id']) ){
                $_SESSION[$cCode]['main']['shoppingcart_order_id'] = strtotime(date("Y-m-d H:i:s"));
            }
        }

        $items = array();

        // detektor tanda kurawal {}
        if (substr($selectorModel, 0, 1) == "{") {
            $selectorModel = trim($selectorModel, "{");
            $selectorModel = trim($selectorModel, "}");
            $selectorModel = str_replace($selectorModel, $_SESSION[$cCode]['main'][$selectorModel], $selectorModel);
        }
        else {
            //            cekkuning("TIDAK mengandung kurawal");
        }
        if (substr($selectorSrcModel, 0, 1) == "{") {
            $selectorSrcModel = trim($selectorSrcModel, "{");
            $selectorSrcModel = trim($selectorSrcModel, "}");
            $selectorSrcModel = str_replace($selectorSrcModel, $_SESSION[$cCode]['main'][$selectorSrcModel], $selectorSrcModel);
        }
        else {
            //            cekkuning("TIDAK mengandung kurawal");
        }

        if($preLocker){
            $mdlPreLocker = $this->configUi[$jenisTr]["lockerCheck"]["mdlName"];
            $this->load->model("Mdls/" . $mdlPreLocker);
            $pl = new $mdlPreLocker();
        }

        $this->load->model("Mdls/" . $mdlName);
        $o = new $mdlName();
        $o->setFilters(array());

        //pairing produk
        $this->load->model("Mdls/" . $selectorSrcModel);

        $b = new $selectorSrcModel();

        $arrFilterCustom = array();
        $filterCustom = false;
        if (sizeof($modelFilterCustom) > 0) {
            if (isset($modelFilterCustom[$_SESSION[$cCode]['main']['pihakMainName']])) {
                $arrFilterCustom = $modelFilterCustom[$_SESSION[$cCode]['main']['pihakMainName']];
                $filterCustom = true;
            }
            else {
                $filterCustom = false;
            }
        }
        else {
            $filterCustom = false;
        }

        if ($filterCustom == true) {
            if (sizeof($arrFilterCustom) > 0) {
                makeFilter($arrFilterCustom, $_SESSION[$cCode]['main'], $o);
            }
            $selectorFields = $selectorMainFields[$_SESSION[$cCode]['main']['pihakMainName']];
            $selectorParamFields = $selectorMainParamFields[$_SESSION[$cCode]['main']['pihakMainName']];

            $selectorProcessor = $this->configUi[$jenisTr]['selectorMainProcessor'][$_SESSION[$cCode]['main']['pihakMainName']];
            $processor = MODUL_PATH . $selectorProcessor . "/$jenisTr";
        }
        else {
            if (sizeof($modelFilter) > 0) {
                foreach ($modelFilter as $f) {
                    $f_ex = explode("=", $f);
                    if (!isset($f_ex[1])) {
                        // cekHitam();
                        $f_ey = explode(">", $f_ex[0]);
                        if (substr($f_ey[1], 0, 1) == ".") {
                            $o->addFilter($f_ey[0] . ">'" . ltrim($f_ey[1], ".") . "'");
                        }
                        else {
                            if (isset($_SESSION[$cCode]['main'][$f_ey[1]])) {
                                $o->addFilter($f_ey[0] . ">'" . $_SESSION[$cCode]['main'][$f_ey[1]] . "'");
                            }
                            else {
                                $o->addFilter($f_ey[0] . ">0");
                            }
                        }
                    }
                    else {
                        if (substr($f_ex[1], 0, 1) == ".") {
                            $o->addFilter($f_ex[0] . "='" . ltrim($f_ex[1], ".") . "'");
                        }
                        else {
                            if (isset($_SESSION[$cCode]['main'][$f_ex[1]])) {
                                $o->addFilter($f_ex[0] . "='" . $_SESSION[$cCode]['main'][$f_ex[1]] . "'");
                            }
                            else {
                                $o->addFilter($f_ex[0] . "=''");
                            }
                        }
                    }
                }
            }
            $processor = $this->modulPath . $this->configUi[$jenisTr]['selectorProcessor'] . "/$jenisTr";

        }

//         mati_disini($processor);
        /* ----------------------------------------------------------------------------------------------------------
         * bila ada main session yg ilang saat clear shoping cart ditambah di inisiasi _shopingCart/reset
         * ----------------------------------------------------------------------------------------------------------*/
//        $this->db->limit(20); //dimatikan karena tidak bisa select all produk

        //ini di paksa order dari data terakhir

        if($jenisTr=='582'){
            $this->db->order_by("id", "asc");
        }
        else{
            $this->db->order_by("id", "desc");
        }

        if($key==""){
            $this->db->limit(10);
        }
        else if(strlen($key)<3){
            $this->db->limit(20);
        }
        else{
            $this->db->limit(20);
        }

         $o->addFilter("status='1'");
//         arrPrint($modelFilter);

//        $o->setFilters(array());
        $tmpO = $o->lookupByKeyword($key)->result();

//        cekLime( $this->db->last_query() );

//        cekHitam($key);
//        cekHitam($mdlName);
//        matiHEre();
//        arrPrint($tmpO);
//        matiHere(__LINE__);
        /*
         * jika tmp0 kosong,
         * maka cari lagi ke satuan relasi dengan membalikan id relasi produk.
         */
//        if(empty($tmpO) && $key!=''){
//            $arrBarcodeSatuan=array();
//            $this->load->model("Mdls/MdlProdukSatuanRelasi");
//            $psr = new MdlProdukSatuanRelasi();
//            $psr->setFilters(array());
//            $psr->addFilter("trash=0");
//            $psrTmp = $psr->lookupAll()->result();
//
//            if(!empty($psrTmp)){
//                foreach($psrTmp as $bt => $bc){
//                    $arrBarcodeSatuan[$bc->barcode] = $bc;
//                }
//            }
//            $tmpO = array();
//            if(isset($arrBarcodeSatuan[$key])){
//                $bc_produk_id = $arrBarcodeSatuan[$key]->produk_id;
//                $tmp2 = $o->lookupByID($bc_produk_id)->result();
//                if(!empty($tmp2)){
//                    foreach($tmp2 as $k => $dtB){
//                        $tmpO[$k] = $dtB;
//                    }
//                }
//                $tmpO[0]->satuan_id     = $arrBarcodeSatuan[$key]->satuan_id;
//                $tmpO[0]->satuan_qty    = $arrBarcodeSatuan[$key]->qty;
//                $tmpO[0]->satuan_nama   = $arrBarcodeSatuan[$key]->satuan_nama;
//                $tmpO[0]->satuan        = $arrBarcodeSatuan[$key]->satuan_nama;
//
////                $tmpO[0]->jumlah = $arrBarcodeSatuan[$key]->qty;
//
//                //inject untuk pengenal barcodesatuan
//                $tmpO[0]->barcodesatuan = "$key";
//            }
//        }

        /*
         * PERHATIAN...!! $tmp0 menggunakan 2 sumber
         * jika sumber pertama tidak di temukan, maka akan mencari pada sumber kedua yaitu satuan multi barcode...
         */

        $use_numpad = 0;

        if (sizeof($tmpO) > 0) {

            $socketConfig = "";
            if(isset($this->configUi[$jenisTr]['selectorSocketNumpad']) && $use_numpad) {
                $socketConfig = $this->configUi[$jenisTr]['selectorSocketNumpad'];
            }
            else{
                if(isset($this->configUi[$jenisTr]['selectorSocket'])){
                    $socketConfig = $this->configUi[$jenisTr]['selectorSocket'];
                }
            }

            $socketParams = array();
            $socketURL = array();

            if (sizeof($modelSrcFilter) > 0) {
                makeFilter($modelSrcFilter, $_SESSION['login'], $b);
            }

            $prodIds = array();
            if ($selectorModel == 'MdlProduk') {
                foreach ($tmpO as $prodItems) {
                    $prodIds[] = $prodItems->id;
                }
            }

            /* -------------------------------------------------
             * aslinya ada dalam foreach dibawah enih, namun performenya akan buruk dikarekan selec yg dalam perulangan
             * ----------------------------------------------*/
            switch ($selectorModel) {
                case "MdlProduk":
                case "MdlProduk2":
                case "MdlNotaItem":
                    if (sizeof($modelFilter) > 0) {
                        foreach ($modelFilter as $f) {
                            $f_ex = explode("=", $f);
                            if (!isset($f_ex[1])) {
                                $f_ey = explode(">", $f_ex[0]);
                                if (substr($f_ey[1], 0, 1) == ".") {
                                    $b->addFilter($f_ey[0] . ">'" . ltrim($f_ey[1], ".") . "'");
                                }
                                else {
                                    if (isset($_SESSION[$cCode]['main'][$f_ey[1]])) {
                                        $b->addFilter($f_ey[0] . ">'" . $_SESSION[$cCode]['main'][$f_ey[1]] . "'");
                                    }
                                    else {
                                        $b->addFilter($f_ey[0] . ">0");
                                    }
                                }
                            }
                            else {
                                if (substr($f_ex[1], 0, 1) == ".") {
                                    $o->addFilter($f_ex[0] . "='" . ltrim($f_ex[1], ".") . "'");
                                }
                                else {
                                    if (isset($_SESSION[$cCode]['main'][$f_ex[1]])) {
                                        $b->addFilter($f_ex[0] . "='" . $_SESSION[$cCode]['main'][$f_ex[1]] . "'");
                                    }
                                    else {
                                        $b->addFilter($f_ex[0] . "=''");
                                    }
                                }
                            }
                        }
                    }
                    break;
            }

//            $this->db->limit(100);
//            $dataSrc = $b->lookupAll()->result();
            $dataSrc = $b->lookupByID($prodIds)->result();

//            arrPrintWebs( $prodIds );
//            cekHitam( $this->db->last_query() );

            $tmpP = array();
            foreach ($dataSrc as $srcItems) {
                $main_key = isset($selectorSrcParamFields['id']) ? $selectorSrcParamFields['id'] : "id";
                if(isset($srcItems->$main_key)){
                    $tmpP[$srcItems->$main_key] = $srcItems;
                }
            }

            $colors = array(
                "#000000",
                "#0056cd",
                "#ff7700",
                "#009900",
                "#9999cc",
            );

//            rsort($tmpO);



            //diskon di keluarkan hanya jika dapet 1 produk
//            $jmlTmpDataPrd = count($tmpO);
            $src_diskon = array();

            foreach ($tmpO as $row) {

                $satuan = isset($row->satuan) && strlen($row->satuan) > 0 ? $row->satuan : "n/a";
                $pID = isset($row->produk_id) ? $row->produk_id : $row->id;
                $barcodesatuan = isset($row->barcodesatuan) ? $row->barcodesatuan : 0;

                //tambahan untuk handle pengenalan produk digital dan non digital
                $prd_jenis = isset($row->jenis) ? $row->jenis : "item";
                $tr_id_digital = isset($row->tr_id_digital) ? $row->tr_id_digital : 0;
                $digi_blob = isset($row->digi_blob) ? $row->digi_blob : "";

//                if($jmlTmpDataPrd==1){
//                    //cek avail diskon
//                    $this->load->library("Diskon");
//                    $dp = new Diskon();
//                    $dp->setTokoId(my_toko_id());
//                    $src_diskon = $dp->CallProdukDiskon($pID);
//                }
                /* ------------------------------------------------------
                 * ngelokup dalam foreacht harap dipertimbangkan ni loadtimenya
                 * ------------------------------------------------------*/
                $defaultValue = isset($tmpP[$pID]->moq) ? $tmpP[$pID]->moq : $selectorDefaultMinValue;
                foreach ($selectorParamFields as $key => $src) {
                    $tmp[$key] = isset($row->$src) && $row->$src != "" ? $row->$src : "$key - null ";
                }

                $tmp['minValue'] = $defaultValue;
                $tmp['setQty'] = isset($setQty) ? $setQty : 0;

                $tmp['tipe_produk'] = $prd_jenis;
                $tmp['tr_id_digital'] = $tr_id_digital;
                $tmp['digi_blob'] = $digi_blob;

                //pengenal jika menggunakan barcodesatuan
                if($barcodesatuan){
                    $tmp['barcodesatuan']    = $barcodesatuan;
                    $tmp['satuan_id']       = $row->satuan_id;
                    $tmp['satuan_nama']     = $row->satuan_nama;
                    $tmp['satuan_qty']      = $row->satuan_qty;
                }

                // ------------------------------------------------------------
                switch ($selectorModel) {
                    case "MdlProduk":
                    case "MdlProduk2":
                        /* ------------------------------
                         * pembeda warna dan link
                         * ---------------------------------*/
                        if (isset($tmpP[$pID]->jumlah) && $tmpP[$pID]->jumlah > 0) {
                            $tmp['target'] = $processor;
                            $tmp['bg'] = "text-red";
                        }
                        else {
                            $tmp['target'] = $processor;
                            $tmp['bg'] = "";
                        }
                        break;
                    default:
                        $tmp['target'] = $processor;
                        break;
                }

                // ----------------------------------------------------------------------------
                $tmp['target'] = $processor;
                $tmp['label'] = "";

                if (sizeof($selectorFields) > 0) {
                    $nCtr = 0;
                    foreach ($selectorFields as $ttt =>$f) {
                        $nilai = isset($row->$f) ? $row->$f : " /";
                        $nCtr++;
                        $align = $nCtr == 1 ? "text-left" : "text-right";
                        $fSize = $nCtr == 1 ? "font-size:1em" : "font-size:0.9em";
                        $color = isset($colors[$nCtr]) ? $colors[$nCtr] : "#000000";
                        if ($f != 'kode' && is_numeric($nilai)) {
                            $tmp['label'] .= "<span style='$fSize ;margin:0px 2px 0px 2px;color:$color;' class='no-padding no-border $align'>" . number_format($nilai) . "</span>";
                        }
                        else {
                            $newFields = in_array($f, arrAvailFields()) ? formatNota($f, $nilai) : $nilai;
                            $tmp['label'] .= "<span style='$fSize ;margin:0px 2px 0px 2px;color:$color;' class='no-padding text-uppercase no-border $align'><i>" . $newFields . "</i></span>";
                        }
                    }
                    $tmp['label'] = rtrim($tmp['label'], "| ");
                    $tmp['label'] = ($tmp['label']);
                }

                $addParams = array(
                    "cCode" => $cCode,
                );

                if($use_numpad){
                    $socketURL[$tmp['id']] = isset($this->configUi[$jenisTr]['selectorSocketNumpad']) ? MODUL_PATH . $this->configUi[$jenisTr]['selectorSocketNumpad']['targetURL'] . "?" : "";
                }
                else{
                    $socketURL[$tmp['id']] = isset($this->configUi[$jenisTr]['selectorSocket']) ? MODUL_PATH . $this->configUi[$jenisTr]['selectorSocket']['targetURL'] . "?" : "";
                }

                $socketParams[$tmp['id']] = isset($socketConfig['inParams']) ? $socketConfig['inParams'] : array();

                if (isset($socketParams[$tmp['id']]) && sizeof($socketParams[$tmp['id']]) > 0) {
                    foreach ($socketParams[$tmp['id']] as $key => $src) {
                        $socketURL[$tmp['id']] .= "&$key={" . $src . "}";
                    }
                    if (sizeof($addParams) > 0) {
                        foreach ($addParams as $key => $src) {
                            $socketURL[$tmp['id']] .= "&$key=$src";
                        }
                    }
                }

                $arrReady = array();

                if ($preLocker) {
                    $stokLocker = $pl->cekLoker($cID, $pID, "active", "", "", $gID);
                    $valLocker = isset($stokLocker['jumlah']) ? $stokLocker['jumlah'] : 0;

                    if ($valLocker > 0) {
                        $items[] = $tmp;
                    }
                }
                else {

                    $items[] = $tmp;
                    if ($selectorModel == 'MdlProduk') {
                        if ($tmp['jumlah'] == 0) {
                            $arrKosong[] = $tmp;
                        }
                        else {
                            $arrReady[] = $tmp;
                        }
                        /* ---------------------------------------------------------
                         * data ini yg ditampilkan pada selektor
                         * -------------------------------------------------------*/
                        $items = array_merge( count($arrReady) > 0 ? $arrReady : array() , count($arrKosong)>0 ? $arrKosong : array() );
                    }
                }
            }

        }
        else {
            //            cekhitam("tidak ada data");
        }

//arrPrintKuning($items);
//        mati_disini(__LINE__);
        $data = array(
            "mode"           => $isMob ? "viewOri" : "view",
            "selectorNaming" => $selectorNaming,
            "cCode"          => "$cCode",
            "items"          => $items,
            "socketParams"   => isset($socketParams) ? $socketParams : array(),
            "socketURL"      => isset($socketURL) ? $socketURL : array(),
            "use_numpad"     => $use_numpad,
            "src_diskon"     => !empty($src_diskon) ? json_encode($src_diskon) : json_encode(array()),
        );

        $this->load->view("$selectorView", $data);

    }

    public function selectItem2()
    {

        $jenisTr = $this->uri->segment(4);
        $cCode = "_TR_" . $jenisTr;
        $mdlName = $this->uri->segment(5);


        $fields = $this->configUi[$jenisTr]['selectorFields'];
        $modelFilter = isset($this->configUi[$jenisTr]['selectorFilters']) ? $this->configUi[$jenisTr]['selectorFilters'] : array();
        $selectorFields = isset($this->configUi[$jenisTr]['selectorViewedFields']) ? $this->configUi[$jenisTr]['selectorViewedFields'] : array();
        $selectorParamFields = isset($this->configUi[$jenisTr]['selectorParamFields']) ? $this->configUi[$jenisTr]['selectorParamFields'] : array();

        $selectorModel = isset($this->configUi[$jenisTr]['selectorModel2']) ? $this->configUi[$jenisTr]['selectorModel2'] : "MdlProduk";
        $selectorSrcModel = isset($this->configUi[$jenisTr]['selectorSrcModel2']) ? $this->configUi[$jenisTr]['selectorSrcModel2'] : "MdlProduk";

        $key = isset($_GET['search']) ? $_GET['search'] : "";
        $items = array();

        $this->load->model("Mdls/" . $mdlName);
        $o = new $mdlName();

        //pairing produk
        $this->load->model("Mdls/" . $selectorSrcModel);
        $b = new $selectorSrcModel();

        if (sizeof($modelFilter) > 0) {
            foreach ($modelFilter as $f) {
                $f_ex = explode("=", $f);
                if (!isset($f_ex[1])) {
                    $f_ey = explode(">", $f_ex[0]);
                    if (substr($f_ey[1], 0, 1) == ".") {
                        $o->addFilter($f_ey[0] . ">'" . ltrim($f_ey[1], ".") . "'");
                    }
                    else {
                        if (isset($_SESSION[$cCode]['main'][$f_ey[1]])) {

                            $o->addFilter($f_ey[0] . ">'" . $_SESSION[$cCode]['main'][$f_ey[1]] . "'");
                        }
                    }
                }
                else {
                    if (substr($f_ex[1], 0, 1) == ".") {
                        $o->addFilter($f_ex[0] . "='" . ltrim($f_ex[1], ".") . "'");
                    }
                    else {
                        if (isset($_SESSION[$cCode]['main'][$f_ex[1]])) {
                            $o->addFilter($f_ex[0] . "='" . $_SESSION[$cCode]['main'][$f_ex[1]] . "'");
                        }

                    }
                }
            }
        }

        //        $o->createSmartSearch($key,$o->getListedFieldsSelectItem());
        $tmpO = $o->lookupByKeyword($key)->result();
        //        cekmerah($this->db->last_query());

        if (sizeof($tmpO) > 0) {
            $processor = MODUL_PATH . $this->configUi[$jenisTr]['selectorProcessor2'] . "/$jenisTr";

            if (isset($this->configUi[$jenisTr]['selectorSocket'])) {
                $socketConfig = $this->configUi[$jenisTr]['selectorSocket'];
            }
            $socketParams = array();
            $socketURL = array();

            //            arrprint($tmpO);

            foreach ($tmpO as $row) {
                $satuan = isset($row->satuan) && strlen($row->satuan) > 0 ? $row->satuan : "n/a";
                $pID = isset($row->produk_id) ? $row->produk_id : $row->id;

                $b->addFilter("id=" . $pID);
                $tmpP = $b->lookupAll($pID)->result();
                //                cekkuning($this->db->last_query());
                $defaultValue = isset($tmpP[0]->moq) ? $tmpP[0]->moq : 0;
                foreach ($selectorParamFields as $key => $src) {
                    $tmp[$key] = $row->$src;
                }
                $tmp['minValue'] = $defaultValue;
                $tmp['target'] = $processor;
                $tmp['label'] = "";
                if (sizeof($selectorFields) > 0) {
                    foreach ($selectorFields as $f) {
                        if (is_numeric($row->$f)) {
                            $tmp['label'] .= "" . number_format($row->$f) . " | ";
                        }
                        else {
                            $tmp['label'] .= "" . $row->$f . " | ";
                        }
                    }
                    $tmp['label'] = rtrim($tmp['label'], "| ");
                    //                    $tmp['label'] = "<div class='no-padding'>". $tmp['label'] . "</div> " . $row->jumlah ;
                    //                    $tmp['label'] = "<div style='font-size:0.8em' class='no-padding'>" . ($tmp['label']) . "</div> ";
                    $tmp['label'] = "<div style='font-size:0.8em' class='no-padding'>" . ($tmp['label']) . "</div> ";

                }


                $addParams = array(
                    "cCode" => $cCode,
                );
                $socketURL[$tmp['id']] = isset($this->configUi[$jenisTr]['selectorSocket']) ? MODUL_PATH . $this->configUi[$jenisTr]['selectorSocket']['targetURL'] . "?" : "";
                $socketParams[$tmp['id']] = isset($socketConfig['inParams']) ? $socketConfig['inParams'] : array();
                if (isset($socketParams[$tmp['id']]) && sizeof($socketParams[$tmp['id']]) > 0) {
                    foreach ($socketParams[$tmp['id']] as $key => $src) {
                        $socketURL[$tmp['id']] .= "&$key={" . $src . "}";
                    }
                    if (sizeof($addParams) > 0) {
                        foreach ($addParams as $key => $src) {
                            $socketURL[$tmp['id']] .= "&$key=$src";
                        }
                    }
                }


                $items[] = $tmp;
            }
        }


        $data = array(
            "mode"         => "view",
            "cCode"        => "$cCode",
            //            "arrayFields"=>$selectorFields,
            "items"        => $items,
            "socketParams" => isset($socketParams) ? $socketParams : array(),
            "socketURL"    => isset($socketURL) ? $socketURL : "",
        );


        //        arrprint($data);die();

        $this->load->view("_selector", $data);

    }

}