<?php
	//find user id
	$getRoomId = $_GET['room'];
	if(isset($_GET['update'])){$getRoomCardId = $_GET['update'];}
?>
<div class="right_col" role="main">
    <div class="">
        <div class="row">
			
              <div class="clearfix"></div>

              <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>Access Card List</h2>
                    <ul class="nav navbar-right panel_toolbox">
                      <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                      </li>
                      <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
                        <ul class="dropdown-menu" role="menu">
                          <li><a href="#">Settings 1</a>
                          </li>
                          <li><a href="#">Settings 2</a>
                          </li>
                        </ul>
                      </li>
                      <li><a class="close-link"><i class="fa fa-close"></i></a>
                      </li>
                    </ul>
                    <div class="clearfix"></div>
                  </div>

                  <div class="x_content">

                    <div class="table-responsive">
                      <table class="table table-striped jambo_table bulk_action">
                        <thead>
                          <tr class="headings">
                            <th class="column-title">Bil </th>
                            <th class="column-title">Serial No </th>
                            <th class="column-title">Type </th>
                            <th class="column-title">Unit </th>
                            <!-- <th class="column-title">File</th> -->
                            <th class="column-title">Issue</th>
                            <th class="column-title">Terminate</th>
                            <th class="column-title">Note</th>
                            <th class="column-title">Admin</th>
                            <th class="column-title no-link last"><span class="nobr">Action</span>
                            </th>
                            <th class="bulk-actions" colspan="7">
                              <a class="antoo" style="color:#fff; font-weight:500;">Bulk Actions ( <span class="action-cnt"> </span> ) <i class="fa fa-chevron-down"></i></a>
                            </th>
                          </tr>
                        </thead>

                        <tbody>
							<?php
								$result=mysqli_query($conn, "SELECT * FROM room_cards");
								while($row = mysqli_fetch_array($result)){
							?>
                          <tr class="even pointer">
							  
                            <td class=" "><?php echo ++$no; ?></td>
                            <td class=" "><?php echo $row['rc_no']; ?></td>
                            <td class=" "><?php echo accessCardType($row['rc_type']); ?></td>
                            <td class=" "><?php echo getDetailByTable('properties',getDetailByTable('rooms',$row['r_id'])['p_id'])['pty_code']; ?> <?php echo getDetailByTable('rooms',$row['r_id'])['r_no']; ?></td>
                            <!-- <td class=" "><a href="?pagecontent=room_cards&room=<?php echo $getRoomId; ?>&update=<?php echo $row['rc_id']; ?>"><?php echo $row['rc_file']; ?></a></td> -->
                            <td class=" "><?php echo $row['rc_issued_at']; ?></td>
                            <td class=" "><?php echo $row['rc_terminated_at']; ?></td>
                            <td class=" "><?php echo urldecode($row['rc_note']); ?></td>
                            <td class=" "><?php echo getDetailByTable('admin',$row['a_id'])['a_username']; ?></td>
                            <td class=" last">
								<a href="?pagecontent=room_cards&room=<?php echo $row['r_id']; ?>&update=<?php echo $row['rc_id']; ?>"><button class="btn btn-xs btn-primary">Update</button></a>
								<a href="?pagecontent=room_cards&room=<?php echo $row['r_id']; ?>&remove=<?php echo $row['rc_id']; ?>"><button class="btn btn-xs btn-danger" disabled>Remove</button></a>
                            </td>
                          </tr>
							<?php } ?>
                        </tbody>
                      </table>
                    </div>
							
						
                  </div>
                </div>
              </div>
        </div>
    </div>
</div>

<?php
	function status($input){
		switch($input){
			case '0': return "NEW REPORT"; break;
			case '1': return "REVIEW"; break;
			case '2': return "IN PROCESS"; break;
			case '3': return "COMPLETED"; break;
		}
	}

	if(isset($_REQUEST['remove'])){
		
		$getRoomCardId = $_REQUEST['remove'];

		$roomCardFile = getDetailByTable('room_cards',$getRoomCardId)['rc_file'];

		mysqli_query($conn,"DELETE FROM room_cards WHERE rc_id=$getRoomCardId");
        unlink("uploads/cards/".$roomCardFile);
		
		header('Location: index.php?pagecontent=room_cards&room='.$getRoomId);  
	}

	if(isset($_POST['addBtn']))
	{
		$postSeralNo = $_POST['serialNo'];
		$postType = $_POST['type'];
		// $postDescription = urlencode($_POST['description']);
		
		$sessionAdminId = $_SESSION['admin'][0];
		
		mysqli_query($conn, "INSERT INTO room_cards (r_id, rc_no, rc_type, a_id)
		VALUES ($getRoomId, '$postSeralNo', '$postType', $sessionAdminId)");
		
		header('Location: ?pagecontent=room_cards&room='.$getRoomId);
	}

	if(isset($_POST['updateBtn']))
	{
		$postSeralNo = $_POST['serialNo'];
		$postType = $_POST['type'];
		$postDescription = urlencode($_POST['note']);
		
		//$sessionAdminId = $_SESSION['admin'][0];
		
		mysqli_query($conn, "UPDATE room_cards SET rc_no='$postSeralNo', rc_type=$postType, rc_note='$postDescription' WHERE rc_id=$getRoomCardId");
		
		header('Location: ?pagecontent=room_cards&room='.$getRoomId);
	}

	if(isset($_POST['issueBtn']))
	{
		$postDate = $_POST['date'];
		
		mysqli_query($conn, "UPDATE room_cards SET rc_issued_at='$postDate' WHERE rc_id=$getRoomCardId");
		
		header('Location: ?pagecontent=room_cards&room='.$getRoomId);
	}

	if(isset($_REQUEST['clearIssue']))
	{
        $getRoomCardId = $_GET['update'];

		mysqli_query($conn, "UPDATE room_cards SET rc_issued_at = NULL WHERE rc_id=$getRoomCardId");
		
		header('Location: ?pagecontent=room_cards&room='.$getRoomId);
	}

	if(isset($_POST['terminateBtn']))
	{
		$postDate = $_POST['date'];
		
		mysqli_query($conn, "UPDATE room_cards SET rc_terminated_at='$postDate' WHERE rc_id=$getRoomCardId");
		
		header('Location: ?pagecontent=room_cards&room='.$getRoomId);
	}

	if(isset($_REQUEST['clearTerminate']))
	{
		mysqli_query($conn, "UPDATE room_cards SET rc_terminated_at=NULL WHERE rc_id=$getRoomCardId");
		
		header('Location: ?pagecontent=room_cards&room='.$getRoomId);
	}

    function cardStatus($input){
        if($input){return "Activated";}else{return "Unknown";}
    }

    function imageResize($imageSrc,$imageWidth,$imageHeight) {

        $newImageWidth = 800;
        //$newImageHeight = 800;
        $newImageHeight = ($newImageWidth*$imageHeight)/$imageWidth;

        $newImageLayer=imagecreatetruecolor($newImageWidth,$newImageHeight);
        imagecopyresampled($newImageLayer,$imageSrc,0,0,0,0,$newImageWidth,$newImageHeight,$imageWidth,$imageHeight);

        return $newImageLayer;
    }
?>