<?php 
require_once __DIR__ . '/../../functions/security.php';
require_once __DIR__ . '/../../functions/logging.php';

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

$action = enrep_request_enum('FUNCTION', ['insert', 'update', 'convert', 'archive', 'reinstate'], '');
enrep_log_request_completion('brokers', 'FUNCTION', [
    'insert' => ['action' => 'created', 'message' => 'Created broker', 'entity_type' => 'brokers'],
    'update' => ['action' => 'updated', 'message' => 'Updated broker', 'entity_type' => 'brokers'],
    'convert' => ['action' => 'converted_to_agent', 'message' => 'Converted broker to agent', 'entity_type' => 'brokers'],
    'archive' => ['action' => 'archived', 'message' => 'Archived broker', 'entity_type' => 'brokers'],
    'reinstate' => ['action' => 'reinstated', 'message' => 'Reinstated broker', 'entity_type' => 'brokers'],
]);

function broker_statuslive(): int
{
    return enrep_request_string('statuslive', '', 20) === 'on' ? 1 : 0;
}

switch ($action) {
    default:
        break;

    case "insert":
        $brokerName = enrep_request_string('broker_name', '', 255);
        $address1 = enrep_request_string('address1', '', 255);
        $address2 = enrep_request_string('address2', '', 255);
        $town = enrep_request_string('town', '', 120);
        $county = enrep_request_string('county', '', 120);
        $post = enrep_request_string('post', '', 40);
        $tel = enrep_request_string('tel', '', 80);
        $fax = enrep_request_string('fax', '', 80);
        $email = enrep_request_string('email', '', 255);
        $billingName = enrep_request_string('billing_name', '', 255);
        $billing1 = enrep_request_string('billing1', '', 255);
        $billing2 = enrep_request_string('billing2', '', 255);
        $billingTown = enrep_request_string('billing_town', '', 120);
        $billingCounty = enrep_request_string('billing_county', '', 120);
        $billingPostcode = enrep_request_string('billing_postcode', '', 40);
        $billingPhone = enrep_request_string('billing_phone', '', 80);
        $billingEmail = enrep_request_string('billing_email', '', 255);
        $priContact = enrep_request_string('pri_contact', '', 255);

        $stmt = $GLOBALS['con']->prepare("INSERT INTO `brokers` VALUES(NULL,?,?,?,?,?,?,?,?,?,'',?,?,?,?,?,?,?,?,'',?,0,'','1')") or die(mysqli_error($GLOBALS['con']));
        $stmt->bind_param(
            'ssssssssssssssssss',
            $brokerName,
            $address1,
            $address2,
            $town,
            $county,
            $post,
            $tel,
            $fax,
            $email,
            $billingName,
            $billing1,
            $billing2,
            $billingTown,
            $billingCounty,
            $billingPostcode,
            $billingPhone,
            $billingEmail,
            $priContact
        );
        $stmt->execute();
        $error = $stmt->error;
        echo empty($error) ? mysqli_insert_id($GLOBALS['con']) : "error" . $error;
        $stmt->close();
        
        exit();

    case "update":
        $brokerNumber = enrep_request_int('broker_number', 0);
        $brokerName = enrep_request_string('broker_name', '', 255);
        $address1 = enrep_request_string('address1', '', 255);
        $address2 = enrep_request_string('address2', '', 255);
        $town = enrep_request_string('town', '', 120);
        $county = enrep_request_string('county', '', 120);
        $post = enrep_request_string('post', '', 40);
        $tel = enrep_request_string('tel', '', 80);
        $fax = enrep_request_string('fax', '', 80);
        $email = enrep_request_string('email', '', 255);
        $password = enrep_request_string('password', '', 255);
        $billingName = enrep_request_string('billing_name', '', 255);
        $billing1 = enrep_request_string('billing1', '', 255);
        $billing2 = enrep_request_string('billing2', '', 255);
        $billingTown = enrep_request_string('billing_town', '', 120);
        $billingCounty = enrep_request_string('billing_county', '', 120);
        $billingPostcode = enrep_request_string('billing_postcode', '', 40);
        $billingPhone = enrep_request_string('billing_phone', '', 80);
        $billingEmail = enrep_request_string('billing_email', '', 255);
        $priContact = enrep_request_string('pri_contact', '', 255);
        $commissionRate = enrep_request_string('commission_rate', '0', 40);
        $statuslive = broker_statuslive();

        $stmt = $GLOBALS['con']->prepare(
            "UPDATE `brokers` SET `broker_name`=?, `address1`=?, `address2`=?, `town`=?, `county`=?, `post`=?, `tel`=?, `fax`=?, `email`=?, `billing_name`=?, `billing1`=?, `billing2`=?, `billing_town`=?, `billing_county`=?, `billing_postcode`=?, `billing_phone`=?, `billing_email`=?, `password`=AES_ENCRYPT(?, ?), `pri_contact`=?, `commission_rate`=?, `statuslive`=? WHERE `broker_number`=?"
        ) or die(mysqli_error($GLOBALS['con']));
        $stmt->bind_param(
            'sssssssssssssssssssssii',
            $brokerName,
            $address1,
            $address2,
            $town,
            $county,
            $post,
            $tel,
            $fax,
            $email,
            $billingName,
            $billing1,
            $billing2,
            $billingTown,
            $billingCounty,
            $billingPostcode,
            $billingPhone,
            $billingEmail,
            $password,
            $email,
            $priContact,
            $commissionRate,
            $statuslive,
            $brokerNumber
        );
        $stmt->execute();
        $stmt->close();
        echo "success";
        exit();

    case "convert":
        $id = enrep_request_int('id', 0);
        $stmt = $GLOBALS['con']->prepare(
            "INSERT INTO `agents`
             SELECT NULL, `broker_name`, `address1`, `address2`, `town`, `county`, `post`, `tel`, `fax`, `email`, `password`, `billing_name`, `billing1`, `billing2`, `billing_town`, `billing_county`, `billing_postcode`, `billing_phone`, `billing_email`, `access`, `pri_contact`, `commission_rate`, `notes`, `statuslive`
             FROM `brokers` WHERE `broker_number`=?"
        ) or die(mysqli_error($GLOBALS['con']));
        $stmt->bind_param('i', $id);
        $stmt->execute();
        $stmt->close();

        $stmt = $GLOBALS['con']->prepare("DELETE FROM `brokers` WHERE `broker_number`=?") or die(mysqli_error($GLOBALS['con']));
        $stmt->bind_param('i', $id);
        $stmt->execute();
        $stmt->close();
        break;

    case "archive":
    case "reinstate":
        $id = enrep_request_int('id', 0);
        $statuslive = $action === 'archive' ? 0 : 1;
        $stmt = $GLOBALS['con']->prepare("UPDATE `brokers` SET `statuslive`=? WHERE `broker_number`=?") or die(mysqli_error($GLOBALS['con']));
        $stmt->bind_param('ii', $statuslive, $id);
        $stmt->execute();
        $stmt->close();
        break;
}
?>
