<?php
namespace homecn\action;
use \model\carmodel;

class carmodelAction extends baseAction
{
    public function index()
    {
        $req = \mvc::$req;
        $carmodel = new carmodel();
        $makeList = $carmodel->getMakeListByFilter($req);
        \view::set('req',$req);
        \view::set('makeList', $makeList);
        \view::set('title','国际车型-'.\mvc::$cfg['siteTitle']);
		\view::put();
    }

    public function getModelBrand()
    {
        $req = \mvc::$req;
        $carmodel = new carmodel();
        $makeList = $carmodel->getMakeListByFilter($req);
        
        $rows = $makeList ?? [];
        $m_type = $req['m_type'] ?? '';
        
        $groups = [];
        $initialArr = [];
        $isGrouped = false;

        if ($m_type != '#') {
            $isGrouped = true;
            $digitKeys = [];
            $letterKeys = [];
            $otherKeys = [];

            foreach ($rows as $item) {
                $first = $item['mk_initial'] ?? '';
                if (!$first || !preg_match('/^[0-9A-Z]$/', $first)) {
                    $first = '#';
                }
                
                if (!isset($groups[$first])) {
                    $groups[$first] = [];
                    if (preg_match('/^[0-9]$/', $first)) {
                        $digitKeys[] = $first;
                    } elseif (preg_match('/^[A-Z]$/', $first)) {
                        $letterKeys[] = $first;
                    } else {
                        $otherKeys[] = $first;
                    }
                }
                $groups[$first][] = $item;
            }
            sort($digitKeys);
            sort($letterKeys);
            sort($otherKeys);
            $initialArr = array_merge($digitKeys, $letterKeys, $otherKeys);
        }

        \view::set('req', $req);
        \view::set('brandList', $rows);
        \view::set('brandGroups', $groups);
        \view::set('initialArr', $initialArr);
        \view::set('isGrouped', $isGrouped);

        ob_start();
        \view::put('carmodel/modelBrand');
        $listHtml = ob_get_clean();
        
        $this->successJson(['listHtml' => $listHtml, 'makeList' => $rows]);
    }

    public function getModelSeries()
    {
        $req = \mvc::$req;
        $carmodel = new carmodel();
        $seriesList = $carmodel->getSeriesListByFilter($req);
        $rows = $seriesList?? [];
        
        $mkLogoResult = $carmodel->getBrandLogo($req['mk_id']);
        $mkLogo = $mkLogoResult[0]['mk_logo'] ?? '';
        
        $groups = [];
        $initialArr = [];

        $digitKeys = [];
        $letterKeys = [];
        $otherKeys = [];

        foreach ($rows as $item){
            $name = $item['ms_name'] ?? '';
            $first = $name[0] ?? '';
            if (!$first || !preg_match('/^[0-9A-Z]$/', $first)) {
                $first = '#';
            }
            if (!isset($groups[$first])) {
                $groups[$first] = [];
                if (preg_match('/^[0-9]$/', $first)) {
                    $digitKeys[] = $first;
                } elseif (preg_match('/^[A-Z]$/', $first)) {
                    $letterKeys[] = $first;
                } else {
                    $otherKeys[] = $first;
                }
            }
            $groups[$first][] = $item;
        }
        sort($digitKeys);
        sort($letterKeys);
        sort($otherKeys);
        $initialArr = array_merge($digitKeys, $letterKeys, $otherKeys);
        
        \view::set('req', $req);
        \view::set('seriesGroups', $groups);
        \view::set('initialArr', $initialArr);
        \view::set('seriesList', $rows);
        ob_start();
        \view::put('carmodel/modelSeries');
        $listHtml = ob_get_clean();
        $this->successJson(['listHtml' => $listHtml, 'seriesList' => $rows, 'mkLogo' => $mkLogo]);
    }

    public function getSeriesList()
    {
        $req = \mvc::$req;
        $carmodel = new carmodel();
        $seriesList = $carmodel->getSeriesListByFilter($req);
        $rows = $seriesList?? [];
        $this->successJson($rows);
    }

    public function getModelCar()
    {
        $req = \mvc::$req;
        $carmodel = new carmodel();
        $modelList = $carmodel->getModelCarListByFilter($req);
        // 年份筛选
        $yearList = array_unique(array_column($modelList, 'mm_year_from'));
        sort($yearList);
        // 排量筛选
        $ccList = array_unique(array_column($modelList, 'mm_cc'));
        sort($ccList);
        if (isset($ccList[0]) && trim((string)$ccList[0]) === '') {
            array_shift($ccList);
        }
        // 驱动筛选
        $driveList = array_unique(array_column($modelList, 'mm_drive'));
        sort($driveList);
        if (isset($driveList[0]) && trim((string)$driveList[0]) === '') {
            array_shift($driveList);
        }
        \view::set('modelList', $modelList);
        \view::set('req', $req);
        ob_start();
        \view::put('carmodel/modelCar');
        $listHtml = ob_get_clean();
        $this->successJson([
            'listHtml' => $listHtml,
            'req' => $req,
            'yearList' => $yearList,
            'ccList' => $ccList,
            'driveList' => $driveList,
        ]);
    }

    // 获取车型详情
    public function getModelInfo()
    {
        $req = \mvc::$req;
        $mmId = (int)($req['mm_id'] ?? 0);
        if (!$mmId) {
            $this->errorJson('请选择车型');
        }
        $carmodel = new carmodel();
        $modelInfo = $carmodel->getModelInfo($mmId);
        $mkId = $modelInfo['mk_id'];
        $mkLogo = '';
        if($mkId) {
           $mkLogoResult = $carmodel->getBrandLogo($mkId);
           $mkLogo = $mkLogoResult[0]['mk_logo'] ?? '';
        }
        $modelInfo['mk_logo'] = $mkLogo;
        $this->successJson($modelInfo);
    }
}
