#!/bin/sh

# disable filename globbing

 BASE=`pwd`
D1=./tmp/D1
[ "$3" = "A" ] && sort -t"^" -k2 $D1 -o $D1
{
APPLDIR=$BASE/appl
DATADIR=$BASE/data
PATH=$PATH:$BASE/ihd/bin:.
ZON=./tmp/ZONN
ZONN=`cat $ZON| awk 'NR==1'`
ZONA=`cat $ZON| awk 'NR==2'`
export PATH APPLDIR DATADIR ZONN

awk -F"^" ' BEGIN { 
T["00"]="<br>REG^RT"
T["01"]="<br>&nbsp;PACIENTE^LT"
T["02"]="<br>&nbsp;COMUNA^LT"
T["03"]="INICIO<br>&nbsp;DE CICLO^CT"
T["04"]="TERMINO<br>&nbsp;DE CICLO^CT"
T["05"]="<br>&nbsp;PRESTACION^LT"
T["06"]="<br>&nbsp;HABER^RT"
T["07"]="FECHA<br>&nbsp;ACTIVACION^CT"
T["08"]="<br>&nbsp;PROFESIONAL^LT"
T["09"]="<br>&nbsp;MODALIDAD^LT"
T["10"]="FECHA<br>&nbsp;PROGRAMA^CT"
T["11"]="<br>&nbsp;AGENDA^RT"
T["12"]="<br>&nbsp;VISITA^RT"
T["13"]="<br>&nbsp;CIERRE^RT"
T["14"]="<b>A</b>^CT"
tx=14
	for (i=0;i<=tx;i++)
	{
		x=sprintf("%.2d",i)
		printf "%s^",T[x]
	}
	print ""
}
{
	for (i=1;i<=tx;i++)
	{
		printf "%s^",$i
		tt[i] +=$i
	}
printf "\n"
}
END {
tt[2]=NR
	printf "TOTAL^"
	for (i=2;i<=tx;i++)
		printf "%s^",tt[i]
print ""
} ' $D1 |

awk -F"^" ' BEGIN {
ttop("Telemed - Agendamiento '"$1 - $2"'") ;bgcolor="#FFFFFF"
alg["C"]="center"
alg["R"]="right"
alg["L"]="left"
}
NR==1 {
print "<thead><tr>"
 for (i=1; i<NF; i+=2) if (!(i==13 || i==15 ))
	{
		al[(i+1)/2]=substr($(i+1),1,1)
		tp[(i+1)/2]=substr($(i+1),2)
		labelf($i,al[(i+1)/2])
	}
print "</tr>"
print "</thead>"
next
}
$1=="TOTAL" {
print "<thead><tr>"
tp[2]="N"
al[2]="C"
for (i=1; i<=NF; i++) if (!(i==7 || i==8 )) labelf($i,al[i],tp[i])
#boton(ctanls,"C",15)

print "</tr>"
print "</thead>"
}
$1!="TOTAL" {
tit[2]=1
tit[12]=1
tit[13]=1
tit[14]=1
al[4]="L"
for (i=1; i<NF-3; i++) if (!(i==7 || i==8 || i==9 || i==11))
if(i==1) boton($i,al[i],60,"S")
else if(i==3) {
	label(substr($i,7),al[i],tp[i],tit[i])
	i=9
	label($i,al[4],tp[4],tit[4])
	i=11
	label($i,al[5],tp[5],tit[5])
	i=3
}
else if (i==5) select($i,3,2)
else label($i,al[i],tp[i],tit[i])
boton("A","","","",tit[i])
i++
boton("V","","","",tit[i])
i++
boton("C","","","",tit[i])
i++
boton("T",FNR-1,"",1)
print "</tr>"
if (NR%2==0 ) bgcolor="#D8D8D8"
else bgcolor="#FFFFFF"
}
END {
 foot() } 
