<?php
namespace app\admin\controller;

use think\facade\Session;
use app\admin\BaseController;
use app\common\model\Article as ArticleModel;
use app\common\model\ArticleCate as ArticleCateModel;
use app\common\model\ArticleComment as ArticleCommentModel;
use app\common\model\ArticleThumb as ArticleThumbModel;
use app\common\model\ArticleCollect as ArticleCollectModel;

class Article extends BaseController
{

	// 评论
	public function articleCommentList()
	{
		$articleid = input('articleid/d');
		return view('artapp/article/articlecommentlist',[
			'articleid' => $articleid
		]);
	}
	
	public function commentList()
	{
		return view('artapp/article/commentlist');
	}
	
	public function listComment()
	{
		$limit = input('limit');
		$page = input('page');
		$map = array();
		$keywords = input('keywords/s');
		if (!empty($keywords)){
			$map[] =['details', 'like', '%'.$keywords.'%'];
		}
		$articleid = input('articleid/d', 0);
		if (!empty($articleid)){
			$map[] = ['articleid', '=', $articleid];
		}
		$status = input('status/d');
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$list = ArticleCommentModel::with(['user','puser'])->where($map)->order('id','desc')->limit($limit)->page($page)->append(['status_text'])->select();
		$count = ArticleCommentModel::where($map)->count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}
	
	public function fieldComment()
	{
		$id = input('id/d');
		$comment = ArticleCommentModel::find($id);
		if ($comment==null){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "信息不存在"
			)));
		}else{
			$comment->save([
				input('field/s')  => input('value/s', "")
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function statusComment()
	{
		$id = input('id/d');
		$comment = ArticleCommentModel::find($id);
		if ($comment==null){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "信息不存在"
			)));
		}elseif($comment->status==2){
			$comment->save([
				'status' => 1
			]);
		}else{
			$comment->save([
				'status' => 2
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function statusCommentAll()
	{
		$idarr = input('idarr/a');
		ArticleCommentModel::update(['status' => 1], ['id'=>$idarr]);
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> ""
		)));
	}
	
	public function delComment()
	{
		$idarr = input('idarr/a');
		$comment = ArticleCommentModel::whereIn('id',$idarr)->select();
		$result = $comment->delete();
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}


	// 文章资讯
	public function articleList()
	{
		$catelist = ArticleCateModel::order(['priority'=>'desc','id'=>'desc'])->select();;
		return view('artapp/article/articlelist',[
			'catelist'	=> $catelist
		]);
	}
	
	public function articleForm()
	{
		$catelist = ArticleCateModel::order(['priority'=>'desc','id'=>'desc'])->select();
		$id = input('id/d, 0');
		$article = ArticleModel::findOrEmpty($id);
		return view('artapp/article/articleform',[
			'catelist'	=> $catelist,
			'article'	=> $article
		]);
	}
	
	public function listArticle()
	{
		$limit = input('limit');
		$page = input('page');
		$map = array();
		$keywords = input('keywords/s');
		if (!empty($keywords)){
			$map[] =['title', 'like', '%'.$keywords.'%'];
		}
		$cateid = input('cateid/d');
		if (!empty($cateid)){
			$map[] = ['cateid', '=', $cateid];
		}
		$atype = input('atype/d');
		if (!empty($atype)){
			$map[] = ['atype', '=', $atype];
		}
		$status = input('status/d');
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$list = ArticleModel::with('articleCate')->withCount(['articleComment','articleThumb','articleCollect'])->where($map)->order(['priority'=>'desc','id'=>'desc'])->limit($limit)->page($page)->append(['atype_text','status_text'])->select();
		$count = ArticleModel::where($map)->count(); 
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}
	
	public function delArticle()
	{
		$idarr = input('idarr/a');
		ArticleCommentModel::whereIn('articleid',$idarr)->delete();
		ArticleThumbModel::whereIn('articleid',$idarr)->delete();
		ArticleCollectModel::whereIn('articleid',$idarr)->delete();
		ArticleModel::whereIn('id',$idarr)->delete();
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> ""
		)));
	}
	
	public function editArticle()
	{
		$id = input('id/d');
		$data = array(
			'cateid'      => input('cateid/d'),
			'title'       => input('title/s'),
			'tilpic'      => input('tilpic/s'),
			'author'      => input('author/s'),
			'summary'     => input('summary/s'),
			'atype'       => input('atype/d'),
			'details'     => input('details/s'),
			'tilurl'      => input('tilurl/s'),
			'priority'    => input('priority/d'),
			'createtime'  => input('createtime/s'),
			'status'	  => input('status/d')==1 ? 1 : 2,
			'volume'      => input('volume/d') 
		);
		if (empty($id)){
			ArticleModel::create($data);
		}else{
			$data['id'] = $id;
			ArticleModel::update($data);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function fieldArticle()
	{
		$id = input('id/d',0); 
		$article = ArticleModel::findOrEmpty($id);
		if ($article->isEmpty()){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "信息不存在"
			)));
		}else{
			$article->save([
				input('field/s')  => input('value/s', "")
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}


	// 分类
	public function cateList()
	{
		return view('artapp/article/catelist');
	}
	
	public function cateForm()
	{
		$id = input('id/d, 0');
		$cate = ArticleCateModel::findOrEmpty($id);
		return view('artapp/article/cateform',[
			'cate'	=> $cate
		]);
	}
	
	public function listCate()
	{
		$limit = input('limit');
		$page = input('page');
		$list = ArticleCateModel::order(['priority'=>'desc','id'=>'desc'])->limit($limit)->page($page)->append(['status_text'])->select();
		$count = ArticleCateModel::count();
		if ($count==0){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "未查询到数据"
			)));
		}
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> "",
			'count' => $count,
			'data'  => $list
		)));
	}	
	
	public function fieldCate()
	{
		$id = input('id/d');
		$cate = ArticleCateModel::find($id);
		if ($cate==null){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "分类信息不存在"
			)));
		}else{
			$cate->save([
				input('field/s')  => input('value/s', "")
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function editCate()
	{
		$id = input('id/d');
		if (empty($id)){
			$cate = ArticleCateModel::create([
				'title' 	=> input('title/s'),
				'status'    => input('status/d')==1 ? 1 : 2,
				'priority'  => input('priority/d')
			]);
		}else{
			$cate = ArticleCateModel::find($id);
			$cate->save([
				'title' 	=> input('title/s'),
				'status'    => input('status/d')==1 ? 1 : 2,
				'priority'  => input('priority/d')
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delCate()
	{
		$id = input('id/d');
		$cate = ArticleCateModel::where('id',$id)->select();
		$result = $cate->delete();
		if ($result){
			exit(json_encode(array(
				'code' 	=> 0,
				'msg'	=> ""
			)));
		}
		exit(json_encode(array(
			'code' 	=> 1,
			'msg'	=> "删除失败，请稍后重试"
		)));
	}

}