<?php
namespace app\admin\controller;

use think\facade\Session;
use app\admin\BaseController;
use app\common\model\Cert as CertModel;

class Cert extends BaseController
{
	
	public function certList()
	{
		return view('appcenter/cert/certlist');
	}
	
	public function certForm()
	{
		$id = input('id/d, 0');
		$cert = CertModel::findOrEmpty($id);
		return view('appcenter/cert/certform',[
			'cert' => $cert
		]);
	}
	
	
	public function listCert()
	{
		$limit = input('limit');
		$page = input('page');
		$map = array();
		$keywords = input('keywords/s');
		if (!empty($keywords)){
			$map[] =['title|cnumber', '=', $keywords];
		}
		$list = CertModel::where($map)->order(['id'=>'desc'])->limit($limit)->page($page)->append(['stype_text'])->select();
		$count = CertModel::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 fieldCert()
	{
		$id = input('id/d');
		$cert = CertModel::find($id);
		if ($cert==null){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "分类信息不存在"
			)));
		}else{
			$cert->save([
				input('field/s')  => input('value')
			]);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	
	public function editCert()
	{
		$id = input('id/d');
		$data = array(
			'title'			=> input('title/s', ""),
			'cnumber'		=> input('cnumber/s', ""),
			'tilpic'		=> input('tilpic/s', ""),
			'remark'		=> input('remark/s', ""),
			'createtime'	=> input('createtime/s', date('Y-m-d H:i'))
		);
		if (empty($id)){
			CertModel::create($data);
		}else{
			$data['id'] = $id;
			CertModel::update($data);
		}
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	public function delCert()
	{
		$idarr = input('idarr/a');
		CertModel::whereIn('id',$idarr)->delete();
		exit(json_encode(array(
			'code' 	=> 0,
			'msg'	=> ""
		)));
	}

}