<?php
define('IN_PHPMPS', true);
require dirname(__FILE__) . '/include/common.inc.php';
require PHPMPS_ROOT . 'include/com.fun.php';
$nolook=1;
$act = $_REQUEST['act'] ? $_REQUEST['act'] : 'list';
switch($act){
 case 'list':
    $catid     = $_REQUEST['catid'] ? intval($_REQUEST['catid']) : '';
	$areago    = $_REQUEST['area'] ? intval($_REQUEST['area']) : '';
	$order     = $_REQUEST['order'] ? trim($_REQUEST['order']) : 'new';
	$check_wf  = $_REQUEST['check_wf'] ? intval($_REQUEST['check_wf']) : '';
	$check_p   = $_REQUEST['check_p'] ? intval($_REQUEST['check_p']) : '';
	$check_wfs = empty($check_wf) ? 1 : 0;
	$check_ps  = empty($check_p) ? 1 : 0;
    $areaid    = $_REQUEST['area'] ? $areago : intval($_COOKIE['cityid']);
	$page      = $_REQUEST['page'] ? intval($_REQUEST['page']) : '1';
	$keyword   = $_REQUEST['keyword'] ? keyword_replace($_REQUEST['keyword']) : '';
		
	if(!empty($keyword)) {
	  $arr = array();
	  $keywords = 'AND (';
	  $keywords .= "(comname LIKE '%$keyword%' OR address LIKE '%$keyword%' OR phone LIKE '%$keyword%')";
	  $keywords .= ')';
    }

	$com_cats = get_com_cat_list($areago,$order,$keyword,$check_wf,$check_p);/* 分类列表 */
	if($catid) {
		$com_cat_info = get_com_cat_info($catid);

		if(empty($com_cat_info['parentid'])) {
			$cats = get_com_cat_children($catid);
			if(empty($cats))$cats=$catid;
		} else {
			$cats = $catid;
		}
		$cat_sql = " and catid in ($cats) ";
	}
if($areaid) {
	$area_info = get_area_info($areaid);
	if(empty($area_info)) showmsg('不存在此地区');
	//$here_arr[] = array('name'=>$area_info['areaname']);
	$area_parent = $area_info['parentid'];
	if(empty($area_parent)) {
		//大分类
		
		$area_row = get_area_children($areaid,'array');
		if($area_row) {
			//导航条
			$area_arr = array();
			foreach($area_row as $val) {
				//EDIT:
				$val['areaid'] = $val['id'];
				$val['areaname'] = $val['name'];
				$val['url'] = url_rewrite('com',array('act'=>'list','catid'=>$catid,'eid'=>$val['areaid'],'order'=>$order,'keyword'=>$keyword,'check_p'=>$check_p,'check_wf'=>$check_wf));
				$area_arr[] = $val;
			}
		} else {
		}
		$areas = get_area_children($areaid);
		if(!empty($areas)) $areas .= ','.$areaid;
	} else {
		//如果有父分类，取得他所有的子分类
		$area_row = get_area_children($area_parent, 'array');
		/* 生成导航 */
		
		foreach($area_row as $val) {
			$val['areaid'] = $val['id'];
			$val['areaname'] = $val['name'];
			$val['url'] = url_rewrite('com',array('act'=>'list','catid'=>$catid,'eid'=>$val['id'],'order'=>$order,'keyword'=>$keyword,'check_p'=>$check_p,'check_wf'=>$check_wf));
			$area_arr[] = $val;
		}
		/* 生成搜索 */
		$areas = $areaid;
	}
	    $area_sql = " and areaid in ($areas) ";

}//
	if($order=='new')$orders='a.postdate DESC';
	if($order=='hot')$orders='a.click DESC';
	if($order=='sale')$orders='min(zk_list) ASC';
	if($order=='star')$orders='sum_cm DESC';
	
	if(intval($_REQUEST['check_wf'])!=1 && intval($_REQUEST['check_p'])!=1){
		$check_sql = '';
	}else{
		$check_sql .= 'and (';
		$check_sql .= intval($_REQUEST['check_wf'])==1 ? ' locate("wifi", icon)>0' : '0';
		$check_sql .= ' or ';
		$check_sql .= intval($_REQUEST['check_p'])==1 ? ' locate("car", icon)>0' : '0';
		$check_sql .= ')';
	}

	$area_array = get_area_array();
	$sql = "SELECT COUNT(*) FROM {$table}com as i WHERE is_check=1 $cat_sql $area_sql $check_sql $keywords";
	$count = $db->getOne($sql);
	$size = 10;//$CFG['com_pagesize'] ? $CFG['com_pagesize'] : 10;
	$pager = page('com',$catid,$areaid,$count,$size,$page,$order,$keyword,$check_p,$check_wf);
	$sql = "SELECT a.*,a.comid as acomid,b.*,count(c.comid) as ccid,((sum(c.cm_pz)+sum(c.cm_fw)+sum(c.cm_hj))/3/count(c.comid)) as sum_cm FROM {$table}com as a left join {$table}com_config as b on a.comid=b.comid left join {$table}com_comment as c on a.comid=c.comid WHERE a.is_check=1 $cat_sql $area_sql $check_sql $keywords group by a.comid ORDER BY $orders limit $pager[start],$pager[size]";
	$res = $db->query($sql);
	$coms = array();
	while($row=$db->fetchRow($res)) {
		$row['postdate']   = date('y年m月d日', $row['postdate']);
		$row['thumbs']      = PHPMPS_PATH.$row['thumb'];
		$row['introduce']  = cut_str($row['introduce'],30);
		$row['areaname']   = $area_array[$row['areaid']];
		$row['catname']    = $cat_array[$row['catid']];
		$row['jifens']     = explode('|',$row['jf_bl']);
	    $row['jd']         = explode('|',$row['jf_day']);
		$row['icon']       = explode(',',$row['icon']);
		$row['url']        = url_rewrite('com',array('act'=>'view','comid'=>$row['acomid']));
		$row['sum_cm']     = $row['sum_cm']!=0 ? number_format($row['sum_cm'],1) : '<span class="f99">0.0</span>';
		if($keyword) {
		   $row['comname'] = preg_replace('/'.$keyword.'/i','<font class="f43">'.$keyword.'</font>', $row['comname']);
		   $row['address'] = preg_replace('/'.$keyword.'/i','<font class="f43">'.$keyword.'</font>', $row['address']);
	    }
		$coms[] = $row;
	}
	$com_city      = !empty($area_info['areaname']) && $area_info['parentid']!=0 ? $area_info['areaname'] : '全城';
	$com_cat       = !empty($com_cat_info['catname']) ? $com_cat_info['catname'] : '分类';
	$com_ordername = array('new'=>'最新','star'=>'评分','hot'=>'人气');
	$com_order     = $com_ordername[$order];
	$page_id = 'com_list';

    $seo['title'] = '商家列表_'.$CFG['webname'];
    include tpl('com_list');
	break;
//View
  case 'view':
	$comid = intval($_REQUEST['id']);
	if(empty($comid)) showwap('缺少参数！');
	$area_array = get_area_array();
	$com_info = $db->getRow("select * from {$table}com where comid='$comid' ");
	if(empty($com_info)) showwap('店铺不存在','index.php');
	unset($com_info['userid']);
	extract($com_info);
	if(!$is_check)showwap('商家尚未审核，审核后可浏览！');
	$mappoint = $mappoint ? explode(',', $mappoint) : '';
	$thumbs = PHPMPS_PATH.$thumb;
	$areaname = $area_array[$areaid];
	$hours = explode(',',$hours);
	$icon  = explode(',',$icon);
	
	//调用商家优惠配置
	$com_cfg = $db->getRow("select * from {$table}com_config where comid='$comid' ");
	$jifens   = explode('|',$com_cfg['jf_bl']);
	$com_cfg['jd'] = explode('|',$com_cfg['jf_day']);
	$zk_list = array();
	foreach (explode('|',$com_cfg['zk_list']) as $row){
		$zk_list[] = explode(',',$row);
    }
	$counts = $db->getRow("SELECT count(userid) as count_user,sum(binary yjia) as count_money FROM {$table}pay_exchange WHERE comid='$comid'");
	
	//EDIT:综合评分
	$stars = $db->getRow("SELECT count(comid) as pf1,sum(cm_pz) as pf2,sum(cm_fw) as pf3,sum(cm_hj) as pf4 FROM {$table}com_comment WHERE comid='$comid' group by comid");
	if (empty($stars)) {
	$all_stars = "0.0";
	$cm_pzs = "0.0";
	$cm_fws = "0.0";
	$cm_hjs = "0.0";
	}else {
	$pfs = ($stars['pf2']+$stars['pf3']+$stars['pf4'])/3;
	$all_stars = number_format($pfs/$stars['pf1'],1);
	$cm_pzs = number_format($stars['pf2']/$stars['pf1'],1);
	$cm_fws = number_format($stars['pf3']/$stars['pf1'],1);
	$cm_hjs = number_format($stars['pf4']/$stars['pf1'],1);
	}
	
	//调用人数及平均消费
	$r = $db->getRow("SELECT count(userid) as all_user,AVG(yjia) as moneys FROM {$table}pay_exchange WHERE comid='$comid'");
	$r['moneys'] = number_format($r['moneys'],2);
	
	//调用评论
	//$sql = "SELECT * FROM {$table}com_comment WHERE comid = '$comid' AND is_check = 1 ORDER by postdate DESC LIMIT 3";
	$sql = "SELECT a.*,b.username,b.face FROM {$table}com_comment as a left join {$table}member as b on a.userid=b.userid WHERE comid = '$comid' AND is_check = 1 ORDER by postdate DESC LIMIT 3";
    $res = $db->query($sql);
    $comment = array();
    while($row = $db->fetchRow($res)) {
	    $row['username'] = $row['username'] ? $row['username'] : '本站网友' ;
		$row['face'] = empty($row['face']) ? $CFG['thumbs'] : $row['face'];
	    $row['cm_jgs'] = floor($row['cm_jg']*(100/3));
		$row['cm_pzs'] = floor($row['cm_pz']*(100/3));
		$row['cm_fws'] = floor($row['cm_fw']*(100/3));
		$row['cm_hjs'] = floor($row['cm_hj']*(100/3));
		$row['postdate'] = mdate($row['postdate']);
		$row['retime'] = mdate($row['retime']);
	    $comment[] = $row;
    }
	
	//发布评论时调用
	$dpid = $db->getRow("SELECT dp,userid FROM {$table}pay_exchange WHERE comid='$comid' and exchangeid='$commentid'");
	
    $seo['title']   = $comname."_".$CFG['webname'];
    include tpl('com_view');
	break;
//评论
  case 'comment':
	$comid = intval($_REQUEST['comid']);
	if(empty($comid)) showwap('缺少参数！');
	$comname = $db->getOne("select comname from {$table}com where comid='$comid' ");
	//EDIT:综合评分
	$stars = $db->getRow("SELECT count(comid) as pf1,sum(cm_pz) as pf2,sum(cm_fw) as pf3,sum(cm_hj) as pf4 FROM {$table}com_comment WHERE comid='$comid' group by comid");
	if (empty($stars)) {
	  $all_stars = "0.0";
	}else {
	  $pfs = ($stars['pf2']+$stars['pf3']+$stars['pf4'])/3;
  	  $all_stars = number_format($pfs/$stars['pf1'],1);
	}
    $page = empty($_REQUEST['page'])? 1: intval($_REQUEST['page']);
    $count = $db->getOne("SELECT COUNT(*) FROM {$table}com_comment WHERE is_check=1 and comid = '$comid'");
    $pager['search'] = array('act'=>'comment','comid' => $comid);
	$pagesizes = '10';
    $pager = get_pager('com.php', $pager['search'], $count, $page, $pagesizes);
    $sql = "SELECT a.*,b.username,b.face FROM {$table}com_comment as a left join {$table}member as b on a.userid=b.userid WHERE comid = '$comid' AND is_check = 1 ORDER by id DESC LIMIT $pager[start],$pager[size]";
    $res = $db->query($sql);
    $comment = array();
    while($row = $db->fetchRow($res)) {
	    $row['username'] = $row['username'] ? $row['username'] : '本站网友' ;
		$row['face'] = empty($row['face']) ? $CFG['thumbs'] : $row['face'];
	    $row['cm_jgs'] = floor($row['cm_jg']*(100/3));
		$row['cm_pzs'] = floor($row['cm_pz']*(100/3));
		$row['cm_fws'] = floor($row['cm_fw']*(100/3));
		$row['cm_hjs'] = floor($row['cm_hj']*(100/3));
		$row['postdate'] = mdate($row['postdate']);
		$row['retime'] = mdate($row['retime']);
	    $comment[] = $row;
    }
	$seo['title']   = $comname."点评_".$CFG['webname'];
    include tpl('com_view_comment');
    break;
  case 'addma':
  get_login();
	 if($_POST) {
		$money = numzh($_REQUEST['money']);
		$id    = intval($_REQUEST['id']);
		chkcom($id);
		if(!is_numeric($money))showwap('金额错误！');
		//判断商家积分余额
		$sql = "select * from {$table}com_config where comid='$id'";
        $cominfo = $db->getRow($sql);
		$cominfo['jf_day']  = explode('|',$cominfo['jf_day']);
		$todays  = date('j', time());
		$cominfo['jf_bl']   = explode('|',$cominfo['jf_bl']);
		if(!empty($cominfo[jf_bl][0]) && !empty($cominfo[jf_bl][1])){
		  if(strpos($cominfo['jf_day'][0],strval(",".$todays.","))!==false){
		    $cominfo['jifen'] = floor($money/$cominfo['jf_bl'][0]*$cominfo['jf_bl'][1]*$cominfo['jf_day'][1]);
		  }else{
			$cominfo['jifen'] = floor($money/$cominfo['jf_bl'][0]*$cominfo['jf_bl'][1]);
		  }
		}else{
			$cominfo['jifen'] = '0';
		}
        $sql = "SELECT jf_num FROM {$table}com WHERE comid='$id'";
        $coms = $db->getOne($sql);
        if($coms < $cominfo['jifen'])showwap('商家积分余额不足！');
	 }else{
		$sql = "select * from {$table}com_config where userid='$_userid'";
		$cominfo = $db->getRow($sql);
		chkcom($cominfo['comid']);
		$cominfo['jf_day']  = explode('|',$cominfo['jf_day']);
		$cominfo['jf_bl']  = explode('|',$cominfo['jf_bl']);
	    $zk_list = array();
		foreach (explode('|',$cominfo['zk_list']) as $row){
			 $zk_list[] = $row ? explode(',',$row) : '0';
        }
		$todays  = date('j', time());
		//$seo['title'] = '扫描二维码收费';
	 }
	 include tpl('com_addma');
	 break;
}
?>