<?php

$order_id = $_GET['order_id'];
// Sertakan file konfigurasi database

$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'];

session_start();

?>

<!DOCTYPE html>
<html>
      <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" href="../../dist/img/logo-sm.png" />
    <title>Payment BilsPro</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">
    <style>
      input[type="radio"]:checked + label img {
            border: 2px solid rgba(200, 200, 200);
            border-radius: 5px;
            padding: 5px;
          }
        body {
          background-image: url('../../dist/img/bg-payment.jpg');
          background-size: cover;
          background-position: center;
          background-repeat: no-repeat;
      }
    </style>
  </head>
    <body>
        <br>
        <br>
          <div class="container" style="max-width: 900px;">
            <center>
              <img src="../../dist/img/logo.png" 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>
                          <!-- Modal -->
                          <div class="modal fade" id="paymentModal" tabindex="-1" aria-labelledby="paymentModalLabel" aria-hidden="true">
                            <div class="modal-dialog modal-dialog-centered modal-sm">
                              <div class="modal-content">
                                <div class="modal-header bg-warning">
                                  <img src="../../dist/img/logo_long_white.png" width="100px">
                                </div>
                                <small><small>
                                <div class="modal-body" style="background-color: rgb(240, 240, 240); text-align: left;">
                                  Total Pembayaran
                                  <h5>Rp. <?php echo number_format($result['nominal']); ?></h5>
                                  <span>Order ID : <?php echo $order_id; ?></span>
                                </div>
                                  <form method="post">
                                    <div class="modal-body" style="text-align: left; min-height: 250px;">
                                      <div class="mb-3">
                                        <label class="form-label">Virtual Account</label><br>
                                        <!-- Virtual Account -->
                                        <div class="form-check form-check-inline">
                                          <input class="form-check-input" type="radio" name="paymentMethod" id="bri" value="bri" style="display: none;">
                                          <label class="form-check-label" for="bri">
                                            <img src="../../dist/img/bri.png" width="50px" class="mb-1" alt="BRI">
                                          </label>
                                        </div>

                                        <div class="form-check form-check-inline">
                                          <input class="form-check-input" type="radio" name="paymentMethod" id="bni" value="bni" style="display: none;">
                                          <label class="form-check-label" for="bni">
                                            <img src="../../dist/img/bni.png" width="50px" class="mb-1" alt="BNI">
                                          </label>
                                        </div>

                                        <div class="form-check form-check-inline">
                                          <input class="form-check-input" type="radio" name="paymentMethod" id="mandiri" value="mandiri" style="display: none;">
                                          <label class="form-check-label" for="mandiri">
                                            <img src="../../dist/img/mandiri.png" width="50px" class="mb-1" alt="Mandiri">
                                          </label>
                                        </div><br>

                                        <!-- Qris -->
                                        <label class="form-label">Qris</label><br>
                                        <div class="form-check form-check-inline">
                                          <input class="form-check-input" type="radio" name="paymentMethod" id="qris" value="qris" style="display: none;">
                                          <label class="form-check-label" for="qris">
                                            <img src="../../dist/img/qris.png" width="50px" class="mb-1" alt="QRIS">
                                          </label>
                                        </div>
                                      </div>
                                    </div>
                                    <div class="modal-body" style="border-bottom-left-radius: 20px; border-bottom-right-radius: 20px;">
                                      <div class="d-grid gap-2">
                                        <input type="submit" name="bayar" class="btn btn-primary btn-sm btn-block" style="border-radius: 20px;" value="Bayar Sekarang">
                                      </div>
                                    </div>
                                  </form>
                                </small></small>
                              </div>
                            </div>
                          </div>
                        </center>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <center style="margin-bottom: 10px;">
                <span><small>Support By </small><img src="https://web.paydisini.co.id/assets/logo.png" width="70px"></span>
              </center>
            </div>
            <?php
            if (isset($_POST['bayar'])) { // Tambahkan tanda kurung penutup
                // Pastikan paymentMethod ada di POST
                if (isset($_POST['paymentMethod'])) {
                    // Tentukan valid_time berdasarkan paymentMethod
                    $valid_time = ($_POST['paymentMethod'] === 'qris') ? '300' : '3600';

                    // Membuat data transaksi
                    $data = [
                        'unique_code' => $order_id,
                        'service' => $_POST['paymentMethod'],
                        'amount' => $result['nominal'],
                        'note' => $result['keterangan'],
                        'valid_time' => $valid_time
                    ];

                    // Panggil fungsi untuk membuat transaksi
                    $response = CreateTransaction($data);

                    $dataCreate = json_decode($response);
                    if ($dataCreate->success === true) {
                      // Persiapkan dan eksekusi query untuk memperbarui data
                      $stmt = $pdo->prepare("UPDATE apk_payment SET data_get = :data_get, status = 'PENDING' WHERE order_id = :order_id");
                      $stmt->execute([
                          ':data_get' => $response,  // Gunakan '=>' untuk menetapkan parameter
                          ':order_id' => $order_id   // Gunakan '=>' untuk menetapkan parameter
                      ]);
                      sweetAlert("Pembayaran telah dibuat", "success");
                    } else {
                      sweetAlert("Error: Gagal buat pembayaran", "error");
                    }
                } else {
                  sweetAlert("Error: Payment method not provided.", "error");
                }
            }
            ?>
            <center>
              <div class="mt-3">
                <a href="<?= "https://" . $_SERVER['HTTP_HOST'] . "/?" . encrypt_url('setting_profile'); ?>">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>
    <script>
      document.getElementById('pay-button').addEventListener('click', function() {
        var myModal = new bootstrap.Modal(document.getElementById('paymentModal'));
        myModal.show();
      });
    </script>
  </body>
</html>
