<?php

require_once __DIR__ . "/../../../Backend/SessionManager.php";
require_once __DIR__ . "/../../../Backend/CommunionManager.php";
if (!isset($_GET) || $_GET["idCommunion"] === NULL) {
    echo "KO";
} else {
    $communion = CommunionManager::getSingleCommunion("id", $_GET["idCommunion"]);
    if ($communion === NULL) {
        echo "KO";
    } else {
        if (!isset($_SESSION) || $_SESSION["user_church"] === NULL) {
            echo "KO";
        } else {
            //Check for permision
            if ($_SESSION["user_type"] == 'A' || $_SESSION["user_church"] == $communion->getIdChurch()) {
                if (CommunionManager::removeCommunion($communion)) {
                    echo "OK";
                } else {
                    echo "KO";
                }
            }
        }
    }
}
if ($_SESSION["user_type"] != 'A') {
    if ($_SESSION["user_church"] == $church->getId()) {
        if ($_POST["status"] === 'insert') {
            if (CommunionManager::addCommunion($communion)) {
                echo "OK";
            } else {
                echo "KO";
            }
        } else {
            if ($_POST["status"] === 'update') {
                CommunionManager::updateCommunion($communion);
                echo "OK";
            }
        }
    } else {
        echo "KO";
    }
} else {
    if ($_POST["status"] === 'insert') {
        if (CommunionManager::addCommunion($communion)) {
            echo "OK";
        } else {
            echo "KO";
        }
    } else {
        if ($_POST["status"] === 'update') {
            CommunionManager::updateCommunion($communion);
            echo "OK";
        }
    }
}
//Get The Data of the Communion
$idCommunion = $_GET["id"];
$communion = CommunionManager::getSingleCommunion('id', $idCommunion);
$ownerId = $communion->getIdOwner();
$owner = PersonManager::getSinglePerson('id', $ownerId);
$owner->getIdFather() !== NULL ? $fatherId = $owner->getIdFather() : ($fatherId = 0);
$owner->getIdMother() !== NULL ? $motherId = $owner->getIdMother() : ($motherId = 0);
$gender = $owner->getGender();
$churchId = $communion->getIdChurch();
$communionDate = DatabaseManager::databaseDateToSingleDate($communion->getCelebrationDate());
$idRector = $communion->getIdRector();
$objRector = RectorManager::getSingleRector('id', $idRector);
$objPerRect = PersonManager::getSinglePerson('id', $objRector->getIdPerson());
$nameRector = $objPerRect->getFullNameBeginName();
$idBookRegistry = $communion->getIdBookRegistry();
$bookRegistry = CommunionManager::getSingleCommunionRegistry('id', $idBookRegistry);
$idGodFather = $communion->getIdGodFather();
$godMother = PersonManager::getSinglePerson('id', $idGodMother);
$godFather = PersonManager::getSinglePerson('id', $idGodFather);
if ($godFather === NULL) {
    $godFather = new Person();
}
if ($godMother === NULL) {
    $godMother = new Person();
}
//Create a String of options of Last Church
$churchString = "";
$churchs = ChurchManager::getAllChurchs('name');
foreach ($churchs as $singleChurch) {
    if ($singleChurch->getId() === $churchId) {
        $churchString = $churchString . "<option selected> " . $singleChurch->getName() . "</option>\n";
        $kbook = $_GET["kbook"];
        $knumber = $_GET["knumber"];
        $kpape = $_GET["kpape"];
        $communionSearch->setId($kid);
        $communionSearch->setCelebrationDate($kcelebration);
        $posibleNames[0] = PersonManager::searchPersonsByNames($knamec, $klastname1c, $klastname2c, false);
        $posibleNames[1] = PersonManager::searchPersonsByNames($knamef, $klastname1f, $klastname2f, false);
        $posibleNames[2] = PersonManager::searchPersonsByNames($knamem, $klastname1m, $klastname2m, false);
        $communionSearch->setIdOwner($posibleNames);
        $posibleChurch = ChurchManager::simpleSearchChurch($kchurch, 'id', -1);
        $communionSearch->setIdChurch($posibleChurch);
        $communionRegistry = CommunionManager::getSingleCommunionRegistry('book', $kbook, 'page', $kpape, 'number', $knumber);
        $communionSearch->setIdBookRegistry($communionRegistry);
        $communionRegistries = CommunionManager::advancedSearchCommunion($communionSearch, 'AND', $sortType, $numberPage);
    } else {
        $communionRegistries = CommunionManager::getAllCommunions($sortType, $numberPage);
    }
}
//Get the total of registries
$totalRegistries = DatabaseManager::getAffectedRows();
$affectedRegistries = DatabaseManager::registriesAffectedLastQuery();
$lastPage = floor($affectedRegistries / 10);
if ($affectedRegistries % 10 === 0) {
    $lastPage = floor($affectedRegistries / 10) - 1;
}
if ($totalRegistries === 0 && $numberPage !== 0) {
    echo "<script src='../JS/functions.js'></script><script>nextPage('set', '0')</script>";
}
//Create contest for button next and prev
$nextButtonString = '<button type="button" 
                                 class="btn btn-warning"