<?php

switch ($mode) {
    case "index":
        $scriptBottom = "";
        $title = "Maps Otorisasi";
        $subTitle = "Request Approval Maps";
        $p = New Layout("$title", "$subTitle", MODUL_TEMPLATE_PATH . "template/maps.html");

        $p->addTags(
            array(
                "menu_left" => callMenuLeft(),
                "trans_menu" => callTransMenu(),
                "float_menu_atas" => callFloatMenu('atas'),
                "float_menu_bawah" => callFloatMenu(),
                "menu_taskbar" => callMenuTaskbar(),
                "btn_back" => callBackNav(),
                "content" => $content,
            )
        );

        $p->render();

        break;
    case "data_table":

        $mdlName = $mdl;
        $this->load->model("Mdls/" . $mdlName);
        $fetch_data = $this->$mdlName->make_datatables();
        $listedFields = $this->$mdlName->getListedFields();

        $tdPaired = "";
        if (method_exists($this->$mdlName, "getPairedData")) {
            $paireds = $this->$mdlName->getPairedData();
            $pairedKolom = array();
            foreach ($paireds as $mdl_nama => $pairAttr) {
                if (is_array($pairAttr['kolom'])) {
                    foreach ($pairAttr['kolom'] as $itemKolom => $itemLabel) {
                        $tdPaired .= "<td>" . $itemLabel . "</td>";
                    }
                }
                else {
                    $tdPaired .= "<td>" . $pairAttr['label'] . "</td>";
                }
            }
        }
        else {
            $tdPaired ="";
        }

        $str = "";
        $str .= "<table id='user_data' border='1' class='table table-sm compact table-condensed table-striped table-bordered'>";
        $str .= "<thead>";
        $str .= "<tr class='bg-primary'>";
        $str .= "<td style='width: 20px;'>no</td>";
        foreach ($listedFields as $kolom => $label) {
            $str .= "<td>$label</td>";
        }
        $str .= $tdPaired;
        $str .= "<td>actions</td>";
        $str .= "</tr>";
        $str .= "</thead>";
        $str .= "<tbody>";
        $str .= "</tbody>";
        $str .= "</table>";
        $fId = $filterId;
        $str_fId = $filterId_str;
        // --------------------script untuk memangil ajax data table-----------------------
        $url = base_url() . $this->modul . "/$segment_2/fetch_data".$str_fId;
        $str_1 = "<script>
                $(document).ready( function () {
                     var posrurl = '$url';
                     var posmdl = '$mdl';
                     var postfid = '$fId';
                     var buttonCommon = {
                        exportOptions: {
                            format: {
                                body: function ( data, row, column, node ) {
                                    var newData = String(data);
                                    console.log(newData);
                                    var pos = newData.indexOf('<a ');

                                    if(pos!==-1){

                                    }
                                    else{
                                        var pos1 = newData.indexOf('<i ');
                                        if(pos1!==-1){
                                        }
                                        else{
                                            return data;
                                        }
                                    }
                                }
                            }
                        }
                    };

                     var dataTable = $('#user_data').DataTable({
                            dom: 'lBfrtip',
                            lengthMenu: [ [10, 20, 50, 100, -1], [10, 20, 50, 100, 'All'] ],
                            pageLength: 20,
//                            rowReorder: {
//                                selector: 'td:nth-child(2)'
//                            },
//                            responsive: {
//                                breakpoints: [
//                                  {name: 'bigdesktop', width: Infinity},
//                                  {name: 'meddesktop', width: 1480},
//                                  {name: 'smalldesktop', width: 1280},
//                                  {name: 'medium', width: 1188},
//                                  {name: 'tabletl', width: 1024},
//                                  {name: 'btwtabllandp', width: 848},
//                                  {name: 'tabletp', width: 768},
//                                  {name: 'mobilel', width: 480},
//                                  {name: 'mobilep', width: 320}
//                                ]
//                            },
                            'processing':true,
                            'serverSide':true,
                            'order':[],
                            'ajax':{
                                url:posrurl,
                                type:'POST',
                                data: {mdl:posmdl,fid:postfid}
                            },
                            'buttons': [
                                $.extend( true, {}, buttonCommon, {
                                    extend: 'copyHtml5'
                                } ),
                                $.extend( true, {}, buttonCommon, {
                                    extend: 'excelHtml5'
                                } ),
                                $.extend( true, {}, buttonCommon, {
                                    extend: 'pdfHtml5'
                                } )
                            ],
                            'columnDefs':[
                                {
                                    'targets':[0],
                                    'orderable':false,
                                },
                            ],
                        });
                });
                </script>";
        // -------------------------------------------

        //region template table
        $template = array(
            'table_open'         => '<table id="table" class="table table-condensed">',
            'thead_open'         => '<thead>',
            'thead_close'        => '</thead>',
            'heading_row_start'  => '<tr class="bg-grey-2">',
            'heading_row_end'    => '</tr>',
            'heading_cell_start' => '<th>',
            'heading_cell_end'   => '</th>',
            'tfoot_open'         => '<tfoot class="ui-widget-header ui-priority-secondary">',
            'footer_row_start'   => '<tr>',
            'footer_row_end'     => '</tr>',
            'footer_cell_start'  => '<th>',
            'footer_cell_end'    => '</th>',
            'tfoot_close'        => '</tfoot>',
            'table_close'        => '</table>',
        );
        $mainTemplate = array(
            'table_open'         => '<table id="main_table" class="table table-condensed margin-top-5">',
            'thead_open'         => '<thead>',
            'thead_close'        => '</thead>',
            'heading_row_start'  => '<tr class="bg-grey-2">',
            'heading_row_end'    => '</tr>',
            'heading_cell_start' => '<th>',
            'heading_cell_end'   => '</th>',
            'tfoot_open'         => '<tfoot class="ui-widget-header ui-priority-secondary">',
            'footer_row_start'   => '<tr>',
            'footer_row_end'     => '</tr>',
            'footer_cell_start'  => '<th>',
            'footer_cell_end'    => '</th>',
            'tfoot_close'        => '</tfoot>',
            'table_close'        => '</table>',
        );
        $itemsTemplate = array(
            'table_open'        => '<table id="table" class="table table-bordered table-condensed anu">',
            'thead_open'        => '<thead class="text text-muted text-capitalize" style="background:#f0f0f0;">',
            'thead_close'       => '</thead>',
            'tfoot_open'        => '<tfoot class="ui-widget-header ui-priority-secondary">',
            'footer_row_start'  => '<tr>',
            'footer_row_end'    => '</tr>',
            'footer_cell_start' => '<th>',
            'footer_cell_end'   => '</th>',
            'tfoot_close'       => '</tfoot>',
            'table_close'       => '</table>',
        );
        $foldersTemplate = array(
            'table_open'        => '<table id="folder_table" class="table table-condensed">',
            'thead_open'        => '<thead class="text text-muted text-capitalize" style="background:#f0f0f0;">',
            'thead_close'       => '</thead>',
            'tfoot_open'        => '<tfoot class="ui-widget-header ui-priority-secondary">',
            'footer_row_start'  => '<tr>',
            'footer_row_end'    => '</tr>',
            'footer_cell_start' => '<th>',
            'footer_cell_end'   => '</th>',
            'tfoot_close'       => '</tfoot>',
            'table_close'       => '</table>',
        );
        $this->table->set_template($template);
        //endregion
        //region onprogress

        if (sizeof($arrayOnProgress) > 0) {
            if (sizeof($arrayProgressLabels) > 0) {
                $header_prog = array();
                foreach ($arrayProgressLabels as $key => $label) {
                    $header_result_f = array('data' => $label, 'class' => '');
                    $header_prog[] = $header_result_f;
                }
                $this->table->set_heading($header_prog);
            }
            foreach ($arrayOnProgress as $key => $val) {

                if (sizeof($arrayProgressLabels) > 0) {
                    $isi = array();
                    foreach ($arrayProgressLabels as $key => $label) {
                        if ($key == "image") {
                            $images = isset($val[$key]) ? $val[$key] : "";
                            if (strlen($images) > 0) {
                                $imgsrc = "src='$images'";
                            }
                            else {
                                $imageAvail = base_url() . "public/images/img_blank.gif?=v1";
                                $imgsrc = "src='$imageAvail'";
                            }

                            $value = "<div class='thumbnail'><img $imgsrc' class='img-responsive' width='150px'></div>";
                        }
                        else {
                            $value = isset($val[$key]) ? $val[$key] : "";
                        }
                        $isi[] = array('data' => "$value ", 'class' => 'text-left');
                    }
                    $this->table->add_row($isi);
                }
            }

            $strDataProposeFooter = "<a class='btn btn-default' href='" . base_url() . $this->uri->segment(1) . "/viewIncomplete/" . "'><span class='glyphicon glyphicon-time'></span> complete list ...</a>";
        }
        else {
            $this->table->add_row(array(
                'data'    => '-the item you specified has no entry-',
                'colspan' => count($arrayProgressLabels) + 2,
                'class'   => 'text-center',
            ));
            $strDataProposeFooter = "";
        }
        $strDataPropose = $this->table->generate();
        //endregion

        //region recap
        if (sizeof($arrayRecap) > 0) {
            if (sizeof($arrayRecapLabels) > 0) {
                $header_recap = array();
                foreach ($arrayRecapLabels as $key => $label) {
                    $header_recap_f = array('data' => $label, 'class' => '');
                    $header_recap[] = $header_recap_f;
                }
                $this->table->set_heading($header_recap);
            }
            foreach ($arrayRecap as $key => $val) {
                if (sizeof($arrayRecapLabels) > 0) {
                    $isi_history = array();
                    foreach ($arrayRecapLabels as $key => $label) {
                        $value = isset($val[$key]) ? $val[$key] : "";
                        $isi_history[] = array('data' => $value, 'class' => 'text-left');
                    }
                    $this->table->add_row($isi_history);
                }
            }
            $strDataHistFooter = "<a class='btn btn-default' href='" . base_url() . $this->uri->segment(1) . "/viewRecap/" . "'><span class='glyphicon glyphicon-time'></span> complete $title reports ...</a>";
        }
        else {
            $this->table->add_row(array(
                'data'    => '-the item you specified has no entry-',
                'colspan' => count($arrayProgressLabels) + 2,
                'class'   => 'text-center',
            ));
            $strDataHistFooter = "";
        }
        $this->table->set_template($template);
        $strDataHist = $this->table->generate();
        //endregion
        $prop_display = sizeof($arrayOnProgress) < 1 ? "none" : "block";

        $error_msg = "";
        if (strlen($error) > 3) {
            $error_msg .= "<div class='alert alert-info'>";
            $error_msg .= "<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>";
            $error_msg .= $error;
            $error_msg .= "</div>";
        }

        $subTitle = "";
        $title = $title;
        $pageTemplate = (isset($_GET['mode']) && $_GET['mode'] == 'print') ? MODUL_TEMPLATE_PATH . "/template/defaultPrint.html" : MODUL_TEMPLATE_PATH .  "/template/maps.html";
        $p = New Layout("$title", "$subTitle", "$pageTemplate");



        $p->addTags(array(
            // -----------------------
            "menu_left" => callMenuLeft(),
            "trans_menu" => callTransMenu(),
            "float_menu_atas" => callFloatMenu('atas'),
            "float_menu_bawah" => callFloatMenu(),
            "menu_taskbar" => callMenuTaskbar(),
            "btn_back" => callBackNav(),
            // --------------------
            "error_msg"            => $error_msg,
            // -------------------
            "data_propose_title"   => $strDataProposeTitle,
            "data_propose_content" => $strDataPropose,
            "data_propose_footer"  => "",
            // -------------
            "data_hist_title"      => $strDataHistTitle,
            "data_hist_content"    => $strDataHist,
            "data_hist_footer"     => "",
            // ----data utama---------
            "data_active_content"  => $str,
            "script_bottom"        => $str_1,
            "link_str"             => $linkStr,
            "add_link"             => $add_link,
            "edit_link"            => $edit_link,
            "folder_link"            => $folder_link,
            //---end data utama--------
            "prop_display"         => $prop_display,
            "lebar_modal"       => "modal-lg",
        ));
        $p->render();
        break;
    case "mb_maps":

        $subTitle = "";
        $title = $title;
        $pageTemplate = (isset($_GET['mode']) && $_GET['mode'] == 'print') ? MODUL_TEMPLATE_PATH . "/template/defaultPrint.html" : MODUL_TEMPLATE_PATH .  "/template/maps.html";
        $p = New Layout("$title", "$subTitle", "$pageTemplate");

        $prop_display = sizeof($arrayOnProgress) < 1 ? "none" : "block";
        $fId = $filterId;
        $str_fId = $filterId_str;

        $url = base_url() . $this->modul . "/$segment_2/fetch_data".$str_fId;
        $str_1 = "<script>
                     var posrurl = '$url';
                     var posmdl = '$mdl';
                     var postfid = '$fId';
                    $.ajax({
                        type: 'GET',
                        url: posrurl,
                        data: {mdl:posmdl,fid:postfid},
                        success: function(cb){
                            var html=''
                            var arrData = JSON.parse(cb)
                            if(arrData.data.length>0){
                                jQuery.each(arrData.data, function(i,b){
                                    console.log(i);
                                    console.log(b);

                                    html += \"<div class='col-xs-12 no-padding panel panel-danger'>\"
                                    html += \"<div style='background-color: #a9a9a950;' class='col-xs-12 no-padding'>\"
                                    html += \"<div style='padding:3px;font-size:14px;' class='col-xs-6 no-paddings text-bold text-blue text-left'>\"+b[6]+\"</div>\"
                                    html += \"<div style='padding:3px;' class='col-xs-6 no-paddings text-right'>\"
                                    html += \"<span class='badge bg-lime'>\"
                                    html += \"<span style='color:#565656;text-align:left;' class=''>\"+b[8]+\"</span>\"
                                    html += \"</span>\"
                                    html += \"</div>\"
                                    html += \"</div>\"

                                    html += \"<div style='padding:3px;' class='col-xs-12'>\"
                                    html += \"<div class='col-xs-3 text-bold'>SELLER</div>\"
                                    html += \"<div class='col-xs-9 text-capitalize'>: \"+b[1]+\"</div>\"

                                    html += \"<div class='col-xs-3 text-bold'>CUSTOMER</div>\"
                                    html += \"<div class='col-xs-9 text-capitalize'>: \"+b[2]+\"</div>\"

                                    html += \"<div class='col-xs-3 text-bold'>ALASAN</div>\"
                                    html += \"<div class='col-xs-9 text-capitalize'>: \"+b[3]+\"</div>\"


                                    html += \"</div>\"

                                    html += \"<div style='padding:3px;' class='col-xs-12 no-paddings'>\"
                                    html += \"<div class='btn-group btn-group-justified'>\"
                                    html += \" \" + b[9] + \" \"
                                    html += \"</div>\"
                                    html += \"</div>\"

//                                    html += \"<div style='background-color: #a9a9a950;' class='col-xs-12 no-padding'>\"
//                                    html += \"<div style='padding:3px;' class='col-xs-6 no-paddings text-left'>request by\"
//                                    html += \"<span class='text-green text-bold'><br><i class='fa fa-user'></i>\"
//                                    html += \"<span style='color:#565656;text-align:center;' class=''>admin</span>\"
//                                    html += \"</span>\"
//                                    html += \"</div>\"
//                                    html += \"<div style='padding:3px;' class='col-xs-6 no-paddings text-right'>\"
//                                    html += \"<span style='color:#dd3300'>purchase order</span><br>\"
//                                    html += \"<span class='fa fa-check-circle text-black'> -</span><br>\"
//                                    html += \"<span class='fa fa-circle text-grey'> -</span><br>\"
//                                    html += \"</div>\"

                                    html += \"</div>\"
                                    html += \"</div>\"

                                })
                            }
                            else{

                            }

                            $('.mb-content').html(html)
                        }
                    })
                </script>";

        $str="<div class='mb-content'></div>";

        $strDataHist="";
        $strDataPropose="";
        $error_msg="";


        $p->addTags(array(
            // -----------------------
            "menu_left" => callMenuLeft(),
            "trans_menu" => callTransMenu(),
            "float_menu_atas" => callFloatMenu('atas'),
            "float_menu_bawah" => callFloatMenu(),
            "menu_taskbar" => callMenuTaskbar(),
            "btn_back" => callBackNav(),
            // --------------------
            "error_msg"            => $error_msg,
            // -------------------
            "data_propose_title"   => $strDataProposeTitle,
            "data_propose_content" => $strDataPropose,
            "data_propose_footer"  => "",
            // -------------
            "data_hist_title"      => $strDataHistTitle,
            "data_hist_content"    => $strDataHist,
            "data_hist_footer"     => "",
            // ----data utama---------
            "data_active_content"  => $str,
            "script_bottom"        => $str_1,
            "link_str"             => $linkStr,
            "add_link"             => $add_link,
            "edit_link"            => $edit_link,
            "folder_link"            => $folder_link,
            //---end data utama--------
            "prop_display"         => $prop_display,
            "lebar_modal"       => "modal-lg",
        ));
        $p->render();

        break;
}

?>