<?
if (!defined('IN_BANDTOPIA'))
{
	exit;
}

	if(ADMIN_FULL == "admin" || ADMIN_USER == "agent") 
	{
		$nav .= '&nbsp;&gt;&nbsp;<a href="index.php?action=agent&function=agentlist">Agent List</a>';
		/******************************************************/
		/*  Agent List Page
		/******************************************************/
		if($_GET['function'] == 'agentlist' || $_GET['function'] == '') 
		{
			$agentObj = new AgentObj();
			$agentList = $agentObj -> listAgent();
			$template -> assign_block_vars("agent_list",array());
			if(count($agentList) > 0)
			{
				foreach($agentList as $key => $value)
				{
					$template -> assign_block_vars("agent_list.agent", array(
						"AGENT_ID" => $value->get_agent_id(),
						"AGENT_NAME" => $value->get_agent_name(),
						"AGENT_CITY" => $value->get_agent_city(),
						"AGENT_STATE" => $value->get_agent_state(),
						"AGENT_EMAIL" => $value->get_agent_email(),
						"AGENT_PHONE" => $value->get_agent_phone()));
				}
			}
			$template -> assign_var_from_handle('AGENT_ADMIN_PAGE', 'agentadminpage');
		}
		/******************************************************/
		/*  Agent View/Edit Page
		/******************************************************/
		elseif($_GET['function'] == 'agentview' || $_GET['function'] == 'agentedit' || $_GET['function'] == 'agentadd')
		{
			if($_POST['post_form'] == 'true' && ($_POST['agentadd'] == 'true' || $_POST['agent_id'] != ''))
			{
				$agentObj = new AgentObj();
				$agentObj->set_agent_id($_POST['agent_id']);
				$agentObj->set_agent_name($_POST['agent_name']);
				$agentObj->set_agent_address($_POST['agent_address']);
				$agentObj->set_agent_city($_POST['agent_city']);
				$agentObj->set_agent_state($_POST['agent_state']);
				$agentObj->set_agent_zip($_POST['agent_zip']);
				$agentObj->set_agent_phone($_POST['agent_phone']);
				$agentObj->set_agent_phone2($_POST['agent_phone2']);
				$agentObj->set_agent_email($_POST['agent_email']);
				$agentMsg = $agentObj->updateAgent();
				
				$ArtistObj = new ArtistObj();
				$ArtistList = $ArtistObj->list_artists();
				foreach($ArtistList AS $key => $value)
				{
					$artistAgentObj = new ArtistAgentObj();
					$artistAgentObj->set_artist_id($value->get_artist_id());
					$artistAgentObj->set_agent_id($agentObj->get_agent_id());
					$permission = $artistAgentObj->getAgentPermission();
					if($_POST['artist_id_'.$value->get_artist_id()] == $value->get_artist_id() && $permission == false)
					{
						$artistAgentObj->set_action("insert");
						$artistAgentObj->updateAgentPermission();
					}
					elseif($_POST['artist_id_'.$value->get_artist_id()] != $value->get_artist_id() && $permission == true)
					{
						$artistAgentObj->set_action("remove");
						$artistAgentObj->updateAgentPermission();
					}
				}
				
				$template -> assign_block_vars("agent_msg", array(
					"AGENT_MSG" => $agentMsg));
			}
			else
			{
				if($_GET['function'] == 'agentadd')
				{
					$template -> assign_block_vars("agent_view", array(
						"DISABLED" => " DISABLED"));
					$template -> assign_block_vars("agent_view.edit", array());
					$template -> assign_block_vars("agent_view.add", array());
					
					$ArtistObj = new ArtistObj();
					$artistList = $ArtistObj->list_artists();
					foreach($artistList AS $key => $value)
					{
						$template -> assign_block_vars("agent_view.artist", array(
							"ARTIST_ID" => $value->get_artist_id(),
							"ARTIST_NAME" => $value->get_artist_name()));
						$template -> assign_block_vars("agent_view.artist.edit", array());
					}
				}
				elseif($_GET['agent_id'] == '' && $_POST['agent_id'] == '')
				{
					header("Location: index.php?action=agent&function=agentlist");
				}
				else
				{	
					$agentObj = new AgentObj();
					$agentObj->set_agent_id($_GET['agent_id']);
					$agentObj->getAgent();
					if($agentObj->get_agent_name() == '')
					{
						$template -> assign_block_vars("page_error",array());
						$template -> assign_block_vars("page_error.error_msg", array(
							"ERROR_MSG" => "No agent found with the provided id.  Please try again."));
					}
					else
					{
						$template -> assign_block_vars("agent_view", array(
							"AGENT_ID" => $agentObj->get_agent_id(),
							"AGENT_NAME" => $agentObj->get_agent_name(),
							"AGENT_ADDRESS" => $agentObj->get_agent_address(),
							"AGENT_CITY" => $agentObj->get_agent_city(),
							"AGENT_STATE" => $agentObj->get_agent_state(),
							"AGENT_ZIP" => $agentObj->get_agent_zip(),
							"AGENT_PHONE" => $agentObj->get_agent_phone(),
							"AGENT_PHONE2" => $agentObj->get_agent_phone2(),
							"AGENT_EMAIL" => $agentObj->get_agent_email()));

						$artistObj = new ArtistObj();
						$artistList = $artistObj->list_artists();
						foreach($artistList AS $key => $value)
						{
							$artistAgentObj = new ArtistAgentObj();
							$artistAgentObj->set_artist_id($value->get_artist_id());
							$artistAgentObj->set_agent_id($agentObj->get_agent_id());
							$permission = $artistAgentObj->getAgentPermission();
							if($permission != true)
							{
								$checked = "";
								$imgCheck = "<img src='images/gray_x.png'>";
							}
							else
							{
								$checked = " CHECKED";
								$imgCheck = "<img src='images/grn_check.png'>";
							}
							$template -> assign_block_vars("agent_view.artist", array(
								"ARTIST_ID" => $value->get_artist_id(),
								"ARTIST_NAME" => $value->get_artist_name(),
								"CHECKED" => $checked,
								"IMGCHECK" => $imgCheck));
							
							if($_GET['function'] == 'agentedit') 
							{
								$template -> assign_block_vars("agent_view.artist.edit", array());
							}
							else
							{
								$template -> assign_block_vars("agent_view.artist.view", array());
							}
						}
						if($_GET['function'] == 'agentedit') 
						{
							$template -> assign_block_vars("agent_view.edit", array());
							$nav .= '&nbsp;&gt;&nbsp;<a href="index.php?action=agent&function=agentview&agent_id='.$_GET['agent_id'].'">Agent View</a>';
						}
						else
						{
							$template -> assign_block_vars("agent_view.view", array());
						}
					}
				}
			}
			$template -> assign_var_from_handle('AGENT_ADMIN_PAGE', 'agentadminpage');
		}
		/******************************************************/
		/*  User Del(Deactivate)
		/******************************************************/
		elseif($_GET['function'] == 'agentdel')
		{
			if($_GET['agent_id'] == '')
			{
				$template -> assign_block_vars("page_error",array());
				$template -> assign_block_vars("page_error.error_msg", array(
					"ERROR_MSG" => "No agent provided.  Please try again."));
			}
			else
			{
				$agentObj = new AgentObj();
				$agentObj->set_agent_id($_GET['agent_id']);
				$agentMsg = $agentObj->delAgent();
				$template -> assign_block_vars("agent_msg", array(
					"AGENT_MSG" => $agentMsg));
			}
			$template -> assign_var_from_handle('AGENT_ADMIN_PAGE', 'agentadminpage');
		}
	} 
	else
	{
		$template -> assign_block_vars("page_error",array());
		$template -> assign_block_vars("page_error.permission", array());
	}
?>