<?php include 'header.php'; ?>

<div class="content-wrapper">

  <section class="content-header">
    <h1>
      Kategori
      <small>Data kategori</small>
    </h1>
    <ol class="breadcrumb">
      <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
      <li class="active">Dashboard</li>
    </ol>
  </section>

  <section class="content">
    <div class="row">
      <section class="col-lg-12">
        <div class="box box-info">

          <div class="box-header">
            <h3 class="box-title">Kategori Transaksi Keuangan</h3>
            <div class="btn-group pull-right">            

              <button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#exampleModal">
                <i class="fa fa-plus"></i> &nbsp Tambah Kategori
              </button>
            </div>
          </div>
          <div class="box-body">

            <!-- Modal -->
            <form action="kategori_act.php" method="post">
              <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                <div class="modal-dialog" role="document">
                  <div class="modal-content">
                    <div class="modal-header">
                      <h5 class="modal-title" id="exampleModalLabel">Tambah Kategori</h5>
                      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                      </button>
                    </div>
                    <div class="modal-body">

                      <div class="form-group">
                        <label>Nama Kategori</label>
                        <input type="text" name="kategori" required="required" class="form-control" placeholder="Nama Kategori ..">
                      </div>

                    </div>
                    <div class="modal-footer">
                      <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
                      <button type="submit" class="btn btn-primary">Simpan</button>
                    </div>
                  </div>
                </div>
              </div>
            </form>


            <div class="table-responsive">
              <table class="table table-bordered table-striped table-hover" id="table-kategori">
                <thead>
                  <tr>
                    <th width="1%" class="text-center">NO</th>
                    <th>NAMA KATEGORI</th>
                    <th width="12%" class="text-center">OPSI</th>
                  </tr>
                </thead>
                <tbody>
                  <?php 
                  include '../koneksi.php';
                  $no=1;
                  $data = mysqli_query($koneksi,"SELECT * FROM kategori ORDER BY kategori ASC");
                  while($d = mysqli_fetch_array($data)){
                    ?>
                    <tr>
                      <td class="text-center"><?php echo $no++; ?></td>
                      <td><?php echo htmlspecialchars($d['kategori']); ?></td>
                      <td class="text-center">    
                        <?php 
                        if($d['kategori_id'] != 1){
                          ?> 
                          <button type="button" class="btn btn-warning btn-sm" data-toggle="modal" data-target="#edit_kategori_<?php echo $d['kategori_id'] ?>">
                            <i class="fa fa-cog"></i>
                          </button>

                          <button type="button" class="btn btn-danger btn-sm btn-hapus-kategori" data-id="<?php echo $d['kategori_id'] ?>">
                            <i class="fa fa-trash"></i>
                          </button>
                          <?php 
                        }
                        ?>

                        <form action="kategori_update.php" method="post">
                          <div class="modal fade" id="edit_kategori_<?php echo $d['kategori_id'] ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                            <div class="modal-dialog" role="document">
                              <div class="modal-content">
                                <div class="modal-header">
                                  <h5 class="modal-title" id="exampleModalLabel">Edit Kategori</h5>
                                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                  </button>
                                </div>
                                <div class="modal-body">

                                  <div class="form-group" style="width:100%">
                                    <label>Nama Kategori</label>
                                    <input type="hidden" name="id" required="required" class="form-control" placeholder="Nama Kategori .." value="<?php echo $d['kategori_id']; ?>">
                                    <input type="text" name="kategori" required="required" class="form-control" placeholder="Nama Kategori .." value="<?php echo $d['kategori']; ?>" style="width:100%">
                                  </div>

                                </div>
                                <div class="modal-footer">
                                  <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
                                  <button type="submit" class="btn btn-primary">Simpan</button>
                                </div>
                              </div>
                            </div>
                          </div>
                        </form>


                      </td>
                    </tr>
                    <?php 
                  }
                  ?>
                </tbody>
              </table>
            </div>
          </div>

        </div>
      </section>
    </div>
  </section>

</div>

<?php include 'footer.php'; ?>

<script>
$(document).ready(function() {
  // DataTables: tampil rapi dengan pencarian, sorting, pagination
  if ($('#table-kategori').length) {
    $('#table-kategori').DataTable({
      order: [[1, 'asc']],
      pageLength: 25,
      lengthMenu: [[10, 25, 50, 100, -1], [10, 25, 50, 100, "Semua"]],
      language: {
        search: 'Cari:',
        lengthMenu: 'Tampilkan _MENU_ data per halaman',
        info: 'Menampilkan _START_ sampai _END_ dari _TOTAL_ data',
        infoEmpty: 'Tidak ada data',
        infoFiltered: '(difilter dari _MAX_ total data)',
        zeroRecords: 'Tidak ada data yang cocok',
        paginate: {
          first: 'Pertama',
          last: 'Terakhir',
          next: 'Selanjutnya',
          previous: 'Sebelumnya'
        }
      },
      columnDefs: [
        { orderable: false, targets: [0, 2] },
        { className: 'text-center', targets: [0, 2] }
      ]
    });
  }

  // Handle hapus kategori dengan SweetAlert
  $(document).on('click', '.btn-hapus-kategori', function(e) {
    e.preventDefault();
    var id = $(this).data('id');
    var deleteUrl = 'kategori_hapus.php?id=' + id;
    
    Swal.fire({
      title: 'Apakah Anda Yakin?',
      html: '<p style="font-size: 1.6rem;">Data kategori yang dihapus tidak dapat dikembalikan!</p><p style="font-size: 1.4rem; color: #666;">Semua transaksi yang berhubungan dengan kategori ini akan dipindahkan ke kategori default.</p>',
      icon: 'warning',
      showCancelButton: true,
      confirmButtonColor: '#d33',
      cancelButtonColor: '#3085d6',
      confirmButtonText: 'Ya, Hapus!',
      cancelButtonText: 'Batal'
    }).then((result) => {
      if (result.isConfirmed) {
        window.location.href = deleteUrl;
      }
    });
  });

  // SweetAlert Notifications
  <?php 
  if(isset($_SESSION['alert']) && isset($_SESSION['message'])):
    $alertType = $_SESSION['alert'];
    $message = $_SESSION['message'];
    unset($_SESSION['alert']);
    unset($_SESSION['message']);
    
    if($alertType == 'success'):
    ?>
      Swal.fire({
        icon: 'success',
        title: 'Berhasil!',
        text: <?php echo json_encode($message); ?>,
        confirmButtonColor: '#3085d6',
        confirmButtonText: 'OK',
        timer: 3000,
        timerProgressBar: true
      }).then((result) => {
        if (result.isConfirmed || result.dismiss === Swal.DismissReason.timer) {
          location.reload();
        }
      });
    <?php elseif($alertType == 'error'): ?>
      Swal.fire({
        icon: 'error',
        title: 'Gagal!',
        text: <?php echo json_encode($message); ?>,
        confirmButtonColor: '#d33',
        confirmButtonText: 'OK'
      });
    <?php elseif($alertType == 'warning'): ?>
      Swal.fire({
        icon: 'warning',
        title: 'Peringatan!',
        text: <?php echo json_encode($message); ?>,
        confirmButtonColor: '#ffc107',
        confirmButtonText: 'OK'
      });
    <?php endif; ?>
  <?php endif; ?>
});
</script>