<?php
$page_title = 'Kelola Hari Kerja';
require_once('includes/load.php');
page_require_level(4); // Hanya level 4 yang bisa mengakses halaman ini

// Proses tambah data
if (isset($_POST['add_hari_kerja'])) {
    $req_fields = array('bulan', 'tahun', 'jumlah_hari_kerja');
    validate_fields($req_fields);

    if (empty($errors)) {
        $p_bulan = $db->escape($_POST['bulan']);
        $p_tahun = $db->escape($_POST['tahun']);
        $p_jumlah_hari = $db->escape($_POST['jumlah_hari_kerja']);

        // Cek duplikasi
        $sql_check = "SELECT * FROM hari_kerja WHERE bulan = '{$p_bulan}' AND tahun = '{$p_tahun}' LIMIT 1";
        $result_check = find_by_sql($sql_check);

        if (!empty($result_check)) {
            $session->msg("d", "Data untuk bulan dan tahun ini sudah ada.");
            redirect('hari_kerja.php', false);
        } else {
            $sql = "INSERT INTO hari_kerja (bulan, tahun, jumlah_hari_kerja, created_at, updated_at) VALUES (";
            $sql .= "'{$p_bulan}', '{$p_tahun}', '{$p_jumlah_hari}', NOW(), NOW()";
            $sql .= ")";

            if ($db->query($sql)) {
                $session->msg("s", "Data hari kerja berhasil ditambahkan.");
                redirect('hari_kerja.php', false);
            } else {
                $session->msg("d", "Gagal menambahkan data hari kerja.");
                redirect('hari_kerja.php', false);
            }
        }
    } else {
        $session->msg("d", $errors);
        redirect('hari_kerja.php', false);
    }
}

// Ambil semua data hari kerja
$all_hari_kerja = find_by_sql("SELECT * FROM hari_kerja ORDER BY tahun DESC, bulan DESC");
?>

<?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-5">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <strong>
                    <span class="glyphicon glyphicon-plus"></span>
                    <span>Tambah Hari Kerja</span>
                </strong>
            </div>
            <div class="panel-body">
                <form method="post" action="hari_kerja.php">
                    <div class="form-group">
                        <label for="bulan">Bulan</label>
                        <select name="bulan" id="bulan" class="form-control" required>
                            <option value="">Pilih Bulan</option>
                            <?php for ($m = 1; $m <= 12; $m++): ?>
                                <option value="<?php echo $m; ?>"><?php echo date('F', mktime(0, 0, 0, $m, 1)); ?></option>
                            <?php endfor; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="tahun">Tahun</label>
                        <select name="tahun" id="tahun" class="form-control" required>
                            <option value="">Pilih Tahun</option>
                            <?php for ($y = date('Y') + 1; $y >= 2020; $y--): ?>
                                <option value="<?php echo $y; ?>"><?php echo $y; ?></option>
                            <?php endfor; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="jumlah_hari_kerja">Jumlah Hari Kerja</label>
                        <input type="number" class="form-control" name="jumlah_hari_kerja" placeholder="Contoh: 22" min="1" max="31" required>
                    </div>
                    <button type="submit" name="add_hari_kerja" class="btn btn-primary">Simpan</button>
                </form>
            </div>
        </div>
    </div>

    <div class="col-md-7">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <strong>
                    <span class="glyphicon glyphicon-list"></span>
                    <span>Daftar Hari Kerja</span>
                </strong>
            </div>
            <div class="panel-body">
                <div class="table-responsive">
                    <table class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                <th class="text-center">#</th>
                                <th>Bulan</th>
                                <th>Tahun</th>
                                <th class="text-center">Jumlah Hari Kerja</th>
                                <th class="text-center">Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!empty($all_hari_kerja)): ?>
                                <?php $i = 1; ?>
                                <?php foreach ($all_hari_kerja as $hari_kerja): ?>
                                    <tr>
                                        <td class="text-center"><?php echo $i++; ?></td>
                                        <td><?php echo date('F', mktime(0, 0, 0, $hari_kerja['bulan'], 1)); ?></td>
                                        <td><?php echo (int)$hari_kerja['tahun']; ?></td>
                                        <td class="text-center"><?php echo (int)$hari_kerja['jumlah_hari_kerja']; ?></td>
                                        <td class="text-center">
                                            <a href="delete_hari_kerja.php?id=<?php echo (int)$hari_kerja['id']; ?>" class="btn btn-danger btn-xs" title="Hapus" data-toggle="tooltip" onclick="return confirm('Apakah Anda yakin ingin menghapus data ini?');">
                                                <span class="glyphicon glyphicon-trash"></span>
                                            </a>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="5" class="text-center">Tidak ada data hari kerja.</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include_once('layouts/footer.php'); ?>