<?php 
include('common/header.php');

if (isset($_SESSION['temp_user_cey']) && isset($_SESSION['guest_user_cey'])) {
  $user_id = htmlspecialchars(strip_tags(trim($_SESSION['temp_user_cey'])), ENT_QUOTES, 'UTF-8');
  $whr = 'tmp_id';

  if(empty($user_id)) {
    $redirectUrl = "index";
    echo "<script type=\"text/javascript\">";
    echo "window.location.href = '$redirectUrl'";
    echo "</script>";

  } else {
    $sqlt = mysqli_query($conn, "SELECT * FROM live_cart WHERE tmp_id=$user_id");
    if(mysqli_num_rows($sqlt) > 0) {
      $row_inv = mysqli_fetch_assoc($sqlt);
      $g_inv = $row_inv['inv_id'];
      $sql_up = mysqli_query($conn, "UPDATE live_cart SET User_ID='G$g_inv', inv_id='$g_inv' WHERE tmp_id=$user_id");
    }
    $guest = 1;
  }

} else {
  if (!isset($_SESSION['normal_user_cey']) || empty($_SESSION['normal_user_cey'])) {
    $redirectUrl = "sign-in";
    echo "<script type=\"text/javascript\">";
    echo "window.location.href = '$redirectUrl'";
    echo "</script>";

  } else {
    $user_id = htmlspecialchars(strip_tags(trim($_SESSION['normal_user_cey'])), ENT_QUOTES, 'UTF-8');
    $whr = 'User_ID';
    $guest = 0;
  }
}

$sql = mysqli_query($conn, "SELECT inv_id, SUM(price*Qty) AS sub_tot, SUM(price*Qty) AS item_tot FROM live_cart WHERE $whr=$user_id");
if(mysqli_num_rows($sql) == 0) {
  $redirectUrl = "index";
  echo "<script type=\"text/javascript\">";
  echo "window.location.href = '$redirectUrl'";
  echo "</script>"; 

} else {
  $row = mysqli_fetch_assoc($sql);
  $item_tot = $row['item_tot'];
  $sub_tot = $row['sub_tot'];
  $tot_discount = $item_tot-$sub_tot;
  $inv_id = $row['inv_id'];
  
  if(empty($inv_id)) {
    $redirectUrl = "index";
    echo "<script type=\"text/javascript\">";
    echo "window.location.href = '$redirectUrl'";
    echo "</script>";

  } else {
    $sqlu = "SELECT * FROM order_details_temp WHERE user_id=$user_id AND inv_id='$inv_id' AND guest=$guest";
    $resultu = mysqli_query($conn, $sqlu);
    $countu = mysqli_num_rows($resultu);

    if($countu == 0) {
      $redirectUrl = "index";
      echo "<script type=\"text/javascript\">";
      echo "window.location.href = '$redirectUrl'";
      echo "</script>";   
      
    } else {
      $sqltmp = mysqli_fetch_assoc($resultu);
      //$del_fee = $flag = 0;
      $country = 'Sri Lanka'; //$sqltmp['country'];
      
      $sql_cou = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM courier_charges"));
      $del_fee = $sql_cou['courier_fee'];
    }
  }
}

?>

<!-- Header Wrap Ends -->

<?php include "common/navbar.php"; ?> 

