<?php
class itemAction extends baseAction
{

	/**
	*1、号码 分类
	*2、关键字 分类 车型
	*/
	function search()
	{
		if(empty(mvc::$cfg['_SEARCH']))
		{
			if(empty($_SESSION['_userid']))
			{
				$OthCheck = new OthCheck();
				$OthCheck->mustLogin('gzhs');
			}
			else
			{
				 LibTpl::Error('非常抱歉，您无权限访问此页面！');
			}
		}
		$Params = mvc::$URL_PARAMS;
		//车架号查询跳转
		if($Params['type'] == 'vin' || ($Params['type'] == 'all' && isset($Params['keyword']) && preg_match('/[0-9a-zA-Z]{17}/', trim($Params['keyword']))))
		{
			$_SESSION['MAIN_MONITOR']['tj'] = 0;
			LibFc::Go(str_ireplace('item/search?', 'item/searchVin?', LibFc::nowUrl()));
		}
		else if($Params['type'] == 'engine')
		{
			LibFc::Go(str_ireplace('item/search?', 'item/searchEngine?', LibFc::nowUrl()));
		}
		$B2cBase = new B2cBase();
		if(!empty($Params['sortId']))
		{
			$res = $B2cBase->get('sh_sort', ['sort_id', 'sort_name'], sprintf(" and sort_id=%d", (int) $Params['sortId']), true);
			LibTpl::Set('sortArr', $res['data']);
		}
		if(!empty($Params['mod3Id']))
		{
			$ComModel = new ComModel();
			$res = $ComModel->getModel(3, ['make_name', 'mod2_name', 'bc_model3.*'], sprintf(" and mod3_id = %d", (int) $Params['mod3Id']), true);
			LibTpl::Set('modArr', $res['data']);
		}
		LibTpl::Set('bodyClass', 'search-res');
		LibTpl::Set('Params', $Params);
		LibTpl::Set('title', '配件列表');
		LibTpl::Put();
	}


