<?php
namespace app\admin\controller;

use think\facade\Session;
use app\admin\BaseController;
use app\common\model\Slide as SlideModel;

class Slide extends BaseController
{
	
	public function index()
	{
		return view('set/slidelist');
	}
	
	public function slideForm()
	{
		$id = input('id/d, 0');
		$slide = SlideModel::findOrEmpty($id);
		return view('set/slideform',[
			'slide' => $slide
		]);
	}
	
	
	public function listSlide()
	{
		$limit = input('limit');
		$page = input('page');
		$map = array();
		$keywords = input('keywords/s');
		if (!empty($keywords)){
			$map[] =['title', 'like', '%'.$keywords.'%'];
		}
		$pagetab = input('pagetab/d');
		if (!empty($pagetab)){
			$map[] = ['pagetab', '=', $pagetab];
		}
		$stype = input('stype/d');
		if (!empty($stype)){
			$map[] = ['stype', '=', $stype];
		}
		$list = SlideModel::where($map)->order(['priority'=>'desc','id'=>'desc'])->limit($limit)->page($page)->append(['stype_text'])->select();
		$count = SlideModel::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 fieldSlide()
	{
		$id = input('id/d');
		$slide = SlideModel::find($id);
		if ($slide==null){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "分类信息不存在"
			)));
		}else{
			$slide->save([
				input('field/s')  => input('value')
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	
	public function editSlide()
	{
		$id = input('id/d');
		$data = array(
			'pagetab'   => input('pagetab/s'),
			'title' 	=> input('title/s'),
			'tilpic' 	=> input('tilpic/s'),
			'stype' 	=> input('stype/d', 1),
			'tilurl' 	=> input('tilurl/s'),
			'priority'  => input('priority/d', 0)
		);
		if (empty($id)){
			SlideModel::create($data);
		}else{
			$data['id'] = $id;
			SlideModel::update($data);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delSlide()
	{
		$idarr = input('idarr/a');
		SlideModel::whereIn('id',$idarr)->delete();
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> ""
		)));
	}

}