<?php

require_once __DIR__ . "/../../../Backend/SessionManager.php";
require_once __DIR__ . "/../../../Backend/ConfirmationManager.php";
if (!isset($_GET) || $_GET["idConfirmation"] === NULL) {
    echo "KO";
} else {
    $confirmation = ConfirmationManager::getSingleConfirmation("id", $_GET["idConfirmation"]);
    if ($confirmation === 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"] == $confirmation->getIdChurch()) {
                if (ConfirmationManager::removeConfirmation($confirmation)) {
                    echo "OK";
                } else {
                    echo "KO";
                }
            }
        }
    }
}
                     $baptism->setIdChurch($church->getId());
                 }
                 BaptismManager::updateBaptism($baptism);
                 $baptismReg = BaptismManager::getSingleBaptismRegistry('id', $baptism->getIdBookRegistry());
                 $baptismReg->setBook($_POST["bookBookRegistryB"]);
                 $baptismReg->setPage($_POST["pageBookRegistryB"]);
                 $baptismReg->setNumber($_POST["numBookRegistryB"]);
                 BaptismManager::updateBaptismRegistry($baptismReg);
             }
         }
     } else {
         echo "KO";
     }
 } else {
     if ($_POST["status"] === 'update') {
         ConfirmationManager::updateConfirmation($confirmation);
         echo "OK";
         if ($_POST["baptismId"] === '0') {
             if ($_POST["baptismChurch"] != "XXXXXXXXXX") {
                 $bookRegistry = new BaptismRegistry();
                 $bookRegistry->setBook($_POST["bookBookRegistryB"]);
                 $bookRegistry->setPage($_POST["pageBookRegistryB"]);
                 $bookRegistry->setNumber($_POST["numBookRegistryB"]);
                 BaptismManager::addBaptismRegistry($bookRegistry);
                 $bookRegistry = BaptismManager::getSingleBaptismRegistry('book', $_POST["bookBookRegistryB"], 'page', $_POST["pageBookRegistryB"], 'number', $_POST["numBookRegistryB"]);
                 $baptism = new Baptism('0', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL');
                 $baptism->setIdCivilRegistry('1');
                 $celb = DatabaseManager::singleDateToDatabaseDate($_POST["baptismDate"]);
                 $baptism->setCelebrationDate($celb);
                 $church = ChurchManager::getSingleChurch('name', $_POST["baptismChurch"]);
                 if ($church !== NULL) {
//Get The Data of the Confirmation
$idConfirmation = $_GET["id"];
$confirmation = ConfirmationManager::getSingleConfirmation('id', $idConfirmation);
$ownerId = $confirmation->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 = $confirmation->getIdChurch();
$confirmationDate = DatabaseManager::databaseDateToSingleDate($confirmation->getCelebrationDate());
$idRector = $confirmation->getIdRector();
$objRector = RectorManager::getSingleRector('id', $idRector);
$objPerRect = PersonManager::getSinglePerson('id', $objRector->getIdPerson());
$nameRector = $objPerRect->getFullNameBeginName();
$idBookRegistry = $confirmation->getIdBookRegistry();
$bookRegistry = ConfirmationManager::getSingleConfirmationRegistry('id', $idBookRegistry);
$idGodFather = $confirmation->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"];
        $confirmationSearch->setId($kid);
        $confirmationSearch->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);
        $confirmationSearch->setIdOwner($posibleNames);
        $posibleChurch = ChurchManager::simpleSearchChurch($kchurch, 'id', -1);
        $confirmationSearch->setIdChurch($posibleChurch);
        $confirmationRegistry = ConfirmationManager::getSingleConfirmationRegistry('book', $kbook, 'page', $kpape, 'number', $knumber);
        $confirmationSearch->setIdBookRegistry($confirmationRegistry);
        $confirmationRegistries = ConfirmationManager::advancedSearchConfirmation($confirmationSearch, 'AND', $sortType, $numberPage);
    } else {
        $confirmationRegistries = ConfirmationManager::getAllConfirmations($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"