<?php

/**
 * Created by PhpStorm.
 * User: aziz
 * Date: 4/16/2019
 * Time: 6:40 PM
 */
require_once "Modul_Controller.php";

class BarcodeReader extends Modul_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function readCode(){
        $srcModel=$_GET['srcModel'];
        $srcColumn=$_GET['srcColumn'];
        $selectorProcessor=blobDecode($_GET['proc']);
        $jenisTr=$_GET['jenisTr'];
        $key_code = $_GET['code'];
        $use_numpad = isset($_GET['use_numpad']) ? $_GET['use_numpad'] : 0;
        $cCode = "_TR_" . $jenisTr;


        if (isset($this->configUi[$jenisTr]['selectorSocketNumpad']) && $use_numpad) {
            cekMerah("numpad mode");
            $socketConfig = $this->configUi[$jenisTr]['selectorSocketNumpad'];
            $socketURL = MODUL_PATH . $this->configUi[$jenisTr]['selectorSocketNumpad']['targetURL'] . "?";
            $addParams = array(
                "cCode" => $cCode,
            );
            $socketParams = $socketConfig['inParams'];
            if (sizeof($socketParams) > 0) {
                foreach ($socketParams as $key => $src) {
                    $socketURL .= "&$key={" . $src . "}";
                }
            }
            if (sizeof($addParams) > 0) {
                foreach ($addParams as $key => $src) {
                    $socketURL .= "&$key=$src";
                }
            }
        }
        else{
            cekMerah("normal mode");
            $socketConfig = $this->configUi[$jenisTr]['selectorSocket'];
            $socketURL = MODUL_PATH . $this->configUi[$jenisTr]['selectorSocket']['targetURL'] . "/$jenisTr?select";
            $addParams = array(
                "cCode" => $cCode,
            );
            $socketParams = $socketConfig['inParams'];
            if (sizeof($socketParams) > 0) {
                foreach ($socketParams as $key => $src) {
                    $socketURL .= "&$key={" . $src . "}";
                }
            }
            if (sizeof($addParams) > 0) {
                foreach ($addParams as $key => $src) {
                    $socketURL .= "&$key=$src";
                }
            }
        }

        $this->load->model("Mdls/$srcModel");
        $o=new $srcModel();
        $o->addFilter("$srcColumn='".$_GET['code']."'");
        $tmp=$o->lookupAll()->result();

        /*
         * jika tmp kosong,
         * maka cari lagi ke satuan relasi dengan membalikan id relasi produk.
         */

//        arrPrint($tmp);
//        die();
        if(empty($tmp) && $key_code!=''){
            $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;
                }
            }
            $tmp = array();

            if(isset($arrBarcodeSatuan[$key_code])){
                $bc_produk_id = $arrBarcodeSatuan[$key_code]->produk_id;
                $o->setFilters(array());
                $o->addFilter("trash=0");
                $tmp2 = $o->lookupByID($bc_produk_id)->result();
                if(!empty($tmp2)){
                    foreach($tmp2 as $k => $dtB){
                        $tmp[$k] = $dtB;
                    }
                }
                $tmp[0]->satuan_id     = $arrBarcodeSatuan[$key_code]->satuan_id;
                $tmp[0]->satuan_qty    = $arrBarcodeSatuan[$key_code]->qty;
                $tmp[0]->satuan_nama   = $arrBarcodeSatuan[$key_code]->satuan_nama;
                $tmp[0]->satuan        = $arrBarcodeSatuan[$key_code]->satuan_nama;

                //inject untuk pengenal barcodesatuan
                $tmp[0]->barcodesatuan = "$key_code";
            }
        }


        if(sizeof($tmp)>0){
            foreach($tmp as $row){

                if (isset($row->minValue)) {
                    $defaultValue = $row->minValue;
                    $defVal = "&minValue=$defaultValue";
                }
                else {
                    $defVal = "";
                }

                if ($socketURL != "") {
                    $row->minValue = isset($row->moq) ? $row->moq : 0;
                    if (sizeof($socketParams) > 0) {
                        foreach ($socketParams as $key => $src) {
                            if (isset($row->$src)) {
                                $iVal = $row->$src;
                            } else {
                                $iVal = 0;
                            }
                            $socketURL = str_replace("{" . $src . "}", $iVal, $socketURL);
                        }
                    }
                }

                $str_barcodesatuan="";
                if(isset($row->barcodesatuan)){
                    $arrBarcodeSatuan = array(
                        "barcodesatuan" => $row->barcodesatuan,
                        "satuan_nama"   => $row->satuan_nama,
                        "satuan_id"     => $row->satuan_id,
                        "satuan_qty"    => $row->satuan_qty,
                    );
                    $str_barcodesatuan = "&barcodesatuan=".blobEncode($arrBarcodeSatuan);
                }

//                matiHere($socketURL. "$str_barcodesatuan&url=".blobEncode("$selectorProcessor?id=".$row->id));

                if ($socketURL != "" && $use_numpad) {
                    $actionTarget = "top.BootstrapDialog.show({
                                        title:'".$row->nama."',
                                        message: " . 'top.$' . "('<div></div>').load('" . $socketURL. "$barcodesatuan&url=".blobEncode("$selectorProcessor?id=".$row->id."")."'),
                                        draggable:false,                                        
                                        type:top.BootstrapDialog.TYPE_DEFAULT,
                                        size:top.BootstrapDialog.SIZE_SMALL,
                                        closable:true,
                                        animate:false,
                                     });";

                    echo "<script>";
                    echo $actionTarget;
                    echo "</script>";
                }
                else{
                    $strContent = "
                    <script> top.$('#itemKeyword').val(''); top.document.getElementById('result').src='".$socketURL. "$str_barcodesatuan&url=".blobEncode("$selectorProcessor?id=".$row->id)."'; </script>
                    ";
                    echo $strContent;

//                    echo $socketURL;
                }
            }
        }
        else{
            echo(lgShowAlert("unregistered item barcode: ".$_GET['code']));
            echo "<script>";
            echo "if(top.document.getElementById('itemKeyword')){";
            echo "top.document.getElementById('itemKeyword').value='';\n";
            echo "top.document.getElementById('itemKeyword').focus();\n";
            echo "}";
            echo "</script>";
            die();
        }
    }
}