<?php ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
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');
}


$companyFunction = (string)($_REQUEST['FUNCTION'] ?? '');
enrep_log_request_completion('company', 'FUNCTION', [
    'update' => ['action' => 'updated', 'message' => 'Updated company settings', 'entity_type' => 'company'],
]);

switch ($companyFunction) {
    default:
        break;


    case "update":

        $stmt = $GLOBALS['con']->prepare("UPDATE `company` SET  `company`=?, `company_short`=?, `address1`=?, `address2`=?, `town`=?, `county`=?, `post`=?, `tel`=?, `sectel`=?, `mob`=?, `fax`=?, `main_email`=?, `finance_email`=?, `web`=?, `vatno`=?, `compno`=?, `pri_name`=?, `pri_add1`=?, `pri_add2`=?, `pri_town`=?, `pri_cou`=?, `pri_post`=?, `pri_tel`=?, `pri_eml`=?, `sec_name`=?, `sec_add1`=?, `sec_add2`=?, `sec_town`=?,`sec_cou`=?, `sec_post`=?, `sec_tel`=?, `sec_eml`=?, `bankName`=?, `accountNo`=?, `sortCode`=?, `vatrate`=?, `ppStatus`=?, `ppEmail`=?, `ppAuth`=?, `tax_YB`=?, `tax_YE`=?, `tax_CY`=?, `tax_CS`=?, `hmrcVRN`=?, `hmrcServerToken`=?, `hmrcClientId`=?, `hmrcClientSecret`=?, `rapid`=?, `repMode`=? WHERE `id`=1") or die(mysqli_error());
        $stmt->bind_param('ssssssssssssssssssssssssssssssssssssssssssssssssi', $_REQUEST['company'], $_REQUEST['company_short'], $_REQUEST['address1'], $_REQUEST['address2'], $_REQUEST['town'], $_REQUEST['county'], $_REQUEST['post'], $_REQUEST['tel'], $_REQUEST['sectel'], $_REQUEST['mob'], $_REQUEST['fax'], $_REQUEST['main_email'], $_REQUEST['finance_email'], $_REQUEST['web'], $_REQUEST['vatno'], $_REQUEST['compno'], $_REQUEST['pri_name'], $_REQUEST['pri_add1'], $_REQUEST['pri_add2'], $_REQUEST['pri_town'], $_REQUEST['pri_cou'], $_REQUEST['pri_post'], $_REQUEST['pri_tel'], $_REQUEST['pri_eml'], $_REQUEST['sec_name'], $_REQUEST['sec_add1'], $_REQUEST['sec_add2'], $_REQUEST['sec_town'], $_REQUEST['sec_cou'], $_REQUEST['sec_post'], $_REQUEST['sec_tel'], $_REQUEST['sec_eml'], $_REQUEST['bankName'], $_REQUEST['accountNo'], $_REQUEST['sortCode'], $_REQUEST['vatrate'], $_REQUEST['ppStatus'], $_REQUEST['ppEmail'], $_REQUEST['ppAuth'], strtotime($_REQUEST['tax_YB']), strtotime($_REQUEST['tax_YE']), $_REQUEST['tax_CY'], $_REQUEST['tax_CS'], $_REQUEST['hmrcVRN'], $_REQUEST['hmrcServerToken'], $_REQUEST['hmrcClientId'], $_REQUEST['hmrcClientSecret'], $_REQUEST['rapid'], $_REQUEST['repMode']);
        $stmt->execute();
        echo "success";
        $stmt->close();
        $admin2fa = isset($_REQUEST['admin_2fa_enabled']) ? 1 : 0;
        if (isset($admin_con) && $admin_con instanceof mysqli) {
            $q = $admin_con->prepare("UPDATE client_subscriptions SET admin_2fa_enabled=? WHERE subdomain_prefix=? LIMIT 1");
            if ($q) {
                $q->bind_param('is', $admin2fa, $dbTenant);
                $q->execute();
                $q->close();
            }
        }

        exit();
        break;
}
