#!/usr/bin/perl -w
use CGI qw/:all/;
use CGI::Carp qw(fatalsToBrowser);
use DBI;
my $dbh=DBI->connect('dbi:mysql:parc_demog:localhost','parcdemog','parc9904')or die "Couldn't connect to database: " . DBI->errstr;
#my $dbh=DBI->connect('dbi:mysql:parc_demog:localhost:3306','root','root') or die "Unable to connect: $DBI::errstr\n";

my $conCode= url_param('conCode');
my $cityCode=url_param('cityCode');
my $locCode=url_param('locCode');
my $typeCode= url_param('typeCode');
my $hasVisited= url_param('hasVisited');


if ($conCode eq '')
{
 $conCode= "All";
}
if ($cityCode eq '')
{
 $cityCode= "All";
}
if ($locCode eq '')
{
 $locCode= "All";
}
if ($typeCode eq '')
{
 $typeCode= "All";
}
if ($hasVisited eq '')
{
 $hasVisited= "All";
}

$strCon="select distinct conCode,conName from cf_location";
$sthCon=$dbh->prepare($strCon);
$sthCon->execute;


$strCity="select distinct cityCode,cityName from cf_location";
$sthC=$dbh->prepare($strCity);
$sthC->execute;

###
$strLoc="select distinct locCode,locName from cf_location";
$sthL=$dbh->prepare($strLoc);
$sthL->execute;
###
#$strS="select distinct ShID,fName,lName from shopper";
#$sthS=$dbh->prepare($strS);
#$sthS->execute;
###

######Checking for Country
$strCond="";
if ($conCode ne "All")
{
    $strCond="l.conCode='$conCode'";
}

######Checking for City
if ($cityCode ne "All")
{
    if ($strCond ne "") 
    {
        $strCond="$strCond and l.cityCode='$cityCode'";
    }
    else
    {
        $strCond="l.cityCode='$cityCode'";
    }
}

#########Checking for Location
if ($locCode ne "All")
{
    if ($strCond ne "") 
    {
        $strCond="$strCond and v.locCode='$locCode'";
    }
    else
    {
        $strCond="v.locCode='$locCode'";
    }
}

#########Checking for Mailed
#if ($hasMailed ne "All")
#{
#   if ($strCond ne "") 
#   {
#       $strCond="$strCond and hasMailed='$hasMailed'";
#   }
#   else
#   {
#       $strCond="hasMailed='$hasMailed'";
#   }
#}
#########Checking for Visited
if ($hasVisited ne "All")
{
    if ($strCond ne "") 
    {
        $strCond="$strCond and hasVisited='$hasVisited'";
    }
    else
    {
        $strCond="hasVisited='$hasVisited'";
    }
}

##
if ($typeCode ne "All")
{
    if ($strCond ne "") 
    {
        $strCond="$strCond and typeCode='$typeCode'";
    }
    else
    {
        $strCond="typeCode='$typeCode'";
    }
}
##
#########Checking for Shopper
#if ($shID ne "All")
#{
#   if ($strCond ne "") 
#   {
#       $strCond="$strCond and v.shID='$shID'";
#   }
#   else
#   {
#       $strCond="v.shID='$shID'";
#   }
#}
#########
if ($strCond eq "")
{
    $strVisit="SELECT v.planCode,v.locCode,l.locName,l.typeName,l.cityName,v.planTime,v.hasVisited from cf_location l,cf_visitPlan v where v.locCode=l.locCode";
}
else
{
    $strVisit="SELECT v.planCode,v.locCode,l.locName,l.typeName,l.cityName,v.planTime,v.hasVisited from cf_location l,cf_visitPlan v where v.locCode=l.locCode and $strCond";
}
$sthV=$dbh->prepare($strVisit);
$sthV->execute;
$cnt=$sthV->rows;

