<?php
class itemAction extends baseAction
{

	/**
	*1、号码 分类
	*2、关键字 分类 车型
	*/
	function search()
	{
		$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()
	{
		$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 pro_count>0 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'] = (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'];
				$sortArr[$v['sort_id']] = $v; 
			}
		}
		LibFc::ajaxJsonEncode(['status'=>true,'data'=>$sortArr]);
	}
	
	function index()
	{
		if(empty(mvc::$cfg['_SEARCH']))
		{
			$OthCheck = new OthCheck();
			$OthCheck->mustLogin('gzhs');
		}
		$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();
	}

}