<?
@session_start();

if (isset($_SESSION["usercode"])) {

} else {
  //header("location:login.php");
  ?>
<script>
document.location.href = "login.php";
</script>
<?
}

date_default_timezone_set('Asia/Bangkok'); //set timezone ให้ตรงกับประเทศไทย

/*INCLUDE "cONNECTIONS/CREATE_NEW_DB.PHP";
 INCLUDE "cONNECTIONS/CREATE_NEW_TB.PHP";
 INCLUDE "cONNECTIONS/INSERT_ADMIM.PHP";*/


include "Connections/connect_mysql.php";
$sqluser = " SELECT *  FROM `user_tb` WHERE `userid` = {$_SESSION['userid']} ";
$resultuser = mysql_query($sqluser) or die(mysql_error());
//mysql_query("set names utf8");
$memberuser = mysql_fetch_array($resultuser); // เก็บข้อมูลไว้ใน recordset
$userid = $memberuser['userid'];
$usercode = $memberuser['usercode'];
$userbranch = $memberuser['userbranch'];
$userposition = $memberuser['userposition'];
$usernameth = $memberuser['usernameth'];
$userlnameth = $memberuser['userlnameth'];
$usernameen = $memberuser['usernameen'];
$userlnameen = $memberuser['userlnameen'];
$userpicpath = $memberuser['userpicpath'];
$userloginname = $memberuser['userloginname'];
$userpassword = $memberuser['userpassword'];

$sql_sb = " select * from branch_tb ";
$result_sb = mysql_query($sql_sb) or die(mysql_error());
if (mysql_num_rows($result_sb) == 0) {
  $branch_row = 0;
} else {
  $sqlbrch = " select * from branch_tb where branid={$userbranch}";
  $resultbrch = mysql_query($sqlbrch) or die(mysql_error());
  $recordbrch = mysql_fetch_array($resultbrch);
  $brancode = $recordbrch['brancode'];
  $branname = $recordbrch['branname'];
}

$sql_sp = " select * from position_tb ";
$result_sp = mysql_query($sql_sp) or die(mysql_error());
if (mysql_num_rows($result_sp) == 0) {
  $position_row = 0;
} else {
  $sqlposit = " select * from position_tb where posid={$userposition}";
  $resultposit = mysql_query($sqlposit) or die(mysql_error());
  $recordposit = mysql_fetch_array($resultposit);
  $posname = $recordposit['posname'];
}

$sql_sg = " select * from groupteam_tb ";
$result_sg = mysql_query($sql_sg) or die(mysql_error());
if (mysql_num_rows($result_sg) == 0) {
  $groupteam = 0;
} else {
  $sqlgrups = " select * from groupteam_tb where groptUserID={$userid}";
  $resultgrups = mysql_query($sqlgrups) or die(mysql_error());
  $recordgrups = mysql_fetch_array($resultgrups);
  $groptGroupID = $recordgrups['groptGroupID'];
}

if ($groptGroupID) {
  $sql_st = " select * from group_tb ";
  $result_st = mysql_query($sql_st) or die(mysql_error());
  if (mysql_num_rows($result_st) == 0) {
    $group_row = 0;
  } else {
    $sql_gp = " select * from group_tb where GroupID={$groptGroupID}";
    $result_gp = mysql_query($sql_gp) or die(mysql_error());
    $record_gp = mysql_fetch_array($result_gp);
    $GroupName_gp = $record_gp['GroupName'];
  }
}

if ($groptGroupID) {
  $gName = $GroupName_gp;
} else {
  $gName = "-";
}



////////////////////////ข้อมูลจัดแผนก/////////////////////////////////
$sql_Gp_M = " select * from grouprp_tb ";
$resultGM = mysql_query($sql_Gp_M) or die(mysql_error());
//mysql_query("set names utf8");
$groupmember = mysql_fetch_array($resultGM); // เก็บข้อมูลไว้ใน recordset
$RPID = $groupmember['rpID'];


