<?php
$page_title = 'Tambah Departemen';
require_once('includes/load.php');
page_require_level(4);

if (isset($_POST['add_department'])) {
    $req_field = 'department-name';
    validate_fields($req_field);
    
    if (empty($errors)) {
        $dept_name = remove_junk($db->escape($_POST['department-name']));
        $sql = "INSERT INTO departments (name) VALUES ('{$dept_name}')";
        if ($db->query($sql)) {
            $session->msg('s', "Departemen berhasil ditambahkan!");
            redirect('departments.php', false);
        } else {
            $session->msg('d', 'Gagal menambahkan departemen.');
            redirect('add_department.php', false);
        }
    } else {
        $session->msg("d", $errors);
        redirect('add_department.php', false);
    }
}
?>

<?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">
                <strong>
                    <span class="glyphicon glyphicon-th"></span>
                    <span>Tambah Departemen Baru</span>
                </strong>
            </div>
            <div class="panel-body">
                <form method="post" action="add_department.php">
                    <div class="form-group">
                        <input type="text" class="form-control" name="department-name" placeholder="Nama Departemen" required>
                    </div>
                    <button type="submit" name="add_department" class="btn btn-primary">Tambah Departemen</button>
                    <a href="departments.php" class="btn btn-default">Kembali</a>
                </form>
            </div>
        </div>
    </div>
</div>
<?php include_once('layouts/footer.php'); ?>