<?php
$page_title = 'Edit Gaji Karyawan';
require_once('includes/load.php');
page_require_level(4);

// Cek apakah ada ID gaji yang dikirim
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    $session->msg('d', 'ID Gaji tidak valid.');
    redirect('gaji.php');
}

$salary_id = (int)$_GET['id'];
$salary = find_by_id('salaries', $salary_id);

// Jika data gaji tidak ditemukan, redirect
if (!$salary) {
    $session->msg('d', 'Data gaji tidak ditemukan.');
    redirect('gaji.php');
}

// Ambil data karyawan terkait
$employee = find_by_id('employees', $salary['employee_id']);
if (!$employee) {
    $session->msg('d', 'Data karyawan tidak ditemukan.');
    redirect('gaji.php');
}

if (isset($_POST['edit_salary'])) {
    $req_fields = array(
        'pay_date',
        'basic_salary',
        'tunjangan_jabatan_percent',
        'adjustment_percent',
        'tunjangan_kehadiran_percent',
        'tunjangan_transport',
        'tunjangan_makan',
        'work_visit',
        'lembur',
        'bpjs_kesehatan_percent',
        'bpjs_ketenagakerjaan_percent'
    );
    validate_fields($req_fields);

    if (empty($errors)) {
        $pay_date = $db->escape($_POST['pay_date']);
        $basic_salary = (float)$db->escape($_POST['basic_salary']);
        $tunjangan_transport = (float)$db->escape($_POST['tunjangan_transport']);
        $tunjangan_makan = (float)$db->escape($_POST['tunjangan_makan']);
        $work_visit = (float)$db->escape($_POST['work_visit']);
        $lembur = (float)$db->escape($_POST['lembur']);
        $tunjangan_jabatan_percent = (float)$db->escape($_POST['tunjangan_jabatan_percent']);
        $adjustment_percent = (float)$db->escape($_POST['adjustment_percent']);
        $tunjangan_kehadiran_percent = (float)$db->escape($_POST['tunjangan_kehadiran_percent']);
        $bpjs_kesehatan_percent = (float)$db->escape($_POST['bpjs_kesehatan_percent']);
        $bpjs_ketenagakerjaan_percent = (float)$db->escape($_POST['bpjs_ketenagakerjaan_percent']);

        // Hitung ulang semua nilai berdasarkan data yang baru
        $tunjangan_jabatan = ($basic_salary * $tunjangan_jabatan_percent) / 100;
        $adjustment = ($basic_salary * $adjustment_percent) / 100;
        $tunjangan_kehadiran = ($basic_salary * $tunjangan_kehadiran_percent) / 100;

        $gross_salary = $basic_salary + $tunjangan_jabatan + $adjustment + $tunjangan_kehadiran + $tunjangan_transport + $tunjangan_makan + $work_visit + $lembur;

        $bpjs_kesehatan = ($gross_salary * $bpjs_kesehatan_percent) / 100;
        $bpjs_ketenagakerjaan = ($gross_salary * $bpjs_ketenagakerjaan_percent) / 100;

        $net_salary = $gross_salary - $bpjs_kesehatan - $bpjs_ketenagakerjaan;

        // Perbarui data di database
        $sql = "UPDATE salaries SET 
                pay_date = '{$pay_date}',
                basic_salary = '{$basic_salary}',
                tunjangan_jabatan = '{$tunjangan_jabatan}',
                adjustment = '{$adjustment}',
                tunjangan_kehadiran = '{$tunjangan_kehadiran}',
                tunjangan_transport = '{$tunjangan_transport}',
                tunjangan_makan = '{$tunjangan_makan}',
                work_visit = '{$work_visit}',
                lembur = '{$lembur}',
                bpjs_kesehatan = '{$bpjs_kesehatan}',
                bpjs_ketenagakerjaan = '{$bpjs_ketenagakerjaan}',
                net_salary = '{$net_salary}',
                updated_at = NOW()
                WHERE id = '{$salary_id}'";

        if ($db->query($sql)) {
            $session->msg('s', "Gaji untuk karyawan telah berhasil diperbarui.");
            redirect('gaji.php', false);
        } else {
            $session->msg('d', 'Maaf, pembaruan gaji gagal. Mungkin ada masalah dengan database.');
            redirect('edit_salary.php?id=' . $salary_id, false);
        }
    } else {
        $session->msg("d", $errors);
        redirect('edit_salary.php?id=' . $salary_id, 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-8">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <strong>
                    <span class="glyphicon glyphicon-usd"></span>
                    <span>Edit Gaji Karyawan: <?php echo remove_junk($employee['first_name'] . ' ' . $employee['last_name']); ?></span>
                </strong>
            </div>
            <div class="panel-body">
                <form method="post" action="edit_salary.php?id=<?php echo (int)$salary['id']; ?>">
                    <div class="form-group">
                        <label for="employee_name">Nama Karyawan</label>
                        <input type="text" class="form-control" value="<?php echo remove_junk($employee['first_name'] . ' ' . $employee['last_name']); ?>" disabled>
                    </div>
                    <div class="form-group">
                        <label for="pay_date">Tanggal Gaji</label>
                        <input type="date" class="form-control" name="pay_date" value="<?php echo remove_junk($salary['pay_date']); ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="basic_salary">Gaji Pokok (Rp)</label>
                        <input type="number" class="form-control" name="basic_salary" value="<?php echo remove_junk($salary['basic_salary']); ?>" required>
                    </div>
                    
                    <hr>
                    <label>Tunjangan</label>
                    <div class="form-group">
                        <label for="tunjangan_jabatan_percent">Tunjangan Jabatan (%)</label>
                        <div class="input-group">
                            <input type="number" class="form-control" name="tunjangan_jabatan_percent" value="<?php echo ($salary['tunjangan_jabatan'] / $salary['basic_salary']) * 100; ?>" placeholder="Persentase dari gaji pokok">
                            <span class="input-group-addon">%</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="adjustment_percent">Adjustment (%)</label>
                        <div class="input-group">
                            <input type="number" class="form-control" name="adjustment_percent" value="<?php echo ($salary['adjustment'] / $salary['basic_salary']) * 100; ?>" placeholder="Persentase dari gaji pokok">
                            <span class="input-group-addon">%</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="tunjangan_kehadiran_percent">Tunjangan Kehadiran (%)</label>
                        <div class="input-group">
                            <input type="number" class="form-control" name="tunjangan_kehadiran_percent" value="<?php echo ($salary['tunjangan_kehadiran'] / $salary['basic_salary']) * 100; ?>" placeholder="Persentase dari gaji pokok">
                            <span class="input-group-addon">%</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="tunjangan_transport">Tunjangan Transport (Rp)</label>
                        <input type="number" class="form-control" name="tunjangan_transport" value="<?php echo remove_junk($salary['tunjangan_transport']); ?>">
                    </div>
                    <div class="form-group">
                        <label for="tunjangan_makan">Tunjangan Makan (Rp)</label>
                        <input type="number" class="form-control" name="tunjangan_makan" value="<?php echo remove_junk($salary['tunjangan_makan']); ?>">
                    </div>
                    <div class="form-group">
                        <label for="work_visit">Work Visit (Rp)</label>
                        <input type="number" class="form-control" name="work_visit" value="<?php echo remove_junk($salary['work_visit']); ?>">
                    </div>
                    <div class="form-group">
                        <label for="lembur">Lembur (Rp)</label>
                        <input type="number" class="form-control" name="lembur" value="<?php echo remove_junk($salary['lembur']); ?>">
                    </div>

                    <hr>
                    <label>Potongan</label>
                    <div class="form-group">
                        <label for="bpjs_kesehatan_percent">BPJS Kesehatan (%)</label>
                        <div class="input-group">
                            <input type="number" class="form-control" name="bpjs_kesehatan_percent" value="<?php 
                                $gross_salary_calc = $salary['basic_salary'] + $salary['tunjangan_jabatan'] + $salary['adjustment'] + $salary['tunjangan_kehadiran'] + $salary['tunjangan_transport'] + $salary['tunjangan_makan'] + $salary['work_visit'] + $salary['lembur'];
                                echo ($gross_salary_calc > 0) ? ($salary['bpjs_kesehatan'] / $gross_salary_calc) * 100 : 0;
                            ?>" placeholder="Persentase dari total gaji">
                            <span class="input-group-addon">%</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="bpjs_ketenagakerjaan_percent">BPJS Ketenagakerjaan (%)</label>
                        <div class="input-group">
                            <input type="number" class="form-control" name="bpjs_ketenagakerjaan_percent" value="<?php
                                $gross_salary_calc = $salary['basic_salary'] + $salary['tunjangan_jabatan'] + $salary['adjustment'] + $salary['tunjangan_kehadiran'] + $salary['tunjangan_transport'] + $salary['tunjangan_makan'] + $salary['work_visit'] + $salary['lembur'];
                                echo ($gross_salary_calc > 0) ? ($salary['bpjs_ketenagakerjaan'] / $gross_salary_calc) * 100 : 0;
                            ?>" placeholder="Persentase dari total gaji">
                            <span class="input-group-addon">%</span>
                        </div>
                    </div>
                    
                    <div class="form-group clearfix">
                        <button type="submit" name="edit_salary" class="btn btn-primary">Perbarui Gaji</button>
                        <a href="gaji.php" class="btn btn-default pull-right">Batal</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

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