<?php 
session_start();
include '../../connection.php';



if (isset($_POST['updating_password'])) {
   

    $id = $_SESSION['user_id'];
    $username = $_POST['username'];
    $oldpass = $_POST['oldpass'];
    $newpass = $_POST['newpass'];

    // ✅ Check if the old password is correct
    $check_password = "SELECT * FROM tbl_user WHERE user_id = '$id' AND password = '$oldpass'";
    $check_result = $conn->query($check_password);

    if ($check_result->num_rows == 0) {
        echo "Oops! Wrong password!";
    } else {
        // ✅ Update username and password
        $query = "UPDATE tbl_user SET username = '$username', password = '$newpass' WHERE user_id = '$id'";
        $query_result = $conn->query($query);

        if ($query_result) {
            echo "Password updated successfully";
        } else {
            echo "Error: Unable to update password!";
        }
    }
}




if (isset($_POST['updating_officials'])) {
	$u_user_id = strtoupper($_POST['u_user_id']);
	$u_first_name = strtoupper($_POST['u_first_name']);
	$u_middle_name = strtoupper($_POST['u_middle_name']);
	$u_last_name = strtoupper($_POST['u_last_name']);
	$u_username = strtoupper($_POST['u_username']);
	$u_address = strtoupper($_POST['u_address']);
	$u_contact = $_POST['u_contact'];
	$u_age = $_POST['u_age'];
	$u_gender = strtoupper($_POST['u_gender']);
	$u_birthday = $_POST['u_birthday'];
	$u_role_type = strtoupper($_POST['u_role_type']);

	$query = "UPDATE `tbl_user` SET `first_name`='$u_first_name',`middle_name`='$u_middle_name',`last_name`='$u_last_name',`username`='$u_username',`address`='$u_address',`contact`='$u_contact',`age`='$u_age',`gender`='$u_gender',`birthday`='$u_birthday',`role_type`='$u_role_type' WHERE `user_id`='$u_user_id'";
	$query_result = $conn->query($query);
}

if (isset($_POST['adding_officials'])) {
	$first_name = strtoupper($_POST['first_name']);
	$middle_name = strtoupper($_POST['middle_name']);
	$last_name = strtoupper($_POST['last_name']);
	$username = strtoupper($_POST['username']);
	$address = strtoupper($_POST['address']);
	$contact = $_POST['contact'];
	$age = $_POST['age'];
	$gender = strtoupper($_POST['gender']);
	$birthday = strtoupper($_POST['birthday']);
	$role_type = strtoupper($_POST['role_type']);


	$query = "INSERT INTO `tbl_user`(
		`first_name`, 
		`middle_name`, 
		`last_name`,
		`username`, 
		`password`, 
		`address`, 
		`contact`, 
		`age`,
		 `gender`,
		  `birthday`,
		   `role_type`, 
		   `remember_token`) VALUES (
		   '$first_name', 
		   '$middle_name',
		   '$last_name',
		   '$username',
		   '123456', 
		   'address',
		   '$contact',
		   '$age',
		   '$gender',
		   '$birthday',
		   '$role_type',
		   '1')";
	$query_result = $conn->query($query);

	if ($query_result) {
		echo "You added 1 record!";
	}else{
		echo "error: failed to fetch the data on server";
	}
}


if (isset($_POST['loading_officials'])) {
	echo 
	'
	<table id="mytable" class="table table-striped table-sm">
  <thead>
    <tr>
      <th scope="col">NO.</th>
      <th scope="col">FIRST NAME</th>
      <th scope="col">MIDDLE NAME</th>
      <th scope="col">LAST NAME</th>
      <th scope="col">ADDRESS</th>
      <th scope="col">CONTACT</th>
      <th scope="col">AGE</th>
      <th scope="col">GENDER</th>
      <th scope="col">BIRTHDAY</th>
      <th scope="col">ROLE</th>
      <th scope="col">DATE</th>
      <th scope="col">ACTION</th>
    </tr>
  </thead>
  <tbody>
	';

	$query = "SELECT * FROM tbl_user ";
	$query_result = $conn->query($query);
	$count = 0;

	foreach ($query_result as $key => $row) {

		switch ($row['role_type']) {
				case 'admin':
				$role = "<span class='badge badge-success'>Admin</span>";
				break;
			
				case 'staff':
				$role = "<span class='badge badge-info'>Staff</span>";
				break;
					case 'official':
				$role = "<span class='badge badge-primary'>Official</span>";
				break;

		}
	echo 
	'
	  <tr>
      <th scope="row">'.++$count.'</th>
      <td>'.$row['first_name'].'</td>
      <td>'.$row['middle_name'].'</td>
      <td>'.$row['last_name'].'</td>
      <td>'.$row['address'].'</td>
      <td>'.$row['contact'].'</td>
      <td>'.$row['age'].'</td>
      <td>'.$row['gender'].'</td>
      <td>'.$row['birthday'].'</td>
      <td>'.$role.'</td>
      <td>'.$row['date_created'].'</td>
      <td>
      <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#update-users" onclick="edit_users(
      \''.$row['user_id'].'\',
      \''.$row['first_name'].'\',
      \''.$row['middle_name'].'\',
      \''.$row['last_name'].'\',
      \''.$row['username'].'\',
      \''.$row['address'].'\',
      \''.$row['contact'].'\',
      \''.$row['age'].'\',
      \''.$row['gender'].'\',
      \''.$row['birthday'].'\',
      \''.$row['role_type'].'\'
      )"><i class="fa fa-edit"></i></button>
      	</td>
    </tr>
	';
	}
	

	echo 
	'
	</tbody>
</table>
	';
}
?>