<?php
$translation        = \DynCom\mysyde\common\classes\Registry::get("translation");
$formname           = "form_contact_list";
$inputname          = "input_id";
$input_page_id      = (isset($_REQUEST["input_page_id"]) ? $_REQUEST["input_page_id"] : "");
$input_component_id = (isset($_REQUEST["input_component_id"]) ? $_REQUEST["input_component_id"] : "");

function load_contact_list_old($input_id){
    $query = "SELECT certificate_contact_link.id, 
    main_contact.name,
    main_mandant.description,
    certificate_contact_link.issue_date,
    main_certificate.validity_period,
    certificate_contact_link.active
    FROM certificate_contact_link
    LEFT JOIN
        main_contact
    ON main_contact.id = certificate_contact_link.main_contact_id
    LEFT JOIN
        main_mandant
    ON main_mandant.id = main_contact.master_mandant_id
    LEFT JOIN
        main_certificate
    ON main_certificate.id = certificate_contact_link.certificate_id
    WHERE certificate_id = '".$input_id."'";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    while($row = @mysqli_fetch_array($result)) {
        $issue_date = new DateTime($row['issue_date']);
        $validity_date = new DateTime($row['issue_date']);
        $current_date = new DateTime();
        $validity_date->modify('+'.$row['validity_period'].' months');
        $class = "";
        $expired = 0;
        if($current_date >= $validity_date){
            $class = "expired";
            $expired = 1;
        }
        if($row['active'] == 0){
            $class = "not_active";
        }
    ?>
    <tr class='<?= $class ?>'>
        <td><?= $expired ?></td>
        <td><?= $row['name'] ?></td>
        <td><?= $row['description'] ?></td>
        <td><?= $issue_date->format('d.m.Y') ?></td>
        <td><?= $validity_date->format('d.m.Y') ?></td>
        <td><?= $row['active'] ?></td>
    </tr>
    <?php
    }
}

function load_contact_list($input_id){
    $query = "SELECT * FROM main_contact WHERE active = 1 ORDER BY name ASC";
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    while($row = @mysqli_fetch_array($result)) {
        $query2 = "SELECT * FROM certificate_contact_submit LEFT JOIN main_certificate ON main_certificate.id = certificate_contact_submit.certificate_id WHERE certificate_id = '".$input_id."' AND main_contact_id = ".$row['id']." ORDER BY certificate_contact_submit.id DESC LIMIT 1";
        $result2 = @mysqli_query($GLOBALS['mysql_con'], $query2);
        $row2 = @mysqli_fetch_array($result2);

        $query_mandant = "SELECT * FROM main_mandant WHERE id = ".$row['master_mandant_id'];
        $result_mandant = @mysqli_query($GLOBALS['mysql_con'], $query_mandant);
        $row_mandant = @mysqli_fetch_array($result_mandant);

        $query_active = "SELECT * FROM certificate_contact WHERE certificate_id= '".$input_id."' AND main_contact_id = ".$row['id'];
        $result_active = @mysqli_query($GLOBALS['mysql_con'], $query_active);
        $row_active = @mysqli_fetch_array($result_active);

        $datetime = "";
        if($row2['issue_date']){
            $datetime = new DateTime($row2['issue_date']);
            $datetime = $datetime->format('d.m.Y');
        }
        $issue_date = new DateTime($row2['issue_date']);
        $validity_date = new DateTime($row2['issue_date']);
        $validity_date->modify('+'.$row2['validity_period'].' months');

        $done = "";
        $active = "";
        $class = "";
        if(isset($row_active) && $row_active['active'] == 1){
            $active = "checked='checked'";
        }
        if($row2['correct'] == 1){
            $done = "checked='checked'";
            $class = "checked";
        }
    ?>
    <tr class='<?= $class ?>'>
        <input type='hidden' name='contact_<?= $row['id']?>'>
        <td><input type='checkbox' class='checkbox' name='done_<?= $row['id']?>' <?= $done?>></td>
        <td><?= $row['name'] ?></td>
        <td><?= $row_mandant['description'] ?></td>
        <td><?= $issue_date->format('d.m.Y')?></td>
        <td><?= $validity_date->format('d.m.Y')?></td>
        <td><input type='checkbox' class='checkbox' name='active_<?= $row['id']?>' <?= $active?>></td>
    </tr>
    <?php
    }
}
?>

<h2>Benutzerliste</h2>
<table class='learning_unit_user_list'>
    <tr>
        <th>Erledigt</th>
        <th>Benutzer</th>
        <th>Mandant</th>
        <th>Ausstellungsdatum</th>
        <th>Ablaufdatum</th>
        <th>Aktiv</th>
    </tr>
    <?= load_contact_list($input_id);?>
</table>