<?php



define('IN_PHPQUAN', true);

require dirname(__FILE__) . '/include/common.php';

chkadmin('juan');



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



switch ($_REQUEST['act'])

{

	case 'list':

		$cats = cat_options();

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

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

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

		

		$where = '';

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

	

		switch($typeid)

		{

			case '1':

				$where .= " and a.is_check=1 ";

			break;



			case '2':

				$where .= " and a.is_check=0 ";

			break;

		}

		$sql = "SELECT COUNT(*) FROM {$table}juan as a WHERE 1 $where";

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



		$pager['search'] = array('act'=>'list','cat' => $catid, 'type'=>$typeid);

		$pager = get_pager('juan.php',$pager['search'],$count,$page,'15'); 



		$sql = "SELECT a.*,c.comname,c.comid as cid FROM {$table}juan as a left join {$table}com as c on a.comid=c.comid WHERE 1 $where ORDER BY id DESC,postdate DESC LIMIT $pager[start],$pager[size]";

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

		$juan = array();

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

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

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

			$juan[$row['id']]      = $row;

		}

		
	    include tpl('list_juan');

	break;

	

	case 'edit':

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

		$sql = "SELECT a.*,m.comid,m.comname,count(c.juanid) as ujid FROM {$table}juan AS a LEFT JOIN {$table}com AS m ON m.userid=a.userid left join {$table}juan_user as c on a.id=c.juanid WHERE a.id='$id'";

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

		if(empty($juan)){show('券不存在','juan.php');}

		extract($juan);

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

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

		$enddate_day  = round(($enddate>0 ? ($enddate-time()) : '0')/(3600*24));

		$info_cat = cat_options($catid);

		$forusers = $foruser==1 ? '仅在本店核销过会员可领' : '全部会员';

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

		$ip = get_ip();

		$refer = $_SERVER['HTTP_REFERER'];

		include tpl('edit_juan');

	break;



	case 'update':	

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

		$catid   = intval($_POST['catid']);

		$def_cat = intval($_POST['default_cat']);

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

		if($catid != $def_cat)$db->query("delete from {$table}cus_value where juanid='$id' ");

		$sql = "update {$table}juan set catid='$catid',jinfo='$jinfo',is_check='$is_check' where id = '$id' ";

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

		admin_log("编辑券 $id 成功");

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

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

	break;



	case 'batch':

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

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

		switch ($_REQUEST['type'])

		{

			case 'delete':

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

				$db->query($sql);

				

				$sql = "DELETE FROM {$table}cus_value where juanid IN ($id)";

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

				

				$res = $db->query("DELETE FROM {$table}juan WHERE id in ($id)");



				admin_log("删除券 $id 成功");

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

			break;



			case 'is_check':

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

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

				admin_log("审核券 $id 成功");

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

			break;

		}

	break;

}

?>