<?php
ini_set('display_errors', 0);
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

session_start();
ob_start();

require_once('configuration/server_connect.php');
require_once('configuration/all_function.php');

$active_id=$_REQUEST['active_id'];
$menu_id=$_REQUEST['menu_id'];

// Common Information

$page_title="Create an Account";
$page="81_user_02.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='user_management';	
$unique='user_id';


$request_id=$_REQUEST['user_id'];

$ms_data=mysqli_fetch_object(mysqli_query($conn,'select * from '.$table.' where user_id="'.$request_id.'"'));

// End Common Information


if(isset($_POST['insert']))

{		

$entry_by = $_SESSION['user']['id'];
		 
$entry_at = $now=date('Y-m-d H:i:s');

$warehouse_id=1;

$emp_code=0;


$max_user_id=find_a_field($conn,'user_management','max(user_id)','1')+1;

$ins_sql = 'INSERT INTO  user_management (user_id, username, password, fname, emp_code, group_for, designation, mobile_no, email, status, entry_by, entry_at, warehouse_id, edit_by, edit_at) VALUES
("'.$max_user_id.'", "'.$_POST['username'].'", "'.$_POST['password'].'", "'.$_POST['fname'].'", "'.$emp_code.'", "'.$_POST['group_for'].'", "'.$_POST['designation'].'", "'.$_POST['mobile_no'].'", "'.$_POST['email'].'", "'.$_POST['status'].'", "'.$entry_by.'", "'.$entry_at.'", "'.$warehouse_id.'", "0", "0000-00-00")';
	
mysqli_query($conn,$ins_sql);

header("location:".$page);

?>

<!--<script language="javascript">
window.location.href = "<?php echo $page;?>";
</script>-->
<?php

}



if(isset($_POST['update']))

{		

$entry_by = $_SESSION['user']['id'];	 
$entry_at = $now=date('Y-m-d H:i:s');

$user_id=$request_id;


 $up_sql = 'update user_management set username="'.$_POST['username'].'", password="'.$_POST['password'].'", fname="'.$_POST['fname'].'",  designation="'.$_POST['designation'].'", mobile_no="'.$_POST['mobile_no'].'", email="'.$_POST['email'].'",  group_for="'.$_POST['group_for'].'", 
 status="'.$_POST['status'].'", edit_by="'.$entry_by.'", edit_at="'.$entry_at.'" 
 where user_id='.$user_id;
mysqli_query($conn,$up_sql);

header("location:".$page);

?>

<!--<script language="javascript">
window.location.href = "<?php echo $page;?>";
</script>-->


<?php

}
?>

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

<?php require_once('inc_files/inc_head.php'); ?>

