<?php
    session_cache_expire(1440);
    session_start();
    session_name("insys");
    
    require_once('include/config.php');
    
    //Loading Nama Aplikasi
    $sqlaps="select nama_aplikasi,ftyear_start from tlaplication";
    $hslaps=mysql_fetch_array(mysql_query($sqlaps));
    $_SESSION["nama_aplikasi"]=$hslaps['nama_aplikasi'];
    $_SESSION["year_aplikasi"]=$hslaps['ftyear_start'];

    if(isset($_POST['ftusername'])) {
                
        $ssql="SELECT count(a.fnid) as jml, a.ftbranch_code, a.ftuser_group_code as level, a.ftprofile, b.ftbranch_name,b.ftbranch_type_code,b.ftregion_code  
                FROM tluser a left join tlbranch b on b.ftbranch_code=a.ftbranch_code
                WHERE  a.fnStatus=1 AND a.ftusername='{$_REQUEST['ftusername']}'
                        AND a.ftpassword='{$_REQUEST['ftpassword']}'  ";
        $sql=mysql_query($ssql." group by a.ftusername");
        $hsl=mysql_fetch_array($sql);

        // Loading user profile picture
        $_SESSION["user_profile"]="pictures/user_profile/" . $hsl['ftprofile'];
        
        switch($hsl['level']){
            case "hadmin":

                break;

            case "administrator":
                $SQL="select ftbranch_type_code  from tlbranch where ftbranch_code='{$_REQUEST['ftbranch_code']}'";
                $hsl=mysql_fetch_array(mysql_query($SQL));

                if ($hsl['ftbranch_type_code']=='KCU'){
                        $ssql.=" and b.ftbranch_code='{$_REQUEST['ftbranch_code']}'";

                } elseif ($hsl['ftbranch_type_code']=='KCP'){
                        $SQL="SELECT ftbranch_main from tlbranch where ftbranch_code='{$_REQUEST['ftbranch_code']}'";
                        $hsl=mysql_fetch_array(mysql_query($SQL));

                        $SQL2="SELECT ftbranch_code from tluser where ftbranch_code='{$hsl['ftbranch_main']}' and ftusername='{$_REQUEST['ftusername']}' and ftpassword='{$_REQUEST['ftpassword']}'";
                        $hsl=mysql_fetch_array(mysql_query($SQL2));

                        if($hsl['ftbranch_code']!=""){
                            $ssql.='';
                        }else{
                                echo '<script>
                                        alert(\'maaf ini bukan KCP anda\');
                                        window.location=\'login.php\';
                                </script>';
                        }
                }
                break;

            default:
                $ssql.=" and b.ftbranch_code='{$_REQUEST['ftbranch_code']}'";
        }

        $ssql.=" group by a.ftusername";
		
        $rs=mysql_query($ssql);
        $r=mysql_fetch_assoc($rs);
        if($r['jml']!=0) {            
                $sql="select ftbranch_code,ftbranch_name,ftregion_code,ftbranch_type_code from tlbranch where ftbranch_code='{$_REQUEST['ftbranch_code']}'";
                $hsl=mysql_fetch_array(mysql_query($sql));
		
                $_SESSION["strregion"]=$hsl['ftregion_code'];
                $_SESSION["namebranch"]=$hsl['ftbranch_name'];
                $_SESSION["strbranch"]=$hsl['ftbranch_code'];
                $_SESSION['username']=$_REQUEST['ftusername'];
                $_SESSION['usernameTemp']=$_REQUEST['ftusername'];
                $_SESSION['level']=$r['level'];
                $_SESSION['typebranch']=$hsl['ftbranch_type_code'];
                
                // call function update and synchronize stock detail qty     
                include "include/functions.php";
                include "tools/update_stock_det.php";
                UpdateStockDetail();
                
                echo "  <script>
                            //alert('Login Sukses ...');
                            window.location='index.php';
                        </script>";
                exit;
        }
        else {
             echo "<script>
                      alert('Login Gagal ...');                     
                    </script>";
        }
    }
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xml:lang="en-gb" xmlns="http://www.w3.org/1999/xhtml" lang="en-gb">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Login</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <!-- link css start-->
    <?php include('class/link_css.php'); ?>
    <!-- link css end -->

    <link rel="stylesheet" type="text/css" href="login/rounded.css">

    <script language="javascript" type="text/javascript">
        function validate()
                {

           if( document.login.ftusername.value == "" )
           {
             alert( "Isi Username Anda!" );
             document.myForm.ftusername.focus() ;
             return false;
           }
           if( document.login.ftpassword.value == "" )
           {
             alert( "Isi Password Anda!" );
             document.myForm.ftusername.focus() ;
             return false;
           }   
           if( document.login.ftbranch_code.value == "" )
           {
             alert( "Silahkan Pilih Cabang Anda!" );
             return false;
           }

           return( true );
        }
    </script>

    <!-- JAVASCRIPTS -->
    <script src="css_custom/js/jquery-2.1.1.min.js"></script>
    <script src="css_custom/js/bootstrap.min.js"></script>
    <script src="css_custom/js/repute-shop.js"></script>
    <script src="css_custom/js/repute-scripts.js"></script>

</head>
<body >

    <!-- WRAPPER -->
    <div class="wrapper"  style="background-color:#f1f1f1">
        <br><br>
        <!-- MAIN -->
        <div class="shop-main login" >
                <div class="container">
                        <!-- LOGIN BOX -->
                        <div class="row">
                                <div class="testimonial-author pull-left" style="background-color:#f1f1f1;width:100%;box-shadow: 0 2px 4px 0 rgba(0,0,0,0.16),0 2px 10px 0 rgba(0,0,0,0.12) !important;padding:3px;margin-bottom:0px;">
                                        <img src="css_custom/img/default_logo.png" alt="Author" class="pull-left">

                                        <span style="font-size:25px"><b><?php echo $hslaps['nama_aplikasi'];?></b> &nbsp;<small>Version 1.0</small></span>
                                </div>	

                                <div class="col-sm-5 col-sm-offset-1 col-lg-4 col-lg-offset-2">

                                        <br><br> 				
                                        <div class="account-box login-box box-with-help">
                                                <h1>Log in to your account</h1>
                                                <form action="" method="post" name="login" id="form-login" style="clear: both;" onsubmit="return(validate());">
                                                        <div class="form-group">
                                                                <label for="inputEmail" class="control-label sr-only">Email</label>
                                                                <div class="col-sm-12">
                                                                        <div class="input-group">
                                                                                <span class="input-group-addon"><i class="fa fa-user"></i></span>
                                                                                <input style="height:29px" type="text" class="form-control" name="ftusername" id="modlgn_username" placeholder="Username">
                                                                        </div>
                                                                </div>
                                                        </div>

                                                        <div class="form-group" >
                                                                <label for="inputPassword" class="control-label sr-only">Password</label>
                                                                <div class="col-sm-12">
                                                                        <div class="input-group" style="margin-top:4px;">
                                                                                <span class="input-group-addon"><i class="fa fa-lock"></i></span>
                                                                                <input style="height:29px" type="password" class="form-control" name="ftpassword" id="modlgn_passwd" placeholder="Password">
                                                                        </div>
                                                                </div>
                                                        </div>

                                                        <div class="form-group" >
                                                                <label for="inputCabang" class="control-label sr-only">Cabang</label>
                                                                <div class="col-sm-12">
                                                                        <div class="input-group" style="margin-top:4px;">

                                                                        <p id="form-login-cabang">	
                                                                        <select id="ftbranch_code" name="ftbranch_code" class="form-control" style="margin-left: 0px;height: 35px;">
                                                                        <option value="" selected>--- Pilih Cabang ---</option>	
                                                                                <?php 
                                                                                $ssql="SELECT UPPER(ftbranch_code) as ftbranch_code, ftbranch_name FROM tlbranch where fnstatus = 1";
                                                                                $rs=mysql_query($ssql);
                                                                                ?>


                                                                                <?php
                                                                                $i=0;
                                                                                while($r=mysql_fetch_assoc($rs)) {
                                                                                        $i++;
                                                                                        // if($r['ftbranch_code']=='PUSAT'){$select='selected';}
                                                                                        // else{$select='';}
                                                                                        $select='selected';
                                                                                 echo "<option value='{$r['ftbranch_code']}' ".$select.">{$r['ftbranch_name']}</option>";
                                                                                }													
                                                                                ?>
                                                                        </select>				
                                                                        </div>
                                                                </div>
                                                        </div>								

                                                        <br>
                                                        <div class="form-group">
                                                                <div class="col-xs-7" style="margin-top:11px;">
                                                                        <button type="submit" onclick="validate();" class="btn btn-primary btn-block"><i class="fa fa-sign-in"></i> Sign in</button>
                                                                </div>
                                                        </div>
                                                </form>
                                                <p><em><br><br><br><br> </em> <a href="#"><strong> </strong></a></p>
                                                <button type="button" class="btn btn-link btn-login-help"><i class="fa fa-question-circle" title="Click for help"></i></button><br> <br>
                                        </div>
                                </div>
                                <div class="col-sm-5 col-lg-4">
                                <br><br>
                                        <div class="login-copytext">
                                                <h2><i class="fa fa-lock"></i> Secure Login</h2>
                                                <p>user login authentication service that permits a user to use one set of login credentials (e.g., name and password) to access applications.</p>
                                                <h2><i class="fa fa-user"></i> What is ERP ?</h2>
                                                <p>Enterprise resource planning (ERP) is defined as the ability to deliver an integrated suite of business applications. ERP tools share a common process and data model, covering broad and deep operational end-to-end processes, such as those found in finance, HR, distribution, manufacturing, service and the supply chain.</p>
                                        </div>
                                </div>
                        </div>
                        <!-- END LOGIN BOX -->
                </div>
        </div><br><br>
        <hr>
        <!-- END MAIN -->
        <footer>
                <!-- COPYRIGHT -->
                <div class="text-center copyright" style="background-color:#fff"> 
                        &copy; <?php echo $_SESSION["year_aplikasi"] . ((date('Y') != $_SESSION["year_aplikasi"]) ? '-' . date('Y') : ' '); ?> INSIGHT SYSTEM . All Rights Reserved.			
                </div>
                <!-- END COPYRIGHT -->
        </footer>
    </div>
    <!-- END WRAPPER -->
</body></html>