<?php
    session_start();
    $usuarioSession = $_SESSION['USUARIO'];
    $grupo = $_SESSION['GRUPO'];
    $contacto = $_SESSION['CONTACTO'];
    $awa_session_id = $_SESSION['AWA_SESION_ID'];
    include('../includes/conex.php');
    include('../includes/eat.php');
    if(!isset($usuarioSession) or $awa_session_id != $awa_id)
    {
      eatCookies();
      header('Location:../index.php');
    }
    if(isset($_COOKIE['usuario']) AND $_COOKIE['usuario'] != $staticUsu )
    {
      eatCookies();
      header('Loction:../index.php');
    }
    if(isset($_COOKIE['idioma']))
    {
      include('../lg/LG_' . $_COOKIE['idioma'] . '.php');
    }
    else
    {
      include('../lg/LG_SP.php');
    }
    function sin_asignar($value,$msg)
    {
        if($value == NULL)
        {
            echo "$msg";
        }
    }
    $codigo = htmlspecialchars(mysqli_real_escape_string($con,$_POST['codigo']));
    $sql = "SELECT C.*
    FROM CONTACTOS C
    WHERE C.CODIGO = ?";
    $stmt = $con->prepare($sql);
    $stmt->bind_param('i',$codigo);
    $stmt->execute();
    $res = $stmt->get_result();
    $filaAgenda = $res->fetch_array();
    if($filaAgenda['CONTACTO'] == 1)
    {
        $option = '<option value="0" selected>Contacto</option><option value="1">Organización</option>';
    }
    else
    {
        $option =  '<option value="0">Contacto</option><option value="1" selected>Organización</option>';
    }
    echo "
    <form class='needs-validation' method='post' action='agenda-crear-o.php' novalidate>
        <div class='form-row'>
            <div class='col-12 col-md-6 mb-4 pb-1'>
                <label for='validationTooltipUsername' class='d-none'>usuario</label>
                <div class='input-group'>
                    <div class='input-group-prepend'>
                        <span class='input-group-text' id='s-01'><i
                                class='fas fa-id-card text-muted'></i></span>
                    </div>
                    <input onkeyup='yelling(this)' type='text' name='nombre' value='" . $filaAgenda['NOMBRE'] . "'
                        class='form-control' id='m-01' placeholder='$nombre_lg'
                        aria-describedby='s-01' required>
                    <div class='invalid-tooltip'>
                        $nombre_requerido_lg
                    </div>
                </div>
            </div>
            <div class='col-12 col-md-6 mb-4 pb-1'>
                <label for='validationTooltipUsername' class='d-none'>segundo_nombre</label>
                <div class='input-group'>
                    <div class='input-group-prepend'>
                        <span class='input-group-text' id='s-02'><i
                                class='far fa-id-card text-muted'></i></span>
                    </div>
                    <input onkeyup='yelling(this)' type='text' name='segundo_nombre'
                        value='" . $filaAgenda['APELLIDO1'] . "' class='form-control' id='m-02'
                        placeholder='$segundo_nombre_lg' aria-describedby='s-02'>
                </div>
            </div>
            <div class='col-12 col-md-6 mb-4 pb-1'>
                <label for='validationTooltipUsername' class='d-none'>teléfono</label>
                <div class='input-group'>
                    <div class='input-group-prepend'>
                        <span class='input-group-text' id='s-03'><i
                                class='fas fa-mobile text-muted'></i></span>
                    </div>
                    <input onkeyup='yelling(this)' type='number' name='telefono' value='" . $filaAgenda['TLFNO1'] . "'
                        class='form-control' id='m-03' placeholder='" . $telefono_lg . "'
                        aria-describedby='s-03' required>
                    <div class='invalid-tooltip'>
                        $telefono_obligatorio_lg
                    </div>
                </div>
            </div>
            <div class='col-12 col-md-6 mb-4 pb-1'>
                <label for='validationTooltipUsername' class='d-none'>teléfono2</label>
                <div class='input-group'>
                    <div class='input-group-prepend'>
                        <span class='input-group-text' id='s-03a'><i
                                class='fas fa-mobile text-muted'></i></span>
                    </div>
                    <input onkeyup='yelling(this)' type='number' name='telefono2' value='" . $filaAgenda['TLFNO2'] . "'
                        class='form-control' id='m-03a'
                        placeholder='" . $telefono_lg . "2'" . " aria-describedby='s-03a'>
                </div>
            </div>
            <div class='col-12 col-md-6 mb-4 pb-1'>
                <label for='validationTooltipUsername' class='d-none'>email</label>
                <div class='input-group'>
                    <div class='input-group-prepend'>
                        <span class='input-group-text' id='s-04'><i
                                class='fas fa-at text-muted'></i></span>
                    </div>
                    <input onkeyup='yelling(this)' type='email' name='email' value='" . $filaAgenda['EMAIL1'] . "'
                        class='form-control' id='m-04' placeholder='" . $email_lg . "'
                        aria-describedby='s-04' required>
                    <div class='invalid-tooltip'>
                        $email_obligatorio_lg
                    </div>
                </div>
            </div>
            <div class='col-12 col-md-6 mb-4 pb-1'>
                <label for='validationTooltipUsername' class='d-none'>email2</label>
                <div class='input-group'>
                    <div class='input-group-prepend'>
                        <span class='input-group-text' id='s-04a'><i
                                class='fas fa-at text-muted'></i></span>
                    </div>
                    <input onkeyup='yelling(this)' type='email' name='email2' value='" . $filaAgenda['EMAIL2'] . "'
                        class='form-control' id='m-04a' placeholder='" . $email_lg . "2'" . "
                        aria-describedby='s-04a'>
                </div>
            </div>
            <div class='col-12 col-md-4 mb-3 pb-1'>
                <label for='validationTooltipUsername' class='d-none'>ciudad</label>
                <div class='input-group'>
                    <div class='input-group-prepend'>
                        <span class='input-group-text' id='s-05'><i
                                class='fas fa-building text-muted'></i></span>
                    </div>
                    <input onkeyup='yelling(this)' type='text' name='ciudad'
                        value='" . $filaAgenda['PROVINCIA_PER'] . "' class='form-control'
                        id='m-05' placeholder='" . $ciudad_lg ."' aria-describedby='s-05'>
                </div>
            </div>
            <div class='col-12 col-md-4 mb-3 pb-1'>
                <label for='validationTooltipUsername' class='d-none'>poblacion</label>
                <div class='input-group'>
                    <div class='input-group-prepend'>
                        <span class='input-group-text' id='s-06'><i
                                class='far fa-building text-muted'></i></span>
                    </div>
                    <input onkeyup='yelling(this)' type='text' name='poblacion'
                        value='" . utf8_encode($filaAgenda['POBLACION_PER']) . "'
                        class='form-control' id='m-06' placeholder='" . $poblacion_lg . "'
                        aria-describedby='s-06'>
                </div>
            </div>
            <div class='col-12 col-md-4 mb-3 pb-1'>
                <label for='validationTooltipUsername' class='d-none'>dirección</label>
                <div class='input-group'>
                    <div class='input-group-prepend'>
                        <span class='input-group-text' id='s-07'><i
                                class='fas fa-map-marked text-muted'></i></span>
                    </div>
                    <input onkeyup='yelling(this)' type='text' name='direccion'
                        value='" . $filaAgenda['DIRECCION_PER'] . "' class='form-control'
                        id='m-07' placeholder='" . strtoupper(mb_strtolower($direccion_lg, 'UTF-8')) . "'
                        aria-describedby='s-07'>
                </div>
            </div>
            <div class='col-12 col-md-4 mb-3 pb-1'>
                <label for='validationTooltipUsername' class='d-none'>nif</label>
                <div class='input-group'>
                    <div class='input-group-prepend'>
                        <span class='input-group-text' id='s-07'><i
                                class='fas fa-portrait text-muted'></i></span>
                    </div>
                    <input onkeyup='yelling(this)' type='text' name='nif' class='form-control' id='m-07' placeholder='" . $nif_cif_lg . "' aria-describedby='s-07' value='" . $filaAgenda['NIF'] . "' required>
                </div>
            </div>
            <div class='col-12 col-md-4 mb-4 pb-1'>
                <label for='validationTooltipUsername' class='d-none'>tipo</label>
                <div class='input-group'>
                    <div class='input-group-prepend'>
                        <span class='input-group-text' id='s-08'><i
                                class='fas fa-project-diagram text-muted'></i></span>
                    </div>
                    <select name='opcion' class='form-control' id='m-08' aria-describedby='s-08'
                        required>" . $option .
                    "</select>
                    <div class='invalid-tooltip'>
                        $requiere_accion_lg
                    </div>
                </div>
            </div>
        </div>


        <div class='mb-3 pb-1'>
            <h4 class='mb-3 pb-3 border-bottom'>Grupos</h4>
            <div class='row pl-3'>";
            $foo = 0;    
            $sqlGR = "SELECT G.*, R.* 
            FROM CONTACTOS_GRUPOS G 
            LEFT JOIN CONTACTOS_RELACIONES R ON G.CODIGO = R.GRUPO AND R.CONTACTO = $codigo
            WHERE CODIGO <> -2";
            $resGR = $con->query($sqlGR);
            while($filaGR = $resGR->fetch_array())
            {
                $foo++;
                $check_grupo = $filaGR['GRUPO'] != NULL ? 'checked' : '';
                echo '<div class="custom-control custom-checkbox col-sm-6 col-md-3">
                        <input type="checkbox" name="contacto_grupo[]" class="custom-control-input" id="checkEdit' . $foo .'" value="' . $filaGR['CODIGO'] . '"' . $check_grupo . '>
                        <label class="custom-control-label" for="checkEdit' . $foo .'">' . $filaGR['DESCRIPCION'] .'</label>
                    </div>';
            }
        echo  "</div>
        </div>




        <div class='form-group'>
            <div class='input-group'>
                <div class='input-group-prepend'>
                    <span class='input-group-text' id='s-11'><i
                            class='fas fa-sticky-note text-muted'></i></span>
                </div>
                <textarea onkeyup='yelling(this)' name='notas' class='form-control' id='exampleFormControlTextarea1'
                    rows='3' placeholder='" . $notas_lg . "'>" . $filaAgenda['NOTAS'] . "</textarea>
            </div>
        </div>";


        
















        echo '
        <h5 class="font-weight-normal py-3 m-2 border-bottom">' . $rgpd_lg . '</h5>
        <div class="d-flex justify-content-between mt-2">';
            $sqlRGPD = "SELECT * FROM RGPD_PERMISOS";
            $resRGPD = $con->query($sqlRGPD);
            while($filaRGPD = $resRGPD->fetch_array())
            {
                if($filaRGPD['CODIGO'] == $filaAgenda['RGPD_1'] OR $filaRGPD['CODIGO'] == $filaAgenda['RGPD_2'] OR $filaRGPD['CODIGO'] == $filaAgenda['RGPD_3'] OR $filaRGPD['CODIGO'] == $filaAgenda['RGPD_4'])
                {
                    $check = "checked";
                }
                else
                {
                    $check = "";
                }
                echo '
                <div class="custom-control custom-switch">
                    <input onkeyup="yelling(this)" type="checkbox" class="custom-control-input" id="customSwitch' . $filaRGPD['CODIGO'] . '" name="rgpd[]" value="' . $filaRGPD['CODIGO'] . '"' . $check . '>
                    <label class="custom-control-label" for="customSwitch' . $filaRGPD['CODIGO'] . '">' . utf8_encode($filaRGPD['TITULO']) . '</label>
                </div>';
            }
        echo '</div>';





















        echo "
        <input onkeyup='yelling(this)' type='hidden' name='control' value='3d17'>
        <input onkeyup='yelling(this)' type='hidden' name='thisAgenda' value='" . $filaAgenda['CODIGO'] . "'>
        <button class='btn btn-primary btn-block mt-4' type='submit'>$actualizar_lg</button> 
    </form>";

    $sql = "SELECT O.CONTACTO, O.ORGANIZACION AS ORGANIZACIONCTRL, O.TIPO, C.CONTACTO AS CONTACTOCTRL, C.NOMBRE, C.APELLIDO1, C.EMAIL1 
            FROM CONTACTOS C
            INNER JOIN CONTACTOS_ORGANIZACIONES O ON O.ORGANIZACION = C.CODIGO
            WHERE O.CONTACTO = ?
            ORDER BY NOMBRE ASC";
    $stmt = $con->prepare($sql);
    $stmt->bind_param('i',$codigo);
    $stmt->execute();
    $res = $stmt->get_result();
    if($res->num_rows)
    {
        echo '
            <div class="px-2 py-2">
            <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center mb-3 mt-4 border-bottom text-muted">
                <h1 class="h5"><i class="fas fa-link"></i> ' . $contactos_relacionados_lg . '</h1>
            </div>';
        echo '
            <table class="table table-striped table-hover table-sm">
                <thead class="text-white bg-secondary">
                    <tr>
                    <th scope="col">#</th>
                    <th scope="col">' . $nombre_lg . '</th>
                    <th scope="col">' . $segundo_nombre_lg . '</th>
                    <th scope="col">' . $email_lg . '</th>
                    <th scope="col"></th>
                </tr>
                </thead>
            <tbody>';
        while($fila = $res->fetch_array())
        {
            
            echo '
                <tr>
                    <th scope="row">';
    
            if($fila['CONTACTOCTRL'] == 1)
            {
                echo '<i class="fas fa-user fa-lg text-primary"></i>';
            }
            else
            {
                echo '<i class="fas fa-city fa-lg text-primary"></i>';
            }      
            echo    '
                    </th>
                    <td>' . $fila['NOMBRE'] . '</td>
                    <td>' . $fila['APELLIDO1'] . '</td>
                    <td>' . $fila['EMAIL1'] . '</td>
                    <td>
                        <form id="modalRelacion" class="form-inline text-center needs-validation" novalidate>
                            <div class="form-group mx-sm-3 mb-2">
                                <label for="inputRelacion" class="sr-only">Password</label>
                                    <input onkeyup="yelling(this)" type="text" class="form-control" id="inputRelacion' . $fila['ORGANIZACIONCTRL'] . '" value="' . $fila['TIPO'] . '" placeholder="' . $descripcion_l_lg . '"  aria-describedby="validationTooltipUsernamePrepend" required>
                                </div>
                                <button onclick="editarRelacionAgenda(' . $codigo . ',' . $fila['ORGANIZACIONCTRL'] . ')" class="btn btn-primary btn-sm mb-2"><i class="fas fa-check"></i></button>
                            </div>
                        </form>
                    </td>
                </tr>';
        }
        echo '
                </tbody>
            </table>
            </div>';

    }
    echo '<div id="asd"></div>';
?>
<script>
    
</script>
