<?php
include '../../connection.php';

if (isset($_POST['updating_resident'])) {
  $res_id = $_POST['u_res_id'];
  $first_name = strtoupper($_POST['u_first_name']);
  $middle_name = strtoupper($_POST['u_middle_name']);
  $last_name = strtoupper($_POST['u_last_name']);
  $email = strtoupper($_POST['u_email']);
   $address = strtoupper($_POST['u_address']);
  $contact = strtoupper($_POST['u_contact']);
  $age = $_POST['u_age'];
  $birthday = strtoupper($_POST['u_birthday']);
  $gender = strtoupper($_POST['u_gender']);

  $query = "UPDATE `tbl_resident` SET  
  `first_name`='$first_name',
  `middle_name`='$middle_name',
  `last_name`='$middle_name',
  `email`='$email',
  `address`='$address',
  `contact`='$contact',
  `age`='$age',
  `birthday`='$birthday',
  `gender`='$gender' WHERE `resident_id`='$res_id'";
  $query_result = $conn->query($query);
}

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

  $id = $_POST['resident_id'];

  $query = "delete from tbl_resident where resident_id = '$id'";
  $query_result = $conn->query($query);

}

if (isset($_POST['adding_residents'])) {
  $first_name = strtoupper($_POST['first_name']);
  $middle_name = strtoupper($_POST['middle_name']);
  $last_name = strtoupper($_POST['last_name']);
  $email = strtoupper($_POST['email']);
  $address = strtoupper($_POST['address']);
  $contact = $_POST['contact'];
  $age = $_POST['age'];
  $birthday = strtoupper($_POST['birthday']);
  $gender = strtoupper($_POST['gender']);
  $password = "123456";

  $query = "INSERT INTO tbl_resident(first_name, 
  middle_name,
  last_name, 
  email, 
  address,
  password, 
  contact, 
  age, 
  birthday, 
  gender,
  status) VALUES ('$first_name','$middle_name','$last_name','$email', '$address', '$password', '$contact','$age','$birthday','$gender','0')";
  $query_result = $conn->query($query);

}


if (isset($_POST['loading_residents'])) {
  
	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">EMAIL</th>
      <th scope="col">ADDRESS</th>
      <th scope="col">CONTACT</th>
      <th scope="col">AGE</th>
      <th scope="col">BIRTHDAY</th>
      <th scope="col">GENDER</th>
      <th scope="col">STATUS</th>
      <th scope="col">DATE</th>
      <th scope="col">ACTION</th>
    </tr>
  </thead>
  <tbody>
	';

	$residents = "SELECT * FROM tbl_resident";
	$result = $conn->query($residents);
	$count = 0;
	foreach ($result as $key => $row) {
		$date = date("Y-m-d", strtotime($row['date_created']));
    if ($row['status'] === "1") {
    $status = '<span class="badge badge-success">Online</span>';
    
  }else{
    $status = '<span class="badge badge-secondary">Offline</span>';
    
  }
		echo 
		'
		    <tr>
      <th scope="row">'.++$count.'</th>
      <td>'.$row['first_name'].'</td>
      <td>'.$row['middle_name'].'</td>
      <td>'.$row['last_name'].'</td>
      <td>'.$row['email'].'</td>
      <td>'.$row['address'].'</td>
      <td>'.$row['contact'].'</td>
      <td>'.$row['age'].'</td>
      <td>'.$row['birthday'].'</td>
      <td>'.$row['gender'].'</td>
      <td>'.$status.'</td>
      <td>'.$date.'</td>
      <td>
      <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#update-residents" onclick="edit_resident(
      \''.addslashes($row['resident_id']).'\',
      \''.addslashes($row['first_name']).'\',
      \''.addslashes($row['middle_name']).'\',
      \''.addslashes($row['last_name']).'\',
      \''.addslashes($row['email']).'\',
      \''.addslashes($row['address']).'\',
      \''.addslashes($row['contact']).'\',
      \''.addslashes($row['age']).'\',
      \''.addslashes($row['birthday']).'\',
      \''.addslashes($row['gender']).'\'
      )"><i class="fa fa-edit"></i></button>
      <button class="btn btn-danger btn-sm" onclick="delete_resident(\''.$row['resident_id'].'\')"><i class="fa fa-trash"></i></button>
      </td>
  
    </tr>

		';

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