<?php
class brandAction extends baseAction
{
	/**
	 * @method    品牌管理
	 * @author 卢
	 * @copyright 2017-10-16
	 */
	public function manageBrand()
	{
		LibRule::Check('MA01');
		$B2cBase=new B2cBase();
		//使用品牌的店铺数量
		$res=$B2cBase->get('bc_probrand left join sh_probrand_link on sprl_prb_id=prb_id',['prb_id','count(sprl_shop_id) shopcount'],' group by prb_id');
		$shopBrandCount=array_column($res['data'], null,'prb_id');
		//配件数量
		// $res=$B2cBase->get('bc_product',['pro_id']);
		$res=$B2cBase->get('bc_probrand',['prb_id','prb_name','prb_is_host','prb_seq'],' order by prb_seq asc,prb_id asc');
		$brandArr=array_column($res['data'], 'prb_id');
		if(count($brandArr))
		{
			$proCountData=$B2cBase->get('bc_product',['count(pro_id) count','pro_prb_id'],sprintf(' and pro_prb_id in (%s) group by pro_prb_id',implode(',',$brandArr)));
			$proCount=array_column($proCountData['data'], null,'pro_prb_id');
		}
		if(count($res['data']))
		{
			foreach ($res['data'] as $k => $v) 
			{
				$res['data'][$k]['shopCount']=isset($shopBrandCount[$v['prb_id']])?$shopBrandCount[$v['prb_id']]['shopcount']:0;
				$res['data'][$k]['proCount']=isset($proCount[$v['prb_id']])?$proCount[$v['prb_id']]['count']:0;
			}
		}
		// print_r($res);
		LibTpl::Set('brandList',$res['data']);
		LibTpl::Set('title','品牌管理-跑街的');
		LibTpl::Put();
	}
	/**
	 * @method    添加/修改品牌入库
	 * @author 卢
	 * @copyright 2017-10-16
	 */
	public function addBrand()
	{
		if($_SERVER['REQUEST_METHOD']=='POST')
		{
			$B2cBase=new B2cBase();
			if(isset($_POST['brandId'])&&(int)$_POST['brandId'])
			{
				//判读是否已存在
				$oriBrandData=$B2cBase->get('bc_probrand',['prb_id'],sprintf(" and prb_name ='%s'",LibFc::Escape($_POST['brandName'])),true);
				$res=['status'=>false,'data'=>'配件已存在，修改失败'];
				if(!count($oriBrandData['data'])||(count($oriBrandData['data'])&&$oriBrandData['data']['prb_id']==(int)$_POST['brandId']))
				{
					$keywordArr=LibFc::keyword($_POST['brandName']);
					$keywordArr[]=LibFc::GetInitial($_POST['brandName']);
					$target=['prb_id'=>(int)$_POST['brandId'],'prb_name'=>$_POST['brandName'],'prb_seq'=>((int)$_POST['seq'])?:9999,'prb_initial'=>LibFc::GetInitial($_POST['brandName']),'prb_keyword'=>implode(',',$keywordArr)];
					$target['prb_is_host']=isset($_POST['isHot'])?:0;
					$res=$B2cBase->set('bc_probrand',$target,sprintf(' and prb_id=%d',(int)$_POST['brandId']));
				}
			}
			else
			{
				//判读是否已存在
				$oriBrandData=$B2cBase->get('bc_probrand',['prb_id'],sprintf(" and prb_name ='%s'",LibFc::Escape($_POST['brandName'])),true);
				$res=['status'=>false,'data'=>'配件已存在，添加失败'];
				if(!count($oriBrandData['data']))
				{
					$keywordArr=LibFc::keyword($_POST['brandName']);
					$keywordArr[]=LibFc::GetInitial($_POST['brandName']);
					$target=['prb_name'=>$_POST['brandName'],'prb_seq'=>((int)$_POST['seq'])?:9999,'prb_initial'=>LibFc::GetInitial($_POST['brandName']),'prb_keyword'=>implode(',',$keywordArr)];
					isset($_POST['isHot'])&&$target['prb_is_host']=1;
					$res=$B2cBase->add('bc_probrand',$target);
				}
			}
			LibFc::ajaxJsonEncode($res);
		}
	}

	/**
	 * @method    品牌删除
	 * @author 卢
	 * @copyright 2017-10-17
	 */
	public function delBrand()
	{
		LibRule::Check('MA04');
		$Params = MVC::$URL_PARAMS;
		if(LibFc::Int($Params['brandId']))
		{
			$B2cBase=new B2cBase();
			$result=['status'=>false,'data'=>'品牌不存在'];
			//判断是否有店铺使用
			$res=$B2cBase->get('sh_probrand_link',['count(*) count'],sprintf(' and sprl_prb_id=%d',$Params['brandId']),true);
			$result=['status'=>false,'data'=>'该品牌有店铺在使用，不允许删除'];		
			if(!$res['data']['count'])
			{		
				//判断是否有配件使用
				$result=['status'=>false,'data'=>'该品牌有配件在使用，不允许删除'];	
				$res=$B2cBase->get('bc_product',['count(*) count'],sprintf(' and pro_prb_id=%d',$Params['brandId']),true);
				if(!$res['data']['count'])
				{
					$result=$B2cBase->del('bc_probrand',sprintf(' and prb_id=%d',(int)$Params['brandId']));
				}
			}
		}
		LibFc::ajaxJsonEncode($result);

	}
	/**
	 * @method    获取品牌添加、修改弹窗
	 * @author 卢
	 * @copyright 2017-10-17
	 */
	public function getEditTpl()
	{
		$B2cBrand=new B2cBrand();
		$Params = MVC::$URL_PARAMS;
		if(isset($Params['brandId'])&&LibFc::Int($Params['brandId']))
		{
			LibRule::Check('MA03');
			$res=$B2cBrand->get('bc_probrand',['prb_id','prb_name','prb_is_host','prb_seq'],sprintf(' and prb_id=%d',$Params['brandId']),true);
			LibTpl::Set('brand',$res['data']);
			// print_r($res);
		}
		else
		{
			LibRule::Check('MA02');
		}
		LibTpl::Set('title','添加品牌-跑街的');
		$data=LibTpl::Fetch('/brand/addBrand.php');
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>$data]);
	}
	/**
	 * @method    ajax修改品牌是否热门
	 * @author 卢
	 * @copyright 2018-01-18
	 */
	public function checkHot()
	{
		LibRule::Check('MA03');
		$Params = MVC::$URL_PARAMS;
		if((int)$Params['brandId']&&in_array($Params['type'], ['check','notCheck']))
		{
			$B2cBase=new B2cBase();
			$type=$Params['type']=='check'?1:0;
			$B2cBase->set('bc_probrand',['prb_is_host'=>$type],sprintf(' and prb_id=%d',(int)$Params['brandId']));
		}
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>true]);
	}
}