<?php
$page_title = 'List Order Background';
require_once('includes/load.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['id'])) {
  $id = $_POST['id'];
  // Prepare the SQL statement to delete the order background
  $delete_sql = "UPDATE queue_shipment SET flag_delete = 1 WHERE id = '$id'";
  // Execute the delete statement
  if ($db->query($delete_sql)) {
    $session->msg('s', "Order Background deleted successfully.");
  } else {
    $session->msg('d', 'Error: ' . $db->error);
  }
  redirect('list_proses_background.php');
}

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add_shipment'])) {
  $shop_id = $_POST['shop_id'];
  $order_sn = $_POST['order_sn'];

  // Fetch access_token and refresh_token from the shop table
  $shop_query = "SELECT access_token, refresh_token, platform_id FROM shop WHERE shop_id = ?";
  $stmt = $db->prepare($shop_query);
  $stmt->bind_param("s", $shop_id);
  $stmt->execute();
  $stmt->bind_result($access_token, $refresh_token, $platform_id);
  $stmt->fetch();
  $stmt->close();

  // Prepare the SQL statement to insert the new shipment
  $insert_sql = "INSERT INTO queue_shipment (order_sn, shop_id, access_token, refresh_token, platform_id, flag_delete) 
                 VALUES (?, ?, ?, ?, ?, 0)";

  // Prepare and bind the insert statement
  $insert_stmt = $db->prepare($insert_sql);
  $insert_stmt->bind_param("ssssi", $order_sn, $shop_id, $access_token, $refresh_token, $platform_id);

  // Execute the insert statement
  if ($insert_stmt->execute()) {
    $session->msg('s', "New Order Background added successfully.");
  } else {
    $session->msg("d", "Error: " . $insert_stmt->error());
  }
  redirect('list_proses_background.php');
}
$sql = "SELECT qs.created_at, qs.order_sn, p.platform_name, s.shop_name, u.name, qs.id FROM queue_shipment qs JOIN shop s ON qs.shop_id = s.shop_id JOIN platform p ON p.id = qs.platform_id JOIN users u ON u.id = s.users_id WHERE qs.flag_delete = 0";

$data = $db->query($sql);

?>
<?php include_once('layouts/header.php'); ?>

<div class="row">
  <div class="col-md-12">
    <?php echo display_msg($msg); ?>
  </div>
</div>
<div class="row">
  <div class="col-md-12">
    <div class="panel panel-default">
      <div class="panel-heading clearfix">
        <strong>
          <span class="glyphicon glyphicon-th"></span>
          <span>List Order Background Proses</span>
        </strong>
        <button type="button" class="btn btn-primary pull-right" data-toggle="modal" data-target="#createModal">
          Add Order Background
        </button>
      </div>
      <div class="panel-body">
        <table class="table table-bordered">
          <thead>
            <tr>
              <th>Created At</th>
              <th>Order SN</th>
              <th>Platform Name</th>
              <th>Shop Name</th>
              <th>Store Operation</th>
              <th>Actions</th>
            </tr>
          </thead>
          <tbody>
            <?php
            if ($data->num_rows > 0) {
              while ($row = $data->fetch_assoc()) {
                echo "<tr>
              <td>" . $row["created_at"] . "</td>
              <td>" . $row["order_sn"] . "</td>
              <td>" . $row["platform_name"] . "</td>
              <td>" . $row["shop_name"] . "</td>
              <td>" . $row["name"] . "</td>
              <td>
                <form method='POST' action='' style='display:inline;' onsubmit='return confirm('Are you sure you want to delete this item?')' <input type='hidden' name='id' value='" . $row["id"] . "'><input type='submit' name='delete' value='Delete' class='btn btn-danger btn-xs'></form>
              </td>
            </tr>";
              }
            } else {
              echo "<tr>
              <td colspan='6' class='text-center'>No results found</td>
            </tr>";
            } ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
<!-- Modal -->
<div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-labelledby="createModalLabel"
  aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="createModalLabel">Create New Shop</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"
          style="right: 15px;top: 15px;position:absolute">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form method="POST">
          <div class="form-group">
            <label for="shop_id">Shop ID:</label>
            <select name="shop_id" class="form-control" required>
              <?php
              $shop_sql = "SELECT shop_id, shop_name, p.platform_name FROM shop s join platform p on p.id = s.platform_id";
              $shop_result = $db->query($shop_sql);

              if ($shop_result->num_rows > 0) {
                while ($shop_row = $shop_result->fetch_assoc()) {
                  echo "<option value='" . $shop_row["shop_id"] . "'>" . $shop_row["platform_name"] . " - " . $shop_row["shop_name"] . "</option>";
                }
              }
              ?>
            </select>
          </div>
          <div class="form-group">
            <label for="order_sn">Nomor Pesanan:</label>
            <p>Bukan Nomor Resi</p>
            <input type="text" name="order_sn" class="form-control" required>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <input type="submit" name="add_shipment" value="Submit" class="btn btn-primary">
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
<script>
  $(document).ready(function() {
    var table = $('.table').DataTable({});
  });
</script>
<?php include_once('layouts/footer.php'); ?>