<?php
// This is just for very basic implementation reference, in production, you should validate the incoming requests and implement your backend more securely.
// Please refer to this docs for snap popup:
// https://docs.midtrans.com/en/snap/integration-guide?id=integration-steps-overview

namespace Midtrans;

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

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

// Uncomment for production environment
Config::$isProduction = true;
Config::$isSanitized = true; 
Config::$is3ds = true;

// $order_id = $_GET['id'];
// 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.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'];

// Required
$transaction_details = array(
    'order_id' => $order_id,
    'gross_amount' => $amount, // no decimal allowed for creditcard
);
// Optional
$item_details = array (
    array(
        'id' => 'a1',
        'price' => $amount,
        'quantity' => 1,
        'name' => $name
    ),
  );
// Optional
$customer_details = array(
    'first_name'    => $first_name,
    'last_name'     => $last_name,
    'email'         => $email,
    'phone'         => $phone
);
// Fill transaction details
$transaction = array(
    'transaction_details' => $transaction_details,
    'customer_details' => $customer_details,
    'item_details' => $item_details,
);

$snap_token = '';
try {
    $snap_token = Snap::getSnapToken($transaction);
}
catch (\Exception $e) {
    $status = $e->getMessage();
}
// echo "snapToken = ".$snap_token;

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();
    } 
}
session_start();

?>

<!DOCTYPE html>
<html>
      <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>PAYMENT </title>
    <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">
                        <table class="table table-borderless">
                          <tbody>
                            <tr>
                              <td>Total</td>
                              <td style="text-align: right;"><h1><?php echo number_format($result['nominal']); ?></h1></td>
                            </tr>
                          </tbody>
                        </table>
                        <hr>
                        <center>
                        <p>Silahkan pilih metode pembayaran untuk melanjutkan transaksi</p>
                          <button id="pay-button" class="btn btn-primary">PILIH METODE PEMBAYARAN</button>
                        </center>
                      </div>
                    </div>
                  </div>
                </div>

                <!-- TODO: Remove ".sandbox" from script src URL for production environment. Also input your client key in "data-client-key" -->
                <script src="https://app.midtrans.com/snap/snap.js" data-client-key="<?php echo Config::$clientKey;?>"></script>
                <script type="text/javascript">
                    document.getElementById('pay-button').onclick = function(){
                        // SnapToken acquired from previous step
                        snap.pay('<?php echo $snap_token?>');
                    };
                </script>
              </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>