function select(name,selv,vals)
{
	file="sort -t\"^\" -k2 ./tmp/"name
printf "<td bgcolor=\"%s\">",bgcolor
printf "<p ><select id=\"S%s\" style=\"font-family: Arial; font-size: 8pt ;background-color:\"%s\">\n",$1$8,bgcolor
printf "<option value=\"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n"
	while ((file | getline linex) >0)
	{
		split(linex,lin) 
		printf "<option value=\"%s\">%s\n",lin[1],lin[2]
	}
	printf "</select></p>\n"
	close(file)
}
function boton(value,alig,lar,type,tt)
{
	dis=""
	if("'"$ZONA"'"!="" && value=="C") dis="disabled"
        printf "<td border=\"1\" bgcolor=\"%s\" id=\"S%s\">\n",bgcolor,$1""$8""i
	#if(tt) printf "<p align=\"center\" onmouseover=\"showdiv(event,%c%s%c);\" onmousemove=\"showdiv(event,%c%s%c);\" onmouseout=\"javascript:document.getElementById(%cflotante%c).style.display=%cnone%c;\">",39,$1,39,39,$1,39,39,39,39,39
	printf "<p align=\"center\">"
	if(type=="S") printf "<input type=\"submit\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"pacieH00\"></p>\n",lar,alg[alig],value
	else printf "<input type=\"button\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"%s\" id=\"K%s\" onmouseup=recov(this) %s></p>\n",lar,alg[alig],value,$1$8,$1$8""i,dis
#	else printf "<input type=\"button\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"%s\" onmouseup=recov(this)></p>\n",lar,alg[alig],value,$1$8,$1$8
	#if(type=="S") printf "<input type=\"submit\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"pacieH00\"></p>\n",lar,alg[alig],value
	#else printf "<p align=\"center\" ><input type=\"button\" style=\"width: %s; text-align:%s\" value=\"%s\" name=\"%s\" onmouseup=recov(\"%s\")></p>\n",lar,alg[alig],"C",$1$8,$1$8
print "</td>"
print "</td>"
}
function chkb(value)
{
	printf "<td><p><input type=\"checkbox\" style=\"font-family: Arial; font-size: 8pt\" size=\"%s\" name=\"C%s\"></p></td>\n",1,++line
}
function punto (dat,dec)
{
        if (dat+0==0) return ""
        sig=""
        if(dat<0)
        {
                dat *= -1
                sig="-"
        }
        pun=sprintf("%%.%df",dec)
        da=sprintf (pun,dat)
        if(dec) l=length(da)-dec-1
        else l=length(da)
        dat=substr(da,l+2)
        dato=substr(da,1,l)
	if (l>9) pun=substr(dato,1,l-9)"."substr(dato,l-8,3)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else if (l>6) pun=substr(dato,1,l-6)"."substr(dato,l-5,3)"."substr(dato,l-2)
        else if(l >3) pun=substr(dato,1,l-3)"."substr(dato,l-2)
        else pun=dato
        if (dec) pun=pun","dat
        return sig""pun
}
function label(value,alig,tip,tt)
{
	printf "<td id=\"P%s%s\" bgcolor=\"%s\"><p ",$1$8,i,bgcolor
	if(tt) printf " onmouseover=\"showdiv(event,%c%s%c);\" onmousemove=\"showdiv(event,%c%s%c);\" onmouseout=\"javascript:document.getElementById(%cflotante%c).style.display=%cnone%c;\" ",39,$1,39,39,$1,39,39,39,39,39
	if(substr(tip,1,1)=="N") if(value <0) printf " align=\"%s\"><font face=\"Arial\" size=\"3\" color=\"#FF0000\" >&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(-value,substr(tip,2))
	else printf " align=\"%s\"><font face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
	else printf " align=\"%s\"><font face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
	print "</td>"
}
function labelf(value,alig,tip)
{
if(tip=="T" && value!="TOTAL") value="&nbsp"
printf "<td>"
#printf "<td bgcolor=\"%s\">","#003399"
if(substr(tip,1,1)=="N") printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
print "</td>"
}
function labeld(value,alig,tip)
{
printf "<td bgcolor=\"%s\">","#003399"
if(substr(tip,1,1)=="N") printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],punto(value,substr(tip,2))
else printf "<p align=\"%s\"><font color=\"#FFFFFF\" face=\"Arial\" size=\"3\">&nbsp;%s&nbsp;</font></p>\n",alg[alig],value
print "</td>"
}
function labels(value,alig)
{
print "<td bgcolor=\"#003399\" align=\"center\">"
printf "<p align=\"%s\"><b><font color=\"#FFFFFF\">&nbsp;%s&nbsp;</font></b></p>\n",alg[alig],value
print "</td>"
}
function ttop(value)
{
print "<html>"
print "<head>"
print "<meta http-equiv=\"Expires\" content=\"0\">"
print "<meta http-equiv=\"Last-Modified\" content=\"0\">"
print "<meta http-equiv=\"Cache-Control\" content=\"no-cache, mustrevalidate\">"
print "<meta http-equiv=\"Pragma\" content=\"no-cache\">"
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1252\">"
print "<meta name=\"GENERATOR\" content=\"Microsoft FrontPage 4.0\">"
print "<meta name=\"ProgId\" content=\"FrontPage.Editor.Document\">"
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"../css/table.css\">"
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"../age1.css\">"
printf "<title>%s</title>\n",value
print "</head>"
print "<center>"
print "<body bgcolor=\"#D0D0DF\">"
print "<form method=\"post\" action=\"../../cgi-bin/pacie.cgi\" name=anlsform>"
print "<center>"
#print " ><p align=\"left\"><img src=\"../logol.png\" width=\"200\" height=\"97\"></p></td><td colspan=8 align=center>"
if("'"$3"'"==2) ttt="INFORMES PENDIENTES"
if("'"$3"'"==0) ttt="AGENDAMIENTOS PENDIENTES"
if("'"$3"'"==1) ttt="VISITAS PENDIENTES"
print " <p align=\"left\"><img src=\"../logol.png\" width=\"200\" height=\"97\"></p></td><td colspan=8 align=center>"
printf "<H3>%s GES - TELEMED<br>%s</H3></td></tr>\n",ttt,"'"$ZONN - $1 - $2"'"
print "<table cellspacing=\"0\" cellpadding=\"0\"  height=\"50\">"
printf "<input type=\"hidden\" value=\"Consultar\" name=\"B1\">\n"
print "<tr>"
print "</center>"
}
function foot()
{
if (1 == 0)
{
print "</table>"
print "<table><tr>"
labels("SALDOS","C")
labels("VALORES","R")
print "</tr>"
print "<tr>"
label("ANTERIOR","L","T")
label(cta[4],"R","N")
print "</tr>"
print "<tr>"
label("DEL PERIODO","l","T")
label(cta[5],"R","N")
print "</tr>"
print "<tr>"
label("ACTUAL","L","T")
label(cta[6],"R","N")
print "</tr>"
}
print "</center>"
print "</form>"
print "<div id=\"flotante\">"
print "        <br /><span id=\"posicion\"></span>"
print "</div>"

print "<script language=\"Javascript\" src=\"../js/agenda.js\"> </script>"
print "<script language=\"Javascript\" src=\"../js/age3.js\"> </script>"
print "<script language=\"Javascript\" src=\"../js/xtoy.js\"> </script>"
print "<script language=\"Javascript\" src=\"../js/pac.js\"> </script>"
print "</body>"
print "</html>"
} ' - 
} 2>./error/anls.err
cat ./error/anls.err
