<?
session_start();

$document = array();
$script = "";
$init = "";

require "../lib/getnpost.php";
require "../lib/config.php";
require "../lib/dbconnect.php";
require "../lib/auth.php";
require "../lib/common.php";

require "shop_common.php";      // shoptopmenu, leftmenu

function mkShopPageList($gdcat, $page, $pagestart, $pageend, $pagelast) {
	if ($page > 1) {
		$prevpage = $page - 1;

		$prevpagestring = <<<EOS
<a href="{$_SERVER['PHP_SELF']}?gdcat=$gdcat&page=$prevpage"><img src="/images/pre01.gif" border="0"></a>
EOS;

		$firstpagestring = <<<EOS
<a href="{$_SERVER['PHP_SELF']}?gdcat=$gdcat&page=1"><img src="/images/pre02.gif" width="22" height="15" border="0"></a>
EOS;
	} else {
		$prevpagestring = <<<EOS
<img src="/images/pre01.gif" border="0">
EOS;

		$firstpagestring = <<<EOS
<img src="/images/pre02.gif" width="22" height="15" border="0">
EOS;
	}

	$pagelist = <<<EOS
<table width="325" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="29">$firstpagestring</td>
<td width="28">$prevpagestring</td>
<td width="201" align="center">

EOS;

	if ($pagestart > 1) {
		$pagelist .= "<a href=\"{$_SERVER['PHP_SELF']}?gdcat=$gdcat&page=1\">1</a>.. ";
	}

	for ($i = $pagestart; $i <= $pageend; $i++) {
		if ($i == $page) {
			$pagelist .= "<b><font color=\"64872C\" size=\"2\">$i</font></b> " ;
		} else {
			$pagelist .= "<a href=\"{$_SERVER['PHP_SELF']}?gdcat=$gdcat&page=$i\">$i</a> ";
		}
	}

	if ($pageend < $pagelast) {
		$pagelist .= "..<a href=\"{$_SERVER['PHP_SELF']}?gdcat=$gdcat&page=$pagelast\">$pagelast</a>";
	}

	if ($page < $pagelast) {
		$nextpage = $page + 1;

		$nextpagestring = <<<EOS
<a href="{$_SERVER['PHP_SELF']}?gdcat=$gdcat&page=$nextpage"><img src="/images/next01.gif" border="0"></a>
EOS;

		$lastpagestring = <<<EOS
<a href="{$_SERVER['PHP_SELF']}?gdcat=$gdcat&page=$pagelast"><img src="/images/next02.gif" width="22" height="15" border="0"></a>
EOS;
	} else {
		$nextpagestring = <<<EOS
<img src="/images/next01.gif" border="0">
EOS;

		$lastpagestring = <<<EOS
<img src="/images/next02.gif" width="22" height="15" border="0">
EOS;
	}

	$pagelist .= <<<EOS
</td>
<td width="28" align="right">$nextpagestring</td>
<td width="29" align="right">$lastpagestring</td>
</tr>
</table>
EOS;

	return $pagelist;
}

