<?php
    include('session.php');
?>

<?php

    date_default_timezone_set('Asia/Singapore');

     if($_SERVER["REQUEST_METHOD"] == "POST")
    {
          if (isset($_POST['saveRecord']))
          {
              $stname = strtoupper($_POST['fullname']);
              $stremail = strtolower($_POST['email']);
              $strcontact = strtoupper($_POST['contact']);
              $struser = strtoupper($_POST['usertype']);
              $username = strtoupper($_POST['email']);
              $tmpuserid = strtolower($_POST['email']);
              $password = strtoupper($_POST['contact']);
              $company = strtoupper($_POST['company']);
              
              $sql = "INSERT INTO $tblworkers (FullName, ContactNo, EmailAdd, UserType, Username, Password, Company) VALUES (?, ?, ?, ?, ?, ?, ?)";
              
              $stmt = mysqli_prepare($conn, $sql);

              mysqli_stmt_bind_param($stmt, "sssssss", $stname, $strcontact, $stremail, $struser, $tmpuserid, $password, $company);

              if (mysqli_stmt_execute($stmt)) {
                  $msg = "New Worker Account Created!";
                  $_SESSION['status_message'] = $msg;
                  echo "<script>alert('" . $msg . "');</script>";
              } else {
                  $msg = "ERROR : Create New Worker Account Record!";
                  $_SESSION['status_message'] = $msg;
                  echo "<script>alert('" . $msg . "');</script>";
              }

              mysqli_stmt_close($stmt);

            }
            else if (isset($_POST['updateRekod']))
              {
                  $strcodeid = strtoupper($_POST['recordid']);
                  $stname = strtoupper($_POST['custname']);
                  $strcontact = strtoupper($_POST['contact']);
                  $stremail = strtolower($_POST['email']);
                  $struser = strtoupper($_POST['usertype']);
                  $strcompany = strtoupper($_POST['company']);

                  $tmpuserid = $login_index;

                  $sql = "SELECT * FROM $tblworkers WHERE AutoIndex = '$strcodeid'";

                  $result = mysqli_query($conn, $sql);
                  $num_rows = mysqli_num_rows($result);

                  if ($num_rows <= 0)
                  {
                      $msg = "ERROR : Worker Account Record ID : $strcodeid NOT EXIST!";
                      $_SESSION['status_message'] = $msg;
                      echo "<script>alert('" . $msg . "');</script>";
                  }
                  else
                  {
                      
                  $tmpuserid = $login_session;
                      
                  $sql = "UPDATE $tblworkers SET FullName = '$stname', ContactNo = '$strcontact', EmailAdd = '$stremail', UserType = '$struser', Company = '$strcompany' WHERE AutoIndex LIKE '$strcodeid'";
                      
                  if ($conn->query($sql) === TRUE) {
                      
                      $msg = "Worker Account Updated!";
                      $_SESSION['status_message'] = $msg;
                      echo "<script>alert('" . $msg . "');</script>";
                  } else {
                      $msg = "ERROR : Worker account Updated!";
                      $_SESSION['status_message'] = $msg;
                      echo "<script>alert('" . $msg . "');</script>";
                  }
                  }

              }
            else if (isset($_POST['deleteRekod']))
              {
                  $strcodeid = strtoupper($_POST['recordid']);
                  
                  $sql=mysqli_query($conn,"SELECT * FROM $tblworkers WHERE AutoIndex LIKE '$strcodeid'");

                  $num_rows = mysqli_num_rows($sql);

                  if ($num_rows <= 0)
                  {
                      $msg = "ERROR : Account Index : $strcodeid NOT EXIST!";
                      $_SESSION['status_message'] = $msg;
                      echo "<script>alert('" . $msg . "');</script>";
                  }
                  else
                  {
                      
                      $tmpuserid = $login_session;
                      
                      $sql = "DELETE FROM $tblworkers WHERE AutoIndex LIKE '$strcodeid'";
                      
                      if ($conn->query($sql) === TRUE) {
                          $msg = "Worker Account Deleted!";
                          $_SESSION['status_message'] = $msg;
                          echo "<script>alert('" . $msg . "');</script>";
                      } else {
                          $msg = "ERROR : Delete Worker Account Record!";
                          $_SESSION['status_message'] = $msg;
                          echo "<script>alert('" . $msg . "');</script>";
                      }
                  }
              }
        //}
            
     }