$sql_main = " select * from mainreport_tb ";
$result_main = mysql_query($sql_main) or die(mysql_error());
if (mysql_num_rows($result_main) == 0) {
  $grouprp = 0;
} else {
  $sql_main2 = " select * from mainreport_tb where ggrpTeamID={$RPID}";
  $result_main2 = mysql_query($sql_main2) or die(mysql_error());
  $result_main2 = mysql_fetch_array($result_main2);
  $groprpRepID = $result_main2['groprpRepID'];
}
//==================================================== dashboard =================================

//ตรวจสอบก่อนว่าเคยบันทึก dashboard หรือปล่าว
$sql_sr = "SELECT * FROM dashboard_tb where dabouserid={$userid} and dabowindowid=1  "; //หาว่ามีการบันทึกอยู่หรือไม่
$result_sr = mysql_query($sql_sr) or die(mysql_error());
if (mysql_num_rows($result_sr) == 0) {
  //
} else {
  //ดึงเลขที่ รายงาน จากตาราง dashboard
  $sql_dhbd1 = " select * from dashboard_tb where dabouserid={$userid} and dabowindowid=1";
  $result_dhdb1 = mysql_query($sql_dhbd1) or die(mysql_error());
  $record_dhdb1 = mysql_fetch_array($result_dhdb1);
  $daboreptid_dhdb1 = $record_dhdb1['daboreptid'];

  //ดึงข้อมูลรายงานจากตาราง report_tb
  $sql_rdh1 = " select * from report_tb where repID={$daboreptid_dhdb1}";
  $result_rdh1 = mysql_query($sql_rdh1) or die(mysql_error());
  $record_rdh1 = mysql_fetch_array($result_rdh1);
  $repName_rdh1 = $record_rdh1['repName'];
  $repLink_rdh1 = $record_rdh1['repLink'];
}

//ตรวจสอบก่อนว่าเคยบันทึก dashboard หรือปล่าว
$sql_sr2 = "SELECT * FROM dashboard_tb where dabouserid={$userid} and dabowindowid=2  "; //หาว่ามีการบันทึกอยู่หรือไม่
$result_sr2 = mysql_query($sql_sr2) or die(mysql_error());
if (mysql_num_rows($result_sr2) == 0) {
  //
} else {
  //ดึงเลขที่ รายงาน จากตาราง dashboard
  $sql_dhbd2 = " select * from dashboard_tb where dabouserid={$userid} and dabowindowid=2";
  $result_dhdb2 = mysql_query($sql_dhbd2) or die(mysql_error());
  $record_dhdb2 = mysql_fetch_array($result_dhdb2);
  $daboreptid_dhdb2 = $record_dhdb2['daboreptid'];

  //ดึงข้อมูลรายงานจากตาราง report_tb
  $sql_rdh2 = " select * from report_tb where repID={$daboreptid_dhdb2}";
  $result_rdh2 = mysql_query($sql_rdh2) or die(mysql_error());
  $record_rdh2 = mysql_fetch_array($result_rdh2);
  $repName_rdh2 = $record_rdh2['repName'];
  $repLink_rdh2 = $record_rdh2['repLink'];
}


//==================================================== dashboard =================================

if (($userpicpath == '-') or ($userpicpath == '')) {
  $userpicpath = 'user.png';
} else {

}


