<?php

define('IN_PHPMPS', true);
require dirname(__FILE__) . '/include/common.php';
require dirname(__FILE__) . '/include/com.fun.php';
$act = $_REQUEST['act'] ? $_REQUEST['act'] : 'list' ;
$durl = 'com';
if($act=='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'=>urlencode($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'=>urlencode($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 = $CFG['com_pagesize'] ? $CFG['com_pagesize'] : 10;
	$pager = page('com',$catid,$areaid,$count,$size,$page,$order,urlencode($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);
	$articles = array();
	while($row=$db->fetchRow($res)) {
		$row['postdate']   = date('y年m月d日', $row['postdate']);
		$row['thumbs']      = !empty($row['thumb']) ? PHPMPS_PATH.$row['thumb'] : $CFG['com_thumbs'];
		$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','<span class=fe43>'.$keyword.'</span>', $row['comname']);
		   $row['address'] = preg_replace('/'.$keyword.'/i','<span class=fe43>'.$keyword.'</span>', $row['address']);
	    }
		$articles[] = $row;
	}
    
	$res = $db->query("SELECT a.comid,a.comname,a.thumb,a.is_check,((sum(b.cm_pz)+sum(b.cm_fw)+sum(b.cm_hj))/3/count(b.comid)) as sum_cm FROM {$table}com a,{$table}com_comment b where a.is_check=1 and a.comid=b.comid $area_sql group by a.comid ORDER BY sum_cm DESC limit 5");
	$match_com = array();
	while($row=$db->fetchrow($res)) {
		$row['insc'] = number_format($row['sum_cm'],1);
		$row['sname'] = cut_str($row['comname'],12);
		$row['url'] = url_rewrite('com', array('act'=>'view', 'comid'=>$row['comid']));
		$match_com[] = $row;
	}
	
	if(empty($com_cat_info) && empty($area_info)) {
		$here_arr[] = array('name'=> '商家列表');
	} elseif(empty($com_cat_info) && !empty($area_info)) {
		$here_arr[] = array('name'=> '商家列表','url'=>url_rewrite('com', array('act'=>'list', 'catid'=>$catid)));
	} else {
		$here_arr[] = array('name'=>$com_cat_info['catname'],'url'=>url_rewrite('com', array('act'=>'list', 'catid'=>$catid)));
	}

	$here_arr[] = array('name'=>$area_info['areaname'],'url'=>url_rewrite('com', array('act'=>'list', 'eid'=>$areaid)));
	$here = get_here($here_arr);

	$seo['title'] = $area_info['areaname'] . $com_cat_info['catname'] . '商家_'.$CFG['webname'];
	$seo['keywords'] = $area_info['areaname'].$com_cat_info['keywords'];
	$seo['description'] = $com_cat_info['description'];

	include template('com_list');
}
elseif($act=='view')
{
	$comid = intval($_REQUEST['id']);
	if(empty($comid)) showmsg('缺少参数！');
	$area_array = get_area_array();
	$com_info = $db->getRow("select * from {$table}com where comid='$comid' ");
	if(empty($com_info)) showmsg('店铺不存在','index.php');
	unset($com_info['userid']);
	extract($com_info);
	if(!$is_check)showmsg('商家尚未审核，审核后可浏览！');
	$mappoint = $mappoint ? explode(',', $mappoint) : '';
	//$thumbs = PHPMPS_PATH.$thumb;
	$thumbs      = !empty($thumb) ? PHPMPS_PATH.$thumb : $CFG['com_thumbs'];
	$introduces  = cut_str($introduce,30)."...";
	$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'");
	
    $riqi=getdate();
    $nian=$riqi['year'];
    $yue=$riqi['mon'];
    $ri=$riqi['mday'];
    $hour=$riqi['hours'];
    $min=$riqi['minutes'];
    $miao=$riqi['seconds'];
    if(isset($_GET["yue"])){
	  $yue=intval($_GET["yue"]);
    }
    if(isset($_GET["nian"])){
	  $nian=intval($_GET["nian"]);
    }
    $tianshu=date('t',mktime(0,0,0,$yue,1,$nian));
    $yuez=date('w',mktime(0,0,0,$yue,1,$nian));
    $tian=0;
	$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);
	
	$res = $db->query("select * from {$table}com_image where comid='$comid' ");
	$com_images = array();
	while($row=$db->fetchRow($res)) {
		$row['path'] = PHPMPS_PATH . $row['path'];
		$com_images[] = $row;
	}
	$db->query("UPDATE {$table}com SET click=click+1 WHERE comid='$comid'");
	$res = $db->query("SELECT a.comid,a.comname,a.thumb,a.is_check,(sum(b.cm_pz)+sum(b.cm_fw)+sum(b.cm_hj))/3 as sum_cm,count(b.comid) as ccid FROM {$table}com a,{$table}com_comment b where a.is_check=1 and a.comid=b.comid and catid='$catid' group by a.comid ORDER BY sum_cm ASC,click DESC limit 6");
	$match_com = array();
	while($row=$db->fetchrow($res)) {
		$row['insc'] = number_format($row['sum_cm']/$row['ccid'],1);
		$row['sname'] = cut_str($row['comname'],12);
		$row['url'] = url_rewrite('com', array('act'=>'view', 'comid'=>$row['comid']));
		$match_com[] = $row;
	}
	
	$cat_info = get_com_cat_info($catid);
	$here_arr[] = array('name'=>$cat_info['catname'],'url'=>url_rewrite('com',array('act'=>'list','catid'=>$catid)));
	$here_arr[] = array('name'=>$comname);
	$here = get_here($here_arr);

	$seo['title']   = $comname."_".$cat_info['catname']."_".$CFG['webname'];
	$seo['keywords']  = $comname.','.$CFG['webname'];
	$seo['description'] = $introduces;

	include template('com_view');
}
?>