<?php

$classMap['PROPERTIES'] = 'DB_PROPERTIES';
$classMap['REFERRALS'] = 'DB_REFERRALS';
$classMap['REFERRAL_KEYS'] = 'DB_REFERRAL_KEYS';

class DB_REFERRALS
{

    public $DB = false;
    public $tablename = 'referrals';
    public $logs;
    public $status;
    use BASICDB;
    use LOGSDB;
    use STATUSDB;
    public function __construct($DB = false)
    {
        if (!$DB) {
            $DB = DB();
        }
        if (!$DB) {
            return false;
        }
        if ($DB) {
            $this->DB = $DB;
        }

        $this->logs = new stdClass();
        $this->logs->setLogbyUID = [$this, 'setLogbyUID'];
        $this->logs->deleteFullLogbyUID = [$this, 'deleteFullLogbyUID'];
        $this->logs->viewLogsbyUID = [$this, 'viewLogsbyUID'];
        $this->logs->deleteLogbyArrayIndex = [$this, 'deleteLogbyArrayIndex'];
        $this->logs->AddLog = [$this, 'AddLog'];
        $this->status = new stdClass();
        $this->status->UpdateStatus = [$this, 'UpdateStatus'];
        $this->status->ViewStatus = [$this, 'ViewStatus'];
        $this->status->SetStatusAsDenied = [$this, 'SetStatusAsDenied'];
        $this->status->SetStatusAsOngoing = [$this, 'SetStatusAsOngoing'];
        $this->status->SetStatusAsNULL = [$this, 'SetStatusAsNULL'];
        $this->status->SetStatusAsNULL = [$this, 'SetStatusAsApproved'];

    }
    function NewReferral($fullname, $mobile, $landline = '', $facebookurl = '', $email = '', $property_location = '', $target_property = '', $target_viewing_date = '', $firstname = '', $middlename = '', $lastname = '', $referral_key = 0, $remarks = '', $alt_contact_details = '', $status = 0)
    {

        if (!$fullname || !$mobile) {

            return false;
        }
        if (!$referral_key) {
            $referral_key = CurrentUserUID();
        }
        if (!$referral_key) {
            $referral_key = '';
        }
        $target_property = $target_property ?? 0;
        $hash = $this->GenerateNewHash();

        if (!$alt_contact_details) {
            $alt_contact_details = '[]';
        } elseif (is_array($alt_contact_details)) {
            $alt_contact_details = json_encode($alt_contact_details);
        }
        $data = [];

        $ConvertToJSONifPossible = function ($datakey, $dataarraay) use (&$data) {
            if (!$datakey) {
                return;
            }
            if ($dataarraay) {
                if (is_array($dataarraay)) {
                    $data[$datakey] = json_encode($dataarraay);
                } else {
                    $data[$datakey] = $dataarraay;
                }
            } else {
                $data[$datakey] = '[]';
            }
        };
        $ifNotNullAddtoArray = function ($datakey, $dataval) use (&$data) {
            if (!$dataval) {
                return;
            }

            $data[$datakey] = $dataval;
        };

        // file_put_contents('results.txt', print_r($email, 1) . print_r($landline, 1));

        //fix target_property in DB

        $data = [
            'hashkey' => $hash,
            'fullname' => $fullname,
            'mobile' => $mobile,
            'referral_key' => $referral_key,
            'email' => $email ?? '',
        ];

        if ($target_property && !is_numeric($target_property)) {
            $target_property = DBQUERY($this->DB)->PROPERTIES()->getUIDfromHashkey($target_property) ?? 0;
        }

        $ifNotNullAddtoArray('referral_key', $referral_key);
        $ifNotNullAddtoArray('landline', $landline);
        $ifNotNullAddtoArray('target_property', $target_property);
        $ifNotNullAddtoArray('property_location', $property_location);


        $ifNotNullAddtoArray('facebookurl', $facebookurl);
        $ifNotNullAddtoArray('firstname', $firstname);
        $ifNotNullAddtoArray('middlename', $middlename);
        $ifNotNullAddtoArray('lastname', $lastname);
        $ifNotNullAddtoArray('remarks', $remarks);
        $ifNotNullAddtoArray('target_viewing_date', $target_viewing_date);



        $ConvertToJSONifPossible('alt_contact_details', $alt_contact_details);




        if ($target_viewing_date) {
            $data['target_viewing_date'] = $target_viewing_date;
        }
        return $this->InsertIntoDB($data);

    }

