<?php
//if (isset($_SESSION['errMsg'])) {
//    echo "<div class='alert alert-danger text-center'>";
//    echo $_SESSION['errMsg'];
//    echo "</div>";
//}

// arrPrint($items);
/* -------------------------------- --------------------
 * variable disini dikirim dari controler _shopingCart
 * ------------------------------------------------------*/

if (isset($items)) {
    if (sizeof($items) > 0) {
        $jmlKolomHeader = sizeof($itemLabels) + 2;

        echo "<div class='table-responsive no-padding no-border' stylee='max-height: 50vh;'>";

        /*=============== BADGE PPN / NON PPN =================*/
        if (sizeof($arrHeaderElement) > 0) {
            foreach ($arrHeaderElement as $el => $eDetails) {
                $elLabel = $eDetails['label'];
                $elClass = $eDetails['class'];
                echo "<div class='$elClass hidden'>";
                echo "<div class='text-center text-bold bg-yellow'> $elLabel </div>";
                foreach ($eDetails['subElements'] as $sels => $seDetails) {
                    $selsLabel = $seDetails['label'];
                    $selsValue = $seDetails['value'];
                    $selsMainTarget = $seDetails['srcMain'];
                    $selsItemsTarget = $seDetails['srcItem'];
                    $currentPPN = isset($main[$selsMainTarget]) ? $main[$selsMainTarget] : 10;
                    $ppnPersenItems = isset($items[0]['ppnPersen']) ? $items[0]['ppnPersen'] : 0;
                    $autoTerapkan = ($ppnPersenItems != $currentPPN) && ($selsValue == $currentPPN) ? true : false;
                    $checked = $selsValue == $currentPPN ? "checked" : "";
                    echo "<label class='badge text-uppercase' style='padding:4px 6px 4px 6px;color:#454545;background:#e0e0e0;'>
                              <input type='radio' name='switch_ppn' value='$selsValue' $checked onclick=\"$('#result').load('" . MODUL_PATH . "_processSelectProductPpn/select/466?ppn='+this.value+'&ppnTargetItems=$selsItemsTarget&ppnTargetMain=$selsMainTarget');\">
                              <span>$selsLabel</span>
                          </label>";

                    if ($autoTerapkan) {
                        echo "
                        <script>
//                            setTimeout( function(){ $('input[name=switch_ppn]:checked').click() }, 500);
                            $('#result').load('" . base_url() . "Selectors/_processSelectProductPpn/select/466?ppn=10&ppnTargetItems=$selsItemsTarget&ppnTargetMain=$selsMainTarget')
                        </script>";
                    }
                }
                echo "</div>";
            }
        }
        /*=============== BADGE PPN / NON PPN =================*/


        echo "<table class='table table-condensed no-padding table-bordered no-margin'>";
        /*===============header shoping cart======================*/
        if (isset($itemLabels)) {
            if (sizeof($itemLabels) && (is_array($itemLabels))) {
                echo "<tr class='bg-grey-2 text-uppercase'>";
                echo "<th style='width:1%;' class='text-muted text-center'>";
                echo "NO";
                echo "</th>";
                foreach ($itemLabels as $key => $label) {
                    echo "<th style='width:1%;white-space: nowrap;' class='text-muted text-center'>";
                    echo $label;
                    echo "</th>";
                }

                if (!$avoidRemove) {
                    // echo "<th style='width:1%;' class='text-muted text-center'>";
                    // echo "x";
                    // echo "</th>";
                }
                echo "</tr>";
            }
        }
        //        arrprint($keyUpEvents);
        //        arrprint($selectedPrices);

        /*===============body shoping cart=======================================*/

        // cekPink(__FILE__ . __LINE__);

        $no = 0;
        foreach ($items as $iSpec) {
            // arrPrint($iSpec);
            $iID = $iSpec['id'];
            $rowRemove = "<a href='javascript:void(0);' class='text-red pull-right' title='remove this item' data-toggle='tooltip' data-placement='left' onclick=\"document.getElementById('result').src='" . $iSpec['removeTarget'] . "';setTimeout( function(){ $('#shopping_cart').trigger('change') }, 1500);\"><i class='fa fa-times'></i></a>";
            $no++;
            $bgColor = "transparent";
            $rowspan = 1;
            if (isset($subItemLabels) && sizeof($subItemLabels) > 0) {
                $rowspan = $rowspan + 1;
            }
            if (isset($_SESSION['errLines'])) {
                if (in_array($iSpec['id'], $_SESSION["errLines"])) {
                    $bgColor = "#ffff77";
                }
            }
            echo "<tr id='tr_" . $iSpec['id'] . "' bgcolor=$bgColor>";
            echo "<td style='vertical-align:middle; width:1%' class='text-center' rowspan='$rowspan'>";
            echo $no;
            echo $rowRemove;
            echo "</td>";

            $colCtr = 0;
            $queryParams = "";
            $colID = array();
            $listMode = array();
            $readOnly = array();
            $qtyParam = "";
            if (isset($itemLabels['jml'])) {
                //                $queryParams .= "&$key='+document.getElementById('" . $colID[$key] . "').value+'";
                $qtyParam = "+removeCommas(document.getElementById('jml_$no').value)";
            }
            foreach ($itemLabels as $key => $label) {
                $listMode[$key] = "input";
                $keyupEvent[$key] = "";
                $keyUpStr[$key] = "";
                if (array_key_exists($key, $keyUpEvents)) {
                    //                    cekbiru("$key has events");
                    if (sizeof($selectedPrices) > 0) {
                        $keyupEvent[$key] = $keyUpEvents[$key];
                        foreach ($selectedPrices as $k => $v) {
                            //                            $nameLabel = "value_" . $yID . "_" . $xID . "_" . $k . ""; //==untuk nama/ID input
                            $nameLabel = $k . "_" . $no;
                            $keyupEvent[$key] = str_replace("{" . $k . "}", $nameLabel, $keyupEvent[$key]);
                        }
                        foreach ($itemLabels as $k => $v) {
                            $nameLabel = $k . "_" . $no;
                            $keyupEvent[$key] = str_replace("{" . $k . "}", $nameLabel, $keyupEvent[$key]);
                        }
                    }
                    if (isset($keyupAction) && $keyupAction == true) {
                        $keyupEvent[$key] = $keyUpEvents[$key];
                        foreach ($selectedPrices as $k => $v) {
                            //                            $nameLabel = "value_" . $yID . "_" . $xID . "_" . $k . ""; //==untuk nama/ID input
                            $nameLabel = $k . "_" . $no;
                            $keyupEvent[$key] = str_replace("{" . $k . "}", $nameLabel, $keyupEvent[$key]);
                        }
                        foreach ($itemLabels as $k => $v) {
                            $nameLabel = $k . "_" . $no;
                            $keyupEvent[$key] = str_replace("{" . $k . "}", $nameLabel, $keyupEvent[$key]);
                        }
                    }
                }
                else {
                }
                if (strlen($keyupEvent[$key]) > 2) {
                    $keyUpStr[$key] = " onkeyup=\"" . $keyupEvent[$key] . "\" ";
                }
                if (in_array($key, $editableFields)) {
                    $readOnly[$key] = "";
                    if (isset($iSpec["jml"]) && $iSpec["jml"] < 1) {
                        $readOnly[$key] = "readonly_xz";
                    }
                }
                else {
                    $readOnly[$key] = "readonly_xxz";
                    $listMode[$key] = "text";
                }
                $colID[$key] = $key . "_" . $no;
                if ($listMode[$key] == "input") {
                    $queryParams .= "&$key='+removeCommas(document.getElementById('" . $colID[$key] . "').value)+'";
                }
            }

            // arrPrintPink($itemLabels);
            foreach ($itemLabels as $key => $label) {
                $colCtr++;
                $color = "343434";
                if (isset($_SESSION['errFields'][$iSpec['id']])) {
                    if (in_array($key, $_SESSION['errFields'][$iSpec['id']])) {
                        $color = "#dd3300";
                    }
                }
                echo "<td align='left'>";

                $colID = $key . "_" . $no;
                $keyID = $key;
                $noID = $no;
                $tabIndexNum = $colCtr . $no;
                $fieldVal = "";
                if (substr($key, 0, 1) == "*") {
                    $key_p = str_replace("*", "", $key);
                    $key_ex = explode("#", $key_p);
                    $pair_name = $key_ex[0];
                    $pair_key = $key_ex[1];
                    $pair_key_val = $iSpec[$pair_key];
                    if (sizeof($key_ex) > 1) {
                        $fieldVal = isset($pairedValue[$pair_name][$pair_key_val]) ? $pairedValue[$pair_name][$pair_key_val] : "0";
                    }
                    else {
                        $fieldVal = isset($pairedValue[$pair_name]) ? $pairedValue[$pair_name] : "0";
                    }
                }
                else {
                    // cekBiru($key . " " . $iSpec[$key]);
                    if (isset($iSpec[$key])) {
                        if (is_numeric($iSpec[$key])) {
                            $fieldVal = $iSpec[$key] + 0;
                        }
                    else {
                            $fieldVal = $iSpec[$key];
                        }
                    }
                }
                if (sizeof($minValues) > 0) {
                    $moq = isset($minValues['moq'][$iID]) ? $minValues['moq'][$iID] : 0;
                    $validateKey_up = true;
                }
                else {
                    $moq = 0;
                    $validateKey_up = false;
                }
                $keyupData = (($key == "qty" || $key == "jml") && $validateKey_up == true) ? "onkeydown=\"if(parseInt(removeCommas(this.value))<$moq){setTimeout(function(){ this.value='" . number_format($iSpec[$key]) . "'}, 1000);} \"" : "";
                //                arrPrint($keyUpStr);
                $bulkdiscPersen = "\n\nvar sendBulkDisc = function(){ \n";
                switch ($listMode[$key]) {
                    case "input":
                        // cekHitam($key);
                        echo "<input *** type='text'  min='$moq' autocomplete='off' " . $readOnly[$key] . " keyid=$keyID noid=$noID id_jml=$iID id=$colID  class='form-control text-right' style='color:$color;' value='" . number_format($fieldVal, 2) . "' onclick='this.select()' " . $keyUpStr[$key] . " ";

                        $baseInputName = isset($unionSelectors['base']) ? "document.getElementById('" . $unionSelectors['base'] . "_" . $no . "')" : "this";
                        $pemicuGerbangAsli = "onblur=\"if(this.value!=this.defaultValue){hiliteDiv(this);document.getElementById('result').src='" . $iSpec['editTarget'] . "'$qtyParam+'$queryParams';} \" $keyupData";
                        $pemicuGerbangAsli .= "onmouseout01=\"if(this.value!=this.defaultValue){hiliteDiv(this);document.getElementById('result').src='" . $iSpec['editTarget'] . "'$qtyParam+'$queryParams';}\" ";
                        $pemicuGerbang = "onblur=\"if($baseInputName.value!=$baseInputName.defaultValue){hiliteDiv($baseInputName);document.getElementById('result').src='" . $iSpec['editTarget'] . "'$qtyParam+'$queryParams';}\" $keyupData ";
                        $pemicuGerbang .= "onmouseout02=\"if($baseInputName.value!=$baseInputName.defaultValue){hiliteDiv($baseInputName);document.getElementById('result').src='" . $iSpec['editTarget'] . "'$qtyParam+'$queryParams';}\" ";
                        $pemicuGerbangUnion = "onchange=\"if($baseInputName.value!=$baseInputName.defaultValue){hiliteDiv($baseInputName);document.getElementById('result').src='" . $iSpec['editTarget'] . "'$qtyParam+'$queryParams';} \" ";

                        if ($keyID == 'discPersen') {
                            $bulkdiscPersen .= "\ntop.$('#result').load('" . $iSpec['editTarget'] . "'$qtyParam+'$queryParams');";
                        }

                        //                        if (isset($unionSelectors['base'])) {
                        //                            if ($unionSelectors['base'] == $key) {//==jadi acuan kiriman
                        //                                echo str_replace("this", $baseInputName, $pemicuGerbang);
                        //                            }
                        //                            else {
                        //                                if (in_array($key, $unionSelectors['members'])) {//==jadi member union, tidak memicu perubahan gerbang
                        //                                    echo $pemicuGerbangUnion;
                        //                                }
                        //                                else {//==biasa aja, memicu perubahan gerbang
                        //                                    echo $pemicuGerbangAsli;
                        //                                }
                        //                            }
                        //                        }
                        //                        else {
                        //                            echo $pemicuGerbangAsli;
                        //                        }

                        if (isset($keyupAction) && $keyupAction == true) {
                            echo "onkeyup=\"document.getElementById('result').src='" . $iSpec['editTarget'] . "'$qtyParam+'$queryParams';\"";
                        }
                        else {
                            //                            echo "onkeyup=\"delay( function(){ $('#shopping_cart').trigger('change') }, 400, this );\"";
                        }
                        echo ">";
                        //tutup input

                        if (isset($keyupAction) && $keyupAction == true) {
                            //                            echo "onkeyup=\"document.getElementById('result').src='" . $iSpec['editTarget'] . "'$qtyParam+'$queryParams';\"";
                        }
                        else {
                            //                            echo "onkeyup=\"delay( function(){ $('#shopping_cart').trigger('change') }, 400, this );\"";


                            echo "
                                \n<script>

                                    \n$('input#$colID').on('keyup', delay_v2( function(){
                                        \n$('#shopping_cart').trigger('change');
                                        \nif(this.value!=this.defaultValue){
                                            \n//hiliteDiv(this);
                                            \ntop.$('#btnProcess').attr('disabled', true);
                                            \ntop.$('#result').load('" . $iSpec['editTarget'] . "'$qtyParam+'$queryParams&spc=1');
                                        \n}
                                    \n}, 1000) );

                                    \n$('input#$colID').on('keyup', delay_v2( function(){
                                        //this.value = addCommas(this.value)
                                    \n}, 200) );

                                \n</script>
                            \n";
                        }

                        if (isset($unionSelectors['base'])) {
                            if ($unionSelectors['base'] == $key) {//==jadi acuan kiriman
                                //                                echo str_replace("this", $baseInputName, $pemicuGerbang);
                            }
                            else {
                                if (in_array($key, $unionSelectors['members'])) {//==jadi member union, tidak memicu perubahan gerbang
                                    //                                    echo $pemicuGerbangUnion;
                                }
                                else {//==biasa aja, memicu perubahan gerbang
                                    //                                    echo $pemicuGerbangAsli;
                                }
                            }
                        }
                        else {
                            //                            echo $pemicuGerbangAsli;
                        }

                        //                        if (isset($keyupAction) && $keyupAction == true) {
                        //                            echo "onkeyup=\"document.getElementById('result').src='" . $iSpec['editTarget'] . "'$qtyParam+'$queryParams';\"";
                        //                        }
                        //                        else {
                        //                            echo "onkeyup=\"delay( function(){ $('#shopping_cart').trigger('change') }, 400, this );\"";
                        //                        }
                        //                        echo ">";

                        break;
                    case "text":

                        $fieldVal_f = str_replace("'", "", strval($fieldVal));
                        if (is_numeric($fieldVal)) {

                            //                            echo "<span keyid=$keyID noid=$noID id=$colID class='form-control text-right' style='color:$color;background:#f0f0f0;'>" . formatField($key, $fieldVal) . "</span>";
                            echo "<span 2 keyid=$keyID noid=$noID id=$colID class='form-control text-right' style='color:$color;background:#f0f0f0;'>" . number_format($fieldVal_f) . "</span>";
                        }
                        else {

                            if (strlen($fieldVal) > 10) {
                                echo "<span 3 keyid=$keyID noid=$noID id=$colID class='' style='color:$color;border:0px;'>" . formatField($key, $fieldVal_f) . "</span>";
                            }
                            else {
                                // subTotal row
                                echo "<span 4 keyid=$keyID noid=$noID id=$colID classs='form-control' style='color:$color;border:0px;'>" . formatField($key, $fieldVal_f) . "</span>";
                            }
                        }
                        break;
                }
                $bulkdiscPersen .= "\n}";
                echo "</td>";
            }


            echo "
            <script>
                $bulkdiscPersen
                \n$('#" . trim($iSpec['id']) . "', $('#pilihan_item')).html(\"<i class='fa fa-check'></i>\");
                \n$('#" . trim($iSpec['id']) . "', $('#pilihan_item')).addClass(\"text-green text-bold\");
            </script>
            ";
            //region remover per row
            // if (!$avoidRemove) {
            //     echo "<td width='1%'>";
            //     // echo "<a class='text-red btn' title='remove this item' data-toggle='tooltip' data-placement='left' onclick=\"document.getElementById('result').src='" . $iSpec['removeTarget'] . "';\"><span class='glyphicon glyphicon-remove'></span></a>";
            //     echo "$rowRemove";
            //     echo "</td>";
            // }
            //endregion

            echo "</tr>";
            //            arrPrintWebs($items2_komposisi);
            //----------------------------------------------
            if (isset($subItemLabels) && sizeof($subItemLabels) > 0) {

                $colspanSubItems = sizeof($itemLabels);

                $strSubItem = "<tr>";
                $strSubItem .= "<td colspan='$colspanSubItems'>";

                $strSubItem .= "<table class='table table-condensed no-padding table-bordered no-margin'>";
                // header
                $strSubItem .= "<tr class='bg-warning'>";
                foreach ($subItemLabels as $sub_key => $sub_label) {
                    $strSubItem .= "<td>";
                    $strSubItem .= "$sub_label";
                    $strSubItem .= "</td>";
                }
                $strSubItem .= "</tr>";
                // isi komposisi per-items
                if ((isset($items2_komposisi)) && (isset($items2_komposisi[$iID])) && (sizeof($items2_komposisi[$iID]) > 0)) {
                    foreach ($items2_komposisi[$iID]['produk'] as $xx => $subSpec) {
                        $strSubItem .= "<tr>";
                        foreach ($subItemLabels as $sub_key => $sub_label) {
                            $sub_value = isset($subSpec[$sub_key]) ? $subSpec[$sub_key] : "";
                            $strSubItem .= "<td align='left'>";
                            $strSubItem .= formatField($sub_key, $sub_value);
                            $strSubItem .= "</td>";
                        }
                        $strSubItem .= "</tr>";
                    }
                }

                $strSubItem .= "</table>";

                $strSubItem .= "</td>";
                $strSubItem .= "</tr>";
                echo $strSubItem;
            }

            if ($noteEnabled == true) {
                $colspan2 = $imageEnable == true ? 1 : -1;
                $colspan = sizeof($itemLabels) - $colspan2;
                echo "<tr>";
                echo "<td>&nbsp;</td>";
                echo "<td colspan='" . $colspan . "'>";
                $noteVal = isset($iSpec['note']) ? $iSpec['note'] : "";
                if (isset($noteType)) {
                    switch ($noteType) {
                        case "textarea":
                            echo "<textarea class='form-control' placeholder='write notes here'
                                onblur=\"if(this.value!=this.defaultValue){hiliteDiv(this);document.getElementById('result').src='" . $noteRecorder . "?val='+encodeURIComponent(this.value)+'&iid=$iID';}\"
                                onmouseout03=\"if(this.value!=this.defaultValue){hiliteDiv(this);document.getElementById('result').src='" . $noteRecorder . "?val='+encodeURIComponent(this.value)+'&iid=$iID';}\"
                                >$noteVal</textarea>";
                            break;
                        case "text":
                        default:
                            echo "<input type=text class='form-control' value='$noteVal' placeholder='write notes here'
                                onblur=\"if(this.value!=this.defaultValue){hiliteDiv(this);document.getElementById('result').src='" . $noteRecorder . "?val='+encodeURIComponent(this.value)+'&iid=$iID';}\"
                                onmouseout04=\"if(this.value!=this.defaultValue){hiliteDiv(this);document.getElementById('result').src='" . $noteRecorder . "?val='+encodeURIComponent(this.value)+'&iid=$iID';}\"
                                >";
                            break;
                    }
                }

                echo "</td>";
                if ($imageEnable == true) {
                    echo "<td colspan='2'>";
                    $imageVal = isset($iSpec['images']) ? $iSpec['images'] : "";
                    if (isset($imageType)) {
                        switch ($imageType) {
                            case "images":

                                $file_e = "";
                                $file = isset($iSpec['images']) ? $iSpec['images'] : "";
                                $file_e = urlencode($file);
                                echo "<div class='input-groups'>";
                                if (strlen($imageVal) > 0) {
                                    $modals = array(
                                        "title" => "Attachment " . $iSpec['nama'],
                                        "body" => array($file),
                                    );
                                    $modal_e = urlencode(blobEncode($modals));
                                    $modal_l = base_url() . "Katalog/modal/$modal_e";

                                    echo "<a href='$modal_l' data-toggle='modal' data-target='#myModal'><img src='$file' class='img-rounder' height='50px' style='float: right;'></a>";
                                    echo "<input type='hidden' name='img_$iID' value='$file'>";
                                }

                                echo "<form class='input-group' id='myForm_$iID' method='post' enctype='multipart/form-data' action='$imageRecorder/$iID?valValue=$file_e' target='result'>";

                                echo "<input type='file' id='file-upload' style='border: none;' name='file' class='file' onchange=\"document.getElementById('myForm_$iID').submit();swal({'text':'uploading image ... ... ',showConfirmButton: false,timer:5000,});\">";

                                echo "</form>";
                                echo "</div>";

                                break;
                            case "text":
                            default:
                                echo "<input type=text class='form-control' value='$noteVal' 
                                onblur=\"if(this.value!=this.defaultValue){hiliteDiv(this);document.getElementById('result').src='" . $noteRecorder . "?val='+encodeURIComponent(this.value)+'&iid=$iID';}\"
                                onmouseout05=\"if(this.value!=this.defaultValue){hiliteDiv(this);document.getElementById('result').src='" . $noteRecorder . "?val='+encodeURIComponent(this.value)+'&iid=$iID';}\"
                                >";
                                break;
                        }
                    }
                    echo "</td>";
                }
                echo "</tr>";
            }
            if ($pairedItemEnabled == true) {
                if (sizeof($pairedItemField) > 0) {
                    $listModePairedItem = array();
                    $readOnlyPairedItem = array();
                    foreach ($pairedItemField as $key => $label) {
                        $listModePairedItem[$key] = "input";
                        if (in_array($key, $editableFields)) {
                            $readOnlyPairedItem[$key] = "";
                            if (isset($iSpec["jml"]) && $iSpec["jml"] < 1) {
                                $readOnlyPairedItem[$key] = "readonly_x";
                            }
                        }
                        else {
                            $readOnlyPairedItem[$key] = "readonly_xx";
                            $listModePairedItem[$key] = "text";
                        }
                    }
                }
                echo "<tr>";
                echo "<td>&nbsp;</td>";
                //                echo "<td colspan='" . sizeof($itemLabels) . "'>";
                $c_itemLabels = sizeof($itemLabels);
                $c_pairedItemField = sizeof($pairedItemField);
                // $c_colspan = ($c_itemLabels - $c_pairedItemField + 1);
                $c_colspan = ($c_itemLabels - $c_pairedItemField);
                echo "<td colspan='" . $c_colspan . "'>";
                //==pairedItems, if any
                if (isset($selItems) && sizeof($selItems) > 0) {
                    echo "<select titlse='Choose one of the following...' data-header='Ketik Nama/Kode/Folder/Barcode' data-size='10' data-container='body' class='selectpicker form-control select2 show-tick'  data-style='btn-primary' data-live-search='true' classs='form-control' onchange=\"document.getElementById('result').src='" . $pairedItemRecorder . "?val='+(this.value)+'&iid=$iID&spc=1';\">";
                    //                    echo "<option value=''>--select item--";
                    echo "</option>";
                    asort($selItems);
                    foreach ($selItems as $piID => $piName) {
                        if ($piID != $iSpec['id']) {
                            $selectedState = (isset($pairedItems[$iID]) && ($piID == $pairedItems[$iID]['id'])) ? "selected" : "";
                            $selItemsKodes = isset($selItemsKode[$piID]) ? $selItemsKode[$piID] : "-";
                            $selItemsFolders = isset($selItemsFolder[$piID]) ? $selItemsFolder[$piID] : "-";
                            $selItemsKeterangans = isset($selItemsKeterangan[$piID]) ? $selItemsKeterangan[$piID] : "-";
                            $selItemsBarcodes = isset($selItemsBarcode[$piID]) ? $selItemsBarcode[$piID] : "-";
                            echo "<option data-subtext='$selItemsKodes' data-tokens='$piID $selItemsFolders $selItemsKeterangans $selItemsBarcodes' value='$piID' $selectedState>$piName ";
                            echo "</option>";
                        }
                    }

                    echo "</select>";
                }

                //                arrPrint($selItems);

                echo "</td>";
                echo "<script>top.$('.select2').selectpicker();</script>";
                if (sizeof($pairedItemField) > 0) {
                    foreach ($pairedItemField as $key => $label) {
                        $pairedItems2ID = isset($pairedItems[$iID]['id']) ? $pairedItems[$iID]['id'] : 0;
                        $pairedItems2Qty = isset($pairedItems[$iID]['jml']) ? $pairedItems[$iID]['jml'] : 0;
                        $fieldVal = isset($pairedItems[$iID][$key]) ? $pairedItems[$iID][$key] : "";
                        echo "<td align='center'>";
                        switch ($listMode[$key]) {
                            case "input":
                                echo "<input sini bukan type='number' class='form-control text-right' value='" . $pairedItems2Qty . "' min='0' autocomplete='off'
                                    onblur=\"document.getElementById('result').src='" . $pairedItemRecorder . "?newQty='+(this.value)+'&iid=$iID&val=$pairedItems2ID&spc=1';\"
                                    onmouseout06=\"document.getElementById('result').src='" . $pairedItemRecorder . "?newQty='+(this.value)+'&iid=$iID&val=$pairedItems2ID&spc=1';\"
                                    >";
                                break;
                            case "text":
                                if (is_numeric($fieldVal)) {
                                    echo "<span class='form-control text-right' style='color:$color;background:#f0f0f0;'>" . number_format($fieldVal) . "</span>";
                                }
                                else {
                                    echo "<span class='form-control text-left' style='color:$color;border:0px;'>" . str_replace(" ", "&nbsp;", $fieldVal) . "</span>";
                                }
                                break;
                        }
                        echo "</td>";
                        //                        echo "<td>";
                        //                        echo "<input type='number' class='form-control text-right' value='" . $pairedItems2Qty . "' min='0' autocomplete='off'
                        //                        onkeyup=\"document.getElementById('result').src='" . $pairedItemRecorder . "?newQty='+(this.value)+'&iid=$iID&val=$pairedItems2ID';\">";
                        //                        echo "</td>";
                    }
                }
                echo "</tr>";

            }

        }

        //region items2, kalau salah satunya untuk produksi dan konversi
        if (isset($items2) && sizeof($items2) > 0) {
            echo "<tr class='bg-info'>";
            echo "<td colspan='$jmlKolomHeader'>";

            // echo "<div class='table-responsive no-padding no-border border-cek overflow-h'>";
            echo "<div class='panel no-margin'>"; // anakan table
            echo "<table class='table table-condensed table-striped no-padding no-border'>";

            if (sizeof($itemLabels2) && (is_array($itemLabels2))) {
                //region header table anakan
                echo "<tr>";
                echo "<td class='text-muted bg-grey-1 text-center'>";
                echo "No";
                echo "</td>";
                foreach ($itemLabels2 as $key => $label) {
                    echo "<td class='text-muted bg-grey-1 text-center text-capitalize'>";
                    echo $label;
                    echo "</td>";
                }
                echo "</tr>";
                //endregion
            }

            $no = 0;
            //region body table anakan
            // arrPrintPink($items2);
            $kurangStoks = array();
            foreach ($items2 as $iSpec) {
                $iID = $iSpec['id'];
                $no++;
                $bgColor = "transparent";
                if (isset($items2_sum_kurang) && is_array($items2_sum_kurang)) {
                    if (isset($items2_sum_kurang[$iID])) {
                        $bgColor = "yellow";
                    }
                }
                if (isset($_SESSION['errLines'])) {
                    if (in_array($iSpec['id'], $_SESSION["errLines"])) {
                        $bgColor = "#ffff77";
                    }
                }
                echo "<tr id='tr_" . $iSpec['id'] . "' bgcolor=$bgColor>";
                echo "<td width='5%'>";
                echo $no;
                echo ".</td>";
                $colCtr = 0;
                $queryParams = "";
                foreach ($itemLabels2 as $key => $label) {
                    // cekBiru($key);
                    //                if(in_array($key,$editableFields)){
                    $colID = $key . "_" . $no;
                    $queryParams .= "&$key='+removeCommas(document.getElementById('$colID').value)+'";
                    //                }
                }

                foreach ($itemLabels2 as $key => $label) {
                    $colCtr++;
                    $color = "343434";
                    if (isset($_SESSION['errFields'][$iSpec['id']])) {
                        if (in_array($key, $_SESSION['errFields'][$iSpec['id']])) {
                            $color = "#dd3300";
                        }
                    }
                    // arrPrintWebs($iSpec[$key]);
                    $cAlign = (isset($iSpec[$key]) && is_numeric($iSpec[$key])) ? "text-right" : "text-left";
                    //region membuat array stok yang kurang
                    if ($key == "sisa") {
                        if ($iSpec[$key] < 0) {
                            $kurangStoks[$iSpec['nama']] = $iSpec['sisa'];
                            $cAlign .= " text-red text-bold";
                        }
                        else {
                            $cAlign .= "";
                        }
                    }
                    //endregion
                    echo "<td class='$cAlign'>";
                    $tabIndexNum = $colCtr . $no;

                    if (isset($iSpec[$key]) && is_numeric($iSpec[$key])) {
                        // echo "<input type=text autocomplete='off' readOnly id=$colID class='form-control text-right' style='color:$color;' value='" . $iSpec[$key] . "' >";
                        if ($key == 'kode') {
                            echo $iSpec[$key];
                        }
                        else {
                            echo formatField($key, number_format($iSpec[$key]));
                        }
                        // echo $iSpec[$key];
                    }
                    else {
                        // echo "<input type=text autocomplete='off' readOnly id=$colID class='form-control' style='color:$color;' value='" . $iSpec[$key] . "' >";
                        echo isset($iSpec[$key]) ? $iSpec[$key] : "";
                    }
                    echo "</td>";
                }
                echo "</tr>";
            }
            //endregion

            echo "</table>";
            echo "</div>"; // anakan table

            // arrPrint($kurangStoks);

            echo "</td>";
            echo "</tr>";
        }
        //endregion

        //region items3
        if (isset($items3) && sizeof($items3) > 0) {
            echo "<tr class='bg-info'>";
            echo "<td colspan='$jmlKolomHeader'>";

            // echo "<div class='table-responsive no-padding no-border border-cek overflow-h'>";
            echo "<div class='panel no-margin'>"; // anakan table
            echo "<table class='table table-condensed table-striped no-padding no-border'>";

            if (sizeof($itemLabels3) && (is_array($itemLabels3))) {
                //region header table anakan
                echo "<tr>";
                echo "<td class='text-muted bg-grey-1 text-center'>";
                echo "No";
                echo "</td>";
                foreach ($itemLabels3 as $key => $label) {
                    echo "<td class='text-muted bg-grey-1 text-center text-capitalize'>";
                    echo $label;
                    echo "</td>";
                }
                echo "</tr>";
                //endregion
            }

            $no = 0;
            //region body table anakan
            $kurangStoks = array();
            foreach ($items3 as $iSpec) {
                $iID = $iSpec['id'];
                $no++;
                $bgColor = "transparent";
                if (isset($_SESSION['errLines'])) {
                    if (in_array($iSpec['id'], $_SESSION["errLines"])) {
                        $bgColor = "#ffff77";
                    }
                }
                echo "<tr id='tr_" . $iSpec['id'] . "' bgcolor=$bgColor>";
                echo "<td width='5%'>";
                echo $no;
                echo ".</td>";
                $colCtr = 0;
                $queryParams = "";
                foreach ($itemLabels3 as $key => $label) {
                    //                if(in_array($key,$editableFields)){
                    $colID = $key . "_" . $no;
                    $queryParams .= "&$key='+document.getElementById('$colID').value+'";
                    //                }
                }

                foreach ($itemLabels3 as $key => $label) {
                    $colCtr++;
                    $color = "343434";
                    if (isset($_SESSION['errFields'][$iSpec['id']])) {
                        if (in_array($key, $_SESSION['errFields'][$iSpec['id']])) {
                            $color = "#dd3300";
                        }
                    }
                    $cAlign = is_numeric($iSpec[$key]) ? "text-right" : "text-left";
                    //region membuat array stok yang kurang
                    if ($key == "sisa") {
                        if ($iSpec[$key] < 0) {
                            $kurangStoks[$iSpec['nama']] = $iSpec['sisa'];
                            $cAlign .= " text-red text-bold";
                        }
                        else {
                            $cAlign .= "";
                        }
                    }
                    //endregion
                    echo "<td class='$cAlign'>";
                    $tabIndexNum = $colCtr . $no;

                    if (is_numeric($iSpec[$key])) {
                        // echo "<input type=text autocomplete='off' readOnly id=$colID class='form-control text-right' style='color:$color;' value='" . $iSpec[$key] . "' >";
                        echo number_format($iSpec[$key]);
                    }
                    else {
                        // echo "<input type=text autocomplete='off' readOnly id=$colID class='form-control' style='color:$color;' value='" . $iSpec[$key] . "' >";
                        echo $iSpec[$key];
                    }
                    echo "</td>";
                }
                echo "</tr>";
            }
            //endregion


            if (isset($sumRows3) && sizeof($sumRows3) > 0) {
                $nr = 0;
                foreach ($sumRows3 as $key => $label) {
                    $val = 0;
                    $nr++;
                    $bottom_borderless = $nr < sizeof($sumRows3) ? "bottom-borderless" : "";

                    if (isset($main[$key]) && $main[$key] > 0) {
                        $val = $main[$key];
                    }
                    else {
                        if (isset($addValues[$key]) && $addValues[$key] > 0) {
                            $val = $addValues[$key];
                        }
                    }

                    echo "<tr class='bg-grey-01'>";
                    echo "<td colspan='" . sizeof($itemLabels3) . "' class='text-right $bottom_borderless valign-m text-uppercase'>$label</td>";
                    echo "<td class='right-borderlesss'>";
                    echo formatField($key, number_format($val));
                    echo "</td>";
                    echo "</tr>";
                }
            }

            echo "</table>";
            echo "</div>"; // anakan table

            echo "</td>";
            echo "</tr>";
        }
        //endregion

        /*=============================sumrows============================*/
        if (isset($sumRows) && sizeof($sumRows) > 0) {
            $nr = 0;
            foreach ($sumRows as $key => $label) {
                $val = 0;
                $nr++;
                $bottom_borderless = $nr < sizeof($sumRows) ? "bottom-borderless" : "";

                if (isset($main[$key]) && $main[$key] > 0) {
                    $val = $main[$key];
                }
                else {
                    if (isset($addValues[$key]) && $addValues[$key] > 0) {
                        $val = $addValues[$key];
                    }
                }

                echo "<tr class='bg-grey-01'>";
                echo "<td colspan='" . sizeof($itemLabels) . "' class='text-right $bottom_borderless valign-m text-uppercase'>$label</td>";
                echo "<td colspan='3' class='right-borderlesss'>";
                echo "<input type='text' id='$key' class='form-control text-right' readonly value='" . number_format($val) . "' >";
                echo "</td>";
                //                echo "<td class='left-borderlesss'></td>";
                echo "</tr>";
            }
        }
        if (isset($sumRows2) && sizeof($sumRows2) > 0) {
            echo "<tr bgcolor='#e0e0e0'>";
            echo "<td colspan='" . (sizeof($itemLabels2) + 1) . "' class='text-left text-muted'><span class='fa fa-cog'></span> additional fees</td>";
            echo "</td>";
            echo "</tr>";
            $nr = 0;
            foreach ($sumRows2 as $key => $label) {
                $nr++;
                $bottom_borderless = $nr < sizeof($sumRows2) ? "bottom-borderless" : "";

                echo "<tr bgcolor='#f0f0f5'>";
                echo "<td colspan='" . sizeof($itemLabels) . "' class='text-right bottom-borderless valign-m text-uppercase'>$label</td>";
                echo "<td>";
                echo $sumSpec2[$key];
                echo "</td>";
                echo "</tr>";
            }
        }
        //arrPrint($addRows);
        //        cekLime(__FILE__." baris ".__LINE__);
        if (sizeof($addRows) > 0) {
            //            arrPrint($addRows);
            $nr = 0;
            foreach ($addRowLabels as $k => $label) {
                $nr++;
                $bottom_borderless = $nr < sizeof($addRowLabels) ? "bottom-borderless" : "";
                echo "<tr>";
                echo "<td colspan='" . sizeof($itemLabels) . "' class='text-right $bottom_borderless valign-m text-uppercase'>$label</td>";
                echo "<td colspan='2' class='text-right'>";
                echo $addRows[$k];
                echo "</td>";
                echo "</tr>";
            }
        }


        //region clear shoping cart
        if (!$avoidRemove) {
            echo "<tr class='bg-grey-2'>";
            // echo "<td>&nbsp;</td>";
            echo "<td colspan='" . (sizeof($itemLabels) + 2) . "'>";
            echo "<span class='pull-left'>";
            echo "<button id='btn_clear_shopping_cart' type='button' class='text-red btn' title='remove ALL ITEMS' data-toggle='tooltip' data-placement='right' onclick=\"confirm_alert_result('Attention !!!','Remove all items on shopping cart?','$resetLink','YES CLEAR');\"><i class='fa fa-trash'> </i> Clear Shoping Cart</button>";
            echo "</span>";
            if (isset($main['oto_diskon'])) {
                echo "<input id='r_oto_diskon' class='hidden' value='" . $main['oto_diskon'] . "'>";
            }
            // arrPrintPink($tmpSaver);
            if (sizeof($tmpSaver) > 0) {
                $link_tmpSave = base_url() . $tmpSaver["exeTarget"];
                $link_reqDiskon = base_url() . $reqDiskon["exeTarget"];
                echo "&nbsp;<button type='button' class='btn btn-primary pull-right' onclick=\"btn_result('$link_tmpSave');\"><i class='fa " . $tmpSaver['btnIcon'] . "'>&nbsp;</i> " . $tmpSaver['btnLabel'] . "</button>";
                if (sizeof($reqDiskon) > 0) {
                    echo "<button type='button' class='btn btn-warning' onclick=\"btn_result('$link_reqDiskon');\"><i class='fa " . $reqDiskon['btnIcon'] . "'>&nbsp;</i> " . $reqDiskon['btnLabel'] . "</button>&nbsp;";
                }
            }
            echo "<span id='btn_kalkulasi' class='pusll-left hidden'>";
            echo "<button type='button' class='btn btn-danger text-normal' titles='kalkulasi' data-toggle='tooltip' data-placement='right' onclick=\"fillBoxes()\"><i class='fa fa-refresh fa-spin'></i> <i id='kalkulasi'></i> Update View</button>";
            echo "</span>";
            echo "</td>";
            echo "</tr>";
        }
        //endregion

        //        echo "<tr>";
        //        echo "<td colspan='" . (sizeof($itemLabels) + 2) . "'>";
        //            /*==========   ==========*/
        //            if( sizeof($arrHeaderElement) > 0 ){
        //                foreach($arrHeaderElement as $el => $eDetails){
        //                    $elLabel = $eDetails['label'];
        //                    $elClass = $eDetails['class'];
        //                    echo "<div class='$elClass'>";
        //                    echo "<div class='text-center text-bold bg-yellow'> $elLabel </div>";
        //                    foreach($eDetails['subElements'] as $sels => $seDetails){
        //                        $selsLabel = $seDetails['label'];
        //                        $selsValue = $seDetails['value'];
        //                        $selsMainTarget = $seDetails['srcMain'];
        //                        $selsItemsTarget = $seDetails['srcItem'];
        //                        $currentPPN = isset($main[$selsMainTarget]) ? $main[$selsMainTarget] : 10;
        //                        $ppnPersenItems = isset($items[0]['ppnPersen']) ? $items[0]['ppnPersen'] : 0;
        //                        $autoTerapkan = ($ppnPersenItems!=$currentPPN)&&($selsValue==$currentPPN) ? true : false;
        //                        $checked = $selsValue == $currentPPN ? "checked" : "";
        //                        echo "<label class='badge text-uppercase' style='padding:4px 6px 4px 6px;color:#454545;background:#e0e0e0;'>
        //                                  <input type='radio' name='switch_ppn2' value='$selsValue' $checked onclick=\"$('#result').load('".base_url()."Selectors/_processSelectProductPpn/select/466?ppn='+this.value+'&ppnTargetItems=$selsItemsTarget&ppnTargetMain=$selsMainTarget');\">
        //                                  <span>$selsLabel</span>
        //                              </label>";
        //                        if($autoTerapkan){
        //                            echo "
        //                            <script>
        //                                $('#result').load('".base_url()."Selectors/_processSelectProductPpn/select/466?ppn=10&ppnTargetItems=$selsItemsTarget&ppnTargetMain=$selsMainTarget')
        //                            </script>";
        //                        }
        //                    }
        //                    echo "</div>";
        //                }
        //            }
        //            /*==========   ==========*/
        //        echo "</td>";
        //        echo "</tr>";

        echo "</table class='table'>";


        echo "</div class='table-responsive'>";


        if (isset($fixedNote)) {
            echo "<div class='alert alert-danger' style='margin-top: 10px;font-size: 15px;'>";
            echo "<span>$fixedNote</span>";
            echo "</div>";
        }

        /*
 * update price
 */
        if (isset($msgWarning) && sizeof($msgWarning)) {
            $msgWarnings = $msgWarning;
            echo "<div class='alert alert-danger text-center'>";
            foreach ($msgWarnings as $msgSpec) {
                echo $msgSpec['label'] . "<br>";
            }
            echo "</div class='alert alert-warning'>";
        }
        else {
            $msgWarnings = array();
        }
        $price = "";
        $custom_js = "";
        if (sizeof($itemPrice) > 0) {
            $price = "<div class='panel-heading'></div>";
            $price = "<div class='table-responsive'>";
            $price .= "<H4 >Setting harga jual</H4>";
            //                    echo "<div class=''>Detail</div>";
            $price .= "<table class='table table-bordered table-condensed table-striped table-hover'>";
            $price .= "<tr bgcolor='#f5f5f5'>";
            $price .= "<th class='text-muted'  style='font-weight:bold;' align='right' width='5'>no.</th>";
            $price .= "<th class='text-green text-bold text-center' width='150'>Apply To All</th>";
            foreach ($itemPriceLabel as $label => $tmpLabel) {
                if (is_array($tmpLabel)) {
                    foreach ($itemPriceLabel2[$label] as $cID => $tmpCID) {
                        $price .= "<th >" . $tmpCID['label'] . "</th>";
                    }
                }
                else {
                    $price .= "<th >$label</th>";
                }
            }
            $price .= "</tr>";
            $i = 0;
            //                arrPrintWebs($items);
            foreach ($items as $itemsData) {
                $i++;
                $pID = $itemsData['id'];
                $hpp = $itemsData['hpp_nppn'];
                $price .= "<tr>";
                $price .= "<td>" . $i . "</td>";
                $price .= "
                        <td>
                            <div class='input-group''>
                              <input type='text' class='form-control' id='val_hj_$pID' placeholder='harga jual'>
                              <span class='input-group-btn'>
                                <button class='btn btn-default' id='btn_hj_$pID' onclick=\"applyToAll_$pID()\" type='button' disabled>Go!</button>
                              </span>
                            </div>
                             <script>
                                $(document).ready( delay_v2( function(){
                                    $('#val_hj_$pID').on('keyup', delay_v2(
                                    function(){
                                        if(parseFloat(this.value)>0){
                                            this.value = addCommas( removeCommas(this.value))
                                            $('#btn_hj_$pID').prop('disabled', false)
                                            $('#btn_hj_$pID').removeClass('btn-warning')
                                            $('#btn_hj_$pID').addClass('btn-success')
                                        }
                                        else{
                                            this.value = 0
                                            $(this).select()
                                            $('#btn_hj_$pID').prop('disabled', true)
                                            $('#btn_hj_$pID').removeClass('btn-success')
                                        }
                                    }, 100) );
                                }, 1000));
                            
                             </script>
                        </td>
                    ";
                $price .= "<td><b>" . $itemsData['kode'] . "</b> " . $itemsData['nama'] . "</td>";
                $encName = blobEncode($itemsData['nama']);
                $addJqueryLoad = "";
                $valNo = 0;

                //                    arrPrintWebs($itemHargaJual);

                // tampilan harga jual untuk desktop
                foreach ($itemPriceLabel2['harga'] as $CID => $cIData) {
                    foreach ($itemPriceEditable as $key => $label) {
                        $val = isset($itemPrice[$CID][$pID][$key]) ? $itemPrice[$CID][$pID][$key] * 1 : 0;
                        $harga_old = isset($itemPrice[$CID][$pID]['harga_jual_old']) && $itemPrice[$CID][$pID]['harga_jual_old'] * 1 >= 0 ? $itemPrice[$CID][$pID]['harga_jual_old'] * 1 : $val;
                        $jual_ideal = isset($itemHargaJual[$CID][$pID]['jual_ideal']) ? $itemHargaJual[$CID][$pID]['jual_ideal'] * 1 : 0;
                        //                            $hpp = isset($itemHargaJual[$CID][$pID]['hpp']) ? $itemHargaJual[$CID][$pID]['hpp'] * 1 : 0;
                        //cekOrange("C $CID  P $pID ||| " . $jual_ideal);
                        $inputType = "text";
                        //                            $inputType = "text";
                        $addEvent = " onchange=\"$('#result').load('$updateItemPriceTarget?spc=1&pid=$pID&key=$key&cid=$CID&h=$hpp&name=$encName&val='+parseFloat(removeCommas(this.value)))\" ";

                        //ini untuk bulk click merubah semua
                        $addJqueryLoad .= "\n$('#result').load('$updateItemPriceTarget?spc=1&pid=$pID&key=$key&cid=$CID&h=$hpp&name=$encName&val='+parseFloat(removeCommas(setValue)));";
                        $addJqueryLoad .= "\n$('input[name=\"$pID" . "_" . "$CID\"]').val(addCommas(setValue));";
                        $addJqueryLoad .= "\n$('input[name=\"$pID" . "_" . "$CID\"]').removeClass('text-green text-bold');";
                        $addJqueryLoad .= "\n$('input[name=\"$pID" . "_" . "$CID\"]').addClass('text-yellow text-bold');";
                        $addJqueryLoad .= "\n";
                        //ini untuk bulk click merubah semua

                        $color_jual = $harga_old < $hpp ? "text-red text-coret text-bold" : "text-info text-bold";
                        $final_hrg_jual = $harga_old >= 0 && $harga_old > $hpp && $val <= $jual_ideal ? $harga_old : ($val > $jual_ideal ? $val : $jual_ideal);
                        $final_hrg_jual_text = $harga_old >= 0 && $harga_old > $hpp && $val <= $jual_ideal ? "" : ($val > $jual_ideal ? "" : "text-bold text-green");
                        $strVal = "<div class='text-center text-bold'>Beli: " . number_format($hpp) . " </div><div class='$color_jual text-center'>Jual: " . number_format($harga_old) . "</div><!--<div>val: $val</div>-->";
                        $strVal .= "<input type=$inputType id='$pID" . "_" . "$CID'  name='$pID" . "_" . "$CID' class='form-control text-right $final_hrg_jual_text' value='$final_hrg_jual' onclick='this.select()' $addEvent>";
                        $tdOpt = "style='margin:0px;padding:0px;' ";
                        $price .= "<td>$strVal</td>";

                        $price .= "<script>";
                        $price .= $harga_old >= 0 && $harga_old > $hpp && $val <= $jual_ideal ? "$(\"input[name='$pID" . "_" . "$CID']\").val(addCommas($(\"input[name='$pID" . "_" . "$CID']\").val()));" : ($val > $jual_ideal ? "$(\"input[name='$pID" . "_" . "$CID']\").val(addCommas($(\"input[name='$pID" . "_" . "$CID']\").val()));" : "$(\"input[name='$pID" . "_" . "$CID']\").trigger('change');$(\"input[name='$pID" . "_" . "$CID']\").val(addCommas($(\"input[name='$pID" . "_" . "$CID']\").val()));");
                        $price .= "\nconsole.log('val: $val');";
                        $price .= "\nconsole.log('final_hrg_jual: $final_hrg_jual');";
                        $price .= "</script>";

                    }
                    $valNo++;
                }
                $price .= "
                            <script>
                                function applyToAll_$pID(){
                                    console.log('$valNo');
                                    var countHarga = '$valNo';
                                    var origValue = $('#val_hj_$pID').val();
                                    var setValue = parseFloat(removeCommas(origValue)) > 0 ? parseFloat(removeCommas(origValue)) : 0;
                                    if( parseFloat(setValue)>0 ){
                                        " . $addJqueryLoad . "
                                        \n $('#btn_hj_$pID').removeClass('btn-success')
                                        \n $('#btn_hj_$pID').addClass('btn-warning')
                                    }
                                    else{
                                        Sweetalert2(\"<b>" . $itemsData['kode'] . "</b> " . $itemsData['nama'] . " <br><br><r>HARGA tidak boleh NOL</r>\");
                                    }
                                }
                            </script>
                            ";
                $price .= "</tr>";
            }
            $price .= "</table>";
            $price .= "</div>";
            $price .= "
                <div class='panel panel-warning' style='padding: 5px;background: antiquewhite;'>
                    <h4 class='text-red'>KETERANGAN / KODE WARNA:</h4>
                    <div class='text-green text-bold'>HIJAU: ADALAH HARGA IDEAL BERDASARKAN <a href=\"javascript:void(0)\" onclick=\"swal('fitur ini sedang dalam perbaikan')\">PENGATURAN</a> HARGA IDEAL ()</div>
                    <div class='text-red text-bold'>MERAH: ADALAH HARGA JUAL LEBIH KECIL DARI HPP</div>
                    <div class='text-blue text-bold'>BIRU: ADALAH HARGA JUAL YANG SUDAH DITETAPKAN SAAT INI</div>
                    <div class='text-orange text-bold'>ORANGE/HITAM: ADALAH HARGA JUAL YANG AKAN DITETAPKAN</div>
                </div>
                ";
        }
        $price .= $custom_js;
        echo $price;

        /*---------------------sum CBM CKD------------------------------------*/
        $volume_gross = "";
        $berat_gross = "";
        if (isset($detilSizeBar)) {
            if (sizeof($detilSizeBar) > 0) {

                $volume_gross = isset($detilSizeBar['volume_gross']) ? $detilSizeBar['volume_gross'] : 0;
                $berat_gross = isset($detilSizeBar['berat_gross']) ? $detilSizeBar['berat_gross'] : 0;

                echo "<div class='row bg-danger' style='background: #ffdecf;padding: 7px;'>";
                echo "<div class='col-md-3 col-lg-3'>
                        <div class='input-group'>
                        <span class='input-group-addon' style='color: #000000;'>CBU CBM</span>
                        <input type='text' class='form-control bg-danger' style='color: #000000;font-weight: bolder;' value='0' disabled=''>
                        </div>
                     </div>";
                echo "<div class='col-md-3 col-lg-3'>
                        <div class='input-group'>
                        <span class='input-group-addon' style='color: #000000;'>CBU (KG)</span>
                        <input type='text' class='form-control bg-danger' style='color: #000000;font-weight: bolder;' value='0' disabled=''>
                        </div>
                     </div>";
                echo "<div class='col-md-3 col-lg-3'>
                        <div class='input-group'>
                        <span class='input-group-addon' style='color: #000000;'>CKD CBM</span>
                        <input type='text' class='form-control bg-danger' style='color: #000000;font-weight: bolder;' value='$volume_gross' disabled=''>
                        </div>
                     </div>";
                echo "<div class='col-md-3 col-lg-3'>
                        <div class='input-group'>
                        <span class='input-group-addon' style='color: #000000;'>CKD (KG)</span>
                        <input type='text' class='form-control bg-danger' style='color: #000000;font-weight: bolder;' value='$berat_gross' disabled=''>
                        </div>
                     </div>";
                echo "</div>";
            }
        }

        // arrPrint($elements);
        if (sizeof($elements) > 0) {
            echo "<div class='panel-body table-responsive'>";
            echo "<div class='row'>";
            echo "<div class='col-md-12'>";
            echo "<h4 class='text-blue text-left'>Please fill in details below</h4>";
            echo "</div class='col-md-12'>";
            echo "</div class='row'>";
            echo "<div class='col-lg-12 no-padding text-center' style='text-align:center;'>";
            $elCtr = 0;
            foreach ($elements as $eName => $pSpec) {
                $elCtr++;
                if (isset($pSpec['type']) && ($pSpec['type'] == "hidden")) {
                    // type hidden tidak perlu tampil di ui //
                }
                else {
                    //region penampil untuk elemen pada shopingcart
                    if ($elCtr % 2 == 0) {
                    }
                    else {
                        echo "<div class='col-lg-12 no-padding'>";
                        echo "<div class='row row-eq-height'>";
                    }
                    echo "<div class='col-md-6 col-lg-6' style='border:2px #e1ece6 solid;margin:0px;background:" . $pSpec['bgColor'] . "'>";
                    //                    echo "<div class='col-md-12 col-lg-12'>";
                    echo "<div id='elTitle_$eName' class='text-left text-muted text-bold text-capitalize'>";
                    //                echo "<h5>";
                    echo $pSpec['label'] . " ";
                    if (isset($elementConfigs[$eName]['autoSelect']) && $elementConfigs[$eName]['autoSelect']) {
                    }
                    else {
                        echo "<a href='javascript:void(0)' onclick=\"hiliteDiv(this);document.getElementById('result').src='" . $elementResetTarget . "$eName';\"><span class='fa fa-eraser'></span></a>";
                    }
                    echo "<span class='pull-right'><sup>" . $pSpec['editStr'] . "&nbsp;" . $pSpec['addStr'] . "</sup></span>";
                    //                echo "</h5>";
                    echo "</div class='box-title'>";

                    if (isset($elementConfigs[$eName]['warningLabel']) && $elementConfigs[$eName]['warningLabel']) {
                        echo "<div class='col-md-12'>" . $elementConfigs[$eName]['warningLabel'] . "</div>";
                    }

                    echo "<div class=''>&nbsp;</div>";
                    $strSpec = $pSpec['string'];
                    /*-----------------------------
                     * penampil element kirimna dari controler
                     * -----------------------*/
                    echo $pSpec['string'];
                    //                    echo "</div>";
                    echo "</div>";
                    // cekLime(__LINE__);

                    if ($elCtr % 2 == 0) {
                        echo "</div>";
                        echo "</div>";
                    }
                    //endregion
                }
            }
            echo "</div class='row'>";
        }
        //        arrprint($inputs);
        //nilai_entry

        if (sizeof($inputs) > 0) {
            echo "<div class='col-lg-12 no-padding' style='margin-top:5px;'>";
            echo "<div class='alert alert-info-dot'>";
            echo "<h4 class='text-left'>additional values</h4>";
            echo "<table class='table table-condensed'>";
            echo "<tr>";
            foreach ($inputs as $eName => $eStr) {
                echo "<td class='text-muted'>";
                echo $inputLabels[$eName];
                echo "</td>";
            }
            echo "</tr>";
            echo "<tr>";
            foreach ($inputs as $eName => $eStr) {
                echo "<td>";
                echo $eStr;
                echo "</td>";
            }
            echo "</div>";
            echo "</div>";
            echo "</tr>";
            echo "</table class='table table-condensed'>";
            echo "</div class='panel-default'>";
            echo "</div class='panel'>";
        }

        //arrPrint($previewJurnal);
        if (isset($previewJurnal) && sizeof($previewJurnal) > 0) {
            $headersJurnal = $previewJurnal['header'];

            //            echo "<div class='panel panel-info col-md-12'>";

            foreach ($previewJurnal['jurnal'] as $cabangID => $subItems) {
                if (sizeof($subItems) > 0) {
                    $cabangNama = isset($previewJurnal['cabang'][$cabangID]) ? $previewJurnal['cabang'][$cabangID] : "";


                    echo "<h4 class='text-blue' style='text-align: left;margin-top: 10px;'><span class='fa fa-book'></span> preview journal entries ($cabangNama)</h4>";

                    echo "<div class='tabel table-responsive'>";
                    echo "<table class='table table-condensed'>";

                    echo "<tr bgcolor='#f0f0f0'>";
                    foreach ($headersJurnal as $key => $label) {
                        echo "<td>";
                        echo "$label";
                        echo "</td>";
                    }
                    echo "</tr>";

                    foreach ($subItems as $iSpec) {
                        echo "<tr>";
                        foreach ($headersJurnal as $key => $label) {
                            echo "<td style='text-align: left;'>";
                            echo formatField($key, $iSpec[$key]);
                            echo "</td>";
                            if (is_numeric($iSpec[$key])) {
                                if (!isset($total[$cabangID][$key])) {
                                    $total[$cabangID][$key] = 0;
                                }
                                $total[$cabangID][$key] += $iSpec[$key];
                            }
                        }
                        echo "</tr>";
                    }

                    echo "<tr style='font-size: 15px;font-weight: bold;'>";
                    foreach ($headersJurnal as $key => $label) {
                        echo "<td>";
                        if (isset($total[$cabangID][$key])) {
                            echo formatField($key, $total[$cabangID][$key]);
                        }
                        echo "</td>";
                    }
                    echo "</tr>";

                    echo "</table>";
                    echo "</div>";

                }
                else {
                    echo "<div class='text-center text-warning'>";
                    echo "- no journal affected by this transaction -<br><br>";
                    echo "</div class='text-center text-warning'>";
                }
            }
            //            echo "</div>";
        }

        echo "
            <script>
                $('#shopping_cart').trigger('change');
                top.$('#btnProcess').attr('disabled', false);
            </script>
        ";
        //bagian print barcode
        // arrPrint($addMainSourceField);

        $no = 0;
        $valCheckBox="";
        if (isset($addMainSourceField) && sizeof($addMainSourceField) > 0) {
            // arrPrint($addMainSource);
            // arrPrint($updateMainSourceTarget);
            $valCheckBox ="";
            if(isset($addMainSource["inputType"])){
                $valCheckBox .="<div class='tabhle-responsive text-left'>";
                foreach ($addMainSourceField as $kol => $label) {
                    $val = isset($mainValues[$kol]) ? $mainValues[$kol] : "";
                    $addEvent = " onclick=\"if($(this).is(':checked')){ document.getElementById('result').src='$updateMainSourceTarget?spc=1&id=print_barcode&val=1'}else{document.getElementById('result').src='$updateMainSourceTarget?spc=1&id=print_barcode&val=0'};\" ";
                    $tdOpt = "style='margin:0px;padding:0px;' ";
                    $checked = $print_barcode == 1 ? "checked":"";
                    $valCheckBox .= "<label class='cek_box'><input type='checkbox' class='fgorm-control' id='cetak_tidak' $checked $addEvent> <span class='text-bold text-red' style='font-size:medium;'> $label</span></label> ";

                }

                $radio_toggle = $print_barcode == 1 ? "" : "disabled";
                $valCheckBox .= "<div id='pilihan_jml' style='display: :none;' class='bg-info'>";
                $valCheckBox .= "<label class='cek_box'><input type='radio' name='barcode_jml' id='tik_standart' $radio_toggle class='fgorm-control' checked> <span class='text-bold text-red' style='font-size:medium;'> jumlah sesuai invoice</span></label> ";
                $valCheckBox .= "<label><input type='radio' name='barcode_jml' id='tik_custom' $radio_toggle class='fgorm-control'> <span class='text-bold text-red cek_box' style='font-size:medium;'> jumlah ditentukan sendiri</span></label> ";
                $valCheckBox .="</div>";

                $valCheckBox .="</div>";

                $no = 0;
                $custom_print = "";
                if (isset($items) && sizeof($items) > 0) {
                    // arrPrintWebs($itemLabels);
                    $custom_print .= "<table class='table table-bordered table-condensed ' style='background:#ffffff;'>";
                    //region header rincian data
                    $custom_print .= "<tr bgcolor='#f0f0f0'>";
                    $custom_print .= "<th class='text-muted' style='font-weight:bold;' align='right' width='60'>no</th>";
                    foreach ($shoppingCartFields as $key => $label) {
                        $custom_print .= "<th class='text-muted' style='font-weight:bold;'>";
                        $custom_print .= $label;
                        $custom_print .= "</th>";
                    }
                    $custom_print .= "</tr>";
                    //endregion

                    //region body rincian data
                    foreach ($items as $i => $iSpec) {
                        $id = $iSpec['id'];
                        if (array_key_exists($id, $msgWarnings)) {
                            $addStyle = "background-color:yellow;color:#000000;";
                        }
                        else {
                            $addStyle = "";
                        }

                        $no++;
                        $custom_print .= "<tr>";
                        $custom_print .= "<td align='right' style='$addStyle'>";
                        $custom_print .= $no . " $id";
                        $custom_print .= "</td>";
                        $allowEdit = true;
                        foreach ($shoppingCartFields as $key => $label) {
                            $replacers = array(
                                "produk_nama"    => "nama",
                                "produk_ord_jml" => "jml",
                            );
                            foreach ($replacers as $orig => $new) {
                                if ($key == $orig) {
                                    $key = $new;
                                }
                            }

                            $subVal = isset($iSpec[$key]) ? $iSpec[$key] : 0;
                            if ($key == "stok") {
                                $val = isset($iSpec[$key]) ? $iSpec[$key] : 0;
                            }
                            else {
                                $val = isset($detailValues[$id][$key]) ? $detailValues[$id][$key] : $subVal;
                            }

                            $val_print = isset($_SESSION['barcode_print']['items'][$id]['jml_print']) ? $_SESSION['barcode_print']['items'][$id]['jml_print'] : $val;

                            if ($allowEdit == true && in_array($key, $editableFields)) {
                                if (is_numeric($val)) {
                                    $val += 0;
                                    $maxVal = isset($iSpec["max_" . $key]) ? $iSpec["max_" . $key] : $iSpec[$key];
                                    $inputType = "number";
                                    $addEvent = "";
                                    if (!$allowIncrement) {
                                        // $addEvent = " oninput=\"if(parseInt(this.value)<1 || parseInt(this.value)>$maxVal){this.value='$maxVal';}\" ";
                                        $addEvent .= " onblur=\"document.getElementById('result').src='$updateItemFieldTarget?id=$id&jml_print='+this.value\" ";
                                        $addEvent .= " onkeyup=\"document.getElementById('result').src='$updateItemFieldTarget?id=$id&jml_print='+this.value\" ";
                                    }
                                    else {
                                        $addEvent = " onblur=\"document.getElementById('result').src='$updateItemFieldTarget?spc=1&id=$id&jml_print='+this.value\" ";
                                    }
                                }
                                else {
                                    $inputType = "text";
                                    $addEvent = "";
                                }

                                $strVal = "<input line='" . __LINE__ . "' type=$inputType name='$key" . "_" . "$id' class='form-control text-right' value='$val_print' onclick='this.select()' $addEvent>";
                                $strVal .= "<script></script>";

                                $tdOpt = "style='margin:0px;padding:0px;$addStyle' ";
                            }
                            else {
                                $strVal = formatField_he_format($key, $val);
                                $tdOpt = "style='$addStyle'";
                            }

                            $custom_print .= "<td $tdOpt >$strVal";
                            $custom_print .= "</td>";
                        }

                        $custom_print .= "</tr>";

                    }
                    //endregion body rincian data
                    $custom_print .= "</table>";
                }
                //
                $valCheckBox .= "<div id='custom_print' class='table-responsive bg-info' style='display:none;padding: 0 10px;'>$custom_print</div>";
                $valCheckBox .= "<div id='custom_print_clear'></div>";

                /* -------------------------------------------------------------------
                 * navigasi klaklik-klaklik
                 * -------------------------------------------------------------------*/
                // $link_to_clear_custom_print = base_url() . "pembelian/_followupLiveEdit/updateItemField/" . $this->jenisTr . "/108148/3/2";
                $link_to_clear_custom_print_main = "$link_to_clear_custom_print?main=0";
                $valCheckBox .="<script>
                            $(document).ready(function(){
                                    $('#tik_custom').click(function(){
                                        console.log('cek');
                                        $('#custom_print').fadeIn();
                                        $('#tik_standart').attr('disabled','disabled');
                                    });
                                    $('#tik_standart').click(function(){
        
                                        $('#custom_print').fadeOut();
                                          // setTimeout(function() {
                                          //    $('#result').load('$link_to_clear_custom_print')
                                          // }, 1000);
                                         $('#custom_print_clear').load('$link_to_clear_custom_print');
                                    });
                                   
                                    $('#cetak_tidak').click(function(){                                                                            
                                            if ($('#cetak_tidak').is(':checked')) {
                                                $('#tik_custom').attr('disabled',false);
                                                if ($('#tik_custom').is(':checked')) {
                                                    $('#custom_print').fadeIn();
                                                    $('#tik_standart').attr('disabled','disabled');
                                                }
                                                else{                                                    
                                                    // $('#tik_standart').attr('disabled',false).prop('checked', true);
                                                    // $('#tik_standart').attr('disabled',false);
                                                }
                                            } else {
                                                $('#tik_custom').attr('disabled','disabled');
                                                $('#tik_standart').attr('disabled','disabled');
                                                $('#custom_print').fadeOut();
                                                // print_barcode
                                                $('#custom_print_clear').load('$link_to_clear_custom_print_main');
                                                $('#cetak_tidak').attr('disabled','disabled');
                                                $('.cek_box').css('color','green');
                                            }
                                           
                                    });
                             });                                        
                        </script>";


            }
            else{
                echo "<div class='table-responsive'>";
                //                    echo "<div class=''>Detail</div>";
                echo "<table class='table table-bordered table-condensed'>";
                echo "<tr bgcolor='#f5f5f5'>";
                echo "<th class='text-muted' style='font-weight:bold;' align='right' width='5'>no.</th>";
                foreach ($addMainSourceField as $key => $label) {
                    echo "<th class='text-muted' style='font-weight:bold;'>";
                    echo $label;
                    echo "</th>";
                }
                echo "</tr>";
                echo "<tr>";
                echo "<td $tdOpt>";
                echo "1";
                echo "</td>";
                foreach ($addMainSourceField as $kol => $alias) {
                    if (isset($addMainSourceEdit[$kol])) {
                        $inputType = $addMainSourceEdit[$kol];
                        $val = isset($mainValues[$kol]) ? $mainValues[$kol] : "";
                        $addEvent = " onblur=\"document.getElementById('result').src='$updateMainSourceTarget?ravPrev=$rawPrevURL&spc=1&rawBuilderURL=$rawBuilderURL&id=$kol&&val='+this.value\" ";
                        $addEvent .= " onkeyup=\"document.getElementById('result').src='$updateMainSourceTarget?ravPrev=$rawPrevURL&spc=1&rawBuilderURL=$rawBuilderURL&id=$kol&&val='+this.value\" ";
                        $strVal = "<input type=$inputType name='$kol' class='form-control text-left' value='$val' onclick='this.select()' $addEvent>";
                        $tdOpt = "style='margin:0px;padding:0px;$addStyle' ";
                    }
                    else {
                        $strVal = formatField_he_format($kol, $mainValues[$kol]);
                    }
                    echo "<td $tdOpt>";
                    echo $strVal;
                    echo "</td>";
                }
                echo "</tr>";
                echo "</div class='table-responsive'>";
            }


            echo $valCheckBox;
        }
    }
    else {
        echo "<div class='panel-body'>";
        echo "<div class='text-danger'>";
        echo "<div><strong>you have not chosen any item yet</strong></div>";
        echo "<div><small>you can do so by selecting items from available selectors</small></div>";
        echo "</div class='text-warning'>";
        echo "</div class='panel-body'>";
    }

    $sessionCleares = array("errLines", "errFields", "errMsg");
    foreach ($sessionCleares as $s) {
        if (isset($_SESSION[$s])) {
            unset($_SESSION[$s]);
        }
    }

}