?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- <link rel="icon" href="images/BI.png" type="image/png" /> -->
    <!--<link rel=“icon” type=“image/png” href=“http://example.com/image.png” />-->

    <title>PROMPT BI</title>

    <!-- jquery-ui css for lobipanel -->
    <link rel="stylesheet" type="text/css" href="vendors/lobipanel/lib/jquery-ui.min.css">
    <!-- Bootstrap -->
    <link href="vendors/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <!--<link rel="stylesheet" type="text/css" href="../vendors/lobipanel/bootstrap/css/bootstrap.min.css">-->
    <!-- Font Awesome -->
    <link href="vendors/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <!-- lobipanel -->
    <link rel="stylesheet" type="text/css" href="vendors/lobipanel/dist/css/lobipanel.min.css">

    <!-- bootstrapdialog -->
    <link rel="stylesheet" type="text/css" href="vendors/bootstrapdialog/dist/css/bootstrap-dialog.min.css">

    <!-- datatable -->
    <link rel="stylesheet" type="text/css" href="vendors/datatables/jquery.Datatable.css">


    <!-- NProgress -->
    <link href="vendors/nprogress/nprogress.css" rel="stylesheet">
    <!-- iCheck -->
    <link href="vendors/iCheck/skins/flat/green.css" rel="stylesheet">

    <!-- bootstrap-progressbar -->
    <link href="vendors/bootstrap-progressbar/css/bootstrap-progressbar-3.3.4.min.css" rel="stylesheet">
    <!-- JQVMap -->
    <link href="vendors/jqvmap/dist/jqvmap.min.css" rel="stylesheet" />
    <!-- bootstrap-daterangepicker -->
    <link href="vendors/bootstrap-daterangepicker/daterangepicker.css" rel="stylesheet">

    <!-- Custom Theme Style -->
    <link href="build/css/custom.min.css" rel="stylesheet">

    <script src="ajax/function.js"></script>

    <style>
    .bootstrap-dialog .modal-header.bootstrap-dialog-draggable {
        cursor: move;
    }

    input.invalid,
    textarea.invalid {
        border: 2px solid red;
    }

    input.valid,
    textarea.valid {
        border: 2px solid green;
    }
    </style>


</head>