    public function SetStatusAsDenied($uidorhashkey)
    {
        return $this->status->UpdateStatus($uidorhashkey, -1);
    }
    public function SetStatusAsOngoing($uidorhashkey)
    {
        return $this->status->UpdateStatus($uidorhashkey, 1);
    }
    public function SetStatusAsNULL($uidorhashkey)
    {
        return $this->status->UpdateStatus($uidorhashkey, 0);
    }
    public function SetStatusAsApproved($uidorhashkey)
    {
        return $this->status->UpdateStatus($uidorhashkey, 2);
    }




}

function DB_REFERRALS($DB = false)
{
    return new DB_REFERRALS($DB);
}




class REFERRALS_QUICKMULTIPLESEARCH
{

    use DBClassSearch;
    public $data;

    public $tablename = 'referrals';

    private $parentidresults = [];

    public $DB;
    public function __construct($data = [], $likefields = [], $fieldstoselectarray = '', $orderby = '', $noindex = 0, $whereappend = ' and ', $dateonlyarray = '', $newdata = false)
    {
        return $this->initialize($data, $likefields, $fieldstoselectarray, $orderby, $noindex, $whereappend, $dateonlyarray, $newdata);
    }

    function FindbyExactFullName($name, $newdata = false)
    {
        return $this->Find('fullname', $name, $exact = true, true, $newdata)['name'];
    }
    function ListbySimilarNames($name, $newdata = false, $usestrpos = false)
    {
        return $this->List('fullname', $name, false, true, $newdata, $usestrpos);
    }

}


function REFERRALS_QUICKMULTIPLESEARCH($DB = false)
{
    return new REFERRALS_QUICKMULTIPLESEARCH($DB);
}


class DB_PROPERTIES
{

    public $DB = false;
    public $tablename = 'properties';

    use BASICDB;
    use LOGSDB;
    use STATUSDB;
    public function __construct($DB = false)
    {
        if (!$DB) {
            $DB = DB();
        }
        if (!$DB) {
            return false;
        }
        if ($DB) {
            $this->DB = $DB;
        }
        if (!$this->tablename) {
            return false;
        }

    }
    function NewProperties($name, $description, $location, $photourl, $sqm = '', $rooms = NULL, $bedrooms = '', $kitchen = '', $toilet = '', $floors = '', $specs = '', $category = '', $subcategory = '', $status = 0, $remarks = '', $forbiddenUIDsArray = '', $createdby = '', $price = '')
    {
        if (!$name || !$description || !$location || !$photourl) {
            return false;
        }
        if ($forbiddenUIDsArray and is_array($forbiddenUIDsArray)) {
            $forbiddenUIDsArray = json_encode($forbiddenUIDsArray);
        }
        $data = [];

        $ConvertToJSONifPossible = function ($datakey, $dataarraay) use (&$data) {
            if (!$datakey) {
                return;
            }
            if ($dataarraay) {
                if (is_array($dataarraay)) {
                    $data[$datakey] = json_encode($dataarraay);
                } else {
                    $data[$datakey] = $dataarraay;
                }
            } else {
                $data[$datakey] = '[]';
            }
        };


        $ifNotNullAddtoArray = function ($datakey, $dataval) use (&$data) {
            if (!$dataval) {
                return;
            }
            $data[$datakey] = $dataval;
        };

        $hashkey = $this->GenerateNewHash();

        if (!$createdby) {
            $createdby = CurrentUserUID();
        }
        if ($createdby) {
            $data['createdby'] = $createdby;
        }
        $data['hashkey'] = $hashkey;
        $data['name'] = $name;
        $data['description'] = $description;
        $data['location'] = $location;
        $ConvertToJSONifPossible('photourl', $photourl);
        $ifNotNullAddtoArray('sqm', $sqm);
        $ifNotNullAddtoArray('rooms', $rooms);
        $ifNotNullAddtoArray('bedrooms', $bedrooms);
        $ifNotNullAddtoArray('kitchen', $kitchen);
        $ifNotNullAddtoArray('toilet', $toilet);
        $ifNotNullAddtoArray('floors', $floors);
        $ifNotNullAddtoArray('category', $category);
        $ifNotNullAddtoArray('subcategory', $subcategory);
        $ifNotNullAddtoArray('price', $price);
        $ConvertToJSONifPossible('specs', $specs);

        $data['status'] = $status ?? 0;
        $data['remarks'] = $remarks ?? '';
        $data['forbiddenuids'] = $forbiddenUIDsArray ?? '';
        $data['logs'] = '[["' . serverdatetimesql() . '","Created Property"]]';

        $Insert = $this->InsertIntoDB($data);
        return $Insert;


    }


}

