<?php 
require_once __DIR__ . '/../../functions/logging.php';
if (file_exists('../../../' . $_REQUEST['tenant'] . '/config.php')) {
    require_once('../../../' . $_REQUEST['tenant'] . '/config.php');
}
if (file_exists('/config.php')) {
    require_once('/config.php');
}

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$locationFunction = (string)($_REQUEST['FUNCTION'] ?? '');
enrep_log_request_completion('locations', 'FUNCTION', [
    'moveAssets' => ['action' => 'assets_moved', 'message' => 'Moved assets between locations', 'entity_type' => 'locations'],
    'delloc' => ['action' => 'deleted', 'message' => 'Deleted location and moved assets', 'entity_type' => 'locations'],
    'archiveLoc' => ['action' => 'archived', 'message' => 'Archived location', 'entity_type' => 'locations'],
    'reinstate' => ['action' => 'reinstated', 'message' => 'Reinstated location', 'entity_type' => 'locations'],
    'dellocEmpty' => ['action' => 'deleted', 'message' => 'Deleted empty location', 'entity_type' => 'locations'],
    'insert' => ['action' => 'created', 'message' => 'Created location', 'entity_type' => 'locations'],
    'update' => ['action' => 'updated', 'message' => 'Updated location', 'entity_type' => 'locations'],
    'moveClient' => ['action' => 'client_moved', 'message' => 'Moved location to another client', 'entity_type' => 'locations'],
]);

