<?php    


// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);

    include_once('homecheck.php');

     $uid =   $_SESSION['uid'] ;
     // $storeID =  $_SESSION['storeid'] ;
      $storeID = $Abranch ; // $_SESSION['storeid'];
      $storeGD = 1 ; //$_SESSION['guard'];

            //   date_default_timezone_set('Africa/Nairobi');

            // $today = date("d.m.Y"); 
            // $today = date("Y-m-d", strtotime($today));
            // $now = date("h:i:s a"); 


      if  ( $mycurrentpage == 'home' or $mycurrentpage == 'homec'  )  { }
      else {
               $mycurrentpage3=  basename($_SERVER['PHP_SELF']);
               $mycurrentpage4 = pathinfo($mycurrentpage3, PATHINFO_FILENAME);
               $ur =    $_SERVER['REQUEST_URI']   ;
       
            //   $activepageid= $conn->query("SELECT id FROM sys_page  where url='$mycurrentpage4' and active=1    ")->fetchAll(); 
            //   $pageid  = $activepageid["id"] ;

                $astmt = $conn->prepare("SELECT id FROM sys_page  WHERE  url=? ");
                $astmt->execute([$mycurrentpage4]);
               // $astmt->execute([$ur]);
                $activepageid =$astmt->fetch();  
                $pageid       =$activepageid["id"];
                

              $CanAccess = $conn->query(" SELECT COALESCE(COUNT(*),0) FROM sys_rights  WHERE  role='$role' and page='$pageid'  ")->fetchColumn();

              // PageVisited($uid,$role,$pageid,$CanAccess,$conn) ;
            //   StopAcess($CanAccess) ;
  
          }
        
            function StopAcess($CanAccess)
               {
                  if ( $CanAccess ==0 ) 
                     { header('Location:home.php'); }      
                   else{}
               } 
            function PageVisited($uid,$role,$pageid,$CanAccess,$conn)
               {
                  $pv = 'INSERT INTO syslogpagevisited(user,role,page,access) VALUES(:user,:role,:page,:access )';
                  $conn->prepare($pv)->execute(['user'=>$uid,'role'=>$role,'page'=>$pageid,'access'=>$CanAccess  ]);
               }


               
            function audittrail($conn,$lastid,$transtype,$transaction,$transmode,$uid,$storeID )
                   {
                     $AT = 'INSERT INTO sysaudittrail(lastid,transtype,transaction,transmode,uid,storeID)
                      VALUES(:lastid,:transtype,:transaction,:transmode,:uid,:storeid )';
                     $conn->prepare($AT)->execute(['lastid'=>$lastid,'transtype'=>$transtype,'transaction'=>$transaction,
                     'transmode'=>$transmode ,'uid'=>$uid ,'storeid'=>$storeID  ]);
    
                   }
    
           

?>