<?php
namespace admin\action;
use \model;
use \model\napro;

class naproAction  extends \action
{
    // 北美产品列表页
    function list(){
        $req = \mvc::$req;
        $napro = new napro();
        $brandList = $napro->getNaProBrandList();
        $selectOptionList = $napro->getSelectOptionList();
        \view::set('selectOptionList', $selectOptionList);
        \view::set('brandList', $brandList);
        \view::put();
    }

    // 获取北美汽车品牌下的所有车型
    function getModelListByMake(){
        $req = \mvc::$req;
        $make_id = $req['base_make_id'] ?? '';
        if(empty($make_id)){
            return $this->errorJson('请先选择汽车品牌');
        }
        $napro = new napro();
        $data = $napro->getModelListByMake($make_id);
        $this->successJson($data);
    }

    // 重新获取北美产品品牌统计数据
    function refreshBrandCount(){
        $req = \mvc::$req;
        $make_id = $req['base_make_id'] ?? '';
        $model_ids = $req['base_model_id'] ?? '';
        if(empty($make_id) && !empty($model_ids)){
            return $this->errorJson('请先选择汽车品牌');
        }
        $napro = new napro();
        $brandList = $napro->getNaProBrandList($req);
        $this->successJson($brandList);
    }

    // 根据北美品牌获取产品列表
    function getProListByBrand(){
        $req = \mvc::$req;
        $make_ids = $req['base_make_id'] ?? '';
        $model_ids = $req['base_model_id'] ?? '';
        if(empty($make_ids) &&  !empty($model_ids)){
            return $this->errorJson('请先选择汽车品牌');
        }
        $napro = new napro();
        $data = $napro->getNaProListByBrand($req);
        $this->successJson($data);
    }

    // 北美产品详情
    function naProDetail(){
        $req = \mvc::$req;
        $naProId = $req['id'] ?? '';
        if(empty($naProId)){
            return $this->errorJson('产品ID不能为空');
        }
        $napro = new napro();
        $data = $napro->getNaProDetail($naProId);
        \view::set('data', $data);
        \view::put();
    }

    // 保存产品到零件库
    function saveOePro(){
        $req = \mvc::$req;
        $naProId = $req['pro_id'] ?? '';
        if(empty($naProId)){
            return $this->errorJson('-1','产品ID不能为空');
        }
        
        $napro = new napro();
        $data = $napro->saveOePro($req);
        $this->successJson($data);
    }

    // 批量添加零件库前校验
    function validateBatchSaveOePro(){
        $req = \mvc::$req;
        $proIds = $req['pro_ids'] ?? [];
        if(empty($proIds)){
            return $this->errorJson('-1','请选择需要处理的北美商品');
        }
        $napro = new napro();
        $data = $napro->validateBatchSavePlan($req);
        $this->successJson($data);
    }

    // 为产品设置零件参照
    function setReferPoe(){
        $req = \mvc::$req;
        $proId = (int)($req['pro_id'] ?? 0);
        $referNo = substr(trim((string)($req['refer_no'] ?? '')), 0, 255);
        $referNoFormat = str_replace([';','-','：','；',], ':', $referNo);
        $numArr = explode(':', $referNoFormat);
        $fac = '';
        $formatNum = '';
        // 如果没有输入厂商，则默认为OE
        if(count($numArr) === 1){
            $fac = 'OE';
            $formatNum = formatNum($numArr[0]);
        }else{
            $fac = $numArr[0];
            $formatNum = formatNum($numArr[1]);
        }
        //  = formatNum($referNo);

        if ($proId <= 0) {
            return $this->errorJson('产品ID不能为空');
        }
        if ($referNo === '') {
            return $this->errorJson('参考号不能为空');
        }
        $napro = new napro();
        $data = $napro->setReferPoe($proId, $fac, $formatNum);
        if($data['success']){
            return $this->successJson($data);
        }
        return $this->errorJson('-1',$data['message']);
    }

}
