Untitled

 avatar
unknown
plain_text
20 days ago
4.7 kB
5
Indexable
$(document).ready(function () {
    function computeTotal(formSelector) {
        let rate = parseFloat($(`${formSelector} select[name="employee_id"] option:selected`).data('rate')) || 0;
        let hours = parseFloat($(`${formSelector} input[name="hours_worked"]`).val()) || 0;
        let total = (rate > 0 && hours > 0) ? rate * hours : 0;
        $(`${formSelector} input[name="total_payment"]`).val(total);
    }

    $('#addProjectModal, #editProjectModal').on('keyup change', 'select[name="employee_id"], input[name="hours_worked"]', function () {
        computeTotal($(this).closest('form'));
    });

    $('#filterProjects').change(function () {
        $('.no-project').toggle(!this.checked);
    });

    function showMessage(text, type) {
        $('#message').attr('class', `alert alert-${type}`).text(text);
        setTimeout(() => $('#message').text('').removeClass(`alert alert-${type}`), 3000);
    }

    function refreshTable() {
        $.get('read.php', function (data) {
            $('#employeeTableBody').html(data);
            if ($('#filterProjects').is(':checked')) $('.no-project').hide();
        });
    }

    $('#addEmployeeForm').submit(function (e) {
        e.preventDefault();
        const data = $(this).serialize() + '&type=employee';
        $.post('add.php', data, function (res) {
            if (res.trim() === "Insert Success") {
                $('#addEmployeeModal').modal('hide');
                showMessage(res, 'success');
                refreshTable();
                $('#addEmployeeForm')[0].reset();
            } else showMessage(res, 'danger');
        });
    });

    $('#editEmployeeForm').submit(function (e) {
        e.preventDefault();
        const data = $(this).serialize() + '&type=employee';
        $.post('edit.php', data, function (res) {
            if (res.trim() === "Update Success") {
                $('#editEmployeeModal').modal('hide');
                showMessage(res, 'success');
                refreshTable();
            } else showMessage(res, 'danger');
        });
    });

    $('#addProjectForm').submit(function (e) {
        e.preventDefault();
        const data = $(this).serialize() + '&type=project';
        $.post('add.php', data, function (res) {
            if (res.trim() === "Insert Success") {
                $('#addProjectModal').modal('hide');
                showMessage(res, 'success');
                refreshTable();
                $('#addProjectForm')[0].reset();
            } else showMessage(res, 'danger');
        });
    });

    $('#editProjectForm').submit(function (e) {
        e.preventDefault();
        const data = $(this).serialize() + '&type=project';
        $.post('edit.php', data, function (res) {
            if (res.trim() === "Update Success") {
                $('#editProjectModal').modal('hide');
                showMessage(res, 'success');
                refreshTable();
            } else showMessage(res, 'danger');
        });
    });

    $(document).on('click', '.edit-employee-btn', function () {
        $('#editEmployeeForm input[name="id"]').val($(this).data('id'));
        $('#editEmployeeForm input[name="name"]').val($(this).data('name'));
        $('#editEmployeeForm input[name="position"]').val($(this).data('position'));
        $('#editEmployeeForm input[name="rate_per_hour"]').val($(this).data('rate'));
        $('#editEmployeeModal').modal('show');
    });

    $(document).on('click', '.edit-project-btn', function () {
        $('#editProjectForm input[name="id"]').val($(this).data('id'));
        $('#editProjectForm select[name="employee_id"]').val($(this).data('employee'));
        $('#editProjectForm input[name="project_name"]').val($(this).data('name'));
        $('#editProjectForm input[name="hours_worked"]').val($(this).data('hours'));
        computeTotal('#editProjectForm');
        $('#editProjectModal').modal('show');
    });

    $(document).on('click', '.delete-employee-btn', function () {
        if (!confirm("Delete this employee?")) return;
        $.post('delete.php', { id: $(this).data('id'), type: 'employee' }, function (res) {
            if (res.trim() === "Delete Success") {
                showMessage(res, 'success');
                refreshTable();
            } else showMessage(res, 'danger');
        });
    });

    $(document).on('click', '.delete-project-btn', function () {
        if (!confirm("Delete this project?")) return;
        $.post('delete.php', { id: $(this).data('id'), type: 'project' }, function (res) {
            if (res.trim() === "Delete Success") {
                showMessage(res, 'success');
                refreshTable();
            } else showMessage(res, 'danger');
        });
    });
});
Editor is loading...
Leave a Comment