if (!isset($_GET) || $_GET["name"] === NULL) {
    echo "KO";
    die;
}
$status = $_GET["status"];
$rector = new Rector();
$person = PersonManager::getSinglePerson('names', $_GET["name"], 'lastname1', $_GET["lastname1"], 'lastname2', $_GET["lastname2"]);
if ($person === NULL) {
    $person = new Person(0, $_GET["name"], $_GET["lastname1"], $_GET["lastname2"]);
    PersonManager::addPerson($person);
    $person = PersonManager::getSinglePerson('names', $_GET["name"], 'lastname1', $_GET["lastname1"], 'lastname2', $_GET["lastname2"]);
}
$church = ChurchManager::getSingleChurch('name', $_GET["actualChurch"]);
$rector->setIdPerson($person->getId());
$rector->setType($_GET["type"]);
$rector->setPosition($_GET["position"]);
$rector->setIdActualChurch($church->getId());
if ($_GET["statusR"] === 'Activo' || $_GET["statusR"] === 'Active') {
    $rector->setStatus('A');
} else {
    $rector->setStatus('I');
}
if ($status === 'update') {
    $rector->setId($_GET["id"]);
    if (RectorManager::updateRector($rector)) {
        echo "OK";
    } else {
        echo "KO";
    }
} else {
    if ($status === 'insert') {
} else {
    if ($kid !== NULL) {
        $rectorSearch = new Rector();
        $kid = $_GET["kid"];
        $kname = $_GET["kname"];
        $klastname1 = $_GET["klastname1"];
        $klastname2 = $_GET["klastname2"];
        $kchurch = $_GET["kchurch"];
        $ktype = $_GET["ktype"];
        $kstatus = $_GET["kstatus"];
        $kposition = $_GET["kposition"];
        $rector = new Rector();
        $rector->setId($kid);
        $rector->setType($ktype);
        $rector->setStatus($kstatus);
        $rector->setPosition($kposition);
        $posibleNames = PersonManager::searchPersonsByNames($kname, $klastname1, $klastname2, false);
        $rector->setIdPerson($posibleNames);
        $posibleChurch = ChurchManager::simpleSearchChurch($kchurch, 'id', -1);
        $rector->setIdActualChurch($posibleChurch);
        $rectorRegistries = RectorManager::advancedSearchRector($rector, 'AND', $sortType, $numberPage);
    } else {
        $rectorRegistries = RectorManager::getAllRectors($sortType, $numberPage);
    }
}
//Get the total of registries
$totalRegistries = DatabaseManager::getAffectedRows();
$affectedRegistries = DatabaseManager::registriesAffectedLastQuery();
if ($totalRegistries === 0 && $numberPage !== 0) {
    echo "<script src='../JS/functions.js'></script><script>nextPage('set', '0')</script>";
}