<?php $_SESSION['redirect'] = $_SERVER['REQUEST_URI'];
	if(isset($_GET['update'])){$getExpensesId = $_GET['update'];}
?>

<div class="right_col" role="main">
	<div class="">

		<?php if($_REQUEST['update']){ ?>
        <div class="row">
        <div class="col-md-6 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
						<h2>Claim</h2>
						<div class="clearfix"></div>
					</div>
					<div class="x_content">
						<form class="form-horizontal form-label-left" method="post" enctype="multipart/form-data">
							<input type="hidden" name="payback" value="<?php echo $getExpensesId; ?>">
							
                        	<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="claimtitle">File 
								</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
						            <img src="uploads/expenses/<?php echo getDetailByTable('room_expenses',$getExpensesId)['re_filename']; ?>" width="100%">
								</div>
							</div>
							
                        	<div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="claimtitle">Title 
								</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<?php echo urldecode(getDetailByTable('room_expenses',$getExpensesId)['re_title']); ?>
								</div>
							</div>
                            
                            <div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="price">Amount 
								</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<?php echo "RM ".getDetailByTable('room_expenses',$getExpensesId)['re_amount']; ?>
								</div>
							</div>
                            
                            <div class="item form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-12" for="price">Date 
								</label>
								<div class="col-md-4 col-sm-3 col-xs-12">
									<?php echo getDetailByTable('room_expenses',$getExpensesId)['re_date']; ?>
								</div>
							</div>

							<div class="ln_solid"></div>
						</form>
					</div>
                </div>
            </div>

            <div class="col-md-6 col-sm-12 col-xs-12">
                <div class="x_panel">
					<div class="x_title">
						<h2>Sent Report</h2>
						<div class="clearfix"></div>
					</div>
					<div class="x_content">
						<form class="form-horizontal form-label-left" method="post" enctype="multipart/form-data">
							<input type="hidden" name="payback" value="<?php echo $getExpensesId; ?>">

							<?php if(getDetailByTable('room_expenses',$getExpensesId)['re_claim_file']){ ?>
                            <img src="uploads/claim/<?php echo $claimFilename = getDetailByTable('room_expenses',$getExpensesId)['re_claim_file']; ?>" width="100%">
                            <?php }else{ ?>
                            <span style="font-weight: bold; color: red;">NO RECORD</span>
                            <?php } ?>
                            <br>
                            <br>

							<div class="ln_solid"></div>

							<div class="form-group">
								<div class="col-md-6 col-md-offset-3">
									<a href="?pagecontent=expenses_claim&withdraw=<?php echo $getExpensesId; ?>" class="btn btn-danger btn-block">Withdraw</a>
								</div>
							</div>

                            <div class="ln_solid"></div>

                            <div class="form-group">
                                <h4>Linked Record</h4>
                                <p>
                                    <?php
                                        $records = mysqlResuitInArray('room_expenses','re_claim_file',$claimFilename);
                                        foreach($records AS $record){ ?>
                                            <a href=""><?php echo $record['re_title']; ?> - RM <?php echo $record['re_amount']; ?></a><br>
                                        <?php }
                                    ?>
                                </p>
                            </div>
						</form>

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

        </div>
		<?php } ?>
		
		
		<div class="clearfix"></div>
		<div class="row">
			<div class="col-md-12 col-sm-12 col-xs-12">
				<div class="x_panel">
					<div class="x_content">
						<table id="datatable" class="table table-striped table-bordered">
							<thead>
								<tr>
                                    <th>Date</th>
									<th>Title</th>
									<th>Room</th>
                                    <th>Amount</th>
                                    <th>Status</th>
                                    <th>Claim By</th>
                                    <th>Action</th>
								</tr>
							</thead>
							<tbody>
								<?php
									$result = mysqli_query($conn,"SELECT * FROM room_expenses WHERE re_claim_by IS NOT NULL AND re_claim_by!=0");
									while($row = mysqli_fetch_assoc($result)){
								?>
								<tr>
									<td><?php echo $row['re_date'];?></td>
									<td><?php echo urldecode($row['re_title']);?></td>
									<td>
                                        <a href="?pagecontent=expenses&room=<?php echo $row['r_id']; ?>">
                                            <?php if($row['r_id']){echo getDetailByTable('rooms',$row['r_id'])['r_no'];}else{echo "N/A";} ?></td>
                                        </a>
									<td>RM <?php echo $row['re_amount'];?></td>
									<td>
										<?php 
											switch($row['re_claim_status']){
												case 0: $title = "NO RECORD"; $color="red"; break;
												case 1: $title = "SENT"; $color="blue"; break;
											}
										?>
										<span style="font-weight: bold; color: <?php echo $color; ?>;"><?php echo $title; ?></span>
									</td>
									<td><?php echo getDetailFromTable('staffs',$row['re_claim_by'])['s_nickname']; ?></td>
									<td>
										<a href="?pagecontent=expenses_claim&update=<?php echo $row['re_id']; ?>"><button class="btn btn-xs btn-primary">Update</button></a>
									</td>
								</tr>
								<?php
									}
										?>
						</table>
					</div>
				</div>
			</div>
        </div>
    </div>
</div>

<?php
	if(isset($_REQUEST['withdraw'])){
        $getExpensesId = $_REQUEST['withdraw'];
        $expensesFilename = getDetailByTable("room_expenses",$getExpensesId)['re_claim_file'];

        $result = mysqli_query($conn,"SELECT * FROM room_expenses WHERE re_claim_file='$expensesFilename'");

        if(mysqli_num_rows($result) > 1){
            echo "<script>alert('There are more records in one receipt');</script>";
        }
        
        $expensesFilename = getDetailByTable("room_expenses",$getExpensesId)['re_claim_file'];

        if(unlink("uploads/claim/".$expensesFilename)){
            while($row = mysqli_fetch_assoc($result)){
                $expensesId = $row['re_id'];
                mysqli_query($conn,"UPDATE room_expenses SET re_claim_file=NULL, re_claim_status=NULL, re_claimed_at=NULL WHERE re_id=$expensesId");
            }
        }

        $claimBy = getDetailByTable("room_expenses",$getExpensesId)['re_claim_by'];
        header("Location: ?pagecontent=expenses_report&claimby=$claimBy"); 
	}

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

		$newImageWidth =200;
		$newImageHeight =200;

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

		return $newImageLayer;
	}
?>