<?php

include("../bd/sql_variables.php");

$db_conexion= mysql_connect("$sql_host", "$sql_usuario", "$sql_pass") or die(header ("No Disponible Servidor de Base de Datos"));
mysql_select_db("$sql_db");

$ugids=$_GET['ugid'];
$idj=$_GET['idj'];
$ids=$_GET['ids'];
$msg=$_GET['msg'];

//pregunta por las jefaturas
$sqljefe="SELECT ug_id as ugid FROM tb_usuprogdep where j_id='$idj' and s_id='$ids'";
$resultjefe = mysql_query($sqljefe);
while($rowj= mysql_fetch_array($resultjefe)){
	$ugid= $rowj['ugid'];
}

$sqlask="select m_id as id from tb_mensajeria where j_id='$idj' and s_id='$ids' and ug_id='$ugid'";
//echo $sqlask;
$resultsql = mysql_query($sqlask);
	if(mysql_num_rows($resultsql)==0){
	$idm = 1;
	}else{
	while($row= mysql_fetch_array($resultsql)){
	$idm= $row['id'] + 1;
	}}

$sqlinsertmail="insert into tb_mensajeria values ($idm, now(),'$idj','$ids','$ugid','$ugids',upper('$msg'))";
$resultconsult = mysql_query($sqlinsertmail);
//echo "<script type='text/javascript'>window.opener.location.reload(); window.close();</script>";


//armando grupo correo
$sqldepa="select j_id as codj, 0 as cods ,j_abrev as nom from tb_jefatura where j_id='$idj'";
$sqlserv="select j_id as codj, s_id as cods, s_abrev as nom from tb_servicio where j_id='$idj'";
$resultdepa = mysql_query($sqldepa);
$resultserv = mysql_query($sqlserv);

$cadenamail="<table align='center' class='FacetFormTABLE'>";
$cadenamail=$cadenamail."<tr><td class='FacetFieldCaptionTDT' colspan='2' align='center'>MENSAJERIA</td></tr>";
$cadenamail=$cadenamail."<tr><td><b>Seleccione Remitente:</b></td><td><b>Escribe Mensaje:</b></td></tr><tr><td>";
$cadenamail=$cadenamail."<table align='center' class='FacetFormTABLE' height='150'>";
while ($rowj= mysql_fetch_array($resultdepa)){
$cadenamail=$cadenamail."<tr><td><input type='radio' id='msn' name='msn' value='".$rowj['cods']."'>".$rowj['nom']."</td></tr>";
}
while ($rows= mysql_fetch_array($resultserv)){
$cadenamail=$cadenamail."<tr><td><input type='radio' id='msn' name='msn' value='".$rows['cods']."'>".$rows['nom']."</td></tr>";
}
$cadenamail=$cadenamail."</table></td><td>";
$cadenamail=$cadenamail."<table align='center' class='FacetFormTABLE' height='150'>";
$cadenamail=$cadenamail."<tr><td align='center'><textarea name='msntexto' id='msntexto' cols='28' rows='6'></textarea></td></tr>";
$cadenamail=$cadenamail."</table></td></tr><tr><td align='center' colspan='2'><input type='button' value='Enviar' class='FacetButton' onClick='enviarmail()'></td></tr></table>";
//echo "<script type='text/javascript'>location.reload();</script>";

//LEYENDO LOS MENSAJES
//leer los mensajes enviados
$sqlmail = "select b.m_id as idm, b.ug_ids as cod, date_format(b.fecha,'%d/%m/%Y %h:%i:%s %p') as dia, a.ua_apepat as apepat, ";
$sqlmail = $sqlmail." a.ua_apemat as apemat, a.ua_nom as nombre from tb_mensajeria b inner join tb_usuasist a on a.ug_id=b.ug_ids ";
$sqlmail = $sqlmail." where b.j_id='$idj' and b.s_id='$ids' order by dia asc";
//echo $sqlmail;

