<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Features;
use app\common\model\Hotsearch;
use app\common\model\Information;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页资讯
     *
     */
    public function index()
    {
        $this->success('请求成功',Information::where('status',1)->field('id,title,content,createtime')->limit(10)->select());
    }


    /**
     * 热门搜索
     */
    public function hotsearch(){
        $this->success('请求成功',Hotsearch::where('status',1)->select());
    }
    /**
     * 首页搜索
     * @param string $type 搜索类型1营养指向 2活性成分 3原料
     * @param string $search 搜索内容
     * @param string $page 页数
     *
     */
    public function search(){
        $rule = [
            'search|搜索内容'=>'require'
        ];
        $input = $this->_validate($rule);
        //搜索资讯
        switch ($input['type']){
            case 1:
                $where['nutrition'] = ['like','%'.$input['search'].'%'];
                break;
            case 2:
                $where['ingredient'] = ['like','%'.$input['search'].'%'];
                break;
            case 3:
                $where['rawmaterial'] = ['like','%'.$input['search'].'%'];
        }
        $where['status'] = 1;
        if($this->auth->rights){
            $searchInformation = Information::where($where)->page($input['page'],10)->selectOrFail();
        }else{
            $searchInformation = Information::where($where)->limit(5)->selectOrFail();
        }
        $this->success('请求成功',$searchInformation);
    }

    /**
     * 热门资讯
     */
    public function hotinformation(){
        $this->success('请求成功',Information::where('status',1)->field('id,title,createtime,status')->limit(5)->selectOrFail());
    }

    /**
     * 首页专题
     */
    public function features(){
        $this->success('请求成功',Features::where('status',1)->limit(3)->selectOrFail());
    }

    /**
     * 资讯列表
     * @param string $page 页数
     */
    public function informationall(){
        $page = $this->_validate(['page|页数'=>'require']);
        $this->success('请求成功',Information::where('status',1)->field('id,title,content,createtime')->page($page,'20')->select());
    }

    /**
     * 资讯详情
     * @param string $id 资讯id
     */
    public function informationdetails(){
        $where = [
          'status'=>1,
          'id'=>input('id')
        ];
        $data = Information::where($where)->findOrFail();
        $this->success('请求成功',$data);
    }

    /**
     * 广告位
     */
    public function advertising(){
        $this->success('请求成功',config('site.advertising'));
    }
}
