require_once 'access.php';
require_once 'dal.php';
require_once 'utils.php';
require_once 'email.php';
$error = null;
$message = null;
$user = null;
$unique_id = null;
$dal = new DAL($opts['hn'], $opts['db'], $opts['un'], $opts['pw']);
try {
    $dal->beginTransaction();
    $user = $dal->selectUser();
    if ($user === null) {
        throw new UserException('Login required.');
    }
    $unique_id = $dal->selectUniqueId($_GET['uniqueid_id']);
    if ($unique_id === null) {
        throw new UserException('Range not found.');
    }
    if ($user['person_id'] !== $unique_id['person_id'] && $user['person_is_moderator'] !== 'y') {
        throw new UserException('Moderator login required.');
    }
    if (isset($_POST['transfer'])) {
        $person = $dal->selectPersonByEmail($_POST['email']);
        if ($person === null) {
            throw new UserException('Profile not found.');
        }
        $unique_id['person_id'] = $person['person_id'];
        $dal->updateUniqueId($unique_id);
        $subject = "OpenLCB Unique ID Range Transferred";
        $body = "Hi " . formatPersonName($unique_id) . ",\r\n\r\nThe following OpenLCB Unique ID Range has been Transferred.\r\n\r\n" . formatUniqueIdHex($unique_id) . "\r\n\r\nFrom delegating organization or person: " . formatPersonName($unique_id) . "\r\nTo delegating organization or person: " . formatPersonName($person) . "\r\nURL: " . $unique_id['uniqueid_url'] . "\r\nComment: " . $unique_id['uniqueid_user_comment'] . "\r\n\r\nThe OpenLCB Group";