<?php 
	date_default_timezone_set('Asia/Kuala_Lumpur');
    $getDate = date('Y-m-d');
?>

<div class="right_col" role="main">
	<div class="">
		<div class="page-title">
			<div class="title_left">
				<h3>DEPOSIT</h3>
			</div>
		</div>
		<div class="clearfix"></div>

<?php if($_GET['forfeit']){ 
    if($_GET['forfeit']){$getDepositId = $_GET['forfeit'];}
    $getReservationId = getDetailByTable('deposit',$getDepositId)['rv_id'];
    $getAmount = getDetailByTable('deposit',$getDepositId)['dp_forfeited_amount'] ? getDetailByTable('deposit',$getDepositId)['dp_forfeited_amount'] : getDetailByTable('deposit',$getDepositId)['dp_amount'];
    ?>
<div class="row">
    <div class="col-md-12 col-sm-12 col-xs-12">
        <div class="x_panel">
            <div class="x_title">
                <h2>Forfeit Detail</h2>
                <div class="clearfix"></div>
            </div>

            <div class="x_content">
                <form class="form-horizontal form-label-left" method="post">
                    <input name="deposit" type="hidden" value="<?php echo $getDepositId; ?>">
                    
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Guest
                        </label>
                        <div class="col-md-3 col-sm-3 col-xs-12">
                            <input class="form-control col-md-3 col-xs-12" type="text" value="<?php echo getDetailByMysqliWhere('reservation','rv_id',getDetailByTable('deposit',$getDepositId)['rv_id'])['rv_guest']; ?>" readonly>
                        </div>
                    </div>
                    
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Room
                        </label>
                        <div class="col-md-3 col-sm-3 col-xs-12">
                            <input class="form-control col-md-3 col-xs-12" type="text" value="<?php echo getDetailByTable('rooms',getDetailByTable('reservation',$getReservationId)['r_id'])['r_no']; ?>" readonly>
                        </div>
                    </div>
                    
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Check Out
                        </label>
                        <div class="col-md-3 col-sm-3 col-xs-12">
                            <input class="form-control col-md-3 col-xs-12" type="text" value="<?php echo getDetailByMysqliWhere('reservation','rv_id',getDetailByTable('deposit',$getDepositId)['rv_id'])['rv_co_date']; ?>" readonly>
                        </div>
                    </div>
                    
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Forfeit Date
                        </label>
                        <div class="col-md-3 col-sm-3 col-xs-12">
                            <input id="night" class="form-control col-md-3 col-xs-12" name="date" type="date" value="<?php echo $getDate; ?>">
                        </div>
                    </div>
                    
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Amount to forfeit 
                        </label>
                        <div class="col-md-3 col-sm-3 col-xs-12">
                            <input id="night" class="form-control col-md-3 col-xs-12" name="amount" type="currency" value="<?php echo $getAmount; ?>">
                        </div>
                    </div>

                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="note">Note</span>
                        </label>
                        <div class="col-md-4 col-sm-3 col-xs-12">
                            <textarea id="message" class="form-control" name="note"><?php echo $getNote; ?></textarea>
                        </div>
                    </div>

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

                    <div class="form-group">
                        <div class="col-md-6 col-md-offset-3">
                            <button id="send" type="submit" class="btn btn-primary" name="forfeitBtn">Forfeit</button>
                            <a href="?pagecontent=deposit&clearForfeit=<?php echo $getDepositId; ?>" class="btn btn-danger">Clear</a>
                        </div>
                    </div>

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

</div>
<?php } ?>