<!-- Main Container Starts -->
<div class="main-container" >
  <!-- Product Container Starts -->
  <div id="product-product" class="container" style="background-image: url(image/bg.jpg); ">
    <!-- Row Starts -->  
    <div class="row col-sm-12">

      <!-- Content Area Starts -->
      <div id="content" class="col-lg-12 login">

       <h3 class="page-heading-1">RECIPIENT INFORMATION</h3>

       <div class="row justify-content-lg-around position-relative pt-5">
        <div class="col-lg-12 col-md-12">
          <div class="p-3 p-md-4 bg-white shadow">
            <div class="row">
              <div class="col-md-12">
                <table width="100%" class="tablechk">
                  <tr>
                    <td width="25%"><b>Name</b></td>
                    <td width="75%"><?php echo $sqltmp['f_name'].' '.$sqltmp['l_name']; ?></td>
                  </tr>
                  <tr>
                    <td><b>Contact Number</b></td>
                    <td><?php echo $sqltmp['tel_no']; ?></td>
                  </tr>
                  <tr>
                    <td><b>Email Address</b></td>
                    <td><?php echo $sqltmp['email']; ?></td>
                  </tr>
                  <tr>
                    <td><b>Delivery Address</b></td>
                    <td><?php echo $sqltmp['address_1'].' '.$sqltmp['address_2'].' '.$sqltmp['city']; ?></td>
                  </tr>
                  <tr>
                    <td><b>City</b></td>
                    <td><?php echo $sqltmp['city']; ?></td>
                  </tr>
                  <tr>
                    <td><b>Postal Code</b></td>
                    <td><?php echo $sqltmp['postal_code']; ?></td>
                  </tr>
                  <tr>
                    <td><b>Special Instructions</b></td>
                    <td><?php echo $sqltmp['special_msg']; ?></td>
                  </tr>
                  <tr>
                    <td><b>Delivery Method</b></td>
                    <td><?php if($sqltmp['delivery_type'] == 1) { echo 'Postal'; } else if($sqltmp['delivery_type'] == 2) { echo 'Courier Service'; } else if($sqltmp['delivery_type'] == 3) { echo 'Expedited Mail Service (EMS)'; } else if($sqltmp['delivery_type'] == 4) { echo 'Cash on Delivery'; } $g_tot = $sub_tot + $del_fee; ?></td>
                  </tr>
                </table>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="row justify-content-lg-around position-relative pt-5">
        <div class="col-lg-12 col-md-12">
          <div class="p-3 p-md-4 bg-light shadow">
            <h5 class="pt-3 pb-2 border-bottom h-100 text-center text-dark">ORDER SUMMARY - INVOICE ID: <?php echo $inv_id; ?></h5>
            <div class="row">
              <div class="col-md-12">
                <div class="table-content wnro__table table-responsive">
                  <table width="100%" class="table table-bordered">
                    <tr class="title-top">
                      <th width="35%">Product Name</th>
                      <th width="15%">Unit Price <br>(LKR)</th>
                      <th width="15%">Quantity</th>
                      <th width="15%">Subtotal  <br>(LKR)</th>
                    </tr>
                    <?php
                    $sql_c = mysqli_query($conn, "SELECT * FROM live_cart WHERE live_cart.$whr =  $user_id");
                    while($row_c = mysqli_fetch_array($sql_c)) { ?>
                      <tr>
                        <td class="bname"><?php echo $row_c['name']; ?></td>
                        <td align="right"><?php echo $row_c['price']; ?></td>
                        <td align="right"><?php echo $row_c['Qty']; ?></td>
                        <td align="right"><?php $subtot = $row_c['price']*$row_c['Qty']; echo number_format($subtot, 2); ?></td>
                      </tr>
                    <?php }
            // Update Grand Total
                    mysqli_query($conn, "UPDATE order_details_temp SET price_total='$item_tot', tot_discount='$tot_discount', sub_total='$sub_tot', delivery_chg='$del_fee', grand_total='$g_tot' WHERE user_id='$user_id' AND inv_id='$inv_id' AND guest=$guest");
                    ?>
                    <tr>
                      <td colspan="3" align="right">
                        <h6 class="my-2">Cart Total</h6>
                        <h6>Delivery Charges </h6>
                        <h4 class="text-primary text-uppercase mt-3 mb-0">GRAND Total</h4>
                      </td>
                      <td colspan="2" align="right">
                        <h6 class="my-2"><?php echo number_format($sub_tot, 2); ?></h6>
                        <h6><?php echo number_format($del_fee, 2); ?></h6>
                        <h4 class="text-primary text-uppercase mt-3 mb-0">LKR <?php echo number_format($g_tot, 2); ?></h4>
                      </td>
                    </tr>
                  </table>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="form-group pt-5 m-0">
        <a href="checkout" class="btn btn-inverse"><i class="fa fa-arrow-left pr-3"></i> BACK</a>
        <a class="btn btn-inverse" href="javascript:void(0);" id="btn_order" style="float:right">CONFIRM ORDER</a>
      </div>


    </div>

  </div>
  <!-- Content Area Ends -->    

</div>
<!-- Row Ends -->  
</div>
<!-- Product Container Ends -->

</div>

<!-- <form method="post" action="https://www.payhere.lk/pay/checkout" id="form_sandbox" style="display:none">
  <input type="hidden" name="merchant_id" value="212597" readonly >-->
  <form method="post" action="https://sandbox.payhere.lk/pay/checkout" id="form_sandbox" style="display:none">  <!--SandBox URL  --> 
    <input type="hidden" name="merchant_id" value="1211943" readonly> <!-- SandBox ID -->
    <input type="hidden" name="return_url" value="localhost/sampath/order-confirm" readonly>
    <input type="hidden" name="cancel_url" value="localhost/sampath/order-cancel" readonly>
    <input type="hidden" name="notify_url" value="localhost/sampath/connection/payment_verify_online" readonly>
    <br>
    <br>
    Item Details<br>
    <input type="hidden" name="order_id" id="order_id" value="<?php echo $inv_no; ?>" readonly>
    <input type="hidden" name="items" value="Sampathbay" readonly>
    <br>
    <input type="hidden" name="currency" value="LKR" readonly>
    <input type="hidden" name="amount" id="tot_amnt" value="<?php echo $g_tot; ?>" readonly>
    <br>
    <br>
    Customer Details<br>
    <input type="hidden" name="first_name" value="<?php echo $sqltmp['f_name'] ?>" readonly>
    <input type="hidden" name="last_name" value="<?php echo $sqltmp['l_name'] ?>" readonly>
    <br>
    <input type="hidden" name="email" value="<?php echo $sqltmp['email']; ?>" readonly>
    <input type="hidden" name="phone" value="<?php echo $sqltmp['tel_no']; ?>" readonly>
    <br>
    <input type="hidden" name="address" value="<?php echo $sqltmp['address_1'].', '.$sqltmp['address_2']; ?>" readonly>
    <input type="hidden" name="city" value="<?php echo $sqltmp['city']; ?>" readonly>
    <input type="hidden" name="country" value="<?php echo $country; ?>" readonly>
    <br>
    <br>
  </form>
  <!-- Main Container Ends -->
  <!-- Footer Starts -->
  <?php include "common/footer.php"; ?> 

  <!-- Footer Ends -->
  <script type="text/javascript">
    $("#btn_order").click(function() {
      $("#pre_loader").show();
      var tot_amnt = $('#tot_amnt').val();
      if (tot_amnt > 0) {
        var inv = $("#order_id").val();
        $.post('connection/payment_confirm_bank.php', {'edit_payment_method': 'data', inv: inv, pay_type: 1}, function (data) {
          if (data.status === 'success') {
            $("#form_sandbox").submit();

          } else if (data.status === 'error') {
            alert('Something Went Wrong. Please Try Again');
            window.location.href = 'checkout';

          } else {
            window.location.href = 'sign-in';
          }
        }, "json");

      } else {
        $("#pre_loader").fadeOut("slow");
        alert('Invalid Total Amount');
      }
    });
  </script>

</body>
</html> 
