<?php

namespace App\Http\Controllers\Api;

use App\Models\Article;
use Illuminate\Http\Request;

class ArticleController extends ApiController {

	/**
	 *
	 * @api {post} article/list 文章-列表
	 * @apiName article
	 * @apiGroup article
	 * @apiParamExample {curl} Example usage:	 
	 */
	function list(Request $request) {
		$page = $request->input ( 'page', 1 );
		$cid=$request->input('cid',1);
		$items = Article::select(['id','title','created_at','head_img'])->where ( 'category_id', $cid )->orderBy('created_at','desc')->paginate ( 150, [ 
				'*' 
		], 'page', $page );
		return $this->success ( $items );
	}
	/**
	 *
	 * @api {post} article/detail 文章-详情
	 * @apiName article
	 * @apiGroup article
	 * @apiParamExample {curl} Example usage:
	 */
	function detail(Request $request) {
		$item = Article::find ( $request->id );
		return $this->success ( $item );
	}
	/**
	 *
	 * @api {post} article/home 家长端-首页
	 * @apiName article
	 * @apiGroup article
	 * @apiParamExample {curl} Example usage:
	 */
	function home(Request $request){
		$fengcai=Article::select(['id','head_img','title','created_at'])->where('category_id',1)->orderBy('created_at','desc')->limit(3)->get();
		$dongtai=Article::select(['id','head_img','title','created_at'])->where('category_id',2)->orderBy('created_at','desc')->limit(3)->get();
		$item['fengcai']=$fengcai;
		$item['dongtai']=$dongtai;
		return $this->success ( $item );
	}
	/**
	 *
	 * @api {post} article/homeForCoach 教师端-首页
	 * @apiName article
	 * @apiGroup article
	 * @apiParamExample {curl} Example usage:
	 */
	function homeForCoach(){
		$tongzhi=Article::select(['id','head_img','title','created_at'])->where('category_id',3)->orderBy('updated_at','desc')->limit(5)->get()->toArray();
		$dongtai=Article::select(['id','head_img','title','created_at'])->where('category_id',2)->orderBy('updated_at','desc')->limit(5)->get()->toArray();
		
		foreach($tongzhi as $k=>$tongzhiItem){
			$tongzhi[$k]['created_at']=substr($tongzhiItem['created_at'],0,10);
		}
		foreach($dongtai as $k=>$dongtaiItem){
			$dongtai[$k]['created_at']=substr($dongtaiItem['created_at'],0,10);
		}
		$item['tongzhi']=$tongzhi;
		$item['dongtai']=$dongtai;
		return $this->success ( $item );
	}
	
}
