<?php

namespace Cas\Controller\Admin\Category;

use KIF\Core\Request;
use Cas\Controller\Admin\Controller;
use KIF\Page\Page;
use Cas\Dao\Category;
use KIF\Verify;

/**
 * "分类相关" - "列表"[分类相关信息]
 *
 * @author rabin
 *         页面: "分类信息"列表 doPageList
 *         页面: 查看&修改 "分类信息" doPageShow
 *         事件: 创建"分类信息" doReqCreate
 *         事件: 修改"分类信息" doReqUp
 *         事件: 删除"分类信息" doReqDel  
 */
class Index extends Controller {
	private $objDCategory;
	private $operatorData;
	
	/**
	 *  初始化
	 */
	public function __construct() {
		header ( "Content-Type: text/html; charset=utf-8" );
		$this->objDCategory = new Category ();
		$this->operatorData = $this->getUser ();
	}
	
	/**
	 * 默认
	 */
	public function doDefault() {
	}
	
	/**
	 * 页面: "分类信息"列表
	 * 地址:http://cas.lishuy.com/?c=Admin_Category_Index&a=PageList
	 */
	public function doPageList() {
		$page = Request::varGetInt ( 'page', 1 );
		$size = 10;
		$offset = ($page - 1) * $size;
		$limit = "{$offset},{$size}";
		$condition = array ();
		$total_num = $this->objDCategory->totals ( $condition );
		$url_tpl = Request::schemeDomain () . "?c=Admin_Category_Index&a=PageList";
		$url_tpl .= "&page={page}";
		$objPage = new Page ( $total_num, $url_tpl, $page, $size );
		$page_html = $objPage->html ();
		
		$order = 'id desc';
		$ids = $this->objDCategory->findIdsBy ( $condition, $limit, $order );
		$CategoryData = $this->objDCategory->gets ( $ids );
		
		$this->tpl = 'admin/category/list';
		$title = '分类列表';
		$this->setOutput('title', $title);
		$this->setOutput('menu_active', array('name' => 'categorylist', 'item' => 'list')); //激活菜单
		$this->addNavMenu('分类设置');
		$this->addNavMenu($title);
		
		$this->setOutput('pagePublicData', $this->getPagePublicData()); // 后台管理相关数据
		$this->setOutput ( 'page_html', $page_html ); // 分页Html
		$this->setOutput ( 'CategoryData', $CategoryData ); // 当前"分类信息"列表数据
	}
	
	/**
	 * 页面: 查看&修改 "分类信息"
	 * 地址:http://cas.lishuy.com/?c=Admin_Category_Index&a=PageShow&id=nn
	 * 参数:id "分类信息"id
	 */
	public function doPageShow() {
		$id = Request::g ( 'id' );
		
		$CategoryData = $this->objDCategory->get ( $id );
		
		$this->tpl = 'admin/category/edit';
		$title = '编辑分类信息';
		$this->setOutput('title', $title);
		$this->setOutput('menu_active', array('name' => 'categorylist', 'item' => 'list')); //激活菜单
		$this->addNavMenu('分类设置');
		$this->addNavMenu('分类列表', Request::schemeDomain() . '/?c=Admin_Category_index&a=PageList');
		$this->addNavMenu($title);
		
		$this->setOutput('pagePublicData', $this->getPagePublicData()); // 后台管理相关数据
		
		$this->setOutput ( 'CategoryData', $CategoryData ); // 当前"分类信息"内容
	}
	
	/**
	 * 事件:创建"分类信息"
	 * 地址:http://cas.lishuy.com/index.php?c=Admin_Category_Index&a=ReqCreate
	 * 参数:见方法内
	 */
	public function doReqCreate() {
		$tableInfo = $_POST;
		$operator_uid = $this->operatorData ['uid'];
		
		$info = array_merge($tableInfo, array(
			'operator_uid' => $operator_uid
		));
		
		$tmpResult = $this->objDCategory->add ( $info );
		if (!$tmpResult) {
			$this->ajax_fail_exit('添加新分类失败');
		}
		
		$this->ajax_success_exit();
	}
	
	/**
	 * 事件: 修改"分类信息"
	 * 地址:http://cas.lishuy.com/index.php?c=Admin_Category_Index&a=ReqUp
	 * 参数:见方法内
	 */
	public function doReqUp() {
		$tableInfo = $_POST;
		
		$id = $tableInfo['id'];
		unset($tableInfo['id']);
		
		$operator_uid = $this->operatorData ['uid'];
		
		$info = array_merge($tableInfo, array(
			'operator_uid' => $operator_uid
		));
		$tmpResult = $this->objDCategory->modify ( $info, array (
				'id' => $id 
		) );
		
		if (!$tmpResult->isSuccess()) {
			$this->ajax_fail_exit($tmpResult->getData());
		}
		
		$this->ajax_success_exit();
	}
	
	/**
	 * 事件: 删除"分类信息"
	 * 地址:http://cas.lishuy.com/index.php?c=Admin_Category_Index&a=ReqDel
	 * 参数:见方法内
	 */
	public function doReqDel() {
		$id = Request::p ( 'id' );
		
		$this->objDCategory->delete ( array (
				'id' => $id 
		) );
		
		// TODO 根据前端自定义返回结果或跳转
		header ( "Location: " . Request::schemeDomain () . "/?c=Admin_Category_Index&a=PageList" );
		exit ();
	}

	public function doDelete() {
		\KIF\Core\PermissionController::requireCompetence();
		
		$id = Request::p('id');
		if (!Verify::unsignedInt($id)) {
			$this->ajax_fail_exit('无效id');
		}
		
		$this->objDCategory->delete ( array (
				'id' => $id 
		) );
		$this->ajax_success_exit();
	}


	public function display() {
		return $this->render ();
	}
}