<?php
// +----------------------------------------------------------------------
// | WANCMS游戏平台管理系统
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.wancms.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: yu <thebestyu@126.com> <http://www.wancms.com>
// +----------------------------------------------------------------------
// | 修改时间: 2016/01/05
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
/**
 * 卡类类型控制器
 * @author yu <thebestyu@126.com>
 */
class CardtypeController extends AdminbaseController {
	
	protected $cardtype_model;
	
	function _initialize() {
		parent::_initialize();
		$this->cardtype_model =D("Common/CardType");
	}
	
	
	/**

	 *  显示

	 */
	public function index() {
		$cards=$this->cardtype_model->select();
	/*	foreach ($cards as $key => $value) {
			if($cards[$key]['tid']=='1')
   				$cards[$key]['tid']="页游卡";
   			else
   				$cards[$key]['tid']="手游卡";
			}	
			*/
		$this->assign("cardtypes",$cards);
		$this->display();
	}
	
	/**

	 *  添加

	 */
	public function add() {
		$tid = $this->cardtype_model->getField('tid');
		$this->assign($tid);
		$this->display();
	}
	
	/**

	 *  添加

	 */
	public function add_post() {
		if (IS_POST) {
			if ($this->cardtype_model->create()) {
				if ($this->cardtype_model->add()) {
					$this->success("添加成功！", U("cardtype/index"));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->cardtype_model->getError());
			}
			
		}
	}
	
	/**

	 * 编辑

	 */
	function edit(){
		$id= intval(I("get.id"));
		$cardtype=$this->cardtype_model->where(array("id"=>$id))->find();
		$tid = $this->cardtype_model->getField('tid');
		//dump($tid);
		$this->assign($tid);
		$this->assign($cardtype);
		$this->display();
	}
	
	/**

	 * 编辑

	 */
	function edit_post(){
		if (IS_POST) {
			if ($this->cardtype_model->create()) {
				if ($this->cardtype_model->save() !== false) {
					$this->success("保存成功！", U("cardtype/index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->cardtype_model->getError());
			}
		}
	}

	/**

	 * 删除

	 */
	function delete(){
	$id = intval(I("get.id"));
	$count = M('card')->where(array("card_type_id" => $id))->count();
	if ($count > 0) {
		$this->error("该卡类类型下还有卡类存在，无法删除！");
	}
	if ($tid = 1) {
		if ($this->cardtype_model->delete($id)!==false) {
			$nav_obj=D("Common/CardType");
			$nav_obj->where("cid=$id")->delete();
			$this->success("删除卡类成功！");
		} else {
			$this->error("删除卡类失败！");
		}
	}else{
			$this->error("手游卡类暂时无法删除！");
		}
}
}