print <<blk1;
content-type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>PAN ARAB RESEARCH CENETER - Mystery Shopping</title>
<link href="resend1.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
function plcCityName()
{
    frm=document.frmMain;
    frm.conCode.value="$conCode";
    frm.cityCode.value="$cityCode";
    frm.locCode.value="$locCode";
    frm.typeCode.value="$typeCode";
    frm.hasVisited.value="$hasVisited";
    
    document.getElementById("msg").style.display="none";
    document.getElementById("tbl").style.display="";
    if("$cnt"==0)
    {
        document.getElementById("msg").style.display="";
        document.getElementById("tbl").style.display="none";
    }
}

function doresend()
{
    a=confirm('Confirm Re-sending');
    if(a==true)
    {
        var cmd=document.getElementById("cmd").value;
        document.getElementById("cmd").value=cmd;
        document.frmMain.method="post";
        document.frmMain.action="http://www.arabdemog.com/cf/visits/sendMails.pl"
        document.frmMain.submit();
        return true;
    }
    else
    {return false;}
}
function selfpost()
{
    var cityCode = document.getElementById('cityCode').value;
    var locCode = document.getElementById('locCode').value;
    var conCode = document.getElementById('conCode').value;
    var hasVisited = document.getElementById('hasVisited').value;
    var typeCode = document.getElementById('typeCode').value;
    
    //var path="http://localhost:8081/cf/visits/adminvisits/resend.pl?conCode=" + conCode + "&cityCode="+cityCode + "&locCode="+locCode + "&typeCode="+ typeCode + "&hasVisited="+ hasVisited;
    var path="http://www.arabdemog.com/cf/visits/resend.pl?conCode=" + conCode + "&cityCode="+cityCode + "&locCode="+locCode + "&typeCode="+ typeCode + "&hasVisited="+ hasVisited;
    lnk=encodeURI(path);
    document.frmMain.method="post";
    document.frmMain.action=lnk;
    document.frmMain.submit();
    return true;
}
</script>
</head>

<body style="background-color: #666666;color: inherit;" onload="plcCityName();">
<table class="TBL">
<tr>
<td style="width:3%;background-color:#666666">&nbsp;</td>


<td style="width:94%;background-color: #FFFFFF">
<div style='height:70px;padding-top: 15px;padding-bottom: 5px;line-height: 3px;'><h1 style='padding-left: 30px;color: #607291;padding-right: 30px;font-size:1.5em;'>PAN ARAB RESEARCH CENTER</h1>
<p style='padding-left: 30px;color: #607291;padding-right: 30px;font-size:1.2em;'>Visit Details</p>
</div>
<div id="navbar">&nbsp; </div>
<div id="content">
<form method="post" name="frmMain">

<div id="srch" style="border:0px solid black;vertical-align:middle;padding-right:15px;"><font size='2'color='#000000'><p align="center"><b>Country:</b>


<select name="conCode" id="conCode"  style="width:90px;border:1px solid #666666;">
                    <option value="All">All</option>
blk1
 while (@cont=$sthCon->fetchrow_array())
    {
        
        print "<option value=$cont[0]>$cont[1]</option>";
    }
print <<blk2;
</select>
&nbsp;<b>City:</b>
<select name="cityCode" id="cityCode"  style="width:90px;border:1px solid #666666;">
                    <option value="All">All</option>
blk2
    while (@City=$sthC->fetchrow_array())
    {
        
        print "<option value=$City[0]>$City[1]</option>";
    }
print <<blk3;
</select>
&nbsp;<b>Location:</b>
<select name="locCode" id="locCode"  style="width:180px;border:1px solid #666666;">
                    <option value="All">All</option>
blk3
 while (@loc=$sthL->fetchrow_array())
    {
        
        print "<option value=$loc[0]>$loc[1]</option>";
    }

print <<blk4;
</select>
&nbsp;Store:
<select name="typeCode" id="typeCode"  style="width:90px;border:1px solid #666666">
                    <option value="All">All</option>
                    <option value="1">Hypermarket</option>
                    <option value="2">Carrefour</option>
