<?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');
    }
    
    if(!empty($_POST['array_chk']))
    {
        $inputRelacion = htmlspecialchars(mysqli_real_escape_string($con,$_POST['inputRelacion']));
        foreach($_POST['array_chk'] as $key)
        {
            $value = htmlspecialchars(mysqli_real_escape_string($con,$key));
            $sql = "SELECT * FROM CONTACTOS_ORGANIZACIONES WHERE CONTACTO = ? AND ORGANIZACION = ?";
            $stmt = $con->prepare($sql);
            $stmt->bind_param('ii',$inputRelacion,$value);
            $stmt->execute();
            $res = $stmt->get_result();
            if($res->num_rows == 0)
            {
              $sql = "INSERT INTO CONTACTOS_ORGANIZACIONES(CONTACTO,ORGANIZACION) VALUES(?,?)";
              $stmt = $con->prepare($sql);
              $stmt->bind_param('ii',$inputRelacion,$value);
              $stmt->execute();
              $_SESSION['agenda_m56'] = 3;
              echo "<script>window.location.replace('agenda.php')</script>";
            }
            else
            {
              $_SESSION['agenda_m56'] = 98;
              echo "<script>window.location.replace('agenda.php')</script>";
            }
        }
    }
    else
    {
      echo '
        <div class="alert alert-warning" role="alert">
          <i class="far fa-square"></i> No hay campos seleccionados!
        </div>';
    }

?>

