<?php
require_once('includes/load.php');
$page_title = 'List Template Message';
$sql = "SELECT * FROM crm_template_message";
$data = $db->query($sql);

// Delete a record if requested
if (isset($_GET['delete'])) {
  delete_by_id('crm_template_message', $_GET['delete']);
  redirect('list_template_message.php');
}

// Check if the form has been submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['create'])) {
  // Get data from the form
  $name = $_POST['name'];
  $message = $_POST['message'];
  $tipe = $_POST['tipe'];
  $status = $_POST['status'];

  // Validate data (optional, add validation as needed)
  if (!empty($name) && !empty($message)) {
    // Query to save data to the database
    $query = "INSERT INTO crm_template_message (name, message, tipe, status) VALUES ('$name', '$message', '$tipe', '$status')";

    if ($db->query($query)) {
      // If successful, redirect or display success message
      echo "<script>alert('Data berhasil ditambahkan!');</script>";
      redirect('list_template_message.php');
    } else {
      // If failed, display error message
      echo "<script>alert('Gagal menambahkan data: " . $db->error . "');</script>";
    }
  } else {
    echo "<script>alert('Harap isi semua data dengan benar!');</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 Template Pesan</span>
        </strong>
        <a href="#" class="btn btn-sm btn-primary mb-4 pull-right" data-toggle="modal" data-target="#createModal">
          Tambah Template
        </a>
      </div>
      <div class="panel-body">

        <table class="table table-bordered">
          <thead>
            <tr>
              <th style="width: 25px">No</th>
              <th>Nama Template</th>
              <th>Isi Pesan</th>
              <th>Tipe</th>
              <th>Status</th>
              <th>Tanggal Dibuat</th>
              <th>Tanggal Diperbarui</th>
              <th>Aksi</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($data as $row): ?>
              <tr>
                <td><?php echo $row['id']; ?></td>
                <td><?php echo htmlspecialchars($row['name']); ?></td>
                <td><?php echo htmlspecialchars($row['message']); ?></td>
                <td><?php echo htmlspecialchars($row['tipe']); ?></td>
                <td><?php echo htmlspecialchars($row['status']); ?></td>
                <td><?php echo $row['created_at']; ?></td>
                <td><?php echo $row['updated_at']; ?></td>
                <td>
                  <a href="edit_template_message.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" aria-labelledby="createModalModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Tambah Template Pesan</h5>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>
      <div class="modal-body">
        <form method="POST" action="list_template_message.php">
          <div class="form-group">
            <label>Nama Template</label>
            <input type="text" name="name" class="form-control" required>
          </div>
          <div class="form-group">
            <label>Isi Pesan</label>
            <textarea name="message" class="form-control" required></textarea>
          </div>
          <div class="form-group">
            <label>Tipe</label>
            <select name="tipe" class="form-control" required>
              <option value="Internal">Internal</option>
              <option value="Marketing">Marketing</option>
            </select>
          </div>
          <div class="form-group">
            <label>Status</label>
            <select name="status" class="form-control" required>
              <option value="Disetujui">Disetujui</option>
              <option value="Menunggu Persetujuan">Menunggu Persetujuan</option>
            </select>
          </div>
          <button type="submit" name="create" class="btn btn-primary">Tambah</button>
        </form>
      </div>
    </div>
  </div>
</div>

<script>
  var table = $('.table').DataTable({});
</script>
<?php include_once('layouts/footer.php'); ?>