<?php 
if ($_REQUEST['mode'] == "removeReport") {

    if (file_exists('../../../' . $_REQUEST['tenant'] . '/config.php')) {
        require_once('../../../' . $_REQUEST['tenant'] . '/config.php');
    }

    if (file_exists('/config.php')) {
        require_once('/config.php');
    }
    $sql = mysqli_query($GLOBALS['con'], "DELETE FROM `reportitems_temp` WHERE `repitem`='" . $_REQUEST['id'] . "'") or die(mysqli_error($GLOBALS['con']));

    exit();
}
if ($_REQUEST['mode'] == "external") {
    if (file_exists('../../../' . $tenant . '/config.php')) {
        require_once('../../../' . $tenant . '/config.php');
    }
    if (file_exists('/config.php')) {
        require_once('/config.php');
    }
    require_once(ROOT_URL . '/form_functions.php');
} else {
    require_once(ROOT_URL . '/includes/master/form_functions.php');
}

switch ($_REQUEST['view']) {
    default:
        $d = explode("|", $_REQUEST['data']);
        $stmt = $GLOBALS['con']->prepare("SELECT `repitem`,`insdate` WHERE `itemnumber`=?") or die(mysqli_error($GLOBALS['con']));
        $stmt->bind_param('i', $d[1]);
        $stmt->execute();
        $r = $stmt->get_result();
        $stmt->close();
        while ($dta = mysqli_fetch_array($r)) { ?>
<p> <?= $dta['repitem'] ?></p>

<?php }
        ?>

<?php break;

    case "new":
        require(ROOT_URL . '/includes/items/new.php');
        break;
    case "list":
        require(ROOT_URL . '/includes/items/list.php');
        break;
    case "incomplete":
        require(ROOT_URL . '/includes/items/incomplete.php');
        break;
    case "view":
        require(ROOT_URL . '/includes/items/view.php');
        break;
}
?>



<script>
$("#itemForm input[type='submit']").click(function(event) {
    event.preventDefault();
    $.ajax({
        url: 'includes/master/location_functions.php',
        type: 'POST',
        data: $('#locationForm').serialize(),
        success: function(result) {
            document.location.href =
                'index.php?t=includes/master&p=locations.php&clientnumber=<?= $_REQUEST['clientnumber'] ?>&clientname=<?= $_REQUEST['clientname'] ?>'
        }
    });
});

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, "mailerBox", "modal-xl", 1060, "true", "Direct Mailer");

    }
});
</script>