<?php
namespace app\admin\controller;

use think\facade\Session;
use app\admin\BaseController;
use app\common\model\Question as QuestionModel;
use app\common\model\QuestionCate as QuestionCateModel;

class Question extends BaseController
{

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

	// 编辑&添加
	public function editQuestion()
	{
		$id = input('id/d');
		if (empty($id)){
			QuestionModel::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{
			QuestionModel::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 questionList()
	{
		$catelist = QuestionCateModel::order(['priority'=>'desc','id'=>'desc'])->select();;
		return view('artapp/question/questionlist',[
			'catelist'	=> $catelist
		]);
	}
	
	public function questionForm()
	{
		$catelist = QuestionCateModel::order(['priority'=>'desc','id'=>'desc'])->select();
		$id = input('id/d, 0');
		$question = QuestionModel::findOrEmpty($id);
		if ($question->isEmpty()){
			$question['atype'] = 1;
		}
		return view('artapp/question/questionform',[
			'catelist'	=> $catelist,
			'question'	=> $question
		]);
	}
	
	public function fieldQuestion()
	{
		$id = input('id/d',0); 
		$info = QuestionModel::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 listQuestion()
	{
		$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 = QuestionModel::with('questionCate')->where($map)->order(['priority'=>'desc','id'=>'desc'])->limit($limit)->page($page)->append(['status_text'])->select();
		$count = QuestionModel::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/question/catelist');
	}
	
	public function cateForm()
	{
		$id = input('id/d, 0');
		$cate = QuestionCateModel::findOrEmpty($id);
		return view('artapp/question/cateform',[
			'cate'	=> $cate
		]);
	}
	
	public function listCate()
	{
		$limit = input('limit');
		$page = input('page');
		$list = QuestionCateModel::order(['priority'=>'desc','id'=>'desc'])->limit($limit)->page($page)->append(['status_text'])->select();
		$count = QuestionCateModel::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 = QuestionCateModel::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 = QuestionCateModel::create([
				'title' 	=> input('title/s'),
				'status'    => input('status/d')==1 ? 1 : 2,
				'priority'  => input('priority/d')
			]);
		}else{
			$cate = QuestionCateModel::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');
		QuestionCateModel::where('id',$id)->delete();
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> ""
		)));
	}

}