<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\S1OrderInfoModel;
use App\Models\S1UserFavoriteModel;
use App\Models\WxUserAddressModel;
use App\Services\Base\ErrorCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class MyController extends Controller
{
    /**
     * @api {get} /api/my/index 首页
     * @apiDescription 首页
     * @apiGroup My
     * @apiParam {string}   appid          appid
     * @apiPermission none
     * @apiVersion 0.1.0
     * @apiSuccessExample {json} Success-Response:
     *  HTTP/1.1 200 OK
    {
        "status": true,
        "status_code": 0,
        "message": "",
        "data": {
            "user": {
            "id": 1,
            "appid": "dsfbdffd32434dfvdf",
            "nickname": "nickname1",
            "openid": "openid123",
            "avatar": "/upload/wx_user/20171012/8cea5bda71481b13edbe54ae2509e063.jpg",
            "password": "password123",
            "score": 1000123,
            "level_id": 1,
            "balance": "",
            "remark": null,
            "status": 1,
            "created_at": "2017-10-12 15:56:20",
            "updated_at": "2017-10-12 15:56:46",
            "deleted_at": null,
            "favorite": [
                {
                "id": 1,
                "name": "商品1",
                "number": "0011",
                "pic": "/upload/s1/goods/face/20171013/effbecdc6d9de83d0128e3f08ec6d636.jpg",
                }
            ]
            },
            "order1": [
                {
                    "id": 13,
                    "amount": "1250100",
                    "goods": [
                        {
                            "id": 1,
                            "name": "商品1",
                            "pic": "/upload/s1/goods/face/20171013/effbecdc6d9de83d0128e3f08ec6d636.jpg",
                        },
                        {
                            "id": 2,
                            "name": "test",
                            "pic": "/upload/s1/goods/face/20171013/cdfe1a254ac611d055cd9aacaa11c58a.png",
                        }
                    ]
                },
            ],
        }
    }
     * @apiErrorExample {json} Error-Response:
     * HTTP/1.1 400 Bad Request
     */
    public function index(Request $request)
    {
        $user = Auth::guard('api')->user();
        if (!$user) return $this->error(ErrorCode::ERROR_POWER);
        $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');
//        1待付款 2待收货 3代发货 4已完成 5售后
        $order1 =S1OrderInfoModel::where('appid',$appid)->where('wx_user_id',$user->id)->where('status',1)
            ->with(['goods'=> function ($query) {
            $query->select('s1_goods_info.id','name','pic','s1_goods_info.price');
        }])->orderBy('order_time','desc')->select('id','amount','trade_no','created_at','status')->get();
        $order2 =S1OrderInfoModel::where('appid',$appid)->where('wx_user_id',$user->id)->where('status',2)
            ->with(['goods'=> function ($query) {
            $query->select('s1_goods_info.id','name','pic','s1_goods_info.price');
        }])->orderBy('order_time','desc')->select('id','amount','trade_no','created_at','status')->get();
        $order3 =S1OrderInfoModel::where('appid',$appid)->where('wx_user_id',$user->id)->where('status',3)
            ->with(['goods'=> function ($query) {
            $query->select('s1_goods_info.id','name','pic','s1_goods_info.price');
        }])->orderBy('order_time','desc')->select('id','amount','trade_no','created_at','status')->get();
        $order4 =S1OrderInfoModel::where('appid',$appid)->where('wx_user_id',$user->id)->where('status',6)
            ->with(['goods'=> function ($query) {
            $query->select('s1_goods_info.id','name','pic','s1_goods_info.price');
        }])->orderBy('order_time','desc')->select('id','amount','trade_no','created_at','status')->get();
        $order5 =S1OrderInfoModel::where('appid',$appid)->where('wx_user_id',$user->id)->
            where(function ($query) {
            $query->where('status',4)->orWhere('status',5);
        })->with(['goods'=> function ($query) {
            $query->select('s1_goods_info.id','name','pic','s1_goods_info.price');
        }])->orderBy('order_time','desc')->select('id','amount','trade_no','created_at','status')->get();
        $favorite = $user->favorite;   //收藏
        return $this->api(compact('user','order1','order2','order3','order4','order5'));
    }


    /**
     * @api {get} /api/my/favorite 收藏
     * @apiDescription 收藏
     * @apiGroup My
     * @apiParam {int}   [page=1]          分页参数
     * @apiPermission none
     * @apiVersion 0.1.0
     * @apiSuccessExample {json} Success-Response:
     *  HTTP/1.1 200 OK
    {
        "status": true,
        "status_code": 0,
        "message": "",
        "data": {
            "current_page": 1,
            "data": [
                {
                    "id": 3,
                    "s1_goods_id": 1,
                    "wx_user_id": 1,
                    "created_at": "2017-10-19 16:36:16",
                    "updated_at": "2017-10-19 16:36:16",
                    "goods": {
                        "id": 1,
                        "pic": "/upload/s1/goods/face/20171013/effbecdc6d9de83d0128e3f08ec6d636.jpg",
                        "name": "商品1"
                    }
                }
            ],
            "first_page_url": "http://www.s1.com/api/my/favorite?page=1",
            "from": 1,
            "last_page": 1,
            "last_page_url": "http://www.s1.com/api/my/favorite?page=1",
            "next_page_url": null,
            "path": "http://www.s1.com/api/my/favorite",
            "per_page": 15,
            "prev_page_url": null,
            "to": 1,
            "total": 1
        }
    }
     * @apiErrorExample {json} Error-Response:
     * HTTP/1.1 400 Bad Request
     */
    public function favorite()
    {
        $user = Auth::guard('api')->user();
        if (!$user) return $this->error(ErrorCode::ERROR_POWER);
        $favorite = S1UserFavoriteModel::where('wx_user_id',$user->id)
            ->with(['goods'=> function ($query) {
                $query->select('id','pic','name','price');
        }])->paginate();
        return $this->api($favorite);
    }

}
