<?php

/**
 *--------------------------------------
 * 会员等级
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-8
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class MemberLevelCtrlr extends ManageCtrlr
{
	public function list_level()
	{
		$_MLL = M('MemberLevel')->get_levelList();
		$this->assign('_MLL', $_MLL);
		$this->display();
	}

	public function add_level_do()
	{
		$data = ARequest::get();
		$result = M('MemberLevel')->add_level($data);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('MEMBER_LEVEL').': '.$result['error'], 0);
			$this->error($result['error'], Url::U('member_level/list_level'));
		}

		M('AdminLog')->add_log(ASession::get('m_userid'), L('ADD').L('MEMBER_LEVEL').': MEMBER_LEVEL_ID['.$result['data'].']');
		$this->success(L('ADD_SUCCESS'), Url::U('member_level/list_level'));
	}

	public function update_level_do()
	{
		$memberLevelId = ARequest::get('member_level_id');
		$mlName = ARequest::get('ml_name');
		$mlRank = ARequest::get('ml_rank');
		$mlMinExperience = ARequest::get('ml_min_experience');
		$mlType = ARequest::get('ml_type');
		$_L_ID = is_array($memberLevelId) ? implode(',', $memberLevelId) : $memberLevelId; /* 日志记录ID */

		if(!is_array($memberLevelId) or empty($memberLevelId))
		{
			$this->error(L('ITEM_NOT_EXIST'), Url::U('member_level/list_level'));
		}

		$data = array();
		$error = false;
		foreach($memberLevelId as $k => $id)
		{
			$data['member_level_id'] = $id;
			$data['ml_name'] = $mlName[$k];
			$data['ml_rank'] = $mlRank[$k];
			$data['ml_min_experience'] = $mlMinExperience[$k];
			$data['ml_type'] = $mlType[$k];
			$result = M('MemberLevel')->edit_level($data);
			if(!empty($result['error']))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('MEMBER_LEVEL').': MEMBER_LEVEL_ID['.$data['member_level_id'].']', 0);
				$this->error(L('EDIT_FAILED'), Url::U('member_level/list_level'));
			}
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('EDIT').L('MEMBER_LEVEL').': MEMBER_LEVEL_ID['.$_L_ID.']');
		$this->success(L('EDIT_SUCCESS'), Url::U('member_level/list_level'));
	}

	public function delete_level_do()
	{
		$memberLevelId = ARequest::get('member_level_id');
		$_L_ID = is_array($memberLevelId) ? implode(',', $memberLevelId) : $memberLevelId; /* 日志记录ID */

		if(is_array($memberLevelId))
		{
			$memberLevelId = implode(',', $memberLevelId);
		}
		$result = M('MemberLevel')->delete_level($memberLevelId);
		if(!empty($result['error']))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('MEMBER_LEVEL').': MEMBER_LEVEL_ID['.$memberLevelId.']'.$result['error'], 0);
			$this->error($result['error'], Url::U('member_level/list_level'));
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('MEMBER_LEVEL').': MEMBER_LEVEL_ID['.$_L_ID.']');
		$this->success(L('DELETE_SUCCESS'), Url::U('member_level/list_level'));
	}
}

?>