<?php
declare (strict_types = 1);
namespace app\admin\controller\v1;

use app\BaseController;
use app\model\admin\Product as ProductModel;
use app\model\admin\SiteProduct;
use app\Request;
use library\services\UtilService;

// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-09-06 21:53
// +----------------------------------------------------------------------

class Product extends  BaseController
{
    /**
     * 基本设置
     */
    public function list(Request $request) {
        $pageSize = 20;
        $post =  UtilService::getMore([
                ['page',1],
                ['warehouse',0],
                ['name',''],
                ['artType','all']
            ],$request);
        $sassid = $request->site['sassid'];
        $siteProduct = new SiteProduct;
        $siteProduct->setSassId($sassid);
        list($pageCount,$data) = $siteProduct->getList($post['page'],$post,$pageSize);
        
        
        $totalCount['count'] = $siteProduct->where('sassid',$sassid)->count();
        $totalCount['downCount'] = $siteProduct->where('sassid',$sassid)->where('status',0)->count();
        $totalCount['upCount'] = $siteProduct->where('sassid',$sassid)->where('status',1)->count();
        
        return app('json')->success([
            'list'      => $data,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page'],
            'totalCount'=>$totalCount
        ]);
    }

    /**
     * 保存数据
     * @param Request $request
     */
    public function save(Request $request) {
       $post =  UtilService::getMore([
            ['id',0],
            ['is_host',0],
            ['is_new',0],
            ['status',''],
            ['ver_bug_count',0],
            ['price',0]
        ],$request);
        $post["status"]=$post["status"]==1?1:0;
        $sassid = $request->site['sassid'];
        $siteProduct = new SiteProduct;
        $siteProduct->setSassId($sassid);
        $bool = $siteProduct->saveProduct($post);
        if(!$bool) {
            return app('json')->fail(SiteProduct::getErrorInfo());
        }
        return app('json')->success("数据保存成功");
    }


    /**
     * 获取产品基本信息
     * @param Request $request
     */
    public function info(Request $request) {
        [$id] = UtilService::getMore([
            ['id','','empty','参数错误']
        ],$request,true);
        $sassid = $request->site['sassid'];
        $siteProduct = new SiteProduct;
        $siteProduct->setSassId($sassid);
        $info = $siteProduct->getProInfo(compact('id'));
        return app('json')->success($info);
    }
}