$resultmail = mysql_query($sqlmail);
$cadenaleermail="<table align='center' class='FacetFormTABLE' width='300'><tr><td class='FacetFieldCaptionTDT' align='center'>BUZON DE MENSAJES</td></tr><tr><td><b>Seleccione Mensaje:</b></td></tr>";
$cadenaleermail=$cadenaleermail."<tr><td><table align='center' class='FacetFormTABLE' width='100'><tr><td class='FacetFieldCaptionTD' align='center'>Fecha y Hora Env</td><td class='FacetFieldCaptionTD' align='center'>Remitente</td><td class='FacetFieldCaptionTD' align='center'>Eliminar</td></tr>";
$cadenaleermail=$cadenaleermail."<tr><td colspan='2'><select id='leermail' size='6' onClick='funleermail()'>";
$p=0;
while ($rowm= mysql_fetch_array($resultmail)){
$codigo=$rowm['cod'];
$apepat=$rowm['apepat'];
$apemat=$rowm['apemat'];
$nombre=$rowm['nombre'];
//$letranom=substr($nombre, 0, 1);
$letraapemat=substr($apemat, 0, 1);
$nombrefijo= $nombre." ".$apepat." ".$letraapemat.".";
$p=$p+1;
$idm = $rowm['idm'];
$codigo =$rowm['cod'];
$idcodigo=$codigo." ".$idm;
if($p==1){
$idm0 = $rowm['idm'];
$codigo0 =$rowm['cod'];
$cadenaleermail=$cadenaleermail."<option value='".$idcodigo."' selected>&nbsp;&nbsp;".$rowm['dia']."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$nombrefijo."</option>";
}else{
$cadenaleermail=$cadenaleermail."<option value='".$idcodigo."'>&nbsp;&nbsp;".$rowm['dia']."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$nombrefijo."</option>";
}
}
/// mensajes de las jefaturas
//leer los mensajes enviados
$sqlmail = "select b.m_id as idm, b.ug_ids as cod, date_format(b.fecha,'%d/%m/%Y %h:%i:%s %p') as dia, a.up_apepat as apepat, ";
$sqlmail = $sqlmail." a.up_apemat as apemat, a.up_nom as nombre from tb_mensajeria b inner join tb_usuprog a on a.ug_id=b.ug_ids ";
$sqlmail = $sqlmail." where b.j_id='$idj' and b.s_id='$ids' order by dia desc";
$resultmail = mysql_query($sqlmail);
while ($rowm= mysql_fetch_array($resultmail)){
$codigo=$rowm['cod'];
$apepat=$rowm['apepat'];
$apemat=$rowm['apemat'];
$nombre=$rowm['nombre'];
//$letranom=substr($nombre, 0, 1);
$letraapemat=substr($apemat, 0, 1);
$nombrefijo= $nombre." ".$apepat." ".$letraapemat.".";
$p=$p+1;
$idm = $rowm['idm'];
$codigo =$rowm['cod'];
$idcodigo=$codigo." ".$idm;
if($p==1){
$idm0 = $rowm['idm'];
$codigo0 =$rowm['cod'];
$cadenaleermail=$cadenaleermail."<option value='".$idcodigo."' selected>&nbsp;&nbsp;".$rowm['dia']."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$nombrefijo."</option>";
}else{
$cadenaleermail=$cadenaleermail."<option value='".$idcodigo."'>&nbsp;&nbsp;".$rowm['dia']."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$nombrefijo."</option>";
}
}

//////////
$sqlmensaje="select m_mail as mail from tb_mensajeria where  m_id ='$idm0' and  j_id='$idj' and s_id='$ids' and ug_id='$codigo0'";

//echo $sqlmensaje;
$resultmensaje = mysql_query($sqlmensaje);
while ($rowmsn= mysql_fetch_array($resultmensaje)){
$mensaje = $rowmsn['mail'];
}
$cadenaleermail=$cadenaleermail."</select></td><td align='center'><a href=''><img src='img/b_edit.png' onClick='eliminarmail()'></a></td></tr></table><tr><td><b>Leer Mensaje:</b></td></tr><tr><td>";
$cadenaleermail=$cadenaleermail."<table align='center' class='FacetFormTABLE' height='80'><tr><td align='center'>";
$cadenaleermail=$cadenaleermail."<div id='divleermail'><TEXTAREA name='msnleer' rows='5' cols='33'>".$mensaje."</TEXTAREA></div>";
$cadenaleermail=$cadenaleermail."</td></tr></table></td></tr></table>";



$salida = "<div id='divlecturamail'>";
$salida = $salida.$cadenaleermail;
$salida = $salida."</div><br>".$cadenamail;
	
echo $salida;
?>