</select>
&nbsp;<b>Visited:</b>
<select name="hasVisited" id="hasVisited"  style="width:90px;border:1px solid #666666">
                    <option value="All">All</option>
                    <option value="T">True</option>
                    <option value="F">False</option>
</select>
<a href="#" style="text-decoration: none;" onclick="selfpost();"><img src="go.png" style='vertical-align:middle;' alt="GO" height="25" border="0" width="25"/> </a>
</p>
</font>
</div>
<div id="msg" style="display:none"><font size='3'color='#000000'><p align="center">No Records to Display</p></font></div>

<font size='2'color='#000000'>
<table width="100%" align='center' class="Mtable" id="tbl">
<tr height='25' bgcolor='#97a8bb'>
<td class="Mtd" width="8%" style="font-size:0.86em;padding-left: 2px;"><b>planCode</b></td>
<td class="Mtd" width="10%" style="font-size:0.86em;padding-left: 2px;"><b>Code</b></td>
<td class="Mtd"width="15%" style="font-size:0.86em;padding-left: 2px;"><b>City</b></td>
<td class="Mtd" width="33%" style="font-size:0.86em;padding-left: 2px;"><b>Location</b></td>
<td class="Mtd" width="15%" style="font-size:0.86em;padding-left: 2px;"><b>Store Type</b></td>
<td class="Mtd" width="13%" style="font-size:0.86em;padding-left: 2px;"><b>Time</b></td>
<td class="Mtd" width="6%"  style="font-size:0.86em;padding-left: 2px;"><b>Visit</b></td>

</tr>
<!-- while loop to populate rows -->

blk4
my $i= 0;
while ($visit=$sthV->fetchrow_hashref())
    {
$i=$i+1;
    my $planCode ="$visit->{'planCode'}";
    my $code ="$visit->{'locCode'}";
    my $city ="$visit->{'cityName'}";
    my $location ="$visit->{'locName'}";
    my $type ="$visit->{'typeName'}";
    my $planTime ="$visit->{'planTime'}";
    my $hasVisited ="$visit->{'hasVisited'}";
$mod=$i%2;
    
    if($mod == 0)
    {
        $col='#D7DADE';
    }
    else
    {
        $col='#FFFFFF';
    }
print <<blk5;


<tr height='25' valign='center' bgcolor='$col'>
<font color='#000000' size='0.8em' face="Verdana, Arial, Helvetica, sans-serif"><b>
<td class="Mtd">
$planCode
</td>
<td class="Mtd">
$code
</td>
<td class="Mtd">
$city
</td>
<td class="Mtd">
$location
</td>
<td class="Mtd">
$type
</td>
<td class="Mtd">
$planTime
</td>
<td class="Mtd" align='center' style='padding-left: 0;'>
$hasVisited
</td>
    </font>
</tr>
blk5
    
    }
print <<blk7;

<!-- End while loop -->
<tr>
<input type="hidden" value="$i" name="cnt" id="cnt">
<td colspan="14" align="center">&nbsp;</td>
</tr>
<tr>
<td colspan="14" align="center" style="vertical-align:middle;"><font  size="2" color='#000000'><div style="vertical-align:middle;"><b>Message: </b></font><br>
<textarea rows="8" cols="105" name="cmd"  id="cmd"> 
• Please use the following link to Request the Carrefour Visit according to our Visit Plan. Kindly choose your location and revert back to us.
</textarea></div></td>
</tr>
<tr>
<td colspan="14" align="center">&nbsp;</td>
</tr>
<tr>
<td colspan="14" align="center"><input type="button" Value="Re-send" name="resend" onclick="doresend();" /></td>
</tr>
</table>
</font>






</form>
</div>

<div id="footer">
<p class="footerpmiddle"><br /></p>
</div>  

</td>



<td style="width:3%;background-color:#666666"></td>
</tr>
</table>

</body>
</html>
blk7
