<?php

namespace app\admin\controller;

use app\common\controller\AdminController;
use app\admin\validate\Admin;
use app\admin\model\Admin as adminModel;
use app\common\model\Cate;
use app\common\model\Comment;
use think\facade\View;
use think\facade\Request;
use think\facade\Db;
use think\facade\Session;
use think\exception\ValidateException;

class Forum extends AdminController
{
    //帖子列表
	public function list()
	{
		if(Request::isAjax()){
			$forumList = Db::name('article')
			->alias('a')
			->join('user u','a.user_id = u.id')
			->field('a.id as aid,name,user_img,title,a.update_time,is_top')
			->where('a.delete_time',0)
			->order('a.create_time', 'desc')
			->paginate(15);
			$res = [];
			if($forumList){
				$res['code'] = 0;
				$res['msg'] = '';
				$res['count'] = $forumList->total();
				foreach($forumList as $k=>$v){
				$res['data'][]= ['id'=>$v['aid'],'poster'=>$v['name'],'avatar'=>$v['user_img'],'content'=>$v['title'],'posttime'=>date("Y-m-d",$v['update_time']),'top'=>$v['is_top']];
				}
			}
			return json($res);
		}
		return View::fetch();
	}
	
	//编辑帖子
	public function listForm()
	{
		if(Request::isAjax()){
			$data = Request::param();
			$form = Db::name('article')->find($data['id']);
			//halt($form);
		}
		return View::fetch();
	}
	
	//帖子分类
	public function tags()
	{
		if(Request::isAjax()){
			$list = Cate::select();
			if($list){
				$res['code'] = 0;
				$res['msg'] = '';
				$res['count']= count($list);
				$res['data'] = [];
				foreach($list as $k=>$v){
				$res['data'][] = ['id' => $v['id'],'tags'=>$v['catename'],'sort'=>$v['sort'],'ename'=>$v['ename']];
				}
			}
			return json($res);
		}
		return View::fetch();
	}
	
	//添加帖子分类
	public function addtags()
	{
		if(Request::isAjax()){
		$data = Request::param();
		//halt($data);
		$list = Db::name('cate')->save($data);
		
			if($list){
				return json(['code'=>0,'msg'=>'添加分类成功']);
			}else{
				return json(['code'=>-1,'msg'=>'添加分类失败']);
			}
		}
		return view('tagsform');
		
	}
	
	//编辑帖子分类
	public function tagsform()
	{
		if(Request::isAjax()){
		$data = Request::param();
		//halt($data);
		$list = Db::name('cate')->where('id',$data['id'])->save($data);
		
			if($list){
				return json(['code'=>0,'msg'=>'修改分类成功']);
			}else{
				return json(['code'=>-1,'msg'=>'修改分类失败']);
			}
		}
		return View::fetch();
	}
	
	//删除帖子分类
	public function tagsdelete()
	{
		if(Request::isAjax()){
		$data = Request::param();

		$cate = new Cate;
		$result = $cate->del($data);
		
		
			if($result == 1){
				return json(['code'=>0,'msg'=>'删除分类成功']);
			}else{
				return json(['code'=>-1,'msg'=>'删除分类失败']);
			}
		}
	}
	
	//帖子评论
	public function replys()
	{
		if(Request::isAjax()) {
			$replys = Comment::with(['user','article'])->paginate(15);
			$count = $replys->total();
			$res = [];
			if ($replys) {
				$res = ['code'=>0,'msg'=>'','count'=>$count];
				foreach($replys as $k => $v){
					//var_dump($v);
					$res['data'][] = ['id'=>$k+1,'replyer'=>$v->user->name,'cardid'=>$v->article->title,'avatar'=>$v->user->user_img,'content'=>$v['content'],'replytime'=>$v['create_time']];
				}
			}
			return json($res);
			}
		
		return View::fetch();
	}
	
	public function replysform()
	{
		return View::fetch();
	}
	
}