<body>

  <!-- ======= Header ======= -->
  <?php 
  require_once('inc_files/inc_header.php');
  ?><!-- End Header -->

  <!-- ======= Sidebar ======= -->
   <?php  require_once('inc_files/inc_menu.php'); ?>
   <!-- End Sidebar-->

  <main id="main" class="main">

    <div class="pagetitle">
      <h1><?php echo $page_title?></h1>
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="home.php">Home</a></li>
          <li class="breadcrumb-item">Admin Panel</li>
          <li class="breadcrumb-item active"><?php echo $page_title?></li>
        </ol>
      </nav>
    </div><!-- End Page Title -->

    <section class="section">
      <div class="row" style="height:auto">
        <div class="col-lg-6"> 

          <div class="card">
            <div class="card-body">
            
              

              <!-- Table with stripped rows -->
              <table class="table datatable" style="font-size:14px;">
                <thead>
                  <tr>
                    <th scope="col"><span class="card-title"> User ID </span></th>
                    <th scope="col"><span class="card-title">Username </span></th>
                    <th scope="col"><span class="card-title">Name</span></th>
                  </tr>
                </thead>
                <tbody>
					<?php
					
						$sql = 'select * from '.$table.' where 1';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
				
                 <tr>
       <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->user_id;?>" style="color:#000000"><?php echo $data->user_id;?></a></td>
      <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->user_id;?>" style="color:#000000"><?php echo $data->username;?></a></td>
      <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->user_id;?>" style="color:#000000"><?php echo $data->fname;?></a></td>
                 </tr>
				  <?php }?>
                </tbody>
              </table>
              <!-- End Table with stripped rows -->

            </div>
          </div>

        </div>

        <div class="col-lg-6">

          <div class="card">
            <div class="card-body">
              <h5 class="card-title"><strong><?php echo $page_title;?></strong></h5>
			  
			

              <!-- Multi Columns Form -->
              <form class="row g-3" action="" method="post" name="form1" id="form1" onSubmit="return check()" style="font-size:14px;">
			  
			  
                
				
				
			  
			 
                
				
				
			   
				<div class="col-md-12">
                 
                </div>
			  
                <div class="row mb-3" >
                  <label for="email" class="col-sm-3 col-form-label"><strong>Username</strong></label>
                  <div class="col-sm-9">
                    <input type="text" class="form-control" id="username" name="username" value="<?php if($request_id>0){ echo $ms_data->username; } ?>" required>
                  </div>
                </div>
				
				<div class="row mb-3">
                  <label for="company_name" class="col-sm-3 col-form-label"><strong>Password</strong></label>
                  <div class="col-sm-9">
                    <input type="password" class="form-control" id="password" name="password" value="<?php if($request_id>0){ echo $ms_data->password; } ?>" required>
                  </div>
                </div>
				
				
				<div class="row mb-3" >
                  <label for="email" class="col-sm-3 col-form-label"><strong>Full Name</strong></label>
                  <div class="col-sm-9">
                    <input type="text" class="form-control" id="fname" name="fname" value="<?php if($request_id>0){ echo $ms_data->fname; } ?>" required>
                  </div>
                </div>
				
				
				<?php /*?><div class="row mb-3">
                  <label for="emp_code" class="col-sm-3 col-form-label"><strong>EMP Code</strong></label>
                  <div class="col-sm-9">
                
			<select id="emp_code" name="emp_code"  class="form-select"    >
			<option></option>
			
			<?php foreign_relation($conn,'employee_info','emp_code','concat("[EMP Code: ",emp_code, "] - ",emp_name)',$ms_data->emp_code,'1 order by emp_code');?>
		
        	</select>
		
                  </div>
                </div><?php */?>
				
				
				<div class="row mb-3" >
                  <label for="email" class="col-sm-3 col-form-label"><strong>Designation</strong></label>
                  <div class="col-sm-9">
                    <input type="text" class="form-control" id="designation" name="designation" value="<?php if($request_id>0){ echo $ms_data->designation; } ?>" required>
                  </div>
                </div>
				
				<div class="row mb-3" >
                  <label for="email" class="col-sm-3 col-form-label"><strong>Mobile No</strong></label>
                  <div class="col-sm-9">
                    <input type="text" class="form-control" id="mobile_no" name="mobile_no" value="<?php if($request_id>0){ echo $ms_data->mobile_no; } ?>" required>
                  </div>
                </div>
				
				
				<div class="row mb-3" >
                  <label for="email" class="col-sm-3 col-form-label"><strong>Email</strong></label>
                  <div class="col-sm-9">
                    <input type="text" class="form-control" id="email" name="email" value="<?php if($request_id>0){ echo $ms_data->email; } ?>" >
                  </div>
                </div>
				
		
				
				
				
				
				
				<div class="row mb-3">
                  <label for="emp_code" class="col-sm-3 col-form-label"><strong>Status</strong></label>
                  <div class="col-sm-9">
                
			<select id="status" name="status"  class="form-select"  required  >
			<option></option>
			
			<?php foreign_relation($conn,'active_status','status','status',$ms_data->status,'1 order by id');?>
		
        	</select>
		
                  </div>
                </div>
				
				<div class="row mb-3">
                  <label for="emp_code" class="col-sm-3 col-form-label"><strong>Company</strong></label>
                  <div class="col-sm-9">
                
			<select id="group_for" name="group_for"  class="form-select"  required  >
			
			
			<?php foreign_relation($conn,'user_group','id','group_name',$ms_data->group_for,'1 order by id');?>
		
        	</select>
		
                  </div>
                </div>
				
				
		
				<br>
				<br>
				
			
            
                
                
                <div class="text-center">
				<?php if($request_id>0) {?>
                  <button type="submit" class="btn btn-success btn-sm" name="update" id="update" ><i class="bi bi-check-all"></i> Update &nbsp;</button>
				 <?php }else {?>
				  <button type="submit" class="btn btn-primary btn-sm" name="insert" id="insert" ><i class="bi bi-plus"></i> Save &amp; New &nbsp;</button>
				 <?php }?>
                  <button type="reset" class="btn btn-secondary btn-sm" onClick="parent.location='<?php echo $page?>'" >Reset</button>
                </div>
              </form><!-- End Multi Columns Form -->

            </div>
          </div>

          

          

        </div>
      </div>
    </section>

  </main><!-- End #main -->

  <!-- ======= Footer ======= -->
  <?php require_once('inc_files/inc_footer.php'); ?>
 <?php 
 selected_two("#emp_code");
 ?>
</body>

</html>