<?php

namespace App\Web\Controllers;

use App\Http\Controllers\BaseController;
use App\Web\Services\SuccessCaseService;
use Illuminate\Http\Request;


class SuccessCaseController extends BaseController
{

    protected $service = null;

    /**
     * 构造函数
     * SuccessCaseController constructor.
     * @param SuccessCaseService $service
     */
    public function __construct(SuccessCaseService $service)
    {
        $this->service = $service;
    }

    /**
     * 获取成功案例列表
     * */
    public function getSuccessCaseList(Request $request)
    {
        $params = $request->only(['page_size', 'page', 'status', 'keyword']);
        $ret = $this->service->getSuccessCaseList($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }


    /**
     * 获取成功案例详情
     * */
    public function getSuccessCaseInfo(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => '成功案例id不能为空',
        ]);
        $id = $request->input('id', 0);
        $ret = $this->service->getSuccessCaseInfo(['id' => $id]);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }


    /**
     * 变更成功案例状态
     * */
    public function changeSuccessCaseStatus(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $params = $request->only(['id', 'status']);
        $ret = $this->service->saveSuccessCase($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 保存成功案例数据
     * */
    public function saveSuccessCase(Request $request)
    {
        $this->validate($request, [
            'title' => 'required',
            'content' => 'required|string',
        ]);
        $params = $request->only(['id', 'pub_date', 'title', 'content','description',
            'image_url', 'image_alt','main_image','main_image_alt', 'up_data', 'function_id', 'industry_id',
            'company_name', 'company_desc', 'company_logo', 'virtual_view', 'seo_id', 'sort', 'status', 'seo_data']);
        $ret = $this->service->saveSuccessCase($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }


    /**
     * 新闻排序上下移
     * @url /api/case/change-sort
     */
    public function changeSuccessCaseSort(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
            'sort' => 'required',
        ]);
        $ret = $this->service->changeSuccessCaseSort($request->input('id'), $request->input('sort'));
        return $this->jsonResponse('ok', $ret);
    }

    /**
     * 更新成功案例的浏览量
     * */
    public function upSuccessCasePv(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $caseId = $request->input('id', 0);
        $incValue = $request->input('inc_val', 1);
        $ret = $this->service->upSuccessCasePv($caseId, $incValue);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 搜索获取已发布的案例
     * */
    public function getSearchCaseList(Request $request){
        $params=$request->only(['page_size','keyword','function_id','industry_id','page','is_handpick']);
        $ret=$this->service->getSuccessCaseList($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 文章推荐设置
     * */
    public function handpickSet(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ]);
        $params = $request->only(['id','is_handpick']);
        $ret=$this->service->handpickSet($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }


}
