<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\CardBannerModel;
use App\Models\CardSettingModel;
use App\Models\CardUserHonorModel;
use App\Models\CardUserInfoModel;
use App\Models\CardUserProgressModel;
use App\Models\CardUserProjectModel;
use App\Models\CardUserTrendModel;
use Illuminate\Http\Request;
use Validator;
use App\Services\Base\ErrorCode;

class CardController extends Controller
{
    /**
     * @api {get} /api/card/index 名片主页(index)
     * @apiDescription 公司详情(index)
     * @apiGroup Card
     * @apiPermission none
     * @apiVersion 0.1.0
     * @apiParam {int}    user_id    user_id
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *     "state": true,
     *     "code": 0,
     *     "message": "",
     *     "data": {
     *         "banner": []//轮播图
     *         "project": []//经营项目
     *         "trend": []//个人动态
     *         "info": []//个人详情
     *     }
     * }
     * @apiErrorExample {json} Error-Response:
     * HTTP/1.1 400 Bad Request
     * {
     *     "state": false,
     *     "code": 1000,
     *     "message": "传入参数不正确",
     *     "data": null or []
     * }
     * 可能出现的错误代码：
     *    1000    CLIENT_WRONG_PARAMS             传入参数不正确
     */
    public function cardIndex(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'user_id' => 'required',
        ], [
            'user_id.required' => '用户信息未知',
        ]);
        if ($validator->fails()) {
            return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS, '');
        }
        $user_id = $request->user_id;
        //小程序設置
        $setting = CardSettingModel::where('appid', $user_id)->first();
        if(!$setting){
            $setting = [
                'banner_height' => 180,
                'banner_hide'   => 0,
                'base_color' => '#0074cd',
                'project_type' => 2,
                'trend_type' => 0,
                'show_style' => 0,
                'copyright' => '',
                'aboutme_icon'=>'../../images/aboutme.png',
                'develo_icon'=>'../../images/develop.png',
                'we_icon'=>'../../images/we.png',
                'honer_icon'=>'../../images/honer.png',
            ];
        }
        $project_num = isset($setting->project_type) && $setting->project_type == 1 ? 4 : 3;
        $trend_num = isset($setting->trend_type) && $setting->trend_type == 1 ? 4 : 3;
        //幻灯片
        $banner = CardBannerModel::where('appid', $user_id)->where('status', '显示')->orderBy('sort', 'DESC')->get();
        //经营项目
        $project = CardUserProjectModel::where('appid', $user_id)->orderBy('sort', 'DESC')->paginate($project_num);
        foreach ($project as $pitem){
            $pitem->detail = strip_tags($pitem->detail);
        }
        //个人动态
        $trend = CardUserTrendModel::where('appid', $user_id)->orderBy('sort', 'DESC')->paginate($trend_num);
        foreach ($trend as $titem){
            $titem->pic = strip_tags($titem->pic);
        }
        //个人名片详情
        $info = CardUserInfoModel::where('appid', $user_id)->first();


        return $this->api(compact('banner', 'project', 'trend', 'info', 'setting'));
    }

    /**
     * @api {get} /api/card/info 个人详情(info)
     * @apiDescription 个人详情(info)
     * @apiGroup Card
     * @apiPermission none
     * @apiVersion 0.1.0
     * @apiParam {int}    user_id    user_id
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *     "state": true,
     *     "code": 0,
     *     "message": "",
     *     "data": {
     *         "info": []//个人详情
     *     }
     * }
     * @apiErrorExample {json} Error-Response:
     * HTTP/1.1 400 Bad Request
     * {
     *     "state": false,
     *     "code": 1000,
     *     "message": "传入参数不正确",
     *     "data": null or []
     * }
     * 可能出现的错误代码：
     *    1000    CLIENT_WRONG_PARAMS             传入参数不正确
     */
    public function cardUserInfo(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'user_id' => 'required',
        ], [
            'user_id.required' => '用户信息未知',
        ]);
        if ($validator->fails()) {
            return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS, '');
        }
        $user_id = $request->user_id;
        //个人名片详情
        $info = CardUserInfoModel::where('appid', $user_id)->first();

        return $this->api(compact('info'));
    }

    /**
     * @api {get} /api/card/progress 个人经历(progress)
     * @apiDescription 个人经历(progress)
     * @apiGroup Card
     * @apiPermission none
     * @apiVersion 0.1.0
     * @apiParam {int}    user_id    user_id
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *     "state": true,
     *     "code": 0,
     *     "message": "",
     *     "data": {
     *         "progress": []//个人经历
     *     }
     * }
     * @apiErrorExample {json} Error-Response:
     * HTTP/1.1 400 Bad Request
     * {
     *     "state": false,
     *     "code": 1000,
     *     "message": "传入参数不正确",
     *     "data": null or []
     * }
     * 可能出现的错误代码：
     *    1000    CLIENT_WRONG_PARAMS             传入参数不正确
     */
    public function cardUserProgress(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'user_id' => 'required',
        ], [
            'user_id.required' => '用户信息未知',
        ]);
        if ($validator->fails()) {
            return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS, '');
        }
        $user_id = $request->user_id;
        $progress = CardUserProgressModel::where('appid', $user_id)->get();

        return $this->api(compact('progress'));
    }

    /**
     * @api {get} /api/card/honor 个人荣誉(honor)
     * @apiDescription 个人荣誉(honor)
     * @apiGroup Card
     * @apiPermission none
     * @apiVersion 0.1.0
     * @apiParam {int}    user_id    user_id
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *     "state": true,
     *     "code": 0,
     *     "message": "",
     *     "data": {
     *         "honor": []//个人荣誉
     *     }
     * }
     * @apiErrorExample {json} Error-Response:
     * HTTP/1.1 400 Bad Request
     * {
     *     "state": false,
     *     "code": 1000,
     *     "message": "传入参数不正确",
     *     "data": null or []
     * }
     * 可能出现的错误代码：
     *    1000    CLIENT_WRONG_PARAMS             传入参数不正确
     */
    public function cardUserHonor(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'user_id' => 'required',
        ], [
            'user_id.required' => '用户信息未知',
        ]);
        if ($validator->fails()) {
            return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS, '');
        }
        $user_id = $request->user_id;
        $honor = CardUserHonorModel::where('appid', $user_id)->get();

        return $this->api(compact('honor'));
    }

    /**
     * @api {get} /api/card/project 经营项目(project)
     * @apiDescription 经营项目(project)
     * @apiGroup Card
     * @apiPermission none
     * @apiVersion 0.1.0
     * @apiParam {int}    user_id    user_id
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *     "state": true,
     *     "code": 0,
     *     "message": "",
     *     "data": {
     *         "project": []//经营项目
     *     }
     * }
     * @apiErrorExample {json} Error-Response:
     * HTTP/1.1 400 Bad Request
     * {
     *     "state": false,
     *     "code": 1000,
     *     "message": "传入参数不正确",
     *     "data": null or []
     * }
     * 可能出现的错误代码：
     *    1000    CLIENT_WRONG_PARAMS             传入参数不正确
     */
    public function cardUserProject(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'user_id' => 'required',
        ], [
            'user_id.required' => '用户信息未知',
        ]);
        if ($validator->fails()) {
            return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS, '');
        }
        $user_id = $request->user_id;
        $project = CardUserProjectModel::where('appid', $user_id)->orderBy('sort', 'DESC')->get();

        return $this->api(compact('project'));
    }

    /**
     * @api {get} /api/card/projectDetail 经营项目详情(projectDetail)
     * @apiDescription 经营项目详情(projectDetail)
     * @apiGroup Card
     * @apiPermission none
     * @apiVersion 0.1.0
     * @apiParam {int}    user_id    user_id
     * @apiParam {int}    pro_id     项目ID
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *     "state": true,
     *     "code": 0,
     *     "message": "",
     *     "data": {
     *         "project": []//经营项目
     *     }
     * }
     * @apiErrorExample {json} Error-Response:
     * HTTP/1.1 400 Bad Request
     * {
     *     "state": false,
     *     "code": 1000,
     *     "message": "传入参数不正确",
     *     "data": null or []
     * }
     * 可能出现的错误代码：
     *    1000    CLIENT_WRONG_PARAMS             传入参数不正确
     */
    public function projectDetail(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'user_id' => 'required',
            'pro_id' => 'required',
        ], [
            'user_id.required' => '用户信息未知',
            'pro_id.required' => '所选项目未知',
        ]);
        if ($validator->fails()) {
            return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS, '');
        }
        $user_id = $request->user_id;
        $pro_id = $request->pro_id;
        $project = CardUserProjectModel::where('appid', $user_id)->where('id', $pro_id)->first();
        if (!$project) return $this->error(1000, '所经营项目不存在');
        return $this->api(compact('project'));
    }

    /**
     * @api {get} /api/card/trend 所有动态(trend)
     * @apiDescription 所有动态(trend)
     * @apiGroup Card
     * @apiPermission none
     * @apiVersion 0.1.0
     * @apiParam {int}    user_id    user_id
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *     "state": true,
     *     "code": 0,
     *     "message": "",
     *     "data": {
     *         "trend": []//所有动态
     *     }
     * }
     * @apiErrorExample {json} Error-Response:
     * HTTP/1.1 400 Bad Request
     * {
     *     "state": false,
     *     "code": 1000,
     *     "message": "传入参数不正确",
     *     "data": null or []
     * }
     * 可能出现的错误代码：
     *    1000    CLIENT_WRONG_PARAMS             传入参数不正确
     */
    public function CardUserTrend(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'user_id' => 'required',
        ], [
            'user_id.required' => '用户信息未知',
        ]);
        if ($validator->fails()) {
            return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS, '');
        }
        $user_id = $request->user_id;
        $trend = CardUserTrendModel::where('appid', $user_id)->orderBy('sort', 'DESC')->get();
        foreach ($trend as $k => $item) {
            if ($item->pic) {
                $trend[$k]->pic = explode(',', $item->pic);
            } else {
                $trend[$k]->pic = [];
            }
        }

        return $this->api(compact('trend'));
    }

    /**
     * @api {get} /api/card/trendDetail 个人动态详情(trendDetail)
     * @apiDescription 个人动态详情(trendDetail)
     * @apiGroup Card
     * @apiPermission none
     * @apiVersion 0.1.0
     * @apiParam {int}    user_id    user_id
     * @apiParam {int}    trend_id   动态ID
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *     "state": true,
     *     "code": 0,
     *     "message": "",
     *     "data": {
     *         "trend": []//经营项目
     *     }
     * }
     * @apiErrorExample {json} Error-Response:
     * HTTP/1.1 400 Bad Request
     * {
     *     "state": false,
     *     "code": 1000,
     *     "message": "传入参数不正确",
     *     "data": null or []
     * }
     * 可能出现的错误代码：
     *    1000    CLIENT_WRONG_PARAMS             传入参数不正确
     */
    public function trendDetail(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'user_id' => 'required',
            'trend_id' => 'required',
        ], [
            'user_id.required' => '用户信息未知',
            'trend_id.required' => '动态ID未知',
        ]);
        if ($validator->fails()) {
            return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS, '');
        }
        $user_id = $request->user_id;
        $trend_id = $request->trend_id;
        $trend = CardUserTrendModel::where('appid', $user_id)->where('id', $trend_id)->first();
        if (!$trend) return $this->error(1000, '动态不存在');
        return $this->api(compact('trend'));
    }

    /**
     * @api {get} /api/card/setting 基础设置
     * @apiDescription 基础设置
     * @apiGroup Card
     * @apiPermission none
     * @apiVersion 0.1.0
     * @apiParam {int}    user_id    user_id
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * @apiErrorExample {json} Error-Response:
     * HTTP/1.1 400 Bad Request
     */
    public function cardSetting(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'user_id' => 'required',
        ], [
            'user_id.required' => '用户信息未知',
        ]);
        if ($validator->fails()) {
            return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS, '');
        }
        $user_id = $request->user_id;
        $setting = CardSettingModel::where('appid', $user_id)->fitst();

        return $this->api(compact('setting'));
    }
}