function mkShopListRow(
	&$document, $gdid, $gdseq, $gdname, $gdpriceo, $gdprice, $gdbonus,
	$gdabstract, $gdimage, $gdcat, $gdvisible, $gdstat,
	$page, $count, $totalrowcount, $limitoffset
) {
	global $PsycheAdmin;
	global $SHOP_GIMGPATH, $SHOP_GIMGURI, $SHOP_GIMGDEFAULT;
	global $gdvisiblearray, $gdstatarray;

	if ($PsycheAdmin) {
		$idx = $count + $limitoffset;

		$shopadminstring = <<<EOS
<p style="text-align: center;">
<input type="button" value="»óÇ° Á¤º¸ ¼öÁ¤" style="width: 120px;"
 onclick="modShop($gdid)"><br />

EOS;

		if ($idx) {
			$shopadminstring .= <<<EOS
<a href="javascript:moveUp($gdid,$gdseq)">[¡è]</a>

EOS;
		}

		if ($idx < $totalrowcount-1) {
			$shopadminstring .= <<<EOS
<a href="javascript:moveDown($gdid,$gdseq)">[¡é]</a>

EOS;
		}

		$shopadminstring .= "</p>";

		if ($gdvisible == "N") {
			$gdvisiblestring = <<<EOS
<span style="color: red;">(»óÇ° ¸ñ·Ï¿¡ ¾È º¸ÀÓ)</span>

EOS;
		} else {
			$gdvisiblestring = "";
		}

		if ($gdstat == "X") {
			$gdstatstring = <<<EOS
<span style="color: red;">(ÆÇ¸Å Áß´Ü)</span>

EOS;
		} else {
			$gdstatstring = "";
		}
	} else {
		$shopadminstring = "";

		$gdvisiblestring = "";
		$gdstatstring = "";
	}

	if ($count) {
		$document['main'] .= <<<EOS
            <table width="580" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td height="10"></td>
              </tr>
              <tr>
                <td height="1" bgcolor="E3E4E3"></td>
              </tr>
              <tr>
                <td height="10"></td>
              </tr>
            </table>

EOS;
	}

	if ($gdpriceo != $gdprice) {
		$gdpriceoc = number_format($gdpriceo) . "¿ø";

		$gdoffrate = (int) (($gdpriceo - $gdprice) * 100 / $gdpriceo);

		$gdpriceostring = <<<EOS
<span style="color: red; text-decoration: line-through;">$gdpriceoc</span> ¡æ

EOS;

		$gdoffratestring = "(" . $gdoffrate . "% ÇÒÀÎ È¿°ú)";
	} else {
		$gdpriceostring = "";
		$gdoffratestring = "";
	}

	$gdpricec = number_format($gdprice) . "¿ø";
	$gdbonusc = number_format($gdbonus) . "¿ø";

	if (!is_file($SHOP_GIMGPATH . "/" . $gdimage)) {
		$gdimage = $SHOP_GIMGDEFAULT;
	}

	$document['main'] .= <<<EOS
            <table width="580" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="160"><table width="160" border="0" cellpadding="2" cellspacing="1" bgcolor="D2D3D2">
<tr>
                      <td bgcolor="#FFFFFF"><table width="154" border="0" cellspacing="0" cellpadding="0">
                          <tr>
                            <td height="110" align="center" bgcolor="#CCCCCC">
<a href="shopview.php?gdid=$gdid&gdcat=$gdcat&page=$page"><img src="$SHOP_GIMGURI/$gdimage" width="154" height="110" border="0" /></a></td>
                          </tr>
                        </table></td>
                    </tr>
                  </table>
                  <table width="160" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td height="25" align="center"><!--<img src="/shop/images/b_zoom.gif" width="58" height="17">--></td>
                    </tr>
                  </table>
$shopadminstring
                </td>
                <td width="420" align="right" valign="top">
                  <table width="400" border="0" cellspacing="0" cellpadding="0">
                    <tr> 
                      <td height="25"><img src="/shop/images/ball.gif" width="11" height="11" align="absmiddle"> 
                        <a href="shopview.php?gdid=$gdid&gdcat=$gdcat&page=$page" style="color: #2C6E40; font-weight: bold;">$gdname</a>
$gdstatstring
$gdvisiblestring
</td>
                    </tr>
                    <tr bgcolor="E3E4E3"> 
                      <td height="3"></td>
                    </tr>
                  </table>
                  <table width="400" border="0" cellspacing="0" cellpadding="0">
                    <tr> 
                      <td width="85" align="center" style="padding: 5px;"><img src="/shop/images/st_01.gif" width="70" height="21"></td>
                      <td width="315" style="padding: 5px;">$gdpriceostring
$gdpricec
$gdoffratestring</td>
                    </tr>
                    <tr> 
                      <td height="1" colspan="2" bgcolor="E3E4E3"></td>
                    </tr>
                    <tr> 
                      <td align="center" style="padding: 5px;"><img src="/shop/images/st_02.gif" width="70" height="21"></td>
                      <td style="padding: 5px;">$gdbonusc</td>
                    </tr>
                    <tr> 
                      <td height="1" colspan="2" bgcolor="E3E4E3"></td>
                    </tr>
                    <tr> 
                      <td align="center" valign="top" style="padding: 5px;"><img src="/shop/images/st_03.gif" width="70" height="21"></td>
                      <td style="line-height:18px; padding: 5px;">
$gdabstract
                      </td>
                    </tr>
                    <tr>
                      <td colspan="2" style="padding: 11px;">
<a href="javascript:addCart($gdid)"><img src="/shop/images/b_cart01s.gif" width="71" height="22" border="0"></a>
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table> 

EOS;
}

