#!/usr/bin/perl

use strict;

use lib qw(/var/www/sites/ubuyne/cgi-perl/modules/);

use PW2_Functions;

use PW2_PrintBits;

&process_main();

exit;

# ---------------------------------------------------------------------------------------------------------------------------

sub process_main{

undef my %FORM;

&process_form_any2(\%FORM);

my $geog	=$FORM{'g'};
my $dircode	=$FORM{'dir'};

my ($catlist,$listing,$pagetitle)='';

$geog=~tr/A-Z/a-z/;	# Lowercase it

if ($geog eq'' && $dircode eq'')
{
&fetch_catlist_large(\$catlist);
}
else
{
&fetch_stores(\$geog,\$dircode,\$listing,\$pagetitle);

&fetch_catlist(\$catlist,\0);
}

&print_myheader();

&html_top_of_page(-title=>\$pagetitle,-scriptflags=>\'pwz1'); #,-bodytags=>\' onLoad="trap();"');

print $listing;

print $catlist if ($geog eq'');

&print_footer(\0);

undef %FORM;

}1;
# ---------------------------------------------------------------------------------------------------------------------------

sub fetch_stores{

my $geog	=shift;	# A reference
my $dir_id	=shift;	#	"
my $listing	=shift;	#	"
my $pagetitle	=shift;	#	"

my ($fields,$SQLQUERY,$dirname)='';

$fields='STOREKEY,STORENAME,TITLE,DESCRIPTION,REALURL,LOGO,CATEGORY,FAVORITES,DATAFEED,SHORTNAME';

if ($dir_id gt '')
{
$SQLQUERY=join('','SELECT CATEGORY FROM PW_CATEGORIES WHERE CATKEY="',$$dir_id,'"');

undef my @record; &fetch_single_record2(\@record,\$SQLQUERY);

$dirname=$record[0];

$$pagetitle=join('','Price-Wizard.com - ',$dirname,' Directory and Mail Order Catalogue Portal');
}

my %countrybits=('gbp'=>'the United Kingdom',
		'usd'=>'the United States',
		'cdn'=>'Canada'
);


if ($$geog gt'' && $$dir_id eq'')
{
$$listing=qq~
<br>
<div class="pad5">
<br>
<div class="lh25 just">
<img src="https://u-buy.net/pw_img/flag-$$geog.gif" class="flag" alt="$countrybits{$$geog} Flag"> Below you will find the online stores and services which we currently list for <b>$countrybits{$$geog}</b>. You can go directly to each of the stores by selecting the associated link, or get a little more information about each store by clicking on the store logos.
</div>
~;
}
elsif ($$geog eq'' && $$dir_id gt'')
{
$$listing=qq~
<div class="pad5">
<br>
<div class="lh25 just">
Below you will find the online stores and services which we currently list for our <b>$dirname</b> category. You can go directly to each of the stores by selecting the associated link, or get a little more information about each store by clicking on the logos.
</div>
~;
}



my $other_countries='';

while (my ($key, $value) = each(%countrybits))
{
	if ($key ne $$geog)
	{
	$value=join('','<a href="http://price-wizard.com/cgi-perl/pw.directory.cgi?g=',$key,'">',$value,'</a>');

		if ($other_countries eq'')
		{
		$other_countries=$value;
		}
		else
		{
		$other_countries.=join('',' and ',$value);
		}
	}
}

$$listing.=qq~
<div class="lh25 just"><br>
Other stores may also be displayed for $other_countries. You can also display stores <a href="http://price-wizard.com/cgi-perl/pw.directory.cgi">by category type</a>, such as stores selling fashion, or electrical goods for example.
</div>
~;


if ($$geog gt'' && $$dir_id gt'')
{
$SQLQUERY=join('','SELECT ',$fields,' FROM STORES WHERE CURRENCY="',$$geog,'" AND CATEGORY LIKE "%',$$dir_id,'%" AND ACTIVE=1 ORDER BY STORENAME');
}
elsif ($$geog gt'' && $$dir_id eq'')
{
$SQLQUERY=join('','SELECT ',$fields,' FROM STORES WHERE CURRENCY="',$$geog,'" AND ACTIVE=1 ORDER BY STORENAME');
}
elsif ($$geog eq'' && $$dir_id gt'')
{
$SQLQUERY=join('','SELECT ',$fields,' FROM STORES WHERE CATEGORY LIKE "%',$$dir_id,'%" AND ACTIVE=1 ORDER BY STORENAME');
}
elsif ($$geog eq'' && $$dir_id eq'')
{
$SQLQUERY=join('','SELECT ',$fields,' FROM STORES WHERE CURRENCY="GBP" ORDER BY STORENAME');
}


undef my @records; &fetch_records2(\@records,\$SQLQUERY,'^');

$$listing.='<br>';

my $counter=1;

for (@records)
{
my ($storekey,$storename,$title,$description,$realurl,$logo,$category,$favs,$datafeed,$shortname)=split(/\^/,$_);

my $caret='<i class="fas fa-caret-right w3-text-green"></i> ';
my $browse_prods=''; 
if ($datafeed==1)
{
$browse_prods=qq~ $caret <a href="http://$ENV{'HTTP_HOST'}/cgi-perl/pw.search.cgi?c=$storekey&amp;p=0">Browse Products</a> &nbsp;~;
}

my $pw_page =qq~<a href="https://price-wizard.com/cgi-perl/pw.displaystore.cgi?store=$storekey">$shortname on Price-Wizard</a> &nbsp;~;
my $official=qq~Official Site: <a href="http://price-wizard.com/cgi-perl/pw.direct.pl?store=$storekey">$realurl</a>~;

my $logolink=join('','https://price-wizard.com/links/',lc($storekey),'.html');
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
if ($wday%2==0){
  $logolink=join('','https://price-wizard.com/links2/',lc($storekey),'.html');
}

$$listing.=qq~
<div class="slist">
  <div class="pad5 just">
  <a href="$logolink" title="$storename" onclick="igoto('https://price-wizard.com/cgi-bin/ubdirect.pl?store=$storekey');return false;">
  <img src="https://price-wizard.com/images/logos/$logo" width="150" height="40" alt="$storename" onmousedown="go2('',3,'$storekey',0);">
  </a>
  </div>
  <div class="lh25 padleft10">
  <b>$storename</b><br>$title<br>
  $caret$pw_page $browse_prods $caret$official<br><br>
  </div>
</div>
~;

$counter++;
}

$$listing.='<br>
<div class="just_c">
<a href="../cgi-bin/ubdirect.pl?store=intf" title="Interflora Flowers &amp; Gifts">
<img src="../images/interflora.730x92.webp" alt="Interflora" class="responsive_img"></a>
</div>

</div>
<br><br>
';

}1;
# ---------------------------------------------------------------------------------------------------------------------------

