<?php

    if (isset($items) && count($items) > 0) {

        $sesMain = $_SESSION[$cCode]['main'];

        //handle modal pembayaran
        echo "<div class='viewModalPembayaran' style=''>";

        /* ----------------------------------------------------------------------------------------------------------
         * payment method
         * ----------------------------------------------------------------------------------------------------------*/
        echo "<div id='py_methode' class='rowx row-eq-height'>";
        if (sizeof($elements) > 0 && !$onPreOrderOnly) {
            $elCtr = 0;
            foreach ($elements as $eName => $pSpec) {
                $elCtr++;
                if (isset($pSpec['type']) && ($pSpec['type'] == "hidden")) {
                    // type hidden tidak perlu tampil di ui //
                }
                else {
                    echo "<div style='border: 1px solid skyblue;margin-right:5px;' class='col-lg-4 showPayment table-bordered text-center no-padding bg-warning'>";
                    $bgColor = isset($pSpec['bgColor']) ? $pSpec['bgColor'] : "";
                    $iconOke = isset($pSpec['iconOke']) ? $pSpec['iconOke'] : "";

                    echo "<div class='text-bold text-uppercase $bgColor badgex no-padding'>$iconOke" . $pSpec['label'] . "</div>";
//                    arrPrint( $pSpec );
                    if (isset($elementConfigs[$eName]['warningLabel']) && $elementConfigs[$eName]['warningLabel']) {
                        echo "<div class='col-md-12'>" . $elementConfigs[$eName]['warningLabel'] . "</div>";
                    }
                    echo $pSpec['string'];
                    echo "</div>";
                    //endregion
                }
            }
        }
        echo "</div class='rowx row-eq-height'>";

        /* ----------------------------------------------------------------------------------------------------------
         * total belanja dan tanggal belanja
         * ----------------------------------------------------------------------------------------------------------*/
        if(isset($main)){

            echo "<div id='dt_belanja' style='margin-top: 15px;padding-right: 15px;padding-left: 15px;' class='panel panel-success'>";

            echo "<div class='row'>";

            echo "<div class='col col-lg-3'>";
            echo "<div style='font-size: 12px;' class='text-left text-bold text-uppercase text-info'>tgl trx</div>";
            echo "<div class='text-left text-bold text-green'>".date('Y-m-d H:i')."</div>";
            echo "</div>";

            $totalItems = count($items);

            $totalQty = 0;
            $totalQtyDigi = 0;
            foreach($items as $p => $rowItems){
                if($rowItems['tipe_produk']=="digital"){
                    $totalQtyDigi += $rowItems['qty_unit'];
                }
                else{
                    $totalQty += $rowItems['qty_unit'];
                }
            }

            echo "<div class='col col-lg-5'>";
            echo "<div style='font-size: 12px;' class='text-center text-bold text-uppercase text-info'>jml.item/qty</div>";
            echo "<div style='font-size: 20px;' class='text-center text-bold text-green'><sub><small><r>Item</r></small></sub>&nbsp;$totalItems <sub><small><r>Qty</r></small></sub>&nbsp;$totalQty</div>";
            echo "</div>";

//            echo "<div class='col col-lg-3'>";
//            echo "<div style='font-size: 12px;' class='text-center text-bold text-uppercase text-info'>jml.item digital</div>";
//            echo "<div style='font-size: 20px;' class='text-center text-bold text-green'><sub><small><r>Qty</r></small></sub>&nbsp;$totalQtyDigi</div>";
//            echo "</div>";

            echo "<div class='col col-lg-4'>";
            echo "<div style='font-size: 12px;' class='text-right text-bold text-uppercase text-info'>Total Belanja</div>";
            echo "<div style='font-size: 20px;' class='text-right text-bold text-green'>" . number_format($main['harga_nett1']) . "</div>";
            echo "</div>";

            echo "</div>";

            echo "</div>";

        }

        /* ------------------------------------------------
         * validasi pilihan diskon
         * ------------------------------------------------*/
        $itemDiskonJml = count($itemDiskons);
        $text_blur = "";
        if($itemDiskonJml > 0){
            $text_blur = "text-blur";
            $infoDiskonItems = "";
            /*---pembuat layout info diskon-------*/
            $infoDiskonItems .= "<div class='alert alert-warning'>";
            $infoDiskonItems .= "<div class='text-bold font-1-2 text-uppercase' style='margin-bottom: 15px;'>";
            $infoDiskonItems .= "<i class='fa fa-circle blink text-red'></i> pilih salah satu sebelum melanjutkan";
            $infoDiskonItems .= "</div>";
            foreach ($itemDiskons as $iproduk_id => $iparams) {
                // arrPrintHijau($iparams);
                $iID= $iproduk_id;
                $sesItem = $sesItems[$iproduk_id];
                $iproduk_nama = $sesItems[$iproduk_id]['nama'];

                $infoDiskonItems .= "<div class='box box-danger box-solid'>";
                $infoDiskonItems .= "<div class='box-header with-border' title='$iproduk_id'> $iproduk_nama</div>";
                $infoDiskonItems .= "<div class='box-body'>";
                foreach ($iparams as $iparam_k => $iparam_v) {
                    // cekBiru($sesItem);
                    $checked_grosir = "";
                    $checked_free = "";
                    switch ($iparam_k){
                        case "discPersen":
                            $qty = $sesItem['qty'];
                            $harga_disc = $sesItem['harga_disc'];
                            $harga_bulk = $qty * $harga_disc;
                            $discNilai = $sesItem['discNilai'];
                            $discPersen = $sesItem['discPersen'];
                            $harga_bulk_f = number_format($harga_bulk,0);
                            $harga_disc_f = number_format($harga_disc,0);
                            $discPersen_f = number_format($discPersen,2);

                            $hasil = "<b><r>Beli</r> $qty harga Rp. $harga_disc_f,-</b> (disc Rp. <r>$discNilai</r>/$discPersen_f%) (Rp.<gb>$harga_bulk_f</gb>/<r>$qty</r>)";
                            $infoDiskonItems .= "<div class='float-container' style='overflow: hidden;'>";
                            $option_d = "<input type='radio' name='produk_$iID' id='grosir_$iID' $checked_grosir value='1' onclick='diskon_off_r(this.value, this.id);'>";
                            $infoDiskonItems .= "<div class='float-child'>$option_d</div>";
                            $infoDiskonItems .= "<div class='float-child'> $hasil</div>";
                            $infoDiskonItems .= "</div>";

                            $diskon_grosir = true;
                            break;
                        case "free_items":
                            $option_df = "<input type='radio' name='produk_$iID' id='free_$iID' $checked_free value='1' onclick='diskon_off_r(this.value, this.id);'>";
                            foreach ($iparam_v as $idDiskon => $rowDiskon) {
                                // arrPrintPink($rowDiskon);
                                // $infoFreeItemss = "<div class='box-free bg-grey-1 overflow-h'>";
                                $infoFreeItemss = "<span class='text-green'><b class='text-red'>Gratis!</b> <span class='text-bold text-capitalize'>" . $rowDiskon['free_qty'] . "x " . $rowDiskon['free_produk_nama'] . "</span></span>";
                                // $infoFreeItemss .= "<br>minimal pembelian " . $rowDiskon['minimal'] . "x <span class='meta'> (" . ($rowDiskon['kelipatan'] == 0 ? "tidak berlaku kelipatan" : "berlaku kelipatan") . ")</span>";
                                // $infoFreeItemss .= "</div>";
                            }

                            if(!empty($infoFreeItemss)){
                                if($diskon_grosir == true){
                                    $infoDiskonItems .= "<div class='box-free bg-grey-1 overflow-h'>";
                                }
                                else{
                                    $infoDiskonItems .= "<div class=''>";
                                }
                                $infoDiskonItems .= "<div  class='float-child'>$option_df</div>";
                                $infoDiskonItems .= "<div  class='float-child' style='word-wrap: break-spaces;'>$infoFreeItemss</div>";
                                $infoDiskonItems .= "</div>";
                            }

                            break;
                    }
                }
                // $infoDiskonItems .= $infoFreeItemss;
                $infoDiskonItems .= "</div>";
                $infoDiskonItems .= "</div>";
            }
            $infoDiskonItems .= "</div>";

            /*---penampilnya pakai echo dibawah ini----*/
            echo $infoDiskonItems;
            echo "<style type='text/css'>
                // .container-fluid{
                //     background-color: #8d8d8d;
                // }
            </style>";

            $btn_lanjut_fake = "";
            // $btn_lanjut_fake .= "<div>Pilih salah satu diskon yang berwarna orange</div>";
            $btn_lanjut_fake .= "<button type=\'button\' class=\'btn btn-danger\' id=\'ibatal\'>BATAL</button>";
            $btn_lanjut_fake .= "<button type=\'button\' class=\'btn btn-danger\' disabled>LANJUTKAN PEMBAYARAN</button>";
            echo "
                <script>
                    // $('.modal-footer').hide();
                    // $('#showViewOtorisasiJualLowerHpp').removeClass('hidden');

                    $('.modal-footer').html('$btn_lanjut_fake');
                    $('#ibatal').click(function() {
                        BootstrapDialog.closeAll();
                    })
                    $('#dt_belanja').addClass('text-blur');
                    $('#py_methode').addClass('text-blur');
                </script>";

        }

        /* ----------------------------------------------------------------------------------------------------------
         * total-totalan
         * ----------------------------------------------------------------------------------------------------------*/
//        echo "<div class='rowx row-eq-height'>";
        if (sizeof($addRows) > 0 && !$onPreOrderOnly ) {

            echo "<div class='row $text_blur'>";
            echo "<div class='container-fluid'>";

            $nr = 0;
            foreach ($addRowLabels as $k => $label) {
                $nr++;
                $bottom_borderless = $nr < sizeof($addRowLabels) ? "bottom-borderless" : "";
                $rowHide = isset($addRowHiddens[$k]) ? $addRowHiddens[$k] : "";

                if($k=="bayar"){
                    echo "<div style='' class=''>";
                    echo "<div style='font-size: 18px;' class='col col-lg-8 no-padding text-capitalize text-muted'>" . $label . "</div>";
                    echo "<div class='col col-lg-4 no-padding text-bold'>" . $addRows[$k] . "</div>";
                    echo "</div>";
                }
                else{
                    echo "<div style='' class=''>";
                    echo "<div style='font-size: 18px;' class='col col-lg-8 no-padding text-capitalize text-bold text-muted'>" . $label . "</div>";
                    echo "<div ids_k='$k' class='col col-lg-4 no-padding text-bold'>" . $addRows[$k] . "</div>";
                    echo "</div>";
                }
            }


            /* ------------------------------------------------
             * $validasiHpp diatur dr coTransaksiUi
             * ------------------------------------------------*/
            if($validasiHpp){
            if( $sesMain['harga_nett3']*1 < $sesMain['hpp_produk']*1 && !isset($sesMain['allow_jual_lower_hpp']) ){

                echo "<div style='' class='hidden'>";
                echo "<div style='font-size: 18px;' class='col col-lg-8 no-padding text-capitalize text-bold text-muted'>NET JUAL</div>";
                echo "<div class='col col-lg-4 no-padding text-bold'>" . $sesMain['harga_nett3'] . "</div>";
                echo "<div style='font-size: 18px;' class='col col-lg-8 no-padding text-capitalize text-bold text-muted'>TOTAL HPP</div>";
                echo "<div class='col col-lg-4 no-padding text-bold'>" . $sesMain['hpp_produk'] . "</div>";
                echo "<div style='font-size: 18px;' class='col col-lg-8 no-padding text-capitalize text-bold text-muted'>LABA</div>";
                echo "<div class='col col-lg-4 no-padding text-bold'>" . $sesMain['laba_jual'] . "</div>";
                echo "</div>";


                echo "<div style='margin-top: 15px;' class='col-lg-12 no-padding'>";
                echo "<div style='font-size: 10px;' class='text-red text-bold text-center'> TOTAL BELANJA TERDETEKSI DI BAWAH TOTAL HPP, JIKA DILANJUTKAN MAKA AKAN TIMBUL KERUGIAN<br>ANDA BISA MELANJUTKAN DENGAN KLIK TOMBOL DIBAWAH DAN MINTA OTORISASI KE-ATASAN</div>";

                echo "<div id='showViewOtorisasiJualLowerHpp' class='btn btn-sm btn-danger btn-block text-bold hidden' onclick='showViewOtorisasiJualLowerHpp()'>OTORISASI TOTAL BELANJA DIBAWAH TOTAL HPP</div>";

                echo "<div style='margin-top: 10px;font-size:10px;' class='text-bold text-left'><u>Penyebab Timbulnya Notifikasi ini:</u><br>";
                echo "- <i>Total penjualan Anda lebih kecil dari total HPP produk yang di jual.</i><br>";
                echo "- <i>Ada 1 atau beberapa produk dengan setingan HPP yang salah.</i><br>";
                echo "- <i>Ada diskon produk / diskon konsumen yang terlalu besar.</i><br>";
                echo "- <i>Diskon tambahan terlalu besar.</i></div>";

                echo "</div>";

                echo "
                <script>
                    $('.modal-footer').hide();
                    $('#showViewOtorisasiJualLowerHpp').removeClass('hidden');
                </script>";


                $this->load->model("Mdls/MdlEmployeeCabang");
                $emp = new MdlEmployeeCabang();
                $emp->addFilter("oto_settlement=1");
                $arrEmployee = $emp->lookUpAll()->result();

                $emplyeeOption = "";
                if (!empty($arrEmployee)) {

                    $emplyeeOption .= "<div class='col-lg-4 no-padding text-right'>";
                    $emplyeeOption .= "<div style='font-size: 14px;' class='text-bold text-muted text-capitalize'>&nbsp;</div>";
                    $emplyeeOption .= "<div class='input-group input-group-md pull-left'>";
                    $emplyeeOption .= "<select class='form-control select-otorisator text-bold text-uppercase'>";
                    $emplyeeOption .= "<option value='0'>PILIH OTORISATOR</option>";

                    if (!empty($arrEmployee)) {
                        foreach ($arrEmployee as $i => $eRow) {
                            $emplyeeOption .= "<option value='" . $eRow->id . "' class='text-bold text-uppercase'>" . $eRow->nama . "</option>";
                        }
                    }

                    $emplyeeOption .= "</select></div></div>";

                    $emplyeeOption .= "<div class='col-lg-8 no-padding text-left pull-right'>";
                    $emplyeeOption .= "<div style='font-size: 14px;' class='text-bold text-muted text-capitalize'>Masukan PIN/OTP SUPERVISOR</div>";
                    $emplyeeOption .= "<div class='input-group input-group-md'>";
                    $emplyeeOption .= "<form autocomplete='off'>";
                    $emplyeeOption .= "<input type='text' autocomplete='false' class='' style='display: none;'>";
                    $emplyeeOption .= "<input type='password' readonly onfocus='this.removeAttribute(`readonly`);' autocomplete='false' name='jualLowerHppPinOtp' id='jualLowerHppPinOtp' class='form-control' placeholder='PIN / OTP'>";
                    $emplyeeOption .= "</form>";
                    $emplyeeOption .= "<span class='input-group-btn'>";
                        $emplyeeOption .= "<div types='button' disabled id='btn-otorisasi' class='btn btn-info btn-flat btn-kirim'><i class='fa fa-send'></i> TOTAL BELANJA < TOTAL HPP</div>";
                    $emplyeeOption .= "<div types='button' onclick='swal.close()' class='btn btn-danger btn-flat btn-close-kirim'><i class='fa fa-times'></i></div>";
                    $emplyeeOption .= "</span>";
                    $emplyeeOption .= "</div>";
                    $emplyeeOption .= "</div>";

                    $emplyeeOption .= "</div>";
                    $emplyeeOption .= "</div>";
                    $emplyeeOption .= "</div>";

                }

                $blobDatas = blobEncode($sesMain);
                $shoppingcart_order_id = $sesMain['shoppingcart_order_id'];
                $gate_set = "main";
                $cCode_set = "_TR_".$this->jenisTr;

                echo "<script>

                    var showViewOtorisasiJualLowerHpp = function(){
                        top.swal({
                           title: 'TOTAL BELANJA < TOTAL HPP',
                           html: 'Total Penjualan Produk lebih kecil dari Total HPP Produk.<br>Masukan PIN/OTP dari SPV Anda untuk melanjutkan Transaksi ini.<br><br>' +
                           \"<div class=''>\" +
                           \"$emplyeeOption\" +
                           \"</div>\" +
                           '',
                           type: 'warning',
                           customClass: 'swal-wide',
                           showConfirmButton: false,
                           allowOutsideClick: false,
                           onOpen: function(){
                                top.$('#jualLowerHppPinOtp').off('keyup');
                                top.$('#jualLowerHppPinOtp').on('keyup', top.delay_v2( function(){
                                    if( this.value.length >= 6){
                                        activeBtnNextJualLowerHpp()
                                    }
                                }, 500) )
                                var activeBtnNextJualLowerHpp = function(){
                                    top.$('#btn-otorisasi').attr('disabled', false);
                                    top.$('#btn-otorisasi').off('click');
                                    top.$('#btn-otorisasi').on('click', function(){
                                        var pin_otp = top.$('#jualLowerHppPinOtp').val();
                                        if( pin_otp != '' ){
                                            top.$.ajax({
                                                url: '". base_url() . $this->modul . "/_shoppingCart/jualLowerHppOtorisator/".$this->jenisTr."',
                                                method: 'post',
                                                data: {
                                                    pin_diskon: pin_otp,
                                                },
                                                success: function(a){
                                                    jsonData = JSON.parse(a);
                                                    if(jsonData.status){
                                                        top.swal('BERHASIL', jsonData.reason, 'warning');
                                                        top.swal.enableLoading();
                                                        if(top.document.getElementById('shopping_cart')){
                                                            top.$('.btn-reload-modal-pembayaran').click();
                                                            setTimeout( function(){ top.swal.close() }, 1000);
                                                        }
                                                    }
                                                    else{
                                                        top.swal('Perbaiki PIN/OTP', jsonData.reason, 'warning');
                                                        top.swal.enableLoading();
                                                        setTimeout(function(){
                                                            showViewOtorisasiJualLowerHpp()
                                                        },1500)
                                                    }
                                                }
                                            })
                                        }
                                        else{
                                            top.swal('Perbaiki PIN/OTP');
                                            top.swal.enableLoading();
                                            setTimeout(function(){
                                                showViewOtorisasiJualLowerHpp()
                                            },1500)
                                        }
                                    })
                                }
                                var pushRequestJualLowerHpp = function(){
                                    top.$('#jualLowerHppPinOtp').attr('disabled', true)
                                    top.$.ajax({
                                        url: '". base_url() . $this->modul . "/_shoppingCart/reqJualLowerHppSave/".$this->jenisTr."',
                                        method: 'post',
                                        data: {
                                            blob_datas: '$blobDatas',
                                            cCode_set: '$cCode_set',
                                            gate_set: '$gate_set',
                                            shoppingcart_order_id: $shoppingcart_order_id,
                                            jenisTr: '".$this->jenisTr."',
                                        },
                                        success: function(a){
                                            jsonData = JSON.parse(a);
                                            if(jsonData.status){
                                                top.$('#jualLowerHppPinOtp').attr('disabled', false)
                                            }
                                            else{

                                            }
                                        }
                                    })
                                }
                                pushRequestJualLowerHpp()
                           }
                        });
                    }

                    //top.$('.btn-reload-modal-pembayaran').click();

                </script>";

            }
            elseif( $sesMain['harga_nett3']*1 < $sesMain['hpp_produk']*1 && isset($sesMain['allow_jual_lower_hpp'])){

                $allow_jual_lower_hpp = $sesMain['allow_jual_lower_hpp'];
                $allow_jual_lower_id = $sesMain['allow_jual_lower_hpp_oto_id'];
                $allow_jual_lower_nama = $sesMain['allow_jual_lower_hpp_oto_nama'];

                echo "<div style='margin-top: 20px;margin-bottom: 5px;' class='col-lg-12 no-padding panel panel-success bg-green'>";
                echo "<div class='text-white text-bold text-center text-uppercase'>PENJUALAN DI BAWAH HPP DI OTORISASI OLEH: <b><r>$allow_jual_lower_nama</r></b></div>";
                echo "<div class='text-white text-bold text-center'>== TRANSAKSI SUDAH BISA DILANJUTKAN == </div>";
                echo "</div>";

                echo "
                <script>
                    $('.modal-footer').show();
                    $('#showViewOtorisasiJualLowerHpp').addClass('hidden');
                </script>";

            }
            else{
                echo "
                <script>
                    $('.modal-footer').show();
                    $('#showViewOtorisasiJualLowerHpp').addClass('hidden');
                </script>";
            }
            }


            echo "</div>";
            echo "</div>";
        }

//        echo "</div class='row row-eq-height'>";


        echo "</div>";

        echo "\n<script>\n";
        echo "\nsetTimeout( function(){ \n";
        echo "  console.log('show_Payment: ' +  $('.modal .showPayment').length );\n";
        echo "  console.log('selected_Radio: ' +  $('.modal input[type=radio]:checked').length );\n";
        echo "top.$('.modal.in input#bayar').focus();\n";
        echo "
//                $('.modal.in input#bayar').keydown(function (e) {
//                    if(e.which==13){
//                        e.preventDefault();
//                        if( top.$(\"#btnProcess\").length > 0 ){
//                            top.$(\"#btnProcess\").click();
//                            console.log('click btnProcess');
//                        }
//                    }
//                });
//                $('.modal.in input#bayar').keydown(delay_v2(function (e) {
//                    $(this).trigger('mouseout')
//                },1000));

                \n";
        echo "\n}, 100)";
        echo "</script>";
    }

else{

    echo "<script>";
    echo "top.$('.modal.in').modal('hide');";
    echo "top.swal('produk pelum lengkap', 'Anda belum memasukan produk.', 'warning');\n";
    echo "</script>";
}

?>