function mkShopList(&$document, &$script, &$init, $gdcat, $page) {
	global $PsycheAdmin;
	global $SHOP_MAXLINE, $SHOP_MAXPAGE;
	global $gdcatarray;

	if (!$gdcat) $gdcat = "0";
	if (!$page) $page = 1;

	if ($PsycheAdmin) {
		$script .= <<<EOS
function modShop(n) {
	open("/admin/shopmod.php?gdid=" + n, "ShopMod", "width=550,height=600,status=yes,scrollbars=yes");
}

function moveUp(n, m) {
	window.location.href = "{$_SERVER['PHP_SELF']}?gdcat=$gdcat&page=$page&move=U&gdid=" + n + "&gdseq=" + m;
}

function moveDown(n, m) {
	window.location.href = "{$_SERVER['PHP_SELF']}?gdcat=$gdcat&page=$page&move=D&gdid=" + n + "&gdseq=" + m;
}

EOS;
	}

	$document['main'] .= <<<EOS
      <table width="938" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="160" valign="top" background="/shop/images/left_pat.gif"> 
            <img src="/shop/images/left_pat.gif" width="160" />
            <!--shop leftamenu//------------>

            {$document['leftmenu']}

            <!--//shop leftamenu------------>
          </td>
          <td width="623" height="673" align="center" valign="top">
            <table width="623" border="0" cellspacing="0" cellpadding="0">
              <tr> 
                <td width="69" height="26" align="center" background="/shop/images/mall_locationbar_title_bg.gif"><img src="/shop/images/mall_locationbar_title.gif"></td>
                <td width="434" background="/shop/images/mall_locationbar_title_bg.gif"><span style="color: #00355E;"><a href="/" style="color: #00355E;">È¨</a> &gt; <a href="/shop/shopmain.php" style="color: #00355E;">¼îÇÎ¸ô</a> &gt; {$gdcatarray[$gdcat][0]}</span></td>
              </tr>
            </table>
            <table width="623" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td height="99"><img src="/shop/images/{$gdcatarray[$gdcat][1]}" width="623" height="99"></td>
              </tr>
            </table>

EOS;

	if ($PsycheAdmin) {
		if ($gdcat) {
			$wherecondition .= "where gd.gdcat = '$gdcat'";
		} else {
			$wherecondition = "";
		}

		$goodsaddstring = <<<EOS
<input type="button" value="»óÇ° Ãß°¡" style="width: 120px;"
 onclick="modShop(0)">

EOS;
	} else {
		if ($gdcat) {
			$wherecondition = "where gd.gdstat = 'O' and gd.gdvisible = 'Y' and gd.gdcat = '$gdcat'";
		} else {
			$wherecondition = "where gd.gdstat = 'O' and gd.gdvisible = 'Y'";
		}

		$goodsaddstring = "";
	}

	$result = mysql_query("select count(*) from goods as gd
$wherecondition");

	list($totalrowcount) = mysql_fetch_row($result);

	if ($totalrowcount) {
		$document['main'] .= <<<EOS
            <table width="600" border="0" cellspacing="0" cellpadding="5">
              <tr>
                <td width="311" height="40" valign="bottom"><img src="/shop/images/arrow_01.gif" width="18" height="19" align="absmiddle"> 
ÇöÀç <b>ÃÑ {$totalrowcount}°³</b>ÀÇ »óÇ°ÀÌ ÁØºñµÇ¾î ÀÖ½À´Ï´Ù.
                </td>
                <td width="312" align="right" valign="bottom">
$goodsaddstring
<!--
<img src="/shop/images/align_t.gif" width="88" height="15"> 
<select name="select">
<option selected>»óÇ°Á¤·Ä ±âÁØ ¼±ÅÃ</option>
</select>
-->
                </td>
              </tr>
            </table> 
            <table width="623" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td height="17" background="/shop/images/display_start.gif">&nbsp;</td>
              </tr>
            </table>
<!-- »óÇ° ¸ñ·Ï ½ÃÀÛ -->

EOS;

		$pagelast = ceil($totalrowcount / $SHOP_MAXLINE);

		$pagestart = max(1, $page - floor(($SHOP_MAXPAGE + 1) / 2));
		// SHOP_MAXPAGE  9: .. 3 4 5 6 [7] 8 9 10 11 ..
		// SHOP_MAXPAGE 10: .. 3 4 5 6 [7] 8 9 10 11 12 ..

		$pageend = min($pagestart + $SHOP_MAXPAGE - 1, $pagelast);

		$pagelist
		= mkShopPageList($gdcat, $page, $pagestart, $pageend, $pagelast);

		$limitoffset = ($page - 1) * $SHOP_MAXLINE;
		$limitrowcount = $SHOP_MAXLINE;

		$result = mysql_query("select
gd.gdid, gd.gdseq, gd.gdname, gd.gdprice1, gd.gdprice3,
gd.gdbonus, gd.gdabstract, gd.gdimage1, gd.gdvisible, gd.gdstat
from goods as gd
$wherecondition
order by gdseq
limit $limitoffset, $limitrowcount");

		$count = 0;

		while (
			list($gdi, $gdsq, $gdn, $gdprc1, $gdprc3,
			$gdbns, $gdabs, $gdimg, $gdvis, $gdst)
			= mysql_fetch_row($result)
		) {
			mkShopListRow(
				$document, $gdi, $gdsq, $gdn, $gdprc1, $gdprc3,
				$gdbns, $gdabs, $gdimg, $gdcat, $gdvis, $gdst,
				$page, $count++, $totalrowcount, $limitoffset
			);
		}

		$document['main'] .= <<<EOS
<!-- »óÇ° ¸ñ·Ï ³¡ -->
            <br>

            <table width="623" border="0" cellspacing="0" cellpadding="0">
              <tr> 
                <td height="17" background="/shop/images/display_end.gif">&nbsp;</td>
              </tr>
            </table>
            <table width="623" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td height="48" align="center">
$pagelist
                  <br>
                </td>
              </tr>
            </table>

EOS;
	} else {
		$document['main'] .= <<<EOS
            <table width="600" border="0" cellspacing="0" cellpadding="5">
              <tr>
                <td width="311" height="40" valign="bottom"><img src="/shop/images/arrow_01.gif" width="18" height="19" align="absmiddle"> 
ÇöÀç ÁØºñµÈ »óÇ°ÀÌ ¾ø½À´Ï´Ù.</td>
                <td width="312" align="right" valign="bottom"></td>
              </tr>
            </table> 

EOS;
	}

	$document['main'] .= <<<EOS
          </td>
          <td width="155" align="right" valign="top" background="/shop/images/right_pat.gif" style="padding-right:5px;padding-top:8px">{$document['rightbanner']}</td>
        </tr>
      </table>

EOS;

	$script .= <<<EOS
function addCart(n) {
	window.location.href = "shopcartadd.php?gdid=" + n + "&sgqty=1";
}

EOS;
}

## Start

/*
if ($from == "shopcart") {
	if (sizeof($ciid)) {
		while (list($i, $cii) = each($ciid)) {
			// SHOPCART:  0 gdid, 1 sgqty, 2 ciid
			if ($_SESSION['SHOPCART'][$i][0]) {
				$_SESSION['SHOPCART'][$i][2] = $cii;
			}
		}

		header("Location: {$_SERVER['PHP_SELF']}");

		return;
	}
}
*/

if ($move && $PsycheAdmin) {
	if ($gdcat) $gdcatcondition = "  and gdcat = '$gdcat'";

	switch ($move) {
	    case "U":
		$result = mysql_query("select gdid, gdseq from goods
where gdstat = 'O'
  and gdvisible = 'Y'
  and gdseq < '$gdseq'
$gdcatcondition
order by gdseq desc
limit 1");

		break;
	    case "D":
		$result = mysql_query("select gdid, gdseq from goods
where gdstat = 'O'
  and gdvisible = 'Y'
  and gdseq > '$gdseq'
$gdcatcondition
order by gdseq asc
limit 1");

		break;
	}

	list($gdid2, $gdseq2) = mysql_fetch_row($result);

	if ($gdid2 && $gdseq2) {
		mysql_query("update goods set gdseq = '$gdseq2'
where gdid = '$gdid'");

		mysql_query("update goods set gdseq = '$gdseq'
where gdid = '$gdid2'");
	}

	header("Location: {$_SERVER['PHP_SELF']}?gdcat=$gdcat&page=$page");

	return;
}

mkShopList($document, $script, $init, $gdcat, $page);

require "shop_template.php";
?>
