<?php

$judul = "Master Job Desc";
$menu  = "job_desc";

include 'layout/header.php';

/* ==========================
   SIMPAN DATA BARU
========================== */

if(isset($_POST['simpan'])){

    $job_desc = mysqli_real_escape_string(
        $conn,
        $_POST['job_desc']
    );

    mysqli_query($conn,"
        INSERT INTO master_job_desc
        (
            job_desc,
            status
        )
        VALUES
        (
            '$job_desc',
            'aktif'
        )
    ");

    echo "
    <script>
        alert('Job Desc berhasil ditambahkan');
        window.location='job_desc.php';
    </script>
    ";
    exit;
}

/* ==========================
   UPDATE DATA
========================== */

if(isset($_POST['update'])){

    $id_job_desc = (int)$_POST['id_job_desc'];

    $job_desc = mysqli_real_escape_string(
        $conn,
        $_POST['job_desc']
    );

    mysqli_query($conn,"
        UPDATE master_job_desc
        SET job_desc='$job_desc'
        WHERE id_job_desc='$id_job_desc'
    ");

    echo "
    <script>
        alert('Job Desc berhasil diperbarui');
        window.location='job_desc.php';
    </script>
    ";
    exit;
}

/* ==========================
   HAPUS DATA
========================== */

if(isset($_GET['hapus'])){

    $id_job_desc = (int)$_GET['hapus'];

    mysqli_query($conn,"
        DELETE FROM master_job_desc
        WHERE id_job_desc='$id_job_desc'
    ");

    echo "
    <script>
        alert('Job Desc berhasil dihapus');
        window.location='job_desc.php';
    </script>
    ";
    exit;
}

/* ==========================
   AMBIL DATA EDIT
========================== */

$edit = null;

if(isset($_GET['edit'])){

    $id_job_desc = (int)$_GET['edit'];

    $qEdit = mysqli_query($conn,"
        SELECT *
        FROM master_job_desc
        WHERE id_job_desc='$id_job_desc'
    ");

    $edit = mysqli_fetch_assoc($qEdit);
}

include 'layout/sidebar.php';

?>

<div class="container-fluid">

    <!-- FORM -->
    <div class="card shadow">

        <div class="card-header">

            <h5 class="mb-0">

                <?php if($edit){ ?>
                    Edit Job Desc
                <?php } else { ?>
                    Tambah Job Desc
                <?php } ?>

            </h5>

        </div>

        <div class="card-body">

            <form method="POST">

                <?php if($edit){ ?>

                    <input
                        type="hidden"
                        name="id_job_desc"
                        value="<?= $edit['id_job_desc']; ?>">

                <?php } ?>

                <div class="mb-3">

                    <label class="form-label">
                        Nama Job Desc
                    </label>

                    <input
                        type="text"
                        name="job_desc"
                        class="form-control"
                        required
                        value="<?= $edit ? htmlspecialchars($edit['job_desc']) : ''; ?>">

                </div>

                <?php if($edit){ ?>

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

                        Update

                    </button>

                    <a
                        href="job_desc.php"
                        class="btn btn-secondary">

                        Batal

                    </a>

                <?php } else { ?>

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

                        Simpan

                    </button>

                <?php } ?>

            </form>

        </div>

    </div>

    <!-- TABEL -->
    <div class="card shadow mt-4">

        <div class="card-header">

            <h5 class="mb-0">
                Data Job Desc
            </h5>

        </div>

        <div class="card-body">

            <div class="table-responsive">

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

                    <thead>

                        <tr>
                            <th width="60">No</th>
                            <th>Job Desc</th>
                            <th width="120">Status</th>
                            <th width="180">Aksi</th>
                        </tr>

                    </thead>

                    <tbody>

                    <?php

                    $no = 1;

                    $query = mysqli_query($conn,"
                        SELECT *
                        FROM master_job_desc
                        ORDER BY id_job_desc DESC
                    ");

                    if(mysqli_num_rows($query) > 0){

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

                    ?>

                        <tr>

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

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

                            <td>

                                <?php if($data['status'] == 'aktif'){ ?>

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

                                <?php } else { ?>

                                    <span class="badge bg-danger">
                                        Non Aktif
                                    </span>

                                <?php } ?>

                            </td>

                            <td>

                                <a
                                    href="?edit=<?= $data['id_job_desc']; ?>"
                                    class="btn btn-warning btn-sm">

                                    Edit

                                </a>

                                <a
                                    href="?hapus=<?= $data['id_job_desc']; ?>"
                                    class="btn btn-danger btn-sm"
                                    onclick="return confirm('Yakin ingin menghapus data ini?')">

                                    Hapus

                                </a>

                            </td>

                        </tr>

                    <?php

                        }

                    } else {

                    ?>

                        <tr>

                            <td colspan="4" class="text-center">
                                Belum ada data Job Desc
                            </td>

                        </tr>

                    <?php } ?>

                    </tbody>

                </table>

            </div>

        </div>

    </div>

</div>

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