<?php



define('IN_PHPQUAN', true);

require 'include/common.php';

require '../include/com.fun.php';



$_REQUEST['act'] = $_REQUEST['act'] ? trim($_REQUEST['act']) : 'list' ;

chkadmin('com');

switch ($_REQUEST['act'])

{

	case 'list':

		$cats = com_cat_options();

		$page = empty($_REQUEST[page])? 1 : intval($_REQUEST['page']);



		$catid    = empty($_REQUEST['cat']) ? 0 : intval($_REQUEST['cat']);

		$areaid   = empty($_REQUEST['area']) ? 0 : intval($_REQUEST['area']);

		$typeid   = empty($_REQUEST['type']) ? 0 : intval($_REQUEST['type']);

		$keywords = empty($_REQUEST['keywords']) ? '' : trim($_REQUEST['keywords']);

		

		$where = '';

		$where = $catid > 0 ? " and catid IN (" . get_cat_children($catid) . ")": '';

		$where .= $areaid > 0 ? " and areaid IN (" . get_area_children($areaid) . ")": '';

		

		switch($typeid)

		{

			case '1':

				$where .= " and is_check=1 ";

			break;



			case '2':

				$where .= " and is_check=0 ";

			break;



			case '3':

				$where .= " and is_pro>".time();

			break;



			case '4':

				$where .= " and is_top>".time();

			break;

		}

		

		if (!empty($keywords)) {

			$where .= " AND (comname LIKE '%$keywords%' OR phone LIKE '%$keywords%')";

		}

		

		if(empty($where) && !isset($_POST['cat']) && !isset($_POST['area']) && !isset($_POST['keywords']) && isset($_GET['page'])) {

			$_SESSION['where'] = $_SESSION['where'];

		} elseif(empty($where) && isset($_POST)) {

			$_SESSION['where'] = '';

		} elseif(!empty($where))

		{

			$_SESSION['where'] = $where;

		}



		$sql = "SELECT COUNT(*) FROM {$table}com WHERE 1 $_SESSION[where]";

		$count = $db->getOne($sql);

		$pager = get_pager('com.php',array('act'=>'list'),$count,$page,'15'); 

		$sql = "SELECT * FROM {$table}com WHERE 1 $_SESSION[where] ORDER BY comid DESC,postdate DESC LIMIT $pager[start],$pager[size]";

		$res = $db->query($sql);

		$articles = array();

		while($row=$db->fetchRow($res)) {

			$row['postdate'] = date('Y-m-d', $row['postdate']);

			$row['is_check'] = $row['is_check']==1 ? '是' : '否';

			$articles[]      = $row;

		}

	    include tpl('list_com', 'com');

	break;



	case 'edit':

		$id = intval($_REQUEST['id']);

		$sql = "SELECT * FROM {$table}com WHERE comid = '$id'";

		$coms = $db->getRow($sql);

		if(empty($coms)){show('信息不存在','index.php');}

		extract($coms);

		$u = member_info($userid);

		$postdate = date('Y年m月d日', $postdate);

		$postjuan = $db->getOne("select count(*) from {$table}juan where comid='$id'");

		$lqjuan = $db->getOne("select count(*) from {$table}juan_user where comid='$id'");

		$hxjuan = $db->getOne("select count(*) from {$table}juan_user where comid='$id' and offdate>0");

		//$addmoney = $db->getOne("select count(*) from {$table}juan where comid='$id'");

		$refer = $_SERVER['HTTP_REFERER'];

		include tpl('edit_com', 'com');

	break;



	case 'update':

		$comid    = intval($_POST['id']);

		$userid    = intval($_POST['userid']);
		$comname  = $_POST['comname'] ? htmlspecialchars(trim($_POST['comname'])) : '';

		$phone    = $_POST['phone'] ? trim($_POST['phone']) : '';

		$address  = $_POST['address'] ? trim($_POST['address']) : '';

		$is_check  = trim($_POST['is_check']);



		if(empty($comname))showmsg("标题不能为空");

		if(empty($phone))showmsg("电话必须填写");



		$sql = "UPDATE {$table}com SET

				comname='$comname',

				userid='$userid',
				phone='$phone',

				address='$address',

				is_check='$is_check'

				where comid = '$comid' ";

		$res = $db->query($sql);


		//变更会员类型
		if($is_check=='1'){
			$sql = "UPDATE {$table}member SET gid=1 WHERE userid='$userid'";
			$re = $db->query($sql);
		}
		admin_log("编辑店铺 $comid 成功");

		$refer = trim($_POST['refer']);

		show('修改成功', $refer);

	break;



	case 'batch':

		$id = is_array($_REQUEST['id']) ? join(',', $_REQUEST['id']) : intval($_REQUEST['id']);

		if(empty($id))show('没有选择记录');

		if(empty($_REQUEST['type']))show('没有选择操作');

		switch ($_REQUEST['type'])

		{

			case 'delete':

				$sql = "DELETE FROM {$table}juan WHERE comid in ($id)";

				$res = $db->query($sql);

				

				$sql = "DELETE FROM {$table}juan_user WHERE comid IN ($id)";

				$db->query($sql);



				$sql = "DELETE FROM {$table}com WHERE comid IN ($id)";

				$db->query($sql);



				admin_log("删除商家 $id 成功");

				show('删除商家成功', $_SERVER['HTTP_REFERER']);

			break;



			case 'is_check':

				$sql = "UPDATE {$table}com SET is_check=1 WHERE comid IN ($id)";

				$re = $db->query($sql);

				admin_log("审核商家 $id 成功");

				show('审核成功', $_SERVER['HTTP_REFERER']);

			break;

		}

	break;

}

?>