<?php
session_start();
$key = md5(date("Y-m-d").":".$_SERVER['REMOTE_ADDR']);
if (isset($_SESSION['aW1iZXN0cmEuY29tLm15']) <> $key) {
    header("location:".$_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME']); // cara baru !
}   else {
    include "_functions.php";
    require "conn.php";	
?>


<!DOCTYPE html>
<html lang="en">
    <head>

        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>WePayYourCost | PayMerchant</title>

        <!-- CSS -->
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:400,100,300,500">
        <link rel="stylesheet" href="assets2/bootstrap/css/bootstrap.min.css">
        <link rel="stylesheet" href="assets2/fontawesome/css/all.css">   
		<link rel="stylesheet" href="assets2/css/form-elements.css">
        <link rel="stylesheet" href="assets2/css/style.css">

        <!-- Favicon and touch icons -->
        <link rel="shortcut icon" href="assets2/ico/wepayyourcost.png">
        <link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets2/ico/apple-touch-icon-144-precomposed.png">
        <link rel="apple-touch-icon-precomposed" sizes="114x114" href="assets2/ico/apple-touch-icon-114-precomposed.png">
        <link rel="apple-touch-icon-precomposed" sizes="72x72" href="assets2/ico/apple-touch-icon-72-precomposed.png">
        <link rel="apple-touch-icon-precomposed" href="assets2/ico/apple-touch-icon-57-precomposed.png">

    <style>
        .card{
    	background-color: #fff;
    	width: 300px;
    	border-radius: 15px;
    	box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
        }
        .name{
        	font-size: 15px;
        	color: #403f3f;
        	font-weight: bold;
        }
        .cross{
        	font-size: 11px;
        	color: #b0aeb7;
        }
        .pin{
        	font-size: 14px;
        	color: #b0aeb7;
        }
        .first{
        	border-radius: 8px;
        	border: 1.5px solid #78b9ff;
        	color: #000;
        	background-color: #eaf4ff;
        }
        .second{
        	border-radius: 8px;
        	border: 1px solid #acacb0;
        	color: #000;
        	background-color: #fff;
        }
        .dot{
        
        }
        .head{
        	color: #137ff3;
        	font-size: 12px;
        }
        .dollar{
        	font-size: 18px;
        	color: #097bf7;
        }
        .amount{
        	color: #007bff;
        	font-weight: bold;
        	font-size: 18px;
        
        }
    
        .back{
        	color: #aba4a4;
        	font-size: 15px;
        	line-height: 73px;
        	font-weight: 400;
        }
        
        .anim {  
            transition: background 5s linear;
        }
    
        .highlight{
            background: #B3CEE5; 
        }
    
        .normal {  
            background: transparent;  
        }
    
    </style>


    </head>

    <body>

        <!-- Top content -->
        <div class="top-content">
        	
            <div class="inner-bg">
                <div class="container">

                    <div class="row">
                        <div class="col-sm-6 col-sm-offset-3 form-box">
<?php

    if (isset($_POST['redeem'])) {
        
        $redeemCat_merchantID   =   mysqli_real_escape_string($con, $_POST['merchant_id'        ]);
        $redeemCat_membersID    =   mysqli_real_escape_string($con, $_SESSION['aidi'            ]);
        $redeem_totalRedeemRow  =   $_POST['totalRedeemRow'     ];
        $redeem_sum_value_row   =   $_POST['sum_value_row'      ];
        $redeemCat              =   $_POST['redeem_cat'         ];
        $i = 0;

					// Detail merchant
					$merchant   = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM merchant WHERE id='$redeemCat_merchantID' "));
					$m_nama     = $merchant[1];
					$m_roc      = $merchant[2];
					$m_alamat   = $merchant[3];
					$m_bandar   = $merchant[4];
					$m_poskod   = $merchant[5];
					$m_negeri   = $merchant[6];
					$m_telefon  = $merchant[8];
					$m_email    = $merchant[9];
					
					// Detail members
					$merchant   = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM ahli WHERE id='$redeemCat_membersID' "));
					$nama     = $merchant[1];
					$alamat   = $merchant[3];
					$bandar   = $merchant[4];
					$poskod   = $merchant[5];
					$negeri   = $merchant[6];
					$telefon  = $merchant[8];
					$email    = $merchant[9];	

                    // Receipt Number
                    $Receipt_sql    = "SELECT * FROM mwallet WHERE merchant_id = '$redeemCat_merchantID' ";
                    if (    $receiptresult = mysqli_query($con,$Receipt_sql)   )    {
                            $receiptcount       = mysqli_num_rows($receiptresult) + 1;
                            $str_length         = 6;
                            $strreceiptcount    = substr("000000{$receiptcount}", -$str_length);
                            $receiptNumber      = $redeemCat_merchantID.$strreceiptcount; 
                    }
                    
                    
		            date_default_timezone_set("Asia/Kuala_Lumpur");
		            $tarikh = date("Y-m-d H:i:s");


				
					
?>  
                            <div class="form-bottom" style="background-color : #FFFFFF; opacity: 0.9; border-radius: 30px;" >
                                    <div class="form-group text-center bg-secondary" style="border-width:1px; margin: auto;  width: 50%; display:table;    align-items: center;    ">
                                        <div style="display:table-cell;  vertical-align:middle;  text-align:center;">
                                            <img src="wepaylogo.png" width="360" height="auto" alt="WePayYourCost">
                                        </div>
                                    </div>
                                    <div class="form-group text-center bg-secondary" style="border-width:1px; margin: auto;  width: 50%; display:table;    align-items: center;    ">
                                        <span style="font-size: 15px;	display: block; color: #403f3f;	font-weight: light;"> <label class="small">Receipt No :</label> <?= $receiptNumber; ?></span>
                                    </div>                                        
                                    
                                    

                               
                                    <div class="form-group">
                                        <hr>
                                            <div class="form-group col-md-8" style="display:inline-block;">
                                                <span style="font-size: 15px;	color: #403f3f;	font-weight: light;"> <label class="small">Date : </label> <?= $tarikh ?></span>
                                            </div>
                                            <div class="form-group col-md-4" style="display:inline-block;">
                                                <span style="font-size: 15px;	color: #403f3f;	font-weight: light;"> <label class="small">Status : </lable></span><span class="text-success"> Success</span>
                                            </div>
                                            

                                            <div class="form-group text-left col-md-12 small" style="pading-bottom:100px; display:block;">
                                            <span class="small text-left" style="font-size: 15px;	color: #403f3f;	font-weight: light;">
                                                <label>Merchant : </lable>
                                            </span>
<?php                                       
                                                echo "<span style='display:block; line-height: normal;'>".$m_nama." <small>(".$m_roc.")</small></span>";
                                                echo "<span style='display:block; line-height: normal;'>".$m_alamat."</span>";
                                                echo "<span style='display:block; line-height: normal;'><div style='display:block'>".$m_poskod."</div><div style='display:block'>".$m_bandar."</div></span>";
                                                echo "<span style='display:block; line-height: normal;'>".$m_negeri."</span>";
                                                echo "<span style='display:block; line-height: normal;'><div style='display:block'>".$m_telefon."   <div style='display:block'>".$m_email."</span>";
?>                                             
                                                                                                         
                                            </div>


        			                       
        			                        </br>
                                            <div class="form-group">
                                                <table class="table" border="0">
                                                    <thead >
                                                        <tr class="small">
                                                            <th scope="col"                     >Items</th>
                                                            <th scope="col" class="text-right"  >Amount (RM)</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                      <input name="totalRedeemRow"  type="hidden" value="0" id="redeemRowID"/>
    <?php
                                                            $redeemData = "";
                                                            while ($i < $redeem_totalRedeemRow)    {
                                                                $i++;
                                                                    if (isset($_POST['total_carwash_'.$i]))         {
                                                                        echo "<tr><td>Carwash".$i." </td>           <td class='text-right'> ".$_POST['total_carwash_'.$i]."</td></tr>";
                                                                        $redeemData         = $redeemData.',"Carwash"*"'.$_POST['total_carwash_'.$i].'"';
                                                                        $keluar_carwash     = $_POST['total_carwash_'.$i];
                                                                        // carwash
                                                    					    mysqli_query($con, "INSERT INTO cwallet_carwash (tarikh, keterangan, rujukan, keluar, userid)
                                                    					    VALUES ('".$tarikh."', 'Redeem Carwash Bonus RM ".$keluar_carwash."', '".$m_nama."', ".$keluar_carwash.", ".$redeemCat_membersID.")");
                                                                    }
                                                                    if (isset($_POST['total_polish_'.$i]))          {
                                                                        echo "<tr><td>Wax & Polish".$i." </td>      <td class='text-right'> ".$_POST['total_polish_'.$i]."</td></tr>";
                                                                        $redeemData         = $redeemData.',"Car Wax & Polish"*"'.$_POST['total_polish_'.$i].'"';
                                                                        $keluar_polish      = $_POST['total_polish_'.$i];
                                                    					// car wax & polish
                                                    					    mysqli_query($con, "INSERT INTO cwallet_travel (tarikh, keterangan, rujukan, keluar, userid)
                                                    					    VALUES ('".$tarikh."', 'Redeem Car Wax Polish Bonus RM ".$keluar_polish."', '".$m_nama."', ".$keluar_polish.", ".$redeemCat_membersID.")");
                                                                   }
                                                                    if (isset($_POST['total_repair_'.$i]))          {
                                                                        echo "<tr><td>Car Repair".$i." </td>        <td class='text-right'> ".$_POST['total_repair_'.$i]."</td></tr>";
                                                                        $redeemData = $redeemData.',"Car Repair"*"'.$_POST['total_repair_'.$i].'"';
                                                                        $keluar_repair  = $_POST['total_repair_'.$i];
                                                    					// car repair
                                                    					    mysqli_query($con, "INSERT INTO cwallet_cash (tarikh, keterangan, rujukan, keluar, userid)
                                                    					    VALUES ('".$tarikh."', 'Redeem Car Repair Bonus RM ".$keluar_repair."', '".$m_nama."', ".$keluar_repair.", ".$redeemCat_membersID.")");
                                                                    }
                                                                    if (isset($_POST['total_accessories_'.$i]))     {
                                                                        echo "<tr><td>Car Accessories".$i." </td>   <td class='text-right'> ".$_POST['total_accessories_'.$i]."</td></tr>";
                                                                        $redeemData = $redeemData.',"Car Accessories"*"'.$_POST['total_accessories_'.$i].'"';
                                                                        $keluar_accessories  = $_POST['total_accessories_'.$i];
                                                                        // car accessories
                                                    					    mysqli_query($con, "INSERT INTO cwallet_accessories  (tarikh, keterangan, rujukan, keluar, userid)
                                                    					    VALUES ('".$tarikh."', 'Redeem Car Accessories Bonus RM ".$keluar_accessories."', '".$m_nama."', ".$keluar_accessories.", ".$redeemCat_membersID.")");
                                                                    }
                                                                    if (isset($_POST['total_insurance_'.$i]))       {
                                                                        echo "<tr><td>Car Insurance".$i." </td>     <td class='text-right'> ".$_POST['total_insurance_'.$i]."</td></tr>";
                                                                        $redeemData = $redeemData.',"Car Insurance"*"'.$_POST['total_insurance_'.$i].'"';
                                                                        $keluar_insurance  = $_POST['total_insurance_'.$i];
                                                                        // car insurance
                                                    					    mysqli_query($con, "INSERT INTO cwallet_insurance  (tarikh, keterangan, rujukan, keluar, userid)
                                                    					    VALUES ('".$tarikh."', 'Redeem Car Insurance Bonus RM ".$keluar_insurance."', '".$m_nama."', ".$keluar_insurance.", ".$redeemCat_membersID.")");         
                                                                    }
                                                            }
                                                            
    ?>
                                                        <tr >
                                                            <td class="small text-right"><span >Total  (RM)</span></td>
                                                            <td class="text-right col-md-3">
                                                                <strong><?= number_format((float)$_POST['sum_value_row'      ], 2, '.', ''); ?></strong>
                                                            </td>
                                                        </tr>
                                                        
                                                    </tbody>    
                                                </table>
                                            </div>
        			                        <hr>
<?php
				$keterangan = '"'.number_format((float)$_POST['sum_value_row'      ], 2, '.', '').'"'.$redeemData;
				$jumlah     = number_format((float)$_POST['sum_value_row'      ], 2, '.', '');
				//echo $jumlah;
				// Bayaran kepada merchant
				mysqli_query($con, "INSERT INTO mwallet (tarikh, keterangan, merchant_id, masuk, userid, receipt, status)
				VALUES ('$tarikh', '$keterangan', '$redeemCat_merchantID', '$jumlah', '$redeemCat_membersID', '$receiptNumber', 'Successful')");

					/* registration log for admin
					1 - pin
					2 - cash
					3 - withdraw
					*/
				//	$senarai = $penaja.",".$penaja1.",".$penaja2.",".$penaja3.",".$penaja4; 
				//	$sql = "SELECT GROUP_CONCAT(username) FROM `ahli` WHERE id IN (".$senarai.")";
				//	$row = mysqli_fetch_array(mysqli_query($con, $sql));

				//	$output = logSub($tarikh, 2, $usrnm." account registration. Bonus for ".$row[0], $jumlah);
				//	if (!$output) {
				//		echo mysqli_error($con);
				//	}		


?>
                                            <a href="receipt.php?r=<?= $receiptNumber ?>" class="btn btn-info" role="button" target="_blank">
                                                <i class="fa-solid fa-print fa-1x"> </i>  Print 
                                            </a>
        			                        
        			                </div>
			                    
                            </div>


<?php
    } 
    
    if (isset($_GET['r'])) {
        
        $receiptID              =   $_GET['r'];
        $i = 0;

                    // Receipt Number
                    $receipt        = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM mwallet WHERE receipt = '$receiptID' "));
                    $id             = $receipt[0];
                    $tarikh	        = $receipt[1];
                    $keterangan	    = $receipt[2];
                    $merchant_id	= $receipt[3];
                    $keluar	        = $receipt[4];
                    $masuk	        = $receipt[5];
                    $userid         = $receipt[6];
                    $receipt	    = $receipt[7];   
                    $status         = $receipt[8];
                    
					// Detail merchant
					$merchant   = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM merchant WHERE id='$merchant_id' "));
					$m_nama     = $merchant[1];
					$m_roc      = $merchant[2];
					$m_alamat   = $merchant[3];
					$m_bandar   = $merchant[4];
					$m_poskod   = $merchant[5];
					$m_negeri   = $merchant[6];
					$m_telefon  = $merchant[8];
					$m_email    = $merchant[9];
					
					// Detail members
					$members    = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM ahli WHERE id='$userid' "));
					$nama       = $members[1];
					$alamat     = $members[3];
					$bandar     = $members[4];
					$poskod     = $members[5];
					$negeri     = $members[6];
					$telefon    = $members[8];
					$email      = $members[9];	

?>  
                            <div class="form-bottom" style="background-color : #FFFFFF; opacity: 0.9; border-radius: 30px;" >
                                    <div class="form-group text-center bg-secondary" style="border-width:1px; margin: auto;  width: 50%; display:table;    align-items: center;    ">
                                        <div style="display:table-cell;  vertical-align:middle;  text-align:center;">
                                            <img src="wepaylogo.png" width="360" height="auto" alt="WePayYourCost">
                                        </div>
                                    </div>
                                    <div class="form-group text-center bg-secondary" style="border-width:1px; margin: auto;  width: 50%; display:table;    align-items: center;    ">
                                        <span style="font-size: 15px;	display: block; color: #403f3f;	font-weight: light;"> <label class="small">Receipt No :</label> <?= $receiptID; ?></span>
                                    </div>                                        
                                    
                                    

                               
                                    <div class="form-group">
                                        <hr>
                                            <div class="form-group col-md-8" style="display:inline-block;">
                                                <span style="font-size: 15px;	color: #403f3f;	font-weight: light;"> <label class="small">Date : </label> <?= $tarikh ?></span>
                                            </div>
                                            <div class="form-group col-md-4" style="display:inline-block;">
                                                <span style="font-size: 15px;	color: #403f3f;	font-weight: light;"> <label class="small">Status : </lable></span><span class="text-success"> Success</span>
                                            </div>
                                            

                                            <div class="form-group text-left col-md-12 small" style="pading-bottom:100px; display:block;">
                                            <span class="small text-left" style="font-size: 15px;	color: #403f3f;	font-weight: light;">
                                                <label class="small">Merchant : </lable>
                                            </span>
<?php                                       
                                                echo "<span style='display:block; line-height: normal;'>".$m_nama." <small>(".$m_roc.")</small></span>";
                                                echo "<span style='display:block; line-height: normal;'>".$m_alamat."</span>";
                                                echo "<span style='display:block; line-height: normal;'><div style='display:block'>".$m_poskod."</div><div style='display:block'>".$m_bandar."</div></span>";
                                                echo "<span style='display:block; line-height: normal;'>".$m_negeri."</span>";
                                                echo "<span style='display:block; line-height: normal;'><div style='display:block'>".$m_telefon."   <div style='display:block'>".$m_email."</span>";
?>                                             
                                                                                                         
                                            </div>


        			                       
        			                        </br>
                                            <div class="form-group">
                                                <table class="table" border="0">
                                                    <thead >
                                                        <tr class="small">
                                                            <th scope="col"                     >Items</th>
                                                            <th scope="col" class="text-right"  >Amount (RM)</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
<?php
                                                            $data           = explode (',',$keterangan);
                                                            $data_count     = count($data);
                                                            
                                                            $j  = 0;
                                                            
                                                            while ($j < $data_count)    {
                                                                $j++;   
                                                                $k = 0;
                                                                
                                                                $items          = explode ('*',$data[$j]);
                                                                $items_count    = count($items);
                                                                
                                                                
                                                                    while ($k < $items_count)    {
                                                                        if ($k == 0)    {
                                                                            echo "<tr><td >".str_replace('"','',$items[0])."</td>";
                                                                        }   else    {
                                                                            echo "<td class='text-right'>".str_replace('"','',$items[1])."</td></tr>";
                                                                        }
                                                                        $k++;    
                                                                    }
                                                                
                                                            }
?>
                                                        <tr >
                                                            <td ></td>
                                                            <td class="text-right ">
                                                                <strong>
                                                                    <span style="padding-right:20px;"><small>Total  (RM)</small></span>
                                                                    <?= $masuk ?>
                                                                </strong>
                                                            </td>
                                                        </tr>
                                                        
                                                    </tbody>    
                                                </table>
                                            </div>
        			                        <hr>
                                            <a href="receipt.php?r=<?= $receiptID ?>" class="btn btn-info" role="button" target="_blank">
                                                <i class="fa-solid fa-print fa-1x"> </i>  Print 
                                            </a>
        			                </div>
			                    
                            </div>


<?php
    } 
    
?>			 
		                    <div class="text-center">
		                        <font align="center"color="#FFF"><strong>Copyright © 2024, WePayYourCost.</strong></font>
		                    </div>
		                        
		                    
                        </div>
                    </div>
                    

                </div>
            </div>


        </div>

        <!-- Javascript -->
        <script src="assets2/js/jquery-1.11.1.min.js"></script>
        <script src="assets2/bootstrap/js/bootstrap.min.js"></script>
        <script src="assets2/js/jquery.backstretch.min.js"></script>
        <script src="assets2/js/scripts.js"></script>
    </body>
</html>

<?php
    }
?>