<?php
namespace homeh5\action;
use \model\carmodel;

class carmodelAction extends baseAction
{
    public function getModelBrand()
    {
        $req = \mvc::$req;
        $carmodel = new carmodel();
        $makeList = $carmodel->getMakeListByFilter($req);
        $this->successJson($makeList);
    }

    public function getModelSeries()
    {
        $req = \mvc::$req;
        $carmodel = new carmodel();
        $seriesList = $carmodel->getSeriesListByFilter($req);
        $rows = $seriesList?? [];
        $groups = [];
        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] = [];
            }
            $groups[$first][] = $item;
        }
        $this->successJson($groups);
    }

    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);
        $this->successJson($modelList);
    }

    // 获取车型详情
    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);
    }
}
