<?php
include "koneksi.php";

// TAMBAH DATA
if(isset($_POST['simpan'])){

    $nama          = $_POST['nama'];
    $alamat        = $_POST['alamat'];
    $email         = $_POST['email'];
    $telepon       = $_POST['telepon'];
    $status_aktif  = $_POST['status_aktif'];

    mysqli_query($koneksi,"
    INSERT INTO anggota
    VALUES(
        '',
        '$nama',
        '$alamat',
        '$email',
        '$telepon',
        '$status_aktif'
    )
    ");

    echo "
    <script>
        alert('Data anggota berhasil ditambahkan');
        window.location='anggota.php';
    </script>
    ";
}

// EDIT DATA
if(isset($_POST['update'])){

    $id_anggota    = $_POST['id_anggota'];
    $nama          = $_POST['nama'];
    $alamat        = $_POST['alamat'];
    $email         = $_POST['email'];
    $telepon       = $_POST['telepon'];
    $status_aktif  = $_POST['status_aktif'];

    mysqli_query($koneksi,"
    UPDATE anggota SET
        nama='$nama',
        alamat='$alamat',
        email='$email',
        telepon='$telepon',
        status_aktif='$status_aktif'
    WHERE id_anggota='$id_anggota'
    ");
    echo mysqli_error($koneksi);

    echo "
    <script>
        alert('Data anggota berhasil diubah');
        window.location='anggota.php';
    </script>
    ";
}

// HAPUS DATA
if(isset($_GET['hapus'])){

    $id = $_GET['hapus'];

    mysqli_query($koneksi,"
    DELETE FROM anggota
    WHERE id_anggota='$id'
    ");

    echo "
    <script>
        alert('Data anggota berhasil dihapus');
        window.location='anggota.php';
    </script>
    ";
}
?>

<?php include "template/header.php"; ?>

<div class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">

<?php include "template/h_menu.php"; ?>
<?php include "sidebar.php"; ?>

<!-- CONTENT -->
<div class="content-wrapper">

<section class="content-header">

    <div class="container-fluid">

        <div class="row mb-2">

            <div class="col-sm-6">
                <h1>Data Anggota</h1>
            </div>

            <div class="col-sm-6">

                <ol class="breadcrumb float-sm-right">

                    <li class="breadcrumb-item">
                        <a href="index.php">Home</a>
                    </li>

                    <li class="breadcrumb-item active">
                        Data Anggota
                    </li>

                </ol>

            </div>

        </div>

    </div>

</section>

<!-- MAIN CONTENT -->
<section class="content">

<div class="container-fluid">

<div class="card">

<div class="card-header">

    <button class="btn btn-primary"
            data-toggle="modal"
            data-target="#modalTambah">

        <i class="fas fa-plus"></i>
        Tambah Anggota

    </button>

</div>

<div class="card-body">

<table id="example1"
       class="table table-bordered table-striped">

<thead>

<tr>

    <th>No</th>
    <th>Nama</th>
    <th>Alamat</th>
    <th>Email</th>
    <th>Telepon</th>
    <th>Status</th>
    <th>Aksi</th>

</tr>

</thead>

<tbody>

<?php
$no = 1;

$query = mysqli_query($koneksi,"
SELECT * FROM anggota
ORDER BY id_anggota DESC
");

while($data = mysqli_fetch_assoc($query)){
?>

<tr>

    <td><?= $no++ ?></td>

    <td><?= htmlspecialchars($data['nama']) ?></td>

    <td><?= htmlspecialchars($data['alamat']) ?></td>

    <td><?= htmlspecialchars($data['email']) ?></td>

    <td><?= htmlspecialchars($data['telepon']) ?></td>

    <td>

        <?php 
        if($data['status_aktif'] == '1'){ ?>

            <span class="badge badge-success">
                Aktif
            </span>

        <?php } else { ?>

            <span class="badge badge-danger">
                Tidak Aktif
            </span>

        <?php } ?>

    </td>

    <td>

        <button class="btn btn-warning btn-sm"
                data-toggle="modal"
                data-target="#edit<?= $data['id_anggota'] ?>">

            <i class="fas fa-edit"></i>

        </button>

        <a href="anggota.php?hapus=<?= $data['id_anggota'] ?>"
           class="btn btn-danger btn-sm"
           onclick="return confirm('Yakin ingin menghapus data?')">

            <i class="fas fa-trash"></i>

        </a>

    </td>

</tr>

<!-- MODAL EDIT -->
<div class="modal fade"
     id="edit<?= $data['id_anggota'] ?>">

<div class="modal-dialog">

<div class="modal-content">

<form method="POST">

<div class="modal-header bg-warning">

    <h4 class="modal-title">
        Edit Anggota
    </h4>

    <button type="button"
            class="close"
            data-dismiss="modal">

        &times;

    </button>

</div>

<div class="modal-body">

<input type="hidden"
       name="id_anggota"
       value="<?= $data['id_anggota'] ?>">

<div class="form-group">

    <label>Nama</label>

    <input type="text"
           name="nama"
           class="form-control"
           value="<?= $data['nama'] ?>"
           required>

</div>

<div class="form-group">

    <label>Alamat</label>

    <textarea name="alamat"
              class="form-control"
              required><?= $data['alamat'] ?></textarea>

</div>

<div class="form-group">

    <label>Email</label>

    <input type="email"
           name="email"
           class="form-control"
           value="<?= $data['email'] ?>"
           required>

</div>

<div class="form-group">

    <label>Telepon</label>

    <input type="text"
           name="telepon"
           class="form-control"
           value="<?= $data['telepon'] ?>"
           required>

</div>

<div class="form-group">

    <label>Status</label>

    <select name="status_aktif"
class="form-control"
required>

<option value="">-- Pilih Status --</option>

<option value="1"
<?= $data['status_aktif']==1 ? 'selected' : '' ?>>
Aktif
</option>

<option value="0"
<?= $data['status_aktif']==0 ? 'selected' : '' ?>>
Tidak Aktif
</option>

</select>
</div>

</div>

<div class="modal-footer">

    <button type="submit"
            name="update"
            class="btn btn-warning">

        Update

    </button>

    <button type="button"
            class="btn btn-secondary"
            data-dismiss="modal">

        Batal

    </button>

</div>

</form>

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

<?php } ?>

</tbody>

</table>

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

<!-- MODAL TAMBAH -->
<div class="modal fade" id="modalTambah">

<div class="modal-dialog">

<div class="modal-content">

<form method="POST">

<div class="modal-header bg-primary">

    <h4 class="modal-title">
        Tambah Anggota
    </h4>

    <button type="button"
            class="close"
            data-dismiss="modal">

        &times;

    </button>

</div>

<div class="modal-body">

<div class="form-group">

    <label>Nama</label>

    <input type="text"
           name="nama"
           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>Email</label>

    <input type="email"
           name="email"
           class="form-control"
           required>

</div>

<div class="form-group">

    <label>Telepon</label>

    <input type="text"
           name="telepon"
           class="form-control"
           required>

</div>

<div class="form-group">

    <label>Status</label>

    <select name="status_aktif"
            class="form-control"
            required>

        <option value="">-- Pilih --</option>
        <option value="1">Aktif</option>
        <option value="0">Tidak Aktif</option>

    </select>

</div>

</div>

<div class="modal-footer">

    <button type="submit"
            name="simpan"
            class="btn btn-primary">

        Simpan

    </button>

    <button type="button"
            class="btn btn-secondary"
            data-dismiss="modal">

        Batal

    </button>

</div>

</form>

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

<?php include "template/footer.php"; ?>