<?php
namespace app\admin\controller;

use think\facade\Session;
use app\admin\BaseController;
use app\common\model\Notice as NoticeModel;
use app\common\model\NoticeCate as NoticeCateModel;

class Notice extends BaseController
{

	// 删除
	public function delNotice()
	{
		$idarr = input('idarr/a');
		NoticeModel::whereIn('id',$idarr)->delete();
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> ""
		)));
	}

	// 编辑&添加
	public function editNotice()
	{
		$id = input('id/d');
		if (empty($id)){
			NoticeModel::create([
				'cateid'      => input('cateid/d'),
				'title'       => input('title/s'),
				'details'     => input('details/s'),
				'priority'    => input('priority/d'),
				'createtime'  => input('createtime/s'),
				'status'	  => input('status/d')==1 ? 1 : 2,
				'volume'      => input('volume/d') 
			]);
		}else{
			NoticeModel::update([
				'id'		  => $id,
				'cateid'      => input('cateid/d'),
				'title'       => input('title/s'),
				'details'     => input('details/s'),
				'priority'    => input('priority/d'),
				'createtime'  => input('createtime/s'),
				'status'	  => input('status/d')==1 ? 1 : 2,
				'volume'      => input('volume/d') 
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	 
	// 列表
	public function noticeList()
	{
		$catelist = NoticeCateModel::order(['priority'=>'desc','id'=>'desc'])->select();;
		return view('artapp/notice/noticelist',[
			'catelist'	=> $catelist
		]);
	}
	
	public function noticeForm()
	{
		$catelist = NoticeCateModel::order(['priority'=>'desc','id'=>'desc'])->select();
		$id = input('id/d, 0');
		$notice = NoticeModel::findOrEmpty($id);
		if ($notice->isEmpty()){
			$notice['atype'] = 1;
		}
		return view('artapp/notice/noticeform',[
			'catelist'	=> $catelist,
			'notice'	=> $notice
		]);
	}
	
	public function fieldNotice()
	{
		$id = input('id/d',0); 
		$info = NoticeModel::findOrEmpty($id);
		if ($info->isEmpty()){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "信息不存在"
			)));
		}else{
			$info->save([
				input('field/s')  => input('value/s', "")
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	
	public function listNotice()
	{
		$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];
		}
		$status = input('status/d');
		if (!empty($status)){
			$map[] = ['status', '=', $status];
		}
		$list = NoticeModel::with('noticeCate')->where($map)->order(['priority'=>'desc','id'=>'desc'])->limit($limit)->page($page)->append(['status_text'])->select();
		$count = NoticeModel::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 cateList()
	{
		return view('artapp/notice/catelist');
	}
	
	public function cateForm()
	{
		$id = input('id/d, 0');
		$cate = NoticeCateModel::findOrEmpty($id);
		return view('artapp/notice/cateform',[
			'cate'	=> $cate
		]);
	}
	
	public function listCate()
	{
		$limit = input('limit');
		$page = input('page');
		$list = NoticeCateModel::order(['priority'=>'desc','id'=>'desc'])->limit($limit)->page($page)->append(['status_text'])->select();
		$count = NoticeCateModel::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 = NoticeCateModel::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 = NoticeCateModel::create([
				'title' 	=> input('title/s'),
				'status'    => input('status/d')==1 ? 1 : 2,
				'priority'  => input('priority/d')
			]);
		}else{
			$cate = NoticeCateModel::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');
		NoticeCateModel::where('id',$id)->delete();
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> ""
		)));
	}

}