sub fetch_catlist_large {

my $catlist	=shift;		# A reference

$$catlist='
<br>
<div class="lh25 pad5 just">
Below you will find all of the directory categories which we currently list. Selecting any of the links will provide a list of online stores relating to those products or services.
<br><br>
You may also like to view stores by country:
&nbsp;&#8226;&nbsp;<a href="http://price-wizard.com/cgi-perl/pw.directory.cgi?g=gbp">Great Britain</a> 
&nbsp;&#8226;&nbsp;<a href="http://price-wizard.com/cgi-perl/pw.directory.cgi?g=usd">United States</a>
&nbsp;&#8226;&nbsp;<a href="http://price-wizard.com/cgi-perl/pw.directory.cgi?g=cdn">Canada</a>
</div><br><br>
';

my $SQLQUERY='SELECT CATEGORY,CATKEY FROM PW_CATEGORIES WHERE ACTIVE=1 ORDER BY CATEGORY ASC';

undef my @records; &fetch_records2(\@records,\$SQLQUERY,'^');

my $colmax=(int(@records/1.9));

my $triangle='<img src="https://price-wizard.com/pw_img/triangle.grey-13x15.webp" width="13" height="15" align="top" alt="">';

$$catlist.=qq~<div class="clist">\n<div class="lh25 pad5 clistm">~;

my $count=0;

for (@records)
{
my ($category,$catkey) = split (/\^/, $_);

my $catlink=join('',$triangle,' <a href="http://price-wizard.com/cgi-perl/pw.directory.cgi?dir=',$catkey,'">',$category,'</a>');

$$catlist.=$catlink.'<br>';

$count++;

if ($count==$colmax){$$catlist.='</div><div class="lh25 pad5">'; $count=0};

}

$$catlist.="</div>\n</div>";

}1;
# --------------------------------------------------------------------------------------------------------------------------