<?php if($_GET['refund']){ 
    if($_GET['refund']){$getDepositId = $_GET['refund'];}
    $getReservationId = getDetailByTable('deposit',$getDepositId)['rv_id'];
    $getAmount = getDetailByTable('deposit',$getDepositId)['dp_amount'] - getDetailByTable('deposit',$getDepositId)['dp_forfeited_amount'];
    $getRefundReceiverAccount = getDetailByTable('deposit',$getDepositId)['dp_receiver_acc'];
    $getRefundReceiverBank = getDetailByTable('deposit',$getDepositId)['dp_receiver_bank'];
    $getRefundReceiverHolder = getDetailByTable('deposit',$getDepositId)['dp_receiver_holder'];
    ?>
<div class="row">
    <div class="col-md-6 col-sm-6 col-xs-6">
        <div class="x_panel">
            <div class="x_title">
                <h2>Receiver Detail</h2>
                <div class="clearfix"></div>
            </div>

            <div class="x_content">
                <form class="form-horizontal form-label-left" method="post">
                    <input name="deposit" type="hidden" value="<?php echo $getDepositId; ?>">
                    
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Booking No
                        </label>
                        <div class="col-md-9 col-sm-9 col-xs-12">
                            <input class="form-control col-md-3 col-xs-12" type="text" value="<?php echo getDetailByMysqliWhere('reservation','rv_id',getDetailByTable('deposit',$getDepositId)['rv_id'])['rv_no']; ?>" readonly>
                        </div>
                    </div>
                    
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Guest
                        </label>
                        <div class="col-md-9 col-sm-9 col-xs-12">
                            <input class="form-control col-md-3 col-xs-12" type="text" value="<?php echo getDetailByMysqliWhere('reservation','rv_id',getDetailByTable('deposit',$getDepositId)['rv_id'])['rv_guest']; ?>" readonly>
                        </div>
                    </div>
                    
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Room
                        </label>
                        <div class="col-md-9 col-sm-9 col-xs-12">
                            <input class="form-control col-md-3 col-xs-12" type="text" value="<?php echo getDetailByTable('rooms',getDetailByTable('reservation',$getReservationId)['r_id'])['r_no']; ?>" readonly>
                        </div>
                    </div>
                    
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Check Out
                        </label>
                        <div class="col-md-9 col-sm-9 col-xs-12">
                            <input class="form-control col-md-3 col-xs-12" type="text" value="<?php echo getDetailByMysqliWhere('reservation','rv_id',getDetailByTable('deposit',$getDepositId)['rv_id'])['rv_co_date']; ?>" readonly>
                        </div>
                    </div>
                    
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Account
                        </label>
                        <div class="col-md-9 col-sm-9 col-xs-12">
                            <input class="form-control col-md-3 col-xs-12" type="text" name="receiverAccount" value="<?php echo $getRefundReceiverAccount; ?>">
                        </div>
                    </div>
                    
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Bank 
                        </label>
                        <div class="col-md-9 col-sm-9 col-xs-12">
                            <input class="form-control col-md-3 col-xs-12" type="text" name="receiverBank" value="<?php echo $getRefundReceiverBank; ?>">
                        </div>
                    </div>
                    
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Holder Name 
                        </label>
                        <div class="col-md-9 col-sm-9 col-xs-12">
                            <input class="form-control col-md-3 col-xs-12" type="text" name="receiverHolder" value="<?php echo $getRefundReceiverHolder; ?>">
                        </div>
                    </div>

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

                    <div class="form-group">
                        <div class="col-md-6 col-md-offset-3">
                            <button id="send" type="submit" class="btn btn-primary" name="receiverBtn">Update</button>
                            <a href="?pagecontent=deposit&clearReceiver=<?php echo $getDepositId; ?>" class="btn btn-danger">Clear</a>
                        </div>
                    </div>

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

    <div class="col-md-6 col-sm-6 col-xs-6">
        <div class="x_panel">
            <div class="x_title">
                <h2>Refund Detail</h2>
                <div class="clearfix"></div>
            </div>

            <div class="x_content">
                <form class="form-horizontal form-label-left" method="post">
                    <input name="deposit" type="hidden" value="<?php echo $getDepositId; ?>">
                    
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Refund Date
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <input id="night" class="form-control col-md-3 col-xs-12" name="date" type="date" value="<?php echo $getDate; ?>">
                        </div>
                    </div>
                    
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Amount to refund 
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <input id="night" class="form-control col-md-3 col-xs-12" name="amount" type="currency" value="<?php echo $getAmount; ?>">
                        </div>
                    </div>

                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="note">Note</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <textarea id="message" class="form-control" name="note"><?php echo $getNote; ?></textarea>
                        </div>
                    </div>

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

                    <div class="form-group">
                        <div class="col-md-6 col-md-offset-3">
                            <button id="send" type="submit" class="btn btn-primary" name="refundBtn">Update</button>
                            <a href="?pagecontent=deposit&clearRefund=<?php echo $getDepositId; ?>" class="btn btn-danger">Clear</a>
                        </div>
                    </div>

                </form>
                            
                            
            </div>
        </div>
    </div>
	
    <?php if(getDetailByTable('deposit',$getDepositId)['dp_refund_at'] != NULL){ ?>
	<div class="col-md-6 col-sm-6 col-xs-12">
		<div class="x_panel">
			<div class="x_content">
				<img src="uploads/deposit/<?php echo getDetailByTable('deposit',$getDepositId)['dp_refund_file']; ?>" width="100%">
			</div>

			<div class="x_content">

				<form class="form-horizontal form-label-left" method="post" enctype="multipart/form-data">
						<div class="item form-group">
							<label class="control-label col-md-3 col-sm-3 col-xs-12" for="night">Upload 
							</label>
							<div class="col-md-6 col-sm-6 col-xs-12">
								<input id="night" class="form-control col-md-3 col-xs-12" name="fileToUpload" type="file" >
							</div>
						</div>

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

						<div class="form-group">
							<div class="col-md-6 col-md-offset-3">
								<button id="send" type="submit" class="btn btn-success" name="uploadBtn">Upload</button>
							</div>
						</div>
					</form>
				
				<?php
						if(isset($_POST['uploadBtn'])){

							if(getDetailByTable("deposit",$getDepositId)['dp_refund_file']){
								$depositRefundFile = getDetailByTable("deposit",$getDepositId)['dp_refund_file'];
								unlink("uploads/deposit/".$depositRefundFile);
							}
							
							
							  //upload receipt
							  $errors = array();
							  $file_name = $_FILES['fileToUpload']['name'];
							  $file_size =$_FILES['fileToUpload']['size'];
							  $file_tmp = $_FILES['fileToUpload']['tmp_name'];
							  $file_type = $_FILES['fileToUpload']['type'];

							  $file_ext = strtolower(end(explode('.',$_FILES['fileToUpload']['name'])));

							  $extensions = array("jpeg","jpg","png");
							
							

							  if(in_array($file_ext,$extensions)=== false){
								 $errors[] = "extension not allowed, please choose a JPEG or PNG file.";
							  }

							  if($file_size > 2097152){
								 $errors[] = 'File size must be excately 2 MB';
								  
							  }

								$file_name = $getDepositId."_".time().".".$file_ext;
								$filePath = "uploads/deposit/".$file_name;

							  if(empty($errors) == true){

								 if(move_uploaded_file($file_tmp,$filePath)){
									 
//									sendMessage2(-1001753305829,"test receipt upload ok");

									 mysqli_query($conn, "UPDATE deposit SET dp_refund_file='$file_name' WHERE dp_id=$getDepositId");

									 $adminName = $_SESSION['admin']['a_username'];
									 $bookingNo = getDetailByTable("reservation",getDetailByTable("deposit",$getDepositId)['rv_id'])['rv_no'];
									 $guest = getDetailByTable("reservation",getDetailByTable("deposit",$getDepositId)['rv_id'])['rv_guest'];
									 $roomNo = getDetailByTable("rooms",getDetailByTable("reservation",getDetailByTable("deposit",$getDepositId)['rv_id'])['r_id'])['r_no'];
									 $amount = getDetailByTable("deposit",$getDepositId)['dp_refund_amount'];

									 sendMessage2(-1001753305829,$adminName." upload guest's deposit receipt for
									 
Booking No : ".$bookingNo."
Guest : ".$guest."
Room : ".$roomNo."
Amount : RM ".$amount."
");
									 sendPhoto(-1001753305829,"https://admin.homestay.dzvisual.my/production/".$filePath);
								 }
							  }

                            //RECORD LOG
                            $adminId = $_SESSION['admin']['a_id'];
                            $logTitle = ucfirst($_SESSION['admin']['a_username'])." upload refund file";
                            mysqli_query($conn,"INSERT INTO deposit_log (dpl_title, dp_id, dpl_ip_address, a_id) VALUE ('$logTitle',$getDepositId,'$ipAddress',$adminId)");

							header("Location: ?pagecontent=deposit&refund=$getDepositId");
						}
				?>
			</div>
		</div>
	</div>
    <?php } ?>

</div>


	  <div class="col-12">
		<div class="x_panel">
		  <div class="x_title">
			<h2>Activity Log</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">
			<table class="table table-hover">
			  <thead>
				<tr>
				  <th>#</th>
				  <th>Log</th>
				  <th>Date/Time</th>
				  <th>Admin</th>
				  <th>Action</th>
				</tr>
			  </thead>
			  <tbody>
				<?php 
                $result = mysqli_query($conn,"SELECT * FROM deposit_log WHERE dp_id=$getDepositId ORDER BY dpl_id DESC");
				while($row = mysqli_fetch_assoc($result)){
				?>
				<tr>
				  <th scope="row"><?php echo ++$no; ?></th>
				  <td><?php echo $row['dpl_title']; ?></td>
				  <td><?php echo $row['timestamp']; ?></td>
				  <td><?php echo getDetailFromTable('admin',$row['a_id'])['a_username']; ?></td>
				  <td></td>
				</tr>
				  <?php } ?>
			  </tbody>
			</table>

		  </div>
		</div>
	  </div>
<?php } ?>

		
		<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>Reservation</th>
								    <th>Platform</th>
									<th>Guest</th>
									<th>Room</th>
									<th>Check Out</th>
									<th>Deposit</th>
									<th>Verified At</th>
									<th>Verified By</th>
									<th>Forfeit</th>
									<th>Refund</th>
									<th>Ready By</th>
									<th>Action</th>
								</tr>
							</thead>
							<tbody>
								<?php
								
									$sql = "SELECT * FROM deposit WHERE dp_refund_at IS NULL ORDER BY dp_ready_at ASC";
									$result = mysqli_query($conn,$sql) or die(mysqli_error($result));
									
									while($row = mysqli_fetch_array($result))
									{
								?>
								<tr style="<?php if(getDetailByTable('reservation',$row['rv_id'])['rv_co_date'] == date('Y-m-d')){echo 'background-color:#D6FFD1; font-weight: bold;'; }elseif(getDetailByTable('reservation',$row['rv_id'])['rv_co_date'] < date('Y-m-d')){echo 'background-color:#FFD5CC; font-weight: bold;';} ?>">
								    <td><?php echo getDetailByTable('reservation',$row['rv_id'])['rv_no']; ?></td>
									
									<td><?php 
										echo platformTitle(getDetailByTable('reservation',$row['rv_id'])['rv_platform']); echo "<br>";
										echo reservationByOwnerIdToTitle($row['rv_byowner']);
										?></td>
									
									<td><a href="?pagecontent=reservation_communication&rv=<?php echo $row['rv_id']; ?>"><?php echo getDetailByTable('reservation',$row['rv_id'])['rv_guest']; ?></a></td>
									<td><?php if(getDetailByTable('reservation',$row['rv_id'])['r_id']){echo getDetailFromTable('properties',getDetailFromTable('rooms',getDetailByTable('reservation',$row['rv_id'])['r_id'])['p_id'])['pty_code']." ".getDetailFromTable('rooms',getDetailByTable('reservation',$row['rv_id'])['r_id'])['r_no'];} ?></td>
									<td><?php echo getDetailByTable('reservation',$row['rv_id'])['rv_co_date']; ?></td>
									<td>
                                        <span style="color:black; font-weight:bold;">
                                            <?php if($row['dp_amount']){echo "RM ".$row['dp_amount'];} ?>
                                        </span>
									</td>
									<td>
                                        <?php if($row['dp_verified_at']){ ?>
                                            <a href="?pagecontent=deposit&verify=n&id=<?php echo $row['dp_id']; ?>"><button class="btn btn-success btn-xs">VERIFIED</button></a>
                                        <?php }else{ ?>
                                            <a href="?pagecontent=deposit&verify=y&id=<?php echo $row['dp_id']; ?>"><button class="btn btn-primary btn-xs">VERIFY NOW</button></a>
                                        <?php } ?>
									</td>
									<td>
                                        <?php
                                            if($row['dp_verified_by']){
                                                echo getDetailByTable('admin',$row['dp_verified_by'])['a_username'];
                                            }else{
                                                echo "";
                                            }
                                        ?>
									</td>
									<td>
                                        <a href="?pagecontent=deposit&forfeit=<?php echo $row['dp_id']; ?>">
                                        <?php if($row['dp_forfeited_amount']){ ?>
                                                <span style="color:red; font-weight:bold;"><?php echo "RM ".$row['dp_forfeited_amount']; ?></span>
                                            <?php }else{ ?>

                                            <?php } ?>
                                        </a>
									</td>
									<td>
                                        <span style="color:blue; font-weight:bold;">
                                            <?php
                                                if(($row['dp_amount'] - $row['dp_forfeited_amount'])==0){
                                                    echo "CLEARED";
                                                }else{
                                                    echo "RM ".($row['dp_amount'] - $row['dp_forfeited_amount']);
                                                }
                                            ?>
                                        </span>
									</td>
									<td>
                                        <?php
                                            if($row['dp_ready_at']){
                                                echo getDetailByTable('admin',$row['dp_ready_by'])['a_username'];
                                            }else{
                                                echo "";
                                            }
                                        ?>
									</td>
									<td>
                                        <?php if($row['dp_ready_at']){ ?>
                                            <a href="?pagecontent=deposit&refund=<?php echo $row['dp_id']; ?>"><button class="btn btn-primary btn-xs">REFUND NOW</button></a>
                                            <a href="?pagecontent=deposit&notready=<?php echo $row['dp_id']; ?>"><button class="btn btn-danger btn-xs">NOT READY</button></a>
                                        <?php }elseif($row['dp_forfeited_amount']){ ?>
                                            <a href="?pagecontent=deposit&ready=<?php echo $row['dp_id']; ?>"><button class="btn btn-success btn-xs">READY NOW</button></a>
                                        <?php }elseif(!$row['dp_forfeited_amount']){ ?>
                                            <a href="?pagecontent=deposit&forfeit=<?php echo $row['dp_id']; ?>"><button class="btn btn-danger btn-xs">FORFEIT</button></a>
                                        <?php } ?>
									</td>
								</tr>
								<?php
									}
								?>
						</table>
					</div>
				</div>
			</div>
        </div>
        
		<div class="clearfix"></div>
		
		<div class="row">
			<div class="col-md-12 col-sm-12 col-xs-12">
                
                <h3>REFUNDED</h3>
				<div class="x_panel">
					<div class="x_content">
						<table id="datatable" class="table table-striped table-bordered">
							<thead>
								<tr>
								    <th>Reservation</th>
								    <th>Platform</th>
									<th>Guest</th>
									<th>Room</th>
									<th>Check Out</th>
									<th>Deposit</th>
									<th>Forfeit</th>
									<th>Refund</th>
									<th>Status</th>
									<th>WA Slip</th>
								</tr>
							</thead>
							<tbody>
								<?php
								    $sql = "SELECT * FROM deposit WHERE dp_refund_at IS NOT NULL ORDER BY dp_id DESC LIMIT 100";

									if(isset($_GET['room'])){
                                        $filterByRoom = "AND r_id=".$_GET['room'];
                                        $sql = "SELECT * FROM deposit AS DP LEFT JOIN reservation AS RV ON DP.rv_id=RV.rv_id WHERE dp_refund_at IS NOT NULL $filterByRoom ORDER BY dp_id DESC LIMIT 100";
                                    }

									if(isset($_GET['forfeited'])){
                                        $filterByForfeit = "AND dp_forfeited_amount IS NOT NULL";
                                        $sql = "SELECT * FROM deposit AS DP LEFT JOIN reservation AS RV ON DP.rv_id=RV.rv_id WHERE dp_refund_at IS NOT NULL $filterByRoom $filterByForfeit ORDER BY dp_id DESC LIMIT 100";
                                    }

									if(isset($_GET['property'])){
                                        $filterByProperty = "AND pty_code='".$_GET['property']."'";
                                        $sql = "SELECT * FROM deposit AS DP LEFT JOIN reservation AS RV ON DP.rv_id=RV.rv_id LEFT JOIN rooms AS R ON RV.r_id=R.r_id LEFT JOIN properties AS P ON R.p_id=P.pty_id WHERE dp_refund_at IS NOT NULL $filterByRoom $filterByForfeit $filterByProperty ORDER BY dp_id DESC LIMIT 100";
                                    }

									$result = mysqli_query($conn,$sql);
									
									while($row = mysqli_fetch_array($result))
									{
								?>
								<tr style="<?php if($row['rv_status'] == 0 && $row['rv_ci_date'] == $getDate){echo 'background-color:#FFD5CC; font-weight: bold;'; }elseif($row['rv_status'] > 1 && $row['r_id']){echo 'background-color:#D6FFD1; font-weight: bold;'; }elseif($row['rv_status'] > 1 && !$row['r_id']){echo 'background-color:#FFFAB3; font-weight: bold;';} ?>">
								    <td><?php echo getDetailByTable('reservation',$row['rv_id'])['rv_no']; ?></td>
									
									<td><?php 
										echo platformTitle(getDetailByTable('reservation',$row['rv_id'])['rv_platform']); echo "<br>";
										echo reservationByOwnerIdToTitle($row['rv_byowner']);
										?></td>
									
									<td><a href="?pagecontent=reservation_communication&rv=<?php echo $row['rv_id']; ?>"><?php echo getDetailByTable('reservation',$row['rv_id'])['rv_guest']; ?></a></td>
									<td><a href="?pagecontent=deposit&room=<?php echo getDetailByTable('reservation',$row['rv_id'])['r_id']; ?>"><?php if(getDetailByTable('reservation',$row['rv_id'])['r_id']){echo getDetailFromTable('properties',getDetailFromTable('rooms',getDetailByTable('reservation',$row['rv_id'])['r_id'])['p_id'])['pty_code']." ".getDetailFromTable('rooms',getDetailByTable('reservation',$row['rv_id'])['r_id'])['r_no'];} ?></a></td>
									<td><?php echo getDetailByTable('reservation',$row['rv_id'])['rv_co_date']; ?></td>
									<td>
                                        <span style="color:black; font-weight:bold;">
                                            <?php if($row['dp_amount']){echo "RM ".$row['dp_amount'];} ?>
                                        </span>
									</td>
									<td>
                                        <a href="?pagecontent=deposit&forfeit=<?php echo $row['dp_id']; ?>">
                                        <?php if($row['dp_forfeited_amount']){ ?>
                                                <span style="color:red; font-weight:bold;"><?php echo "RM ".$row['dp_forfeited_amount']; ?></span>
                                            <?php }else{ ?>
                                                <span style="color:grey; font-weight:bold;">NO RECORD</span>
                                            <?php } ?>
                                        </a>
									</td>
									<td>
                                        
                                    <?php
                                                if(($row['dp_amount'] - $row['dp_forfeited_amount'])==0){ ?>
                                                    <?php echo '<span style="color:red; font-weight:bold;">RM 0</span>'; ?>
                                                <?php }else{
                                                    echo "RM ".($row['dp_amount'] - $row['dp_forfeited_amount']);
                                                }
                                            ?>

                                            <?php
                                            if($row['dp_refund_file']){ ?>
                                                <a href="https://admin.homestay.dzvisual.my/production/uploads/deposit/<?php echo $row['dp_refund_file']; ?>" target="blank"><button class="btn btn-default btn-xs">SLIP</button></a>
                                            <?php }else{ ?>
                                                <a href="?pagecontent=deposit&refund=<?php echo $row['dp_id']; ?>"><button class="btn btn-primary btn-xs">Upload</button></a>
                                            <?php }
                                        ?>
									</td>
									<td>
                                        <a href="?pagecontent=deposit&refund=<?php echo $row['dp_id']; ?>">
											<span style="font-weight: bold; color:blue;"><?php echo "REFUNDED"; ?></span>
										</a>
									</td>
									<td>
                                        <?php if($row['dp_wa_sent_at']){ ?>
                                            <a href="?pagecontent=deposit&wa=<?php echo $row['dp_id']; ?>" onclick="return confirm('Are you sure?');"><button class="btn btn-success btn-xs">RESEND</button></a>
                                        <?php }else{ ?>
                                            <a href="?pagecontent=deposit&wa=<?php echo $row['dp_id']; ?>" onclick="return confirm('Are you sure?');"><button class="btn btn-primary btn-xs">SEND</button></a>
                                        <?php } ?>
									</td>
								</tr>
								<?php
									}
								?>
						</table>
					</div>
				</div>
			</div>
        </div>
		<div class="clearfix"></div>

    </div>
</div>

<?php

	function reservationByOwnerIdToTitle($id){
		switch($id){
			case 0 : $title = ""; break;
			case 1 : $title = "By Owner"; break;
		}
		
		return $title;
	}

    if(isset($_POST['forfeitBtn']))
    {
        $postDepositId = $_POST['deposit'];
        $postDate = $_POST['date'];
        $postAmount = $_POST['amount'];
        $postNote = $_POST['note'];
        
        //admin id
        $adminId = $_SESSION['admin']['a_id'];
        
        mysqli_query($conn, "UPDATE deposit SET dp_forfeited_at='$postDate', dp_forfeited_amount='$postAmount', dp_forfeit_note='$postNote' WHERE dp_id=$postDepositId");
        
        //RECORD LOG
        $logTitle = ucfirst($_SESSION['admin']['a_username'])." forfeit deposit RM ".$postAmount;
        mysqli_query($conn,"INSERT INTO deposit_log (dpl_title, dp_id, dpl_ip_address, a_id) VALUE ('$logTitle',$postDepositId,'$ipAddress',$adminId)");
        
        telegramUpdateDeposit();

        header("Location: ?pagecontent=deposit");
    }

    if(isset($_REQUEST['clearForfeit']))
    {
        $postDepositId = $_REQUEST['clearForfeit'];
        
        mysqli_query($conn, "UPDATE deposit SET dp_forfeited_at=NULL, dp_forfeited_amount=NULL, dp_forfeit_note=NULL WHERE dp_id=$postDepositId");

        //RECORD LOG
        $adminId = $_SESSION['admin']['a_id'];
        $logTitle = ucfirst($_SESSION['admin']['a_username'])." clear deposit details";
        mysqli_query($conn,"INSERT INTO deposit_log (dpl_title, dp_id, dpl_ip_address, a_id) VALUE ('$logTitle',$postDepositId,'$ipAddress',$adminId)");
        
        telegramUpdateDeposit();
        
        header("Location: ?pagecontent=deposit");
    }

    if(isset($_REQUEST['ready']))
    {
        $id = $_REQUEST['ready'];

        $adminId = $_SESSION['admin']['a_id'];

        $currentDateTime = date("Y-m-d h:i:s");
        mysqli_query($conn, "UPDATE deposit SET dp_ready_at='$currentDateTime', dp_ready_by=$adminId WHERE dp_id=$id");

        //RECORD LOG
        $logTitle = ucfirst($_SESSION['admin']['a_username'])." set as ready for refund";
        mysqli_query($conn,"INSERT INTO deposit_log (dpl_title, dp_id, dpl_ip_address, a_id) VALUE ('$logTitle',$postDepositId,'$ipAddress',$adminId)");
        
        telegramUpdateDeposit();

        header("Location: ?pagecontent=deposit");
    }

    if(isset($_REQUEST['notready']))
    {
        $id = $_REQUEST['notready'];

        $adminId = $_SESSION['admin']['a_id'];

        mysqli_query($conn, "UPDATE deposit SET dp_ready_at=NULL, dp_ready_by=NULL WHERE dp_id=$id");

        //RECORD LOG
        $logTitle = ucfirst($_SESSION['admin']['a_username'])." set as not ready for refund";
        mysqli_query($conn,"INSERT INTO deposit_log (dpl_title, dp_id, dpl_ip_address, a_id) VALUE ('$logTitle',$postDepositId,'$ipAddress',$adminId)");
        
        telegramUpdateDeposit();

        header("Location: ?pagecontent=deposit");
    }

    if(isset($_REQUEST['verify']))
    {
        $verify = $_GET['verify'];
        $id = $_GET['id'];

        $currentDateTime = date("Y-m-d h:i:s");

        $adminId = $_SESSION['admin']['a_id'];

        if($verify == "y"){
            mysqli_query($conn, "UPDATE deposit SET dp_verified_at='$currentDateTime', dp_verified_by=$adminId WHERE dp_id=$id");
        }elseif($verify == "n"){
            mysqli_query($conn, "UPDATE deposit SET dp_verified_at=NULL, dp_verified_by=NULL WHERE dp_id=$id");
        }

        //RECORD LOG
        $logTitle = ucfirst($_SESSION['admin']['a_username'])." verify deposit";
        mysqli_query($conn,"INSERT INTO deposit_log (dpl_title, dp_id, dpl_ip_address, a_id) VALUE ('$logTitle',$postDepositId,'$ipAddress',$adminId)");
        
        telegramUpdateDeposit();

        header("Location: ?pagecontent=deposit");
    }

    if(isset($_REQUEST['wa']))
    {
        $id = $_GET['wa'];
        $reservationId = getDetailByTable('deposit',$id)['rv_id'];
        $contact = getDetailByTable('reservation',$reservationId)['rv_contact'];
        $refundFilename = getDetailByTable('deposit',$id)['dp_refund_file'];

        $currentDateTime = date("Y-m-d h:i:s");

        $adminId = $_SESSION['admin']['a_id'];

        mysqli_query($conn, "UPDATE deposit SET dp_wa_sent_at='$currentDateTime', dp_wa_sent_by=$adminId WHERE dp_id=$id");

        //RECORD LOG
        $logTitle = ucfirst($_SESSION['admin']['a_username'])." send slip by Whatsapp";
        mysqli_query($conn,"INSERT INTO deposit_log (dpl_title, dp_id, dpl_ip_address, a_id) VALUE ('$logTitle',$postDepositId,'$ipAddress',$adminId)");

        //send via whatsapp
        $message = urlencode("AUTO MESSAGE :\nPlease be informed that your deposit refund had been sent. Kindly please confirm that you have received the refund. Thank you & have a nice day :)");
        // sendWA($contact,$message);

        //send wa photo
        $curl = curl_init();

        curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://app.respond.io/api/v1/message/sendContent/60107744530',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS =>'{ 
        "body" : [ 
        { 
        "type" : "whatsapp_template" , 
        "template" : { 
        "name" : "deposit_refund_slip" , 
        "languageCode" : "en" , 
        "components" : [ 
        { 
        "type" : "header" , 
        "format" : "image" , 
        "parameters" : [ 
        { 
        "type" : "image" , 
        "image" : { 
        "link" : "https://admin.homestay.dzvisual.my/production/uploads/deposit/'.$refundFilename.'" 
        } 
        } 
        ] 
        }, 
        { 
        "type" : "body" , 
        "text" : "insert body template message" 
        } 
        ] 
        } 
        } 
        
        ] 
        }',
        CURLOPT_HTTPHEADER => array(
            'Authorization: Bearer 35aea252ed4d90aa6904c44ff9837f232ff51c6dd6b628318127b24b214730b30bbd793fac5cc135853d85cbc927c25d8401f36e17ac0f17c9abcd7b8c4e1ef1',
            'Content-Type: application/json'
        ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        // echo $response;


        // $response = sendWAImage($contact,"http");
        // sendMessage4(json_decode($response));

        header("Location: ?pagecontent=deposit");
    }

    if(isset($_POST['refundBtn']))
    {
        $postDepositId = $_POST['deposit'];
        $postDate = $_POST['date'];
        $postAmount = $_POST['amount'];
        $postNote = $_POST['note'];
        
        mysqli_query($conn, "UPDATE deposit SET dp_refund_at='$postDate', dp_refund_amount='$postAmount', dp_refund_note='$postAmount' WHERE dp_id=$postDepositId");
        
        //RECORD LOG
        $adminId = $_SESSION['admin']['a_id'];
        $logTitle = ucfirst($_SESSION['admin']['a_username'])." refund deposit";
        mysqli_query($conn,"INSERT INTO deposit_log (dpl_title, dp_id, dpl_ip_address, a_id) VALUE ('$logTitle',$postDepositId,'$ipAddress',$adminId)");
        
        telegramUpdateDeposit();

        header("Location: ?pagecontent=deposit");
    }

    if(isset($_REQUEST['clearRefund']))
    {
        $postDepositId = $_REQUEST['clearRefund'];
        
        mysqli_query($conn, "UPDATE deposit SET dp_refund_at=NULL, dp_refund_amount=NULL, dp_refund_note=NULL WHERE dp_id=$postDepositId");

        //RECORD LOG
        $logTitle = ucfirst($_SESSION['admin']['a_username'])." clear refund details";
        mysqli_query($conn,"INSERT INTO deposit_log (dpl_title, dp_id, dpl_ip_address, a_id) VALUE ('$logTitle',$postDepositId,'$ipAddress',$adminId)");
        
        telegramUpdateDeposit();
        
        header("Location: ?pagecontent=deposit");
    }

    if(isset($_POST['receiverBtn']))
    {
        $postDepositId = $_POST['deposit'];
        $postReceiverAccount = $_POST['receiverAccount'];
        $postReceiverBank = $_POST['receiverBank'];
        $postReceiverHolder = $_POST['receiverHolder'];
        
        mysqli_query($conn, "UPDATE deposit SET dp_receiver_acc='$postReceiverAccount', dp_receiver_bank='$postReceiverBank', dp_receiver_holder='$postReceiverHolder' WHERE dp_id=$postDepositId");
        
        //RECORD LOG
        $adminId = $_SESSION['admin']['a_id'];
        $logTitle = ucfirst($_SESSION['admin']['a_username'])." update receiver details";
        mysqli_query($conn,"INSERT INTO deposit_log (dpl_title, dp_id, dpl_ip_address, a_id) VALUE ('$logTitle',$postDepositId,'$ipAddress',$adminId)");

        header("Location: ?pagecontent=deposit&refund=$postDepositId");
    }

    if(isset($_REQUEST['clearReceiver']))
    {
        $postDepositId = $_REQUEST['clearReceiver'];
        
        mysqli_query($conn, "UPDATE deposit SET dp_receiver_acc=NULL, dp_receiver_bank=NULL, dp_receiver_holder=NULL WHERE dp_id=$postDepositId");
        
        //RECORD LOG
        $adminId = $_SESSION['admin']['a_id'];
        $logTitle = ucfirst($_SESSION['admin']['a_username'])." clear receiver details";
        mysqli_query($conn,"INSERT INTO deposit_log (dpl_title, dp_id, dpl_ip_address, a_id) VALUE ('$logTitle',$postDepositId,'$ipAddress',$adminId)");

        header("Location: ?pagecontent=deposit&refund=$postDepositId");
    }
?>