<?php
require_once('includes/load.php');
page_require_level(1);
$page_title = 'List Pelanggan';
// Ambil data pelanggan
$data = find_all('pelanggan');

// Hapus data jika ada request delete
if (isset($_GET['delete'])) {
  delete_by_id('pelanggan', $_GET['delete']);
  redirect('list_pelanggan.php');
}

if (isset($_POST['create'])) {
  // Sanitize and validate input data
  $nama_toko = $db->escape(trim($_POST['nama_toko']));
  $pic = $db->escape(trim($_POST['pic']));
  $alamat = $db->escape(trim($_POST['alamat']));
  $nomor_telp = $db->escape(trim($_POST['nomor_telp']));

  // Check if all fields are filled
  if (!empty($nama_toko) && !empty($pic) && !empty($alamat) && !empty($nomor_telp)) {
    // Insert data into the pelanggan table
    $query = "
          INSERT INTO pelanggan (nama_toko, pic, alamat, nomor_telp) 
          VALUES ('$nama_toko', '$pic', '$alamat', '$nomor_telp')
      ";

    if ($db->query($query)) {
      // Success message
      echo "<script>alert('Pelanggan berhasil ditambahkan!'); window.location.href='list_pelanggan.php';</script>";
    } else {
      // Error message
      echo "<script>alert('Gagal menambahkan pelanggan: " . $db->error . "');</script>";
    }
  } else {
    // Validation error message
    echo "<script>alert('Semua field harus diisi!');</script>";
  }
}
?>

<?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>Daftar Pelanggan</span>
        </strong>
        <button type="button" class="btn btn-primary btn-sm mb-4 pull-right" data-toggle="modal"
          data-target="#createModal">
          Tambah Pelanggan
        </button>
        <a href="export_excel_pelanggan.php" target="_blank" rel="noopener noreferrer"
          class="btn btn-success btn-sm mr-2 pull-right">Download Excel</a>
      </div>
      <div class="panel-body">

        <table class="table table-bordered" id="datatable">
          <thead>
            <tr>
              <th style="width: 25px;">No</th>
              <th style="width: 15%;">Nama Toko</th>
              <th style="width: 15%;">PIC</th>
              <th>Alamat</th>
              <th style="width: 7%;">Telp</th>
              <th style="width: 7%;">Aksi</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($data as $row): ?>
            <tr>
              <td><?php echo $row['id']; ?></td>
              <td><?php echo $row['nama_toko']; ?></td>
              <td><?php echo $row['pic']; ?></td>
              <td><?php echo $row['alamat']; ?></td>
              <td><?php echo $row['nomor_telp']; ?></td>
              <td>
                <a href="edit_pelanggan.php?id=<?php echo $row['id']; ?>" class="btn btn-warning btn-sm"><i
                    class="glyphicon glyphicon-pencil"></i></a>
                <a href="?delete=<?php echo $row['id']; ?>" class="btn btn-danger btn-sm"
                  onclick="return confirm('Yakin ingin menghapus?')"><i class="glyphicon glyphicon-remove"></i></a>
              </td>
            </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

<!-- Modal Tambah -->
<div class="modal fade" id="createModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Tambah Pelanggan</h5>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>
      <div class="modal-body">
        <form method="POST" action="list_pelanggan.php">
          <div class="form-group">
            <label>Nama Toko</label>
            <input type="text" name="nama_toko" class="form-control" required>
          </div>
          <div class="form-group">
            <label>PIC</label>
            <input type="text" name="pic" class="form-control" required>
          </div>
          <div class="form-group">
            <label>Alamat</label>
            <textarea name="alamat" class="form-control" required></textarea>
          </div>
          <div class="form-group">
            <label>Nomor Telepon</label>
            <input type="text" name="nomor_telp" class="form-control" required>
          </div>
          <button type="submit" name="create" class="btn btn-primary">Tambah</button>
        </form>
      </div>
    </div>
  </div>
</div>
<script>
var table = $('#datatable').DataTable({});
</script>
<?php include_once('layouts/footer.php'); ?>