<?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 {
    require "conn.php";
    $title          = 'Print Receipt';
    $certTITLE      = $title; 
} 
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>WePayYourCost | Receipt <?= $_GET['r'] ?></title>

    <link rel="shortcut icon" href="assets2/ico/wepayyourcost.png">
    <!-- BOOTSTRAP STYLES-->
    <link href="assets/css/bootstrap.css" rel="stylesheet" />
     <!-- FONTAWESOME STYLES-->
    <link rel="stylesheet" href="/assets2/fontawesome/css/all.css">  
<!--    <link href="assets/css/font-awesome.css" rel="stylesheet" />    -->
     <!-- MORRIS CHART STYLES-->
    <link href="assets/js/morris/morris-0.4.3.min.css" rel="stylesheet" />
        <!-- CUSTOM STYLES-->
    <link href="assets/css/custom.css" rel="stylesheet" />


     <!-- Google Font: Source Sans Pro -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">

<style>
    @media print {
   body {
      -webkit-print-color-adjust: exact;
   }
}
</style>

</head>
<body>
<div class="wrapper">
  <!-- Main content -->
  
<?php
    
    $receipt_id     = $_GET['r'];
    $result = mysqli_query($con, "SELECT * FROM mwallet WHERE receipt='".$receipt_id."'");
    $rows   = mysqli_fetch_array($result);  

    $resultCheck    = mysqli_num_rows($result);


	if ($resultCheck < 1) {
?>
            <div class="row">
                <div class="col-12">
                    <div class="callout callout-info">
                        <h5><i class="fas fa-info"></i> <span style="padding-left:20px;">Sorry, no record found. </span></h5>
                    </div>
                </div><!-- /.col -->
            </div><!-- /.row -->
             <!-- /.container-fluid -->
<?php	    
	} else  {

?>


   


                    <!-- Main content -->
    <section class="invoice">
        <div class="p-3 mb-3">
                      <!-- title row -->
            <div class="inner-bg">
                <div class="container">
                    <div class="row">                      
                        <div class="col-12 bg-secondary" >
                                                        <!-- Table row -->
                            <div class="row pt-5" style="position: relative;  text-align: center;">

                                
                                      <div class="form-group col-md-12" style="position: absolute;  top: 30px; width: 100%;" >
<?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="col-md-12">
                                            <div class="form-group text-left" style="display:inline-block; padding-right:30px;">
                                                <span style="font-size: 15px;	color: #403f3f;	font-weight: light;"> <label class="small">Date : </label> <?= $tarikh ?></span>
                                            </div>
                                            <div class="form-group text-right" style="display:inline-block; padding-left:30px;">
                                                <span style="font-size: 15px;	color: #403f3f;	font-weight: light;"> <label class="small">Status : </lable></span><span class="text-success"> Success</span>
                                            </div>
                                        </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>
        			                </div>
                            </div>


<?php
    }
?>                                    </div>
                            </div>
                        </div>
                      
                       <footer style="position: fixed; padding: 10px 10px 0px 10px; bottom: 0; width: 100%; height: 100%px; ">
                                      <div class="row">
                                      <!-- info row -->
                                      <div class="row invoice-info pt-5 text-center">
                                        <div class="col-sm-12 invoice-col">
                                          <h4><span >----------  Thank You   ----------  </span></h4> <br>
                                        </div>
                                        <!-- /.col -->
                                      </div>
                                        </div>
                                        <!-- /.col -->
                      </footer>    
                      <!-- /.row -->

                      <!-- this row will not appear when printing -->
                    </div>
                </div>
            </div>
        </div>
    </section> 
  <!-- /.content -->
  
 <?php
	}
 ?>
</div>
<!-- ./wrapper -->
<!-- Page specific script -->
<script>
  window.addEventListener("load", window.print());
</script>
</body>
</html>