function DB_PROPERTIES($DB = false)
{
    return new DB_PROPERTIES($DB);
}

class PROPERTIES_QUICKMULTIPLESEARCH
{

    use DBClassSearch;
    public $data;

    public $tablename = 'properties';

    private $parentidresults = [];
    public $DB;

    public function __construct($data = [], $likefields = [], $fieldstoselectarray = '', $orderby = '', $noindex = 0, $whereappend = ' and ', $dateonlyarray = '', $newdata = false)
    {
        return $this->initialize($data, $likefields, $fieldstoselectarray, $orderby, $noindex, $whereappend, $dateonlyarray, $newdata);
    }

    function FindName($uid)
    {
        return $this->Find($fieldname = 'uid', $contenttosearch = $uid, $exact = true)[0]['name'];
    }

}

function PROPERTIES_QUICKMULTIPLESEARCH($DB = false)
{
    return new PROPERTIES_QUICKMULTIPLESEARCH($DB);
}

class DB_REFERRAL_KEYS
{

    public $DB = false;
    public $tablename = 'referral_keys';

    use BASICDB;
    public function __construct($DB = false)
    {
        if (!$DB) {
            $DB = DB();
        }
        if (!$DB) {
            return false;
        }
        if ($DB) {
            $this->DB = $DB;
        }
        if (!$this->tablename) {
            return false;
        }

    }
    function NewReferralKey($useruid = '', $propertyuid = 0, $referral_code = 0)
    {
        if (!$useruid) {
            $useruid = CurrentUserUID();
        }
        if (!$useruid) {
            $useruid = '';
        }
        if ($useruid and is_numeric($useruid)) {
            $useruid = DB_USERS($this->DB)->GetUserDatabyUID($useruid)['uid'] ?? '';
        }
        $hashkey = $this->GenerateNewHash();
        $data['hashkey'] = $hashkey;
        $data['useruid'] = $useruid ?? 0;
        $data['propertyuid'] = $propertyuid ?? 0;



        if ($referral_code) {
            $referral_code_exists = $this->getDetailsbyReferral_Key($referral_code);
            if ($referral_code_exists) {
                return false;
            }
            $data['referral_code'] = $referral_code;
        } else {
            $data['referral_code'] = generateUniqueReferralCode($this->DB, 'referral_code', $this->tablename);
        }

        $Insert = $this->InsertIntoDB($data);
        return $Insert;
    }
    function getDetailsbyReferral_Key($referral_code, $fieldstoselect = '')
    {
        if (!$referral_code) {
            return false;
        }
        $details = $this->ListFromDB(['referral_code' => $referral_code], [], $fieldstoselect);
        return $details[0] ?? false;
    }



}

function DB_REFERRAL_KEYS($DB = false)
{
    return new DB_REFERRAL_KEYS($DB);
}

class REFERRAL_KEYS_QUICKMULTIPLESEARCH
{

    use DBClassSearch;
    public $data;

    public $tablename = 'referral_keys';

    private $parentidresults = [];
    public $DB;

    public function __construct($data = [], $likefields = [], $fieldstoselectarray = '', $orderby = '', $noindex = 0, $whereappend = ' and ', $dateonlyarray = '', $newdata = false, $DB = false)
    {
        return $this->initialize($data, $likefields, $fieldstoselectarray, $orderby, $noindex, $whereappend, $dateonlyarray, $newdata, $DB);
    }

    function FindName($uid)
    {
        return $this->Find($fieldname = 'uid', $contenttosearch = $uid, $exact = true)[0]['name'];
    }
    function GetReferralCodebyUID($uidorhashkey, $newdata = false)
    {
        return $this->getValueByUIDorHashkey($uidorhashkey, 'referral_code', $newdata);
    }

}

function REFERRAL_KEYS_QUICKMULTIPLESEARCH($DB = false)
{
    return new REFERRAL_KEYS_QUICKMULTIPLESEARCH($DB);
}

?>