	/**获取某种参数下的分类**/
	function ajaxGetSearchSort()
	{
		if(empty(mvc::$cfg['_SEARCH']))
		{
			if(empty($_SESSION['_userid']))
			{
				$OthCheck = new OthCheck();
				$OthCheck->mustLogin('gzhs');
			}
			else
			{
				LibFc::ajaxJsonEncode(['status'=>false,'data'=>'非常抱歉，您无权限访问此页面！']);
			}
		}
		$Params = mvc::$URL_PARAMS;
		$B2cBase = new B2cBase();
		$table = 'bc_product join sh_shop on pro_shop_id=shop_id and shop_status = 1 and pro_status =1 and shop_id='.mvc::$cfg['SHOPID'].' join bc_product_sort on prs_pro_id = pro_id';
		$where = '';
		if($Params['type'] == 'number')
		{
			$Params['formatNum'] = LibFc::FormatNum($Params['keyword']);
			if(strlen($Params['formatNum']) > 2)
			{
				$where .= sprintf(" and (num_format like '%s%%' or num_reformat like '%s%%')", $Params['formatNum'], strrev($Params['formatNum']));
				$table .= ' join bc_number on pro_id = num_pro_id';
			}
			else
			{
				$where .= ' and 1=0';
			}
		}
		else
		{
			$Params['mod3Id'] = !empty($Params['mod3_id'])?  (int) $Params['mod3_id'] : (int) $Params['mod3Id'];
			if($Params['mod3Id'] > 0)
			{
				$table .= ' join bc_product_model3_link on pro_id = pml3_pro_id';
				$where .= sprintf(" and pml3_mod3_id=%d", $Params['mod3Id']);
			}
			
			//配件关键字
			if($Params['keyword'] != '')
			{
				$keywordArr = LibFc::keyword($Params['keyword']);
				$B2cKeyword = new B2cKeyword();
				$res = $B2cKeyword->getKeywordKeyIds($keywordArr);
				if(!empty($res['data']))
				{
					$tempWhere = sprintf(" and pro_id in (select prk_pro_id from bc_product_keyword where prk_key_id in (%s) group by prk_pro_id having count(*) = %d)", implode(',', $res['data']), count($res['data']));
				}
				else
				{
					$tempWhere = sprintf(" and pro_name like '%%%s%%'", LibFc::Escape($Params['keyword']));
				}
				$where .= $tempWhere;
			}
		}
		$res = $B2cBase->get($table, ['distinct prs_sort_id'], $where);
		if(!empty($res['data']))
		{
			$sortIdArr = array_column($res['data'], 'prs_sort_id');
			//追加父级
			$res = $B2cBase->get('sh_sort_tree', ['distinct sot_psort_id'], sprintf(" and sot_sort_id in(%s) and sot_psort_id > 0", implode(',', $sortIdArr)));
			$sortIdArr = array_merge($sortIdArr, array_column($res['data'], 'sot_psort_id'));
			$res = $B2cBase->get('sh_sort_tree join sh_sort on sot_sort_id = sort_id', ['sort_id', 'sort_name', 'sot_psort_id'], sprintf(" and sort_id in(%s) and sort_shop_id=%d order by sort_seq, sort_name", implode(',', $sortIdArr), mvc::$cfg['SHOPID']));
			$sortArr = [];
			$sortTreeArr = [];
			foreach($res['data'] as $v)
			{
				$sortTreeArr[$v['sot_psort_id']][] = $v['sort_id'];
				//只出顶级 2021/8/5
				if($v['sot_psort_id'] == 0)
					$sortArr[$v['sort_id']] = $v; 
			}
			$sortArr = array_values($sortArr);
		}
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>$sortArr]);
	}
	
	function index()
	{
		if(empty(mvc::$cfg['_SEARCH']))
		{
			if(empty($_SESSION['_userid']))
			{
				$OthCheck = new OthCheck();
				$OthCheck->mustLogin('gzhs');
			}
			else
			{
				 LibTpl::Error('非常抱歉，您无权限访问此页面！');
			}
		}
		$B2cBase = new B2cBase();
		$res = $B2cBase->get('sh_sort_tree join sh_sort on sot_sort_id = sort_id', ['sort_id', 'sort_name'], sprintf(" and sot_psort_id = 0 and sort_shop_id=%d order by sort_seq, sort_name", mvc::$cfg['SHOPID']));
		$sortArr = $res['data'];
		LibTpl::Set('sortArr', $sortArr);
		LibTpl::Set('offCanvas', 1);
		LibTpl::Set('footMenu', 'item');
		LibTpl::Set('title', '配件查询');
		LibTpl::Put();
	}

	function ajaxSearch()
	{
		if(empty(mvc::$cfg['_SEARCH']))
		{
			if(empty($_SESSION['_userid']))
			{
				$OthCheck = new OthCheck();
				$OthCheck->mustLogin('gzhs');
			}
			else
			{
				LibFc::ajaxJsonEncode(['status'=>false,'data'=>'非常抱歉，您无权限访问此页面！']);
			}
		}
		$Params = mvc::$URL_PARAMS;
		if($Params['level'] == 'item') $Params['type'] = 'item';
		
		
		$B2cSearch = new B2cSearch();

		$res = $B2cSearch->get('sh_shop', ['shop_id, shop_smainnum, shop_srefnum, shop_sorder'], sprintf(" and shop_id=%d", mvc::$cfg['SHOPID']), true);
		if(empty($res['data'])) LibFc::ReturnJson(false, '店铺不存在或者尚未开通！');
		$shopInfo = $res['data'];
		$Params['order'] = $shopInfo['shop_sorder'];
		$perPage = 10;
		if(!empty($_SESSION['_userid']))
			$Params['uid'] = $_SESSION['_userid'];

		$res = $B2cSearch->searchShopItem(mvc::$cfg['SHOPID'], $Params, $perPage, ['pro_show_app'=>1]);
		$result = $res['data'];
		unset($result['param']['callback']);
		unset($result['param']['_']);
		unset($result['param']['uid']);
		$itemArr = [];
		if(!empty($result['proIds']))
		{
			$B2cProduct = new B2cProduct();
			$res = $B2cProduct->getAnyProductInfo($result['proIds'], $_SESSION['_userid'],null, [], $Params['mod3_id']);
			$itemArr = $res['data'];
			//不可以购买的人不可以查看第二个号码
			if(empty(mvc::$cfg['_CANBUY']) || mvc::$cfg['HIDEMAINNUM'])
			{
				foreach($itemArr as $k=>$v)
				{
					foreach($v['num'] as $kk=>$vv)
					{
						$itemArr[$k]['num'] = [$vv];
						break;
					}
				}
			}
		}
		LibTpl::Set('Params', $Params);
		LibTpl::Set('itemArr', $itemArr);
		$html = LibTpl::Fetch();
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>['html'=>$html, 'Params'=>$result['param']]]);
	}
}