<body class="nav-md">
    <div class="container body">
        <div class="main_container">
            <div class="col-md-3 left_col">
                <div class="left_col scroll-view">
                    <div class="navbar nav_title" style="border: 0;">
                        <a href="index.php" class="site_title"><i class="fa fa-line-chart"></i> <span>PROMPT
                                BI</span></a>
                    </div>

                    <div class="clearfix"></div>

                    <!-- menu profile quick info -->
                    <div class="profile clearfix">
                        <!-- <div class="profile_pic">
                            <img src="modules/user/fileupload/<?= $userpicpath ?>" alt="..."
                                class="img-circle profile_img">
                        </div> -->
                        <div class="profile_info">
                            <span>Welcome,</span>
                            <h2><?= $usernameen ?> <?= $userlnameen ?></h2>
                        </div>
                    </div>
                    <!-- /menu profile quick info -->

                    <br />

                    <!-- sidebar menu -->
                    <div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
                        <div class="menu_section">
                            <h3>User : <?= $posname ?></h3>
                            <ul class="nav side-menu">

                                <li><a data-toggle="tooltip" data-placement="bottom" title="แดชบอร์ด"
                                        href="index.php"><i class="fa fa-dashboard"></i> BI DashBoard </a></li>
                                <?
                if ($GroupName_gp == "Admin") {
                  $mainmenu1 = "Administrator";
                } elseif ($GroupName_gp == "Executive") {
                  $mainmenu1 = "Executive";
                } else {
                  $mainmenu1 = "" . $brancode . " " . $branname . "";
                }
                ?>



                                <? if ($GroupName_gp) { ?>
                                <li><a data-toggle="tooltip" data-placement="bottom"><i class="fa fa-area-chart"></i>
                                        <?= $mainmenu1 ?>
                                        <span class="fa fa-chevron-down"></span></a>
                                    <ul class="nav child_menu">

                                        <?
                      $sql_MainG = " SELECT * From grouprp_tb order by rpID ";
                      $RS1 = mysql_query($sql_MainG);
                      while ($RB1 = mysql_fetch_array($RS1)) {
                        $rpName = $RB1['rpName'];
                        $RPID2 = $RB1['rpID'];


                        ?>


                                        <!--//////////////////////     จัดGroup แผนก   ///////////////////////////////////-->
                                        <li><a data-toggle="tooltip" data-placement="bottom"><i
                                                    class="fa fa-area-chart"></i> <?= $rpName ?>
                                                <span class="fa fa-chevron-down"></span></a>
                                            <ul class="nav child_menu">

                                                <?


                            $sql_report = "SELECT * FROM `groupreport_tb` LEFT JOIN mainreport_tb ON groupreport_tb.groprpRepID = mainreport_tb.ggrpRepID where groprpGTeamID={$groptGroupID} and ggrpTeamID ={$RPID2}   ";
                            $Objreport = mysql_query($sql_report);
                            $nor = 1;
                            while ($row = mysql_fetch_array($Objreport)) {
                              $groprpRepID = $row['groprpRepID'];
                              $sql_rpt = " select * from report_tb where repID={$groprpRepID}";
                              $result_rpt = mysql_query($sql_rpt) or die(mysql_error());
                              $record_rpt = mysql_fetch_array($result_rpt);
                              $repName_rpt = $record_rpt['repName'];
                              $repLink_rpt = $record_rpt['repLink'];

                              ?>

                                                <li><a href="javascript:;"
                                                        onClick="loadybi('<?= $nor ?>','<?= $repName_rpt ?>','<?= $repLink_rpt ?>');"><?= $repName_rpt ?></a>
                                                </li>
                                                <?
                              $nor = $nor + 1;
                            }
                            ?>
                                            </ul>
                                        </li>






                                        <? }

                } ?>







                        </div>
                        <? if (($userloginname == "Admin") || ($userid == 1) || ($gName == "Admin")) { ?>
                        <div class="menu_section">
                            <h3>Admin Menu</h3>
                            <ul class="nav side-menu">
                                <li><a data-toggle="tooltip" data-placement="bottom" title="ข้อมูลพื้นฐาน"><i
                                            class="fa fa-cogs"></i>
                                        Master File <span class="fa fa-chevron-down"></span></a>
                                    <ul class="nav child_menu">
                                        <li><a href="javascript:loadcompany();">Manage Company</a></li>
                                        <li><a href="javascript:loadbranch();">Manage Branch</a></li>
                                        <li><a href="javascript:loaddepartment();">Manage Department</a></li>
                                        <li><a href="javascript:loadposition();">Manage Position</a></li>
                                        <li><a href="javascript:loaduser();">Manage User</a></li>
                                        <li><a href="javascript:loadusergroup();">Manage User Group</a></li>
                                        <li><a href="javascript:loaduserpermiss();">Manage User Permission</a></li>
                                        <li><a href="javascript:loadreportgroup();">Manage Report Group</a></li>

                                    </ul>
                                </li>
                                <li><a data-toggle="tooltip" data-placement="bottom" title="จัดการรายงาน"><i
                                            class="fa fa-file-text-o"></i> BI Report <span
                                            class="fa fa-chevron-down"></span></a>
                                    <ul class="nav child_menu">
                                        <li><a href="javascript:loadreport();"> Manage Report</a>
                                        <li><a href="javascript:loadreportpermiss();"> Report Permission</a>
                                        <li><a href="javascript:loadreportpermissGroup();"> Report Group</a>
                                        </li>
                                </li>
                            </ul>
                            </li>

                            </ul>
                        </div>
                        <? } ?>
                    </div>
                    <!-- /sidebar menu -->

                    <!-- /menu footer buttons -->
                    <div class="sidebar-footer hidden-small">
                        <a data-toggle="tooltip" data-placement="top" title="Settings">
                            <span class="glyphicon glyphicon-cog" aria-hidden="true"></span>
                        </a>
                        <a data-toggle="tooltip" data-placement="top" title="FullScreen">
                            <span class="glyphicon glyphicon-fullscreen" aria-hidden="true"></span>
                        </a>
                        <a data-toggle="tooltip" data-placement="top" title="Lock">
                            <span class="glyphicon glyphicon-eye-close" aria-hidden="true"></span>
                        </a>
                        <a data-toggle="tooltip" data-placement="top" title="Logout" href="javascript:logoutuser();">
                            <span class="glyphicon glyphicon-off" aria-hidden="true"></span>
                        </a>
                    </div>
                    <!-- /menu footer buttons -->
                </div>
            </div>

            <!-- top navigation -->
            <div class="top_nav">
                <div class="nav_menu">
                    <nav>
                        <div class="nav toggle">
                            <a id="menu_toggle"><i class="fa fa-bars"></i></a>
                        </div>

                        <ul class="nav navbar-nav navbar-right">
                            <li class="">
                                <a href="javascript:;" class="user-profile dropdown-toggle" data-toggle="dropdown"
                                    aria-expanded="false">
                                    <img src="modules/user/fileupload/<?= $userpicpath ?>" alt=""><?= $usernameen ?>
                                    <?= $userlnameen ?>
                                    <span class=" fa fa-angle-down"></span>
                                </a>
                                <ul class="dropdown-menu dropdown-usermenu pull-right">
                                    <li><a href="javascript:loadpro();"> Profile</a></li>
                                    <!-- <li><a href="javascript:loadhelp();"> Help</a></li> -->
                                    <li><a href="javascript:logoutuser();"><i class="fa fa-sign-out pull-right"></i> Log
                                            Out</a></li>
                                </ul>
                            </li>
                        </ul>
                    </nav>
                </div>
            </div>
            <!-- /top navigation -->

            <!-- page content -->
            <div class="right_col" role="main">
                <!-- top tiles -->
                <div class="row" id="rowdashboard">
                    <div class="col-md-12">
                        <!--- แจ้งเตือนว่ายังไม่เปลี่ยน password --->
                        <? if ($userpassword == "0000") { ?>
                        <div id="alcp" class="alert alert-danger alert-dismissible fade in" role="alert">
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span
                                    aria-hidden="true">×</span>
                            </button>
                            <strong><i class="fa fa-info-circle"></i> คำแนะนำ!</strong> คุณควรเปลี่ยน password
                            เพื่อความปลอดภัยในการเข้าถึง user ของคุณ <button type="button"
                                class="btn btn-success btn-xs" onClick="javascript:changmypass(<?= $userid ?>);">
                                เปลี่ยน password ตอนนี้ </button>
                        </div>
                        <? } ?>
                        <!--- //////////////////////// --->
                        <div class="row">
                            <div class="col-md-6">
                                <!-- panedabo1 -->
                                <div id="pndabo1" class="panel panel-success lobipanel" data-body-height="500">
                                    <div id="pnhdabo1" class="panel-heading"> <i class="fa fa-dashboard"></i><b>
                                            <? if ($repName_rdh1) { ?>
                                            <?= $repName_rdh1 ?>
                                            <? } else { ?> BI Report
                                            <? } ?>
                                        </b> </div>
                                    <div class="panel-body">
                                        <div id="apndabo1">
                                            <iframe src='<?= $repLink_rdh1 ?>' height='700' width='100%' frameborder='0'
                                                align='middle' scrolling='no'></iframe>
                                        </div>
                                    </div>
                                    <div class="panel-footer" style="text-align:right;">
                                        <button type="button" id="btndabo1" class="btn btn-info"
                                            onClick="javascript:loadreportselect(1,<?= $groptGroupID ?>);"><i
                                                class="fa fa-check-circle"></i>
                                            เลือกรายงาน</button>
                                    </div>
                                </div>
                                <!-- //panedabo1 -->
                            </div>
                            <div class="col-md-6">
                                <!-- panedabo2 -->
                                <div id="pndabo2" class="panel panel-info lobipanel" data-body-height="500">
                                    <div id="pnhdabo2" class="panel-heading"> <i class="fa fa-dashboard"></i><b>
                                            <? if ($repName_rdh2) { ?>
                                            <?= $repName_rdh2 ?>
                                            <? } else { ?> BI Report
                                            <? } ?>
                                        </b> </div>
                                    <div class="panel-body">
                                        <div id="apndabo2">
                                            <iframe src='<?= $repLink_rdh2 ?>' height='700' width='100%' frameborder='0'
                                                align='middle' scrolling='no'></iframe>
                                        </div>
                                    </div>
                                    <div class="panel-footer" style="text-align:right;">
                                        <button type="button" id="btndabo2" class="btn btn-info"
                                            onClick="javascript:loadreportselect(2,<?= $groptGroupID ?>);"><i
                                                class="fa fa-check-circle"></i>
                                            เลือกรายงาน</button>
                                    </div>
                                </div>
                                <!-- //panedabo2 -->
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row tile_count">
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" id="c1"></div>
                </div>

                <!-- footer content -->
                <footer>
                    <div class="pull-right">
                        Copyright © <a id="footer"></a> All Rights Reserved - Create by <a href="#"> Yong Group IT
                            Teams.</a>
                    </div>
                    <div class="clearfix"></div>
                </footer>
                <script>
                var d = new Date();
                document.getElementById("footer").innerHTML = d.getFullYear();
                </script>
                <!-- /footer content -->
            </div>
        </div>

        <!-- jQuery -->
        <script src="vendors/jquery/dist/jquery.min.js"></script>
        <!-- Bootstrap -->
        <script src="vendors/bootstrap/dist/js/bootstrap.min.js"></script>
        <!-- FastClick -->
        <script src="vendors/fastclick/lib/fastclick.js"></script>
        <!-- NProgress -->
        <script src="vendors/nprogress/nprogress.js"></script>
        <!-- Chart.js -->
        <script src="vendors/Chart.js/dist/Chart.min.js"></script>
        <!-- gauge.js -->
        <script src="vendors/gauge.js/dist/gauge.min.js"></script>
        <!-- bootstrap-progressbar -->
        <script src="vendors/bootstrap-progressbar/bootstrap-progressbar.min.js"></script>
        <!-- iCheck -->
        <script src="vendors/iCheck/icheck.min.js"></script>
        <!-- Skycons -->
        <script src="vendors/skycons/skycons.js"></script>
        <!-- Flot -->
        <script src="vendors/Flot/jquery.flot.js"></script>
        <script src="vendors/Flot/jquery.flot.pie.js"></script>
        <script src="vendors/Flot/jquery.flot.time.js"></script>
        <script src="vendors/Flot/jquery.flot.stack.js"></script>
        <script src="vendors/Flot/jquery.flot.resize.js"></script>
        <!-- Flot plugins -->
        <script src="vendors/flot.orderbars/js/jquery.flot.orderBars.js"></script>
        <script src="vendors/flot-spline/js/jquery.flot.spline.min.js"></script>
        <script src="vendors/flot.curvedlines/curvedLines.js"></script>
        <!-- DateJS -->
        <script src="vendors/DateJS/build/date.js"></script>
        <!-- JQVMap -->
        <script src="vendors/jqvmap/dist/jquery.vmap.js"></script>
        <script src="vendors/jqvmap/dist/maps/jquery.vmap.world.js"></script>
        <script src="vendors/jqvmap/examples/js/jquery.vmap.sampledata.js"></script>
        <!-- bootstrap-daterangepicker -->
        <script src="vendors/moment/min/moment.min.js"></script>
        <script src="vendors/bootstrap-daterangepicker/daterangepicker.js"></script>

        <!-- Custom Theme Scripts -->
        <script src="build/js/custom.min.js"></script>

        <!-- lobipanel -->
        <!--<script src="../vendors/lobipanel/lib/jquery.1.11.min.js"></script>-->
        <script src="vendors/lobipanel/lib/jquery-ui.min.js"></script>
        <script src="vendors/lobipanel/lib/jquery.ui.touch-punch.min.js"></script>
        <!--<script src="../vendors/lobipanel/bootstrap/js/bootstrap.min.js"></script>-->
        <script src="vendors/lobipanel/dist/js/lobipanel.js"></script>
        <!-- bootstrapdialog -->
        <script src="vendors/bootstrapdialog/dist/js/bootstrap-dialog.min.js"></script>

        <!-- datatable -->
        <script src="vendors/datatables/jquery.Datatable.js"></script>

        <!-- bootstrap-ckeditor -->
        <script src="vendors/ckeditor4std/ckeditor/ckeditor.js"></script>





</body>

</html>
<?
//ตรวจสอบสิทธฺ ในการเปิดรายงาน
mysql_close($c); //close connection
?>