<p class='sidebar-title text-danger produk-title'> Laporan Data Pesanan Anda</p>
<?php
if ($this->uri->segment(3) == 'success') {
  echo "<div class='alert alert-success'><b>SUCCESS</b> - Terima kasih telah Melakukan Konfirmasi Pembayaran!</div>";
} elseif ($this->uri->segment(3) == 'orders') {
  echo "<div class='alert alert-success'><b>SUCCESS</b> - Orderan anda sukses terkirim, silahkan melakukan pembayaran ke rekening reseller pesanan anda dan selanjutnya lakukan konfirmasi pembayaran!</div>";
}
?>
<table id='example2' style='overflow-x:scroll; width:96%' class="table table-striped table-condensed">
  <thead>
    <tr>
      <th width="20px">No</th>
      <th>Kode Transaksi</th>
      <th>Nama Lapak</th>
      <th>Subtotal</th>
      <th>Ongkir</th>
      <th>Status</th>
      <th>Total + Ongkir</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <?php
    $no = 1;
    foreach ($record->result_array() as $row) {
      if ($row['proses'] == '0') {
        $proses = '<i class="text-danger">Pending</i>';
      } elseif ($row['proses'] == '1') {
        $proses = '<i class="text-success">Proses</i>';
      } elseif ($row['proses'] == '2') {
        $proses = '<i class="text-info">Konfirmasi</i>';
      } elseif ($row['proses'] == '3') {
        $proses = '<i class="text-info">Diterima</i>';
      }
      $total = $this->db->query("SELECT sum((a.harga_jual*a.jumlah)-a.diskon) as total FROM rb_penjualan_detail a where a.id_penjualan='$row[id_penjualan]'")->row_array();


      // Apply 10% discount if total is greater than or equal to 500000
      $discount = 0;
      if ($total['total'] >= 500000) {
        $discount = 0.1 * $total['total'];
      }
      $final_total = $total['total'] - $discount;

      echo "<tr><td>$no</td>
                          <td><span class='text-success'>$row[kode_transaksi]</span></td>
                          <td><a href='" . base_url() . "members/detail_reseller/$row[id_reseller]'>$row[nama_reseller]</a></td>
                          <td><span style='color:blue;'>Rp " . rupiah($final_total) . "</span></td>
                          <td><i style='color:green;'><b style='text-transform:uppercase'>$row[kurir]</b> - Rp " . rupiah($row['ongkir']) . "</i></td>
                          <td>$proses</td>
                          
                          <td style='color:red;'>Rp " . rupiah($final_total + $row['ongkir']) . "</td>
                          <td width='130px'>";
      if ($row['proses'] == '0') {
        echo "<a style='margin-right:3px' class='btn btn-success btn-xs' title='Konfirmasi Pembayaran' href='" . base_url() . "konfirmasi?kode=$row[kode_transaksi]'>Konfirmasi</a>";
      } else {
        echo "<a style='margin-right:3px' class='btn btn-default btn-xs' href='" . base_url() . "konfirmasi?kode=$row[kode_transaksi]'  onclick=\"return confirm('Maaf, Pembayaran ini sudah dikonfirmasi, ingin konfirmasi ulang? ')\">Konfirmasi</a>";
      }

      echo "<a class='btn btn-info btn-xs' title='Detail data pesanan' href='" . base_url() . "members/keranjang_detail/$row[id_penjualan]'><span class='glyphicon glyphicon-search'></span></a></td>
      <td>";
      if ($row['proses'] == '2') {
        echo "<button type='button' class='btn btn-primary' onclick='updateDatabase(\"$row[kode_transaksi]\")'>Selesaikan Pesanan</button>";
      } elseif ($row['proses'] == '3') {
        echo "<button type='button' class='btn btn-warning')'>Pesanan diterima</button>";
      }
      echo "
       </td>
                      </tr>";
      $no++;
    }
    ?>


  </tbody>
</table>

<script>
  function updateDatabase(kode_transaksi) {
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '', true);
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4 && xhr.status == 200) {
        alert('Pesanan Selesai Untuk kode transaksi: ' + kode_transaksi);
        location.reload(); // Reload the page to see the changes
      }
    };

    xhr.send('action=update&kode_transaksi=' + kode_transaksi);
  }
</script>

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action']) && $_POST['action'] == 'update') {
  $kode_transaksi = $_POST['kode_transaksi'];

  // Koneksi ke database (ganti dengan informasi koneksi Anda)
  $servername = "localhost";
  $username = "dbditsco_bersyukur";
  $password = "Bersyukur_123_123_";
  $dbname = "dbditsco_marketpong21";

  $conn = new mysqli($servername, $username, $password, $dbname);

  if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  }

  // Kueri update ke database
  $result = $conn->query("SELECT proses FROM rb_penjualan WHERE kode_transaksi = '$kode_transaksi'");
  if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    if ($row['proses'] == '3') {
      echo 'already_updated';
    } else {
      // Kueri update ke database
      $sql = "UPDATE rb_penjualan SET proses = '3' WHERE kode_transaksi = '$kode_transaksi'";
      if ($conn->query($sql) === TRUE) {
        echo 'update_successful';
      } else {
        echo "Error updating record: " . $conn->error;
      }
    }
  }


  $conn->close();
}
?>