switch ($locationFunction) {
    default:
        break;

    case "moveAssets":
        $q = mysqli_query($GLOBALS['con'], "UPDATE `itemdetails` SET `locnumber`='" . $_REQUEST['newloc'] . "' WHERE `clientnumber`='" . $_REQUEST['clientnumber'] . "' AND `locnumber`='" . $_REQUEST['oldloc'] . "'") or die(mysqli_error($GLOBALS['con']));
        echo "s";
        break;

    case "delloc":
        $q = mysqli_query($GLOBALS['con'], "UPDATE `itemdetails` SET `locnumber`='" . $_REQUEST['newloc'] . "' WHERE `clientnumber`='" . $_REQUEST['clientnumber'] . "' AND `locnumber`='" . $_REQUEST['oldloc'] . "'") or die(mysqli_error($GLOBALS['con']));
        $q = mysqli_query($GLOBALS['con'], "DELETE FROM `locations` WHERE `locnumber`='" . $_REQUEST['oldloc'] . "'") or die(mysqli_error($GLOBALS['con']));
        echo "s";
        break;

    case "archiveLoc":
        $q = mysqli_query($GLOBALS['con'], "UPDATE `itemdetails` SET `statuslive`='0' WHERE `clientnumber`='" . $_REQUEST['clientnumber'] . "' AND `locnumber`='" . $_REQUEST['locnumber'] . "'") or die(mysqli_error($GLOBALS['con']));
        $q = mysqli_query($GLOBALS['con'], "UPDATE `locations` SET `statuslive`='0' WHERE `locnumber`='" . $_REQUEST['locnumber'] . "'") or die(mysqli_error($GLOBALS['con']));
        echo "s";
        break;

    case "reinstate":
        $q = mysqli_query($GLOBALS['con'], "UPDATE `itemdetails` SET `statuslive`='1' WHERE `clientnumber`='" . $_REQUEST['clientnumber'] . "' AND `locnumber`='" . $_REQUEST['locnumber'] . "'") or die(mysqli_error($GLOBALS['con']));
        $q = mysqli_query($GLOBALS['con'], "UPDATE `locations` SET `statuslive`='1' WHERE `locnumber`='" . $_REQUEST['locnumber'] . "'") or die(mysqli_error($GLOBALS['con']));
        echo "s";
        break;

    case "dellocEmpty":
        $q = mysqli_query($GLOBALS['con'], "DELETE FROM `locations` WHERE `locnumber`='" . $_REQUEST['oldloc'] . "'") or die(mysqli_error($GLOBALS['con']));
        echo "s";
        break;

    case "insert":
       $contractorNumber = isset($_SESSION['contractorNumber']) && $_SESSION['contractorNumber'] !== ''
    ? (int)$_SESSION['contractorNumber']
    : null;

// ...
$q = mysqli_query($GLOBALS['con'], "INSERT INTO `locations` VALUES(
'" . (int)$_REQUEST['clientnumber'] . "',
NULL,
'" . mysqli_real_escape_string($con, $_REQUEST['locname']) . "',
'" . mysqli_real_escape_string($con, $_REQUEST['locaddress1']) . "',
'" . mysqli_real_escape_string($con, $_REQUEST['locaddress2']) . "',
'" . mysqli_real_escape_string($con, $_REQUEST['locaddress3']) . "',
'" . mysqli_real_escape_string($con, $_REQUEST['locaddress4']) . "',
'" . mysqli_real_escape_string($con, $_REQUEST['postcode']) . "',
'" . mysqli_real_escape_string($con, $_REQUEST['phone1']) . "',
'" . mysqli_real_escape_string($con, $_REQUEST['phone2']) . "',
'" . mysqli_real_escape_string($con, $_REQUEST['mobile']) . "',
'" . mysqli_real_escape_string($con, $_REQUEST['fax']) . "',
'" . mysqli_real_escape_string($con, $_REQUEST['email']) . "',
'" . mysqli_real_escape_string($con, $_REQUEST['contactlc']) . "',
'" . mysqli_real_escape_string($con, $_REQUEST['contactpm']) . "',
'" . mysqli_real_escape_string($con, $_REQUEST['contacttel']) . "',
'" . mysqli_real_escape_string($con, $_REQUEST['contactother']) . "',
" . ((isset($_REQUEST['statuslive']) && $_REQUEST['statuslive']==='on') ? 1 : 0) . ",
'" . mysqli_real_escape_string($con, $_REQUEST['locnotes']) . "',
'" . mysqli_real_escape_string($con, $_REQUEST['report_format']) . "',
" . ($contractorNumber === null ? "NULL" : (int)$contractorNumber) . ",
'" . mysqli_real_escape_string($con, $_REQUEST['locref']) . "'
)") or die(mysqli_error($GLOBALS['con']));

        break;

    case "update":
        if ($_REQUEST['statuslive'] == "on") {
            $_REQUEST['statuslive'] = "1";
        }
        if ($_REQUEST['statuslive'] == "off" || $_REQUEST['statuslive'] == "") {
            $_REQUEST['statuslive'] = "0";
        }
        $_REQUEST['locnotes'] = mysqli_real_escape_string($con, $_REQUEST['locnotes']);
        $_REQUEST['locname'] = mysqli_real_escape_string($con, $_REQUEST['locname']);
        $_REQUEST['locref'] = mysqli_real_escape_string($con, $_REQUEST['locref']);
        $_REQUEST['locaddress1'] = mysqli_real_escape_string($con, $_REQUEST['locaddress1']);
        $_REQUEST['locaddress2'] = mysqli_real_escape_string($con, $_REQUEST['locaddress2']);
        $_REQUEST['locaddress3'] = mysqli_real_escape_string($con, $_REQUEST['locaddress3']);
           $_REQUEST['contactlc'] = mysqli_real_escape_string($con, $_REQUEST['contactlc']);
         $_REQUEST['contactpm'] = mysqli_real_escape_string($con, $_REQUEST['contactpm']);
        $q = mysqli_query($GLOBALS['con'], "UPDATE `locations` SET `locname`='" . $_REQUEST['locname'] . "', `locaddress1`='" . $_REQUEST['locaddress1'] . "', `locaddress2`='" . $_REQUEST['locaddress2'] . "', `locaddress3`='" . $_REQUEST['locaddress3'] . "',
`locaddress4`='" . $_REQUEST['locaddress4'] . "', `postcode`='" . $_REQUEST['postcode'] . "', `phone1`='" . $_REQUEST['phone1'] . "', `phone2`='" . $_REQUEST['phone2'] . "', `mobile`='" . $_REQUEST['mobile'] . "', `fax`='" . $_REQUEST['fax'] . "',
`email`='" . $_REQUEST['email'] . "', `contactlc`='" . $_REQUEST['contactlc'] . "', `contactpm`='" . $_REQUEST['contactpm'] . "', `contacttel`='" . $_REQUEST['contacttel'] . "', `contactother`='" . $_REQUEST['contactother'] . "',
`statuslive`='" . $_REQUEST['statuslive'] . "', `locnotes`='" . $_REQUEST['locnotes'] . "', `report_format`='" . $_REQUEST['report_format'] . "', `locref`='" . $_REQUEST['locref'] . "'
WHERE `clientnumber`='" . $_REQUEST['clientnumber'] . "' AND `locnumber`='" . $_REQUEST['locnumber'] . "'") or die(mysqli_error($GLOBALS['con']));

        echo $_REQUEST['statuslive'];

        exit();
        break;

case "loadInfo":
    header('Content-Type: application/json; charset=utf-8');

    $clientnumber = isset($_REQUEST['clientnumber']) ? (int)$_REQUEST['clientnumber'] : 0;

    $stmt = $GLOBALS['con']->prepare(
        "SELECT address1,address2,town,county,postcode,phone1,mobile,email,pri_contact_name,pri_contact_tel
         FROM clientdetails WHERE clientnumber=?"
    ) or die(mysqli_error($GLOBALS['con']));
    $stmt->bind_param('i', $clientnumber);
    $stmt->execute();
    $res = $stmt->get_result();
    $row = $res->fetch_assoc() ?: [];
    $stmt->close();

    echo json_encode($row, JSON_UNESCAPED_UNICODE);
    exit;

    case "moveClient":
        $q = mysqli_query($GLOBALS['con'], "UPDATE `itemdetails` SET `clientnumber`='" . $_REQUEST['newClient'] . "' WHERE `clientnumber`='" . $_REQUEST['clientnumber'] . "' AND `locnumber`='" . $_REQUEST['locnumber'] . "'") or die(mysqli_error($GLOBALS['con']));
        $q = mysqli_query($GLOBALS['con'], "UPDATE `locations` SET `clientnumber`='" . $_REQUEST['newClient'] . "' WHERE `locnumber`='" . $_REQUEST['locnumber'] . "'") or die(mysqli_error($GLOBALS['con']));
        echo "s";
        exit();
        break;
}
