<?php
class ShopBase
{
	/**
	 * @method 获取某模块的HTML数据
	 * @author soul
	 * @copyright 2017/9/25
	 */
	function getModuleHtml($shopId, $moduleConfig)
	{
		$tmpPathTpl = mvc::$cfg['PATH_TPL'];
		//初始化店铺路径
		mvc::$cfg['ROOT_SHOP'] = mvc::$cfg['ROOT'].'shop/'.$shopId.'/';
		$shopCfg = require(mvc::$cfg['ROOT_SHOP'].'shop.config.php');
		$config['PATH_TPL'] = mvc::$cfg['ROOT'].'shop/demo/tpl/';
		mvc::$cfg = array_merge(mvc::$cfg, $shopCfg);

		$shopInfo = $this->getShopInfo($shopId);
		LibTpl::Set('shopInfoData', $shopInfo);

		$allModuleConfig = require(mvc::$cfg['ROOT_SHOP_SYS'].'module.config.php');
		
		//模块数据
		$returnArr = ['html'=>'', 'css'=>[], 'js'=>[]];

		//系统模块配置
		$sysModulConfig = $allModuleConfig[$moduleConfig['module']];
		// print_r($sysModulConfig);
		//模块需要加载的数据
		if(!empty($sysModulConfig['data']))
		{
			// print_r($moduleConfig['config']);
			$class = new $sysModulConfig['data']['class'];
			LibTpl::Set($moduleConfig['module'].'Config', $moduleConfig['config']);
			LibTpl::Set($moduleConfig['module'].'Data', call_user_func_array(array($class, $sysModulConfig['data']['function']), [$shopId, $moduleConfig['config']]));
		}
		// print_r($sysModulConfig['tpl']);
		//模块需要调用的模板
		if(!empty($sysModulConfig['tpl']))
		{
			$returnArr['html'] = LibTpl::Fetch($sysModulConfig['tpl'],$config['PATH_TPL']);
		}
		//模块需要加载的CSS
		if(isset($sysModulConfig['css']))
		{
			$returnArr['css'] =  $sysModulConfig['css'];
		}
		//模块需要加载的JS
		if(isset($sysModulConfig['js']))
		{
			$returnArr['js'] = $sysModulConfig['js'];
		}
		mvc::$cfg['PATH_TPL'] = $tmpPathTpl;
		return $returnArr; 
	}
	
	
	/*
	 *店铺基本信息
	 */
	function getShopInfo($shopId)
	{
		$B2cShop = new B2cShop();
		$shopArr = array();
		$re = $B2cShop->getShopInfo($shopId);
		$shopArr = $re['data'];		
		if(!empty($shopArr))
		{
			$YpwAddr = new YpwAddr();
			$arr = explode(',',$shopArr['shop_pct_ids']);
			$arr = implode(',',array_slice($arr, 0,2));
			$addrArr = $YpwAddr->getAddrByIds($arr);
			if(!empty($addrArr['data']))
			{	
				foreach($addrArr['data'] as $k=>$v)
				{
					$addstr .=$v; 
				}
			}
			$shopArr['addr'] = $addstr;
		}
		//获取店铺平均分
		$res = $B2cShop->getShopAverage();
		//获取店铺最高分
		$hight = $B2cShop->getShopHighest();
		$shopArr['DESCRIBE_DIF'] = $shopArr['shop_describe_score'] - $res['data']['describe'];
		$shopArr['SERVICE_DIF'] = $shopArr['shop_service_score'] - $res['data']['service'];
		$shopArr['LOGISTICS_DIF'] = $shopArr['shop_logistics_score'] - $res['data']['logistics'];
		$shopArr['AVG_DESCRIBE'] = $res['data']['describe'];
		$shopArr['AVG_SERVICE'] = $res['data']['service'];
		$shopArr['AVG_LOGISTICS'] = $res['data']['logistics'];
		$shopArr['HIGHT_DESCRIBE_DIF'] = $hight['data']['describe'];
		$shopArr['HIGHT_SERVICE_DIF'] = $hight['data']['service'];
		$shopArr['HIGHT_LOGISTICS_DIF'] = $hight['data']['logistics'];
		return $shopArr;
	}
	/*
	 *获取店铺招牌 
	 */
	function getShopBanner($shopId,$config)
	{
		if($config['type']=='customize' && LibFc::Int($config['data']['sartId']))
		{
			if(isset($config['data']['info']) && !empty($config['data']['info']))
			{

			}
			else
			{
				$B2cBase = new B2cBase();
				$re = $B2cBase->Get('sh_article',['sart_txt'],' and sart_id='.$config['data']['sartId'],true);
				if($re['status'])
				{
					$config['data']['info']=$re['data']['sart_txt'];
				}
			}
		}
		return $config;

	}
	//详情页获取当前店铺分类数据
	function getDetailShopSort($shopId,$config)
	{
		// 获取当前店铺所有店铺分类数据
		$B2cShopSort = new B2cShopSort();
		$AllShopSortData = $B2cShopSort->getShopSort($shopId);
		$AllShopSortData = $AllShopSortData['data'];
		return $AllShopSortData;
	}


	/**
	 * @method    店铺顶部导航栏获取分类数据
	 * @author xu
	 * @copyright 2017-09-18
	 */
	function getshopMenu($shopId,$config)
	{
		$B2cBase = new B2cBase();
		$data = array();

		if(empty($config['sort'])) return $data;


		foreach($config['sort'] as $k=>$v)
		{
			foreach($v as $kk=>$vv)
			{
				if($kk === 'is_new')
				{
					$data[$k]['is_new'] = $vv;
				}else if($kk === 'son')
				{
					foreach($vv as $sonk=>$sonv) 
					{
						$res = $B2cBase->get('sh_sort',['sort_name'],sprintf(' and sort_id=%d and sort_shop_id=%d',$sonv,$shopId),true);
						if(!empty($res['data']['sort_name']))
							$data[$k]['son'][$sonv]=$res['data']['sort_name'];
					}
				}else
				{
					$re = $B2cBase->get('sh_sort',['sort_name'],sprintf(' and sort_id=%d and sort_shop_id=%d',$vv,$shopId),true);
					if(!empty($re['data']['sort_name']))
						$data[$k][$vv]=$re['data']['sort_name'];
				}
			}
		}

		// print_r($data);
 		return $data;
	}

	/**
	 * @method    自定义模块
	 * @author xu
	 * @copyright 2017-09-18
	 */
	function custom($shopId,$config)
	{	
		// 样式
		$data['className'] = 'custom-wrapper-'.$config['viewType'];
		
		if(isset($config['articleTxt']))
		{	//临时存储
			$data['article']=$config['articleTxt'];
			return $data;
		}
		
		if(empty($config['articleId']) || !LibFc::Int($articleId = $config['articleId']))
		{	//文章不存在
			$data['nomsg'] = 'nomsg';
			return $data;
		}

		// 读取文章
		$B2cBase = new B2cBase();
		$articleMsg = $B2cBase->get('sh_article',['sart_txt'],sprintf(" and sart_id=%d and sart_shop_id=%d ",$articleId,$shopId),true);
		$data['article'] = $articleMsg['data']['sart_txt'];	
		return $data;
	}
}