<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Services\AdminUserService;
use App\Http\Services\CompanyService;
use App\Http\Services\StandardBrandService;
use App\Model\BrandModel;
use App\Model\StandardBrandModel;
use App\Model\SupplierChannelModel;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\DB;

//通用API,比如获取品牌列表,分类列表等
class CommonApiController extends Controller
{
    public function Entrance(Request $request, $id)
    {
        $this->$id($request, $id);
    }


    //获取所有品牌
    public function getBrandList($request)
    {
        $type = $request->get('type');
        //如果传的type=0,默认给自营
        $type = $type ?: 1;
        $model = new BrandModel();
        $brandList = $model->getBrandListByType($type);
        $this->response(0, 'ok', $brandList);
    }

    //获取标准品牌
    public function getStandardBrandList($request)
    {
        $list = (new StandardBrandService())->getStandardBrandList($request->all());
        $data = array_get($list, 'data');
        $lastPage = array_get($list, 'last_page');
        $total = array_get($list, 'total');
        echo json_encode([
            'err_code' => 0,
            'err_msg' => 'ok',
            'total' => $total,
            'count' => $total,
            'data' => $data,
            'last_page' => $lastPage
        ]);
        exit();
    }

    private function SearchBrand($request)
    {
        $brandName = $request->input('brand_name');
        $type = $request->input('type');
        if (empty($type)) {
            $type = 1;
        }
        $connection = $type == 1 ? 'spu' : 'self';
        $table = $type == 1 ? 'brand' : 'brand';
        $db = DB::connection($connection)->table($table)->where('status', '1');
        if ($brandName) {
            $db = $db->where('brand_name', 'like', $brandName . '%');
        }
        $count = $db->count();
        $list = $db->select('brand_name', 'brand_id')->paginate(17);
        $list = $list ? $list->toArray() : [];
        $data = array_get($list, 'data');
        $lastPage = array_get($list, 'last_page');
        echo json_encode([
            'errcode' => 0,
            'errmsg' => 'ok',
            'total' => $count,
            'count' => $count,
            'data' => $data,
            'last_page' => $lastPage
        ]);
    }

    public function checkCompanyInfo($request)
    {
        //请求天眼查校验接口
        $supplierName = $request->get('supplier_name');
        $taxNumber = $request->get('tax_number');
        $region = $request->get('region');
        $company = [];
        if (in_array($region, config('field.NeedCheckCompanyRegion'))) {
            $regionType = $region == SupplierChannelModel::REGION_CN ? 1 : 2;
            $company = (new CompanyService())->getCompanyInfo($supplierName, $taxNumber, $regionType);
            if (!$company) {
                $this->response(-1, '获取不到公司');
            }
        } else {
            $this->response(-2, '供应商跳过天眼查校验');
        }

        $this->response(0, 'ok', $company);

    }

    //批量校验标准品牌,并且返回具体的id和错误信息
    public function checkStandardBrandNameList(Request $request)
    {
        //接收的是标准品牌名称
        $standardBrandNameList = $request->input('standard_brand_name_list');
        if (empty($standardBrandNameList)) {
            $this->response(0, 'ok', [
                'invalid_brand_name_list' => [],
                'valid_brand_name_list' => [],
                'valid_brand_ids' => [],
            ]);
        }
        $standardBrandNameList = explode(',', trim($standardBrandNameList, ','));
        $result = (new StandardBrandService())->checkStandardBrandNameList($standardBrandNameList);
        $this->response(0, 'ok', $result);
    }

}