<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\BaseSettingsModel;
use App\Models\S1GoodsCateModel;
use App\Models\S1GoodsInfoModel;
use App\Services\Base\ErrorCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class HomeController extends Controller
{

    /**
     * @api {get} /api/home/index 首页
     * @apiDescription 首页
     * @apiGroup Home
     * @apiParam {string}   appid          appid
     * @apiParam {int}   [head_cate_id]          头部分类id
     * @apiPermission none
     * @apiVersion 0.1.0
     * @apiSuccessExample {json} Success-Response:
     *  HTTP/1.1 200 OK
    {
        "status": true,
        "status_code": 0,
        "message": "",
        "data": {
            "banner": [                              轮播图
                {
                    "id": 1002,
                    "value": "/upload/banner/20171015/cd985cb0aac48ca5ea7b34c366c711fa.jpg",
                    "sort": 1,
                }
            ],
            "cates": [                                分类
                {
                    "id": 4,
                    "name": "商品分类2",
                    "pic": "/upload/goods/cate/20171013/923996f99388fa90acfee1bf31a909fa.jpg",
                    "status": 1,
                    "sort": 11,
                    "goods": [
                        {
                            "id": 1,
                            "is_home_cate": 1,
                            "home_cate_id": 3,
                            "name": "商品1",
                            "number": "0011",
                            "pic": "/upload/s1/goods/face/20171013/effbecdc6d9de83d0128e3f08ec6d636.jpg",
                            "status": 1,
                        }
                    ]
                }
            ],
            "goods": [                               商品
            {
                "id": 1,
                "name": "首页分类1",
                "pic": "/upload/goods/cate/20171013/2454820f914e59dd4a90b575cd47e03c.jpg",
                "goods": [
                    {
                        "id": 1,
                        "is_home_cate": 1,
                        "home_cate_id": 3,
                        "name": "商品1",
                        "number": "0011",
                        "pic": "/upload/s1/goods/face/20171013/effbecdc6d9de83d0128e3f08ec6d636.jpg",
                        "status": 1,
                    }
                ]
                },
            ]
        }
    }
     * @apiErrorExample {json} Error-Response:
     * HTTP/1.1 400 Bad Request
     */
    public function index(Request $request)
    {
        $validator = Validator::make($request->all(),
            [
                'appid'         => 'required',
            ],
            [
                'appid.required'        => 'appid不存在',
            ]
        );

        if ($validator->fails()) {
            return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
        }
        $appid = $request->input('appid');
        $header_cate_id = $request->input('head_cate_id');
        $banner = BaseSettingsModel::where('appid',$appid)->where('category','banner')->orderBy('sort','desc')->get();
        $cates = S1GoodsCateModel::where('appid',$appid)->where('type_id',1)->
            with(['goods'=>function ($query) {
                $query->orderBy('sort','desc');
            }])->orderBy('sort','desc')->get();
        if (!empty($header_cate_id)) {  //获取头部分类商品
//            where('appid',$appid)->where('cate_id',$header_cate_id)->where('type_id',1)
            $goods = S1GoodsCateModel::
            with(['goods'=>function ($query) {
                $query->orderBy('sort','desc');
            }])->orderBy('sort','desc')->where('id',$header_cate_id)->get();
        }else{
            $goods = S1GoodsCateModel::where('appid',$appid)->where('type_id',2)->
            with(['home_goods'=>function ($query) {
                $query->orderBy('sort','desc');
            }])->orderBy('sort','desc')->get();
        }

        return $this->api(compact('banner','cates','goods'));
    }

    /**
     * @api {get} /api/home/search 搜索
     * @apiDescription 搜索
     * @apiGroup Home
     * @apiParam {string}   appid          appid
     * @apiParam {string}   keyword          keyword
     * @apiPermission none
     * @apiVersion 0.1.0
     * @apiSuccessExample {json} Success-Response:
     *  HTTP/1.1 200 OK
    {
        "status": true,
        "status_code": 0,
        "message": "",
        "data": {
            "banner": [
                {
                    "value": "/upload/banner/20171012/71a9962b9b9de1876c4d76ed6d879afd.jpg",
                },
            ],
            "cates": [
                {
                    "name": "商品分类2",
                    "pic": "/upload/goods/cate/20171013/923996f99388fa90acfee1bf31a909fa.jpg",
                    "goods": []
                }
            ],
            "goods": {
                "current_page": 1,
                "data": [
                    {
                        "id": 1,
                        "name": "商品1",
                        "pic": "/upload/s1/goods/face/20171013/effbecdc6d9de83d0128e3f08ec6d636.jpg",
                    }
                ],
                "first_page_url": "http://www.s1.com/api/home/search?page=1",
                "from": 1,
                "last_page": 1,
                "last_page_url": "http://www.s1.com/api/home/search?page=1",
                "next_page_url": null,
                "path": "http://www.s1.com/api/home/search",
                "per_page": 10,
                "prev_page_url": null,
                "to": 1,
                "total": 1
                }
            }
        }
    }
     * @apiErrorExample {json} Error-Response:
     * HTTP/1.1 400 Bad Request
     */
    public function search(Request $request)
    {
        $validator = Validator::make($request->all(),
            [
                'appid'         => 'required',
                'keyword'         => 'required',
            ],
            [
                'appid.required'        => 'appid不存在',
                'keyword.required'        => '关键字不能为空',
            ]
        );

        if ($validator->fails()) {
            return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
        }
        $appid = $request->input('appid');
        $banner = BaseSettingsModel::where('appid',$appid)->where('category','banner')->orderBy('sort','desc')->get();
        $cates = S1GoodsCateModel::where('appid',$appid)->where('type_id',1)->
        with(['goods'=>function ($query) {
            $query->orderBy('sort','desc');
        }])->orderBy('sort','desc')->get();
        $keyword = '%'.$request->input('keyword').'%';
        $goods = S1GoodsInfoModel::where('appid',$appid)->where('name','like',$keyword)->paginate();
        return $this->api(compact('banner','cates','goods'));
    }
}
