<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-08-25 17:23
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace app\api\controller\v1;

use app\BaseController;
use app\model\api\SiteProduct;
use app\model\system\News;
use app\Request;
use library\services\UtilService;
use library\utils\AdvertUtils;

class Index extends BaseController
{

    /**
     * 获取首页数据
     */
    public function getIndex(Request $request) {
        $sassid = $request->site['sassid'];
        $siteProduct = new SiteProduct;
        $siteProduct->setSass($sassid);
        $banner = (new AdvertUtils($sassid))->getData('62');
        $data['banner'] = $banner;
        $data['sort_product'] =  $siteProduct->SortList();//排行榜
        foreach ($data['sort_product'] as $k=>$v) {
            $imgAr =  explode(',',$v['img']);
            $data['sort_product'][$k]['img'] = empty($imgAr) ? '' : $imgAr[0];
        }
        $newList = $siteProduct->NewsList();//新品上市
        foreach ($newList as $k=>$v) {
            $imgAr =  explode(',',$v['img']);
            $newList[$k]['img'] = empty($imgAr) ? '' : $imgAr[0];
        }
        shuffle($newList);
        $data['new_product'] = $newList;
        shuffle($newList);
        $data['hot_product'] = $newList;
       return app('json')->success($data);
    }




    /**
     * 获取产品列表数据
     * @param Request $request
     */
    public function proList(Request $request){
        $post =  UtilService::getMore([
                ['page',1],
                ['warehouse',0],
                ['countType','0'],
                ['type','all'],
                ['pageSize',20]
            ],$request);
        $pageSize = $post['pageSize'];
        $sassid = $request->site['sassid'];
        $siteProduct = new SiteProduct;
        $siteProduct->setSass($sassid);
        list($pageCount,$data) = $siteProduct->getList($post['page'],$post,$pageSize);
        foreach ($data as $k=>$v) {
            $imgAr =  explode(',',$v['img']);
            $data[$k]['img'] = empty($imgAr) ? '' : $imgAr[0];
        }

        return app('json')->success([
            'list'      => $data,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page']
        ]);
    }

    /**
     * 产品详情数据
     * @param Request $request
     * @return mixed
     */
    public function proItem(Request $request) {
        $post =  UtilService::getMore([['id',0,'empty','请输入产品ID']],$request);
        $sassid = $request->site['sassid'];
        $siteProduct = new SiteProduct;
        $siteProduct->setSass($sassid);
        $data = $siteProduct->getItem($post['id']);
        if(empty($data)) {
            return app('json')->fail("找不到产品数据");
        } else {
            return app('json')->success($data);
        }
    }

    /**
     * 随机产品
     */
    public function proRand(Request $request) {
        $sassid = $request->site['sassid'];
        $siteProduct = new SiteProduct;
        $siteProduct->setSass($sassid);
        $data = $siteProduct->getRand(5);
        return app('json')->success($data);
    }

    /**
     * 新闻数据
     * @param Request $request
     */
    public function news(Request $request) {
        $post =  UtilService::getMore([['id',0,'empty','新闻数据']],$request);
        $data = (new News)->where('id',$post['id'])->find()->toArray();
        if(empty($data)) {
            return app('json')->fail('请输入正确新闻数据');
        }
        return app('json')->success($data);
    }



    public function newsList(Request $request) {
        [$page,$data] = (new News)->getList(0,['cate_id'=>2,'is_show'=>1],'6','*','time desc');
        return app('json')->success($data);
    }


}