<?php switch($_REQUEST['view']){
default:exit();break;
case 'new': require_once(ROOT_URL.'/includes/contractor/contractor_new.php');break;
case 'edit': require_once(ROOT_URL.'/includes/contractor/contractor_edit.php');break;
case"load": require_once(ROOT_URL.'/includes/contractor/contractor_edit.php');break;
case "list": require_once(ROOT_URL.'/includes/contractor/contractor_list.php');break;
case "clientlist": require_once(ROOT_URL.'/includes/contractor/contractor_clientlist.php');break;
case "assign": require_once(ROOT_URL.'/includes/contractor/contractor_assign.php');break;
}
?>
<script>
function inlineFilter(i) {
    var table = $('#datatable').DataTable();
    table.search(i).draw();
};

document.getElementById('contractorForm').addEventListener('submit', function(event) {
    event.preventDefault();
    const form = document.getElementById('contractorForm');
    const formData = new FormData(form);

    fetch(ROOT_URL + '/includes/contractor/contractor_functions.php', {
            method: 'POST',
            body: new URLSearchParams(formData)
        })
        .then(response => response.text())
        .then(result => {

            const functionType = document.getElementById('function').value;
            if (functionType === "insert") {
                window.location.href = 'index.php?t=includes/master&p=contractor.php&contractor_number=' + result + '&view=edit';
            } else {
                if (result.trim() === "success") {
                    showResponse('success', 'Your changes have been saved successfully');
                } else {
                    showResponse('danger', 'There was a problem, please check your details and try again<br><br>' + result);
                }
            }
        })
        .catch(error => console.error('Error:', error));
})

// Function to handle email button clicks
document.body.addEventListener('click', function(event) { // Event delegation on body
    if (event.target.classList.contains('emailButton')) {
        const target = event.target.dataset.href;
        const string = event.target.dataset.values;
        var dest = encodeURI(target + "?data=" + string + "&tenant=" + TENANT_URL);
        loadModalContent(dest, "popupBox", "modal-xl", 1060, "true");

    }
});

function archiveContractor(c) {
    fetch(ROOT_URL + '/includes/contractor/contractor_functions.php', {
            method: 'POST',
            body: new URLSearchParams({
                FUNCTION: 'archive',
                contractornumber: c,
                tenant: TENANT_URL
            })
        })
        .then(response => response.text())
        .then(result => {
            if (result.trim() === "success") {
                showResponse('success', 'Your changes have been saved successfully');
            }
            window.location.href = 'index.php?t=includes/master&p=contractor.php&view=list';
        })
        .catch(error => console.error('Error:', error));
}

// Activate contractor function
function activateContractor(c) {
    fetch(ROOT_URL + '/includes/contractor/contractor_functions.php', {
            method: 'POST',
            body: new URLSearchParams({
                FUNCTION: 'activate',
                contractornumber: c,
                tenant: TENANT_URL
            })
        })
        .then(response => response.text())
        .then(result => {
            if (result.trim() === "success") {
                showResponse('success', 'Your changes have been saved successfully');
            }
            window.location.href = 'index.php?t=includes/master&p=contractor.php&view=list';
        })
        .catch(error => console.error('Error:', error));
}
</script>