else
{
    $month = date('m');
    $day = date('d');
    $strmonth = date('m');
    $year = date('Y');

    $todaydate =  date('Y') . '-' . date('m') . '-' . date('d');
    $todaydatedisplay =  date('d') . '-' . date('m') . '-' . date('Y');
    
    $d = cal_days_in_month(CAL_GREGORIAN,date('m'),date('Y'));

    $begindate = date('Y') . '-' . date('m') . '-' .'01';
    $begindate = date('Y') . '-' . "01" . '-' .'01';
    $enddate = date('Y') . '-' . date('m') . '-' .$d;

    $displaybegindate = "01-". date('m') . '-' . date('Y');
    $displayenddate = $d."-". date('m') . '-' . date('Y') ;
}
?>


<!DOCTYPE html>
<html lang="en" class="h-100">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, shrink-to-fit=9">
        <meta name="description" content="<?php echo $sitetitle ?>">
        <meta name="author" content="<?php echo $sitetitle ?>">
        <title><?php echo $sitetitle ?></title>
		
		<!-- Favicon Icon -->
        <link rel="icon" type="image/png" href="images/logo.png">
		
        <!-- Stylesheets -->
        <?php include 'plugincss.php' ?>
        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css">


	</head>

<body class="d-flex flex-column h-100">

        <!-- Create Coupon Model Start-->
        <div class="modal fade" id="AddRecords" tabindex="-1" aria-labelledby="AddRecordsLabel" aria-hidden="false">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="AddRecordsLabel">User Account Details</h5>
                        <button type="button" class="close-model-btn" data-bs-dismiss="modal" aria-label="Close"><i class="uil uil-multiply"></i></button>
                    </div>
                    <form class="form" method="post" action="">
                        <div class="modal-body">
                            <div class="model-content main-form">
                                <div class="row">
                                        <div class="model-content main-form">
                                            <div class="row">
                                                <div class="col-lg-12 col-md-12">
                                                    <div class="form-group mt-4">
                                                        <label class="form-label">Name</label>
                                                        <input class="form-control h_40" id="fullname" name="fullname" type="text">
                                                    </div>
                                                </div>
                                                <div class="col-lg-6 col-md-12">
                                                    <div class="form-group mt-4">
                                                        <label class="form-label">Contact No</label>
                                                        <input class="form-control h_40" id="contact" name="contact" type="text">
                                                    </div>
                                                </div>
                                                <div class="col-lg-6 col-md-12">
                                                    <div class="form-group mt-4">
                                                        <label class="form-label">Email Address</label>
                                                        <input class="form-control h_40" id="email" name="email" type="text">
                                                    </div>
                                                </div>
                                                <div class="col-lg-6 col-md-12">
                                                    <div class="form-group mt-4">
                                                        <label class="form-label">Company</label>
                                                            <select class="form-control" id="company" name="company">
                                                            <option value="ALL">ALL</option>
                                                            <?php
                                                           
                                                            $sqlcomp = "SELECT AutoIndex, Company FROM $tblcompany ORDER BY Company ASC";
                                                            $resultcomp = mysqli_query($conn, $sqlcomp);
                                                        
                                                            if (mysqli_num_rows($resultcomp) > 0) {
                                                                while ($rowcomp = mysqli_fetch_assoc($resultcomp)) {
                                                                    echo '<option value="'. htmlspecialchars($rowcomp['Company']) .'">'. htmlspecialchars($rowcomp['Company']) .'</option>';
                                                                }
                                                            } else {
                                                                echo '<option value="">No companies found</option>';
                                                            }
                                                            ?>
                                                        </select>
                                                    </div>
                                                </div>
                                                
                                                <div class="col-lg-6 col-md-12">
                                                    <div class="form-group mt-4">
                                                        <label class="form-label">User Type</label>
                                                        <input class="form-control h_40" id="usertype" name="usertype" type="text" value ="USER" readonly>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <input type="hidden" name="action" id="action" value="saveRecord" />
                                    <button type="button" class="btn btn-warning" data-bs-toggle="modal" data-bs-dismiss="modal"><i class="fa-solid fa-cancel me-3"></i>Close</button>
                                    <button type="submit" class="btn btn-success" id = "saveRecord" name = "saveRecord"><i class="fa-solid fa-save me-3"></i>Save</button>
                                </div>
                            </div>
                        </form>
                    </div>
                    </div>
                    </div>



	<!-- Add Organisation Model End-->
	<!-- Header Start-->
	<header class="header">
        <?php include 'topmenu.php' ?>
	</header>
	<!-- Header End-->
	<!-- Left Sidebar Start -->
	<nav class="vertical_nav">
        <?php include 'sidemenu.php' ?>
	</nav>



	<!-- Left Sidebar End -->
	<!-- Body Start -->
	<div class="wrapper wrapper-body">
		<div class="dashboard-body">
			<div class="container-fluid">
				<div class="row">
					<div class="col-md-12">
						<div class="d-main-title">
							<h3>Workers</h3>
						</div>
					</div>

                        <div class="col-md-12">
                            <div class="d-md-flex flex-wrap align-items-center">
                                <div class="rs ms-auto mt_r4">
                                    <button class="btn btn-success" data-bs-toggle="modal" data-bs-target="#AddRecords">New</button>
                                </div>

                            </div>
                        </div>

                        
                        <div class="event-list">
                            <div class="tab-content">
                                <div class="tab-pane fade show active" id="orders-tab" role="tabpanel">
                                    <div class="table-card mt-4">
                                        <div class="main-table">
                                            <div class="table-responsive">
                                                <table id="dataTable" class="table">
                                                    <thead class="thead-dark">
                                                        <tr>
                                                            <th scope="col" style="width: 5%;">#</th>
                                                            <th scope="col" style="width: 30%;">Nama</th>
                                                            <th scope="col" style="width: 15%;">Contact No</th>
                                                            <th scope="col" style="width: 10%;">Email</th>
                                                            <th scope="col" style="width: 20%;">User Type</th>
                                                            <th scope="col" style="width: 20%;">Company</th>
                                                            <th scope="col" style="width: 10%;">Action</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php
                                                                $i=0;

                                                                    $sql = "SELECT * FROM $tblworkers ORDER BY FullName ASC";

                                                                   if($resultrec=mysqli_query($conn,$sql)){
                                                                        while ($rowrec=mysqli_fetch_array($resultrec)){
                                                                            $i=$i+1;
                                                                            
                                                                            $selectedCompany = $rowrec['Company'];
                                                                            
                                                                        
                                                                    ?>
                                                                        <tr>
                                                                            <td align = "left"><?php echo $i?></td>
                                                                            <td align = "left"><?php echo $rowrec['FullName'] ;?></td>
                                                                            <td align = "left"><?php echo $rowrec['ContactNo'] ;?></td>
                                                                            <td align = "left"><?php echo $rowrec['EmailAdd'] ;?></td>
                                                                            <td align = "left"><?php echo $rowrec['UserType'] ;?></td>
                                                                            <td align = "left"><?php echo $rowrec['Company'] ;?></td>
                                                                           
                                                                            <td align ="left">
                                                                            <button type="button" id = "btnUpdate" name = "btnUpdate" class="btn btn-warning" data-bs-toggle="modal" data-bs-target="#editRecord<?php echo $rowrec['AutoIndex']?>"><i class="fa-regular fa-pen"></i></button>
                                                                            
                                                                                <!-- Create Coupon Model Start-->
                                                                                <div class="modal fade" id="editRecord<?php echo $rowrec['AutoIndex']?>" tabindex="-1" aria-labelledby="editRecordLabel<?php echo $rowrec['AutoIndex']?>" aria-hidden="false">
                                                                                    <div class="modal-dialog modal-lg">
                                                                                        <div class="modal-content">
                                                                                            <div class="modal-header">
                                                                                                <h5 class="modal-title" id="editRecordLabel<?php echo $rowrec['AutoIndex']?>">User</h5>
                                                                                                <button type="button" class="close-model-btn" data-bs-dismiss="modal" aria-label="Close"><i class="uil uil-multiply"></i></button>
                                                                                            </div>
                                                                                                    <form class="form" method="post" action="">
                                                                                                        <div class="modal-body">
                                                                                                            <div class="model-content main-form">
                                                                                                                <div class="row">
                                                                                                                        <div class="model-content main-form">
                                                                                                                            <div class="row">
                                                                                                                                <div class="form-group mt-4">
                                                                                                                                    <input class="form-control h_40" id="recordid" name="recordid" type="hidden" value="<?php echo $rowrec['AutoIndex'];?>">
                                                                                                                                </div>
                                                                                                                                    <div class="col-lg-12 col-md-12">
                                                                                                                                        <div class="form-group mt-4">
                                                                                                                                            <label class="form-label">Name</label>
                                                                                                                                            <input class="form-control h_40" id="custname" name="custname" type="text" value="<?php echo $rowrec['FullName'] ;?>">
                                                                                                                                        </div>
                                                                                                                                    </div>
                                                                                                                                    <div class="col-lg-12 col-md-12">
                                                                                                                                        <div class="form-group mt-4">
                                                                                                                                            <label class="form-label">Contact Number</label>
                                                                                                                                            <input class="form-control h_40" id="contact" name="contact" type="contact" value="<?php echo $rowrec['ContactNo'] ;?>">
                                                                                                                                        </div>
                                                                                                                                    </div>
                                                                                                                                    <div class="col-lg-12 col-md-12">
                                                                                                                                        <div class="form-group mt-4">
                                                                                                                                            <label class="form-label">Email</label>
                                                                                                                                            <input class="form-control h_40" id="email" name="email" type="text" value="<?php echo $rowrec['EmailAdd'] ;?>">
                                                                                                                                        </div>
                                                                                                                                    </div>
                                                                                                                                    <div class="col-lg-12 col-md-12">
                                                                                                                                        <div class="form-group mt-4">
                                                                                                                                            <label class="form-label">Company</label>
                                                                                                                                                <select class="form-control" id="company" name="company">
                                                                                                                                                    <option value=""></option>
                                                                                                                                                    <option value="ALL" <?= (isset($selectedCompany) && $selectedCompany == "ALL") ? 'selected' : '' ?>>ALL</option>
                                                                                                                                                    <?php
                                                                                                                                                    $sqlcomp = "SELECT AutoIndex, Company FROM $tblcompany ORDER BY Company ASC";
                                                                                                                                                    $resultcomp = mysqli_query($conn, $sqlcomp);
                                                                                                                                                
                                                                                                                                                    if ($resultcomp && mysqli_num_rows($resultcomp) > 0) {
                                                                                                                                                        while ($rowcomp = mysqli_fetch_assoc($resultcomp)) {
                                                                                                                                                            $companyName = htmlspecialchars($rowcomp['Company']);
                                                                                                                                                            $isSelected = (isset($selectedCompany) && $selectedCompany == $companyName) ? 'selected' : '';
                                                                                                                                                            echo '<option value="'. $companyName .'" '. $isSelected .'>'. $companyName .'</option>';
                                                                                                                                                        }
                                                                                                                                                    } else {
                                                                                                                                                        echo '<option value="">No companies found</option>';
                                                                                                                                                    }
                                                                                                                                                    ?>
                                                                                                                                                </select>
                                                                                                                                        </div>
                                                                                                                                    </div>
                                                                                                                                    <div class="col-lg-12 col-md-12">
                                                                                                                                        <div class="form-group mt-4">
                                                                                                                                            <label class="form-label">User Type</label>
                                                                                                                                            <input class="form-control h_40" id="usertype" name="usertype" type="text" value ="USER" readonly>
                                                                                                                                        </div>
                                                                                                                                    </div>
                                                                                                                            </div>
                                                                                                                        </div>
                                                                                                                    </div>
                                                                                                                </div>
                                                                                                                <div class="modal-footer">
                                                                                                                    <button type="button" class="btn btn-warning" data-bs-toggle="modal" data-bs-dismiss="modal"><i class="fa-solid fa-cancel me-3"></i>Close</button>
                                                                                                                    <button type="submit" class="btn btn-danger" id = "deleteRekod" name = "deleteRekod"><i class="fa-solid fa-trash me-3"></i>Delete</button>
                                                                                                                    <button type="submit" class="btn btn-success" id = "updateRekod" name = "updateRekod"><i class="fa-solid fa-save me-3"></i>Save</button>
                                                                                                                </div>
                                                                                                            </div>
                                                                                                        </div>
                                                                                                    </form>
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                                
                                                                                
                                                                                
                                                                                </td>
                                                                            </tr>
                                                                    <?php
                                                                    }
                                                                       }

                                                                    ?>

                                                    </tbody>
                                                    <tfoot>
                                                        <tr>
                                                            <th scope="col" style="width: 5%;">#</th>
                                                            <th scope="col" style="width: 30%;">Nama</th>
                                                            <th scope="col" style="width: 15%;">Contact No</th>
                                                            <th scope="col" style="width: 10%;">Email</th>
                                                            <th scope="col" style="width: 20%;">User Type</th>
                                                            <th scope="col" style="width: 20%;">Company</th>
                                                            <th scope="col" style="width: 10%;">Action</th>
                                                        </tr>
                                                    </tfoot>

                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                </div>
            </div>
        </div>
    </div>
	
    <div class="copyright-footer">
        <?php echo $copyrights ?>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <!-- Include DataTable JS -->
    <script src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js"></script>
    <script>
        // Initialize DataTable
        $(document).ready(function() {
          $('#dataTable').DataTable({
                  "ordering": false,
                  "searching": false,
                  "lengthChange": false,
                  "pageLength": 5
              });
        });
    </script>

	<!-- Body End -->
    <?php include 'pluginjs.php' ?>

</body>
</html>
