<?php

namespace Midtrans;
require_once dirname(__FILE__) . '/midtrans/Midtrans.php';
// Set Your server key
// can find in Merchant Portal -> Settings -> Access keys
Config::$serverKey = $serverKeyApi;

// non-relevant function only used for demo/example purpose
printExampleWarningMessage();


// $order_id = $_GET['id'];

Config::$isProduction = true;
Config::$isSanitized = true; 
Config::$is3ds = true;


// $orderId = '645977615';
// Get transaction status to Midtrans API
$status = '';
try {
    $status = Transaction::status($order_id);
} catch (\Exception $e) {
    echo $e->getMessage();
    die();
}

$transaction = $status->transaction_status;

function printExampleWarningMessage() {
    if (strpos(Config::$serverKey, 'your ') != false ) {
        echo "<code>";
        echo "<h4>Please set your server key from sandbox</h4>";
        echo "In file: " . __FILE__;
        echo "<br>";
        echo "<br>";
        echo htmlspecialchars('Config::$serverKey = \'<your server key>\';');
        die();
    } 
}

// Sertakan file konfigurasi database
include '../config/connect.php';
// Query untuk mengambil data dari tabel apk_payment dan data_server
$stmt = $pdo->prepare("SELECT apk_payment.id,apk_payment.status,apk_payment.expaired_time,apk_payment.nominal, apk_payment.keterangan, data_server.nama_pemilik, data_server.nama_server, data_server.username, data_server.nomor_whatsapp
    FROM apk_payment
    LEFT JOIN data_server ON apk_payment.id_data_server = data_server.id
    WHERE apk_payment.order_id = :order_id");
$stmt->bindParam(':order_id', $order_id);
$stmt->execute();
// Ambil satu baris data hasil query
$result = $stmt->fetch();
    // Ambil data dari hasil query
    $amount = $result['nominal'];
    $name = $result['keterangan'];
    $first_name = $result['nama_pemilik'];
    $last_name = '('.$result['nama_server'].')';
    $email = $result['username'].'@mail.com'; // Mengganti $mail.com dengan @mail.com
    $phone = $result['nomor_whatsapp'];

session_start();

?>

<!--<!DOCTYPE html>-->
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>BilsPro</title>
    <link rel="icon" href="../dist/img/logo-sm.png" />
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
  </head>
    <body>
        <br>
        <br>
          <div class="container" style="width: 55%;">
            <center>
              <img src="../dist/img/logo.jpg" width="230px" class="mb-4">
            </center>
            <div class="card">
              <div class="card-header bg-warning">
                <h5>Detail Pembayaran</h5>
              </div>
              <div class="card-body">
                <div class="row">
                  <div class="col-md-6">
                    <table class="table table-borderless">
                      <tbody>
                        <tr>
                          <td>Nama Usaha</td>
                          <td><b><?php echo $result['nama_server']; ?></b></td>
                        </tr>
                        <tr>
                          <td>Nama Pemilik</td>
                          <td><b><?php echo $result['nama_pemilik']; ?></b></td>
                        </tr>
                        <tr>
                          <td>Telepon</td>
                          <td><b><?php echo $result['nomor_whatsapp']; ?></b></td>
                        </tr>
                        <tr>
                          <td>Keterangan</td>
                          <td><b><?php echo $result['keterangan']; ?></b></td>
                        </tr>
                        <tr>
                          <td>Expaired Date</td>
                          <td><h5 style="color: orange;"><?php echo $result['expaired_time']; ?></h5></td>
                        </tr>
                      </tbody>
                    </table>
                  </div>
                  <div class="col-md-6">
                    <div class="card">
                      <div class="card-body">
                        <center>
                          <p class="m-0">Status Pembayaran</p>
                          <h3 class="mt-2" style="color: orange;"><b><?php echo $result['status']; ?></b></h3>
                        </center>
                        <hr>
                        <table class="table table-borderless">
                          <tbody>
                            <tr>
                              <td>Total</td>
                              <td style="text-align: right;"><h2><?php echo number_format($result['nominal']); ?></h2></td>
                            </tr>
                          </tbody>
                        </table>
                        <hr>
                        <center>
                          <?php 
                          if ($status->transaction_status == 'pending') {
                            if ($status->payment_type == 'bank_transfer') {
                              $va = $status->va_numbers;
                              echo "Virtual Account ".strtoupper($va[0]->bank);
                              echo "<h4>".$va[0]->va_number."</h4>";
                              echo "<hr>";
                              echo "Copy virtual account di atas untuk melakukan pembayaran";
                            }
                            else {
                              try {
                                  $cancel = Transaction::cancel($order_id);
                              } catch (\Exception $e) {
                                  echo $e->getMessage();
                                  die();
                              }
                              function generateOrderId($pdo) {
                                  do {
                                      $order_id = sprintf('%013d', mt_rand(0, 9999999999999)); // Generate 13 digit number
                                      $stmt = $pdo->prepare("SELECT COUNT(*) FROM apk_payment WHERE order_id = :order_id");
                                      $stmt->bindParam(':order_id', $order_id);
                                      $stmt->execute();
                                      $exists = $stmt->fetchColumn();
                                  } while ($exists > 0);
                                  return $order_id;
                              }

                              $order_id = generateOrderId($pdo);
                              $stmt = $pdo->prepare("UPDATE apk_payment SET status = 'BELUM DIBAYAR', order_id = '$order_id' WHERE id = :id");
                              $stmt->bindParam(':id', $result['id']);
                              $stmt->execute();
                              header('Location: ../index.php?c2V0dGluZ19wcm9maWxl');
                              exit();
                            }
                          }
                          elseif ($status->transaction_status == 'settlement') {
                            echo '<img src="../dist/img/tick.gif" width="40%">';
                            echo "<p>Pembayaran Berhasil</p>";
                          }
                          ?>
                        </center>
                      </div>
                    </div>
                  </div>
                </div>  
              </div>
            </div>
            <center>
              <div class="mt-3">
                <a href="../index.php?c2V0dGluZ19wcm9maWxl">Kembali ke Dashboard</a>
              </div>
            </center>
          </div>
          
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script>
  </body>
</html>