<?php

/**
 *
 * @author Peter Csubak (chuby)
 * @copyright Peter Csubak (chuby)
 *
 * //Annotations:
 * @AjaxStartPoint
 */
class CustomerMain {

	private $custSF;

	/**
	 *
	 * @return void
	 * @AjaxMethod
	 */
	public static function ajaxMain(){
		$sh = SessionPeaHandler::getInstance();
		$sid = $sh->startSession("ajax", "ajax");
		$loginBtn = new PDiv("Bejelentkezés", "loginbtn", "menuelement", "", "loginlock");
		$loginBtn->activateEvents(PDiv::$evtOnClick);
		$loginBtn->bind("menu");
		$sh->addComponent($loginBtn);
		$dvdListBtn = new PDiv("Dvd-k listázása", "dvdlistbtn", "menuelement", "", "dvdlistlock");
		$dvdListBtn->activateEvents(PDiv::$evtOnClick);
		$dvdListBtn->bind("menu");
		$sh->addComponent($dvdListBtn);
		$ordersStateBtn = new PDiv("Megrendelt Dvd-k állapota", "ordstatebtn", "menuelement", "", "ordstlock");
		$ordersStateBtn->activateEvents(PDiv::$evtOnClick);
		$ordersStateBtn->bind("menu");
		$sh->addComponent($ordersStateBtn);
		$registrateBtn = new PDiv("Regisztráció", "regbtn", "menuelement", "", "reglock");
		$registrateBtn->activateEvents(PDiv::$evtOnClick);
		$registrateBtn->bind("menu");
		$sh->addComponent($registrateBtn);
		$cust = new CustomerMain();
		$sh->addTriggeredObject($cust);
		EventHandler::sendEvent(new AjaxExportableEvent($loginBtn));
		EventHandler::sendEvent(new AjaxExportableEvent($dvdListBtn, "append"));
		EventHandler::sendEvent(new AjaxExportableEvent($ordersStateBtn, "append"));
		EventHandler::sendEvent(new AjaxExportableEvent($registrateBtn, "append"));
		$sh->pauseSession($sid);
	}

	/**
	 *
	 * @param unknown_type $event
	 * @return unknown_type
	 *
	 * @Trigger(eventType="AjaxOnClick", senderType="PDiv", key="loginlock")
	 */
	public function loginClick($event){
		$loginform = new PForm("", "loginfrm", "loginformcss", "loginfrmlock");
		$userTF = new PTextField("uname", "", 40);
		$passTF = new PTextField("passwd", "", 40);
		$btn = new PButton("Bejelentkezés");
		$btn->type = "submit";
		$content = array(new PDiv("Felhasználói név:"), $userTF, new PDiv("Jelszó:"), $passTF, new PDiv(""), $btn);
		$loginform->content = $content;
		$loginform->activateEvents(PForm::$evtOnSend);
		$loginform->bind("main");
		SessionPeaHandler::getInstance()->addComponent($loginform);
		EventHandler::sendEvent(new AjaxExportableEvent($loginform));
	}

	public function setListDvds(){
		$where = new Where("OrderId", "=", null);
		$dvds = PersistenceHandler::getInstance()->selectEntities("Dvd", $where);
		$dvdlist = new PEntityList("95%");
		$dvdlist->evenRowsCSSClass = "even";
		$dvdlist->oddRowsCSSClass = "odd";
		$dvdlist->headerCSSClass = "thead";
		$dvdlist->setEntities($dvds);
		if (isset($this->custSF)){
			$dvdlist->visibleFields = array("Cím" => "title", "Kategória" => "category",
											"Rendelési ár" => "rentalPrice",
											"Kiadás éve" => "releaseDate",
											"Szereplők" => "staring", "Megrendelés" => "order");
		}else{
			$dvdlist->visibleFields = array("Cím" => "title", "Kategória" => "category",
											"Rendelési ár" => "rentalPrice",
											"Kiadás éve" => "releaseDate",
											"Szereplők" => "staring");
		}
		$dvdlist->bind("main");
		$dvdlist->activateEvents(array(PEntityList::$evtOnHeaderClick, PEntityList::$evtOnFieldClick));
		$dvdlist->sessionPH = SessionPeaHandler::getInstance();

		SessionPeaHandler::getInstance()->addComponent($dvdlist);
		EventHandler::sendEvent(new AjaxExportableEvent($dvdlist));

		if (isset($this->custSF)){
			$ordrBtn = new PDiv(" ", "orderbtn", "basebtn", "", "orderbtnlock");
			$ordrBtn->activateEvents(PDiv::$evtOnClick);
			$ordrBtn->bind("main");
			SessionPeaHandler::getInstance()->addComponent($ordrBtn);
			EventHandler::sendEvent(new AjaxExportableEvent($ordrBtn, "append"));
			if (SessionPeaHandler::getInstance()->getComponent("cartbtn") == null){
				$cartBtn = new PDiv("Kosár(".count($this->custSF->getCart()).") a kosár ürítéséhez kattintson ide.", "cartbtn", "cartbtn", "", "cartbtnlock");
				$cartBtn->activateEvents(PDiv::$evtOnClick);
				$cartBtn->bind("top");
				SessionPeaHandler::getInstance()->addComponent($cartBtn);
				EventHandler::sendEvent(new AjaxExportableEvent($cartBtn, "append"));
			}
		}
	}

	/**
	 *
	 * @param unknown_type $event
	 * @return unknown_type
	 *
	 * @Trigger(eventType="AjaxOnClick", senderType="PDiv", key="orderbtnlock")
	 */
	public function onOrderBtnClick($event){
		if (isset($this->custSF)){
			$this->custSF->makeOrder();
			$div = new PDiv("<h1>Sikeres megrendelés.</h1>");
			$div->bind("main");
			$cartBtn = SessionPeaHandler::getInstance()->getComponent("cartbtn");
			$cartBtn->content = "Kosár(".count($this->custSF->getCart()).") a kosár ürítéséhez kattintson ide.";
			EventHandler::sendEvent(new AjaxExportableEvent($cartBtn, "refresh"));
			EventHandler::sendEvent(new AjaxExportableEvent($div));
		}else{
			throw new Exception("A rendeléshez előbb be kell lépni.");
		}
	}


	/**
	 *
	 * @return unknown_type
	 *
	 * @Trigger(eventType="OnEntityListFieldClick", senderType="PEntityList")
	 */
	public function onFieldClick($event){
		if (($event->fieldName == "order") && (isset($this->custSF))){
			$this->custSF->addToCart($event->entity);
			if (SessionPeaHandler::getInstance()->getComponent("cartbtn") == null){
				$ordrBtn = new PDiv(" ", "orderbtn", "basebtn", "", "orderbtnlock");
				$ordrBtn->activateEvents(PDiv::$evtOnClick);
				$ordrBtn->bind("main");
				SessionPeaHandler::getInstance()->addComponent($ordrBtn);
				EventHandler::sendEvent(new AjaxExportableEvent($ordrBtn, "append"));
				$cartBtn = new PDiv("Kosár(".count($this->custSF->getCart()).") a kosár ürítéséhez kattintson ide.", "cartbtn", "cartbtn", "", "cartbtnlock");
				$cartBtn->activateEvents(PDiv::$evtOnClick);
				$cartBtn->bind("top");
				SessionPeaHandler::getInstance()->addComponent($cartBtn);
				EventHandler::sendEvent(new AjaxExportableEvent($cartBtn, "append"));
			}else{
				$cartBtn = SessionPeaHandler::getInstance()->getComponent("cartbtn");
				$cartBtn->content = "Kosár(".count($this->custSF->getCart()).") a kosár ürítéséhez kattintson ide.";
				EventHandler::sendEvent(new AjaxExportableEvent($cartBtn, "refresh"));
				if (SessionPeaHandler::getInstance()->getComponent("orderbtn") == null){
					$ordrBtn = new PDiv(" ", "orderbtn", "basebtn", "", "orderbtnlock");
					$ordrBtn->activateEvents(PDiv::$evtOnClick);
					$ordrBtn->bind("main");
					SessionPeaHandler::getInstance()->addComponent($ordrBtn);
					EventHandler::sendEvent(new AjaxExportableEvent($ordrBtn, "append"));
				}else{
					$orderBtn = SessionPeaHandler::getInstance()->getComponent("orderbtn");
					EventHandler::sendEvent(new AjaxExportableEvent($orderBtn, "refresh"));
				}
			}
		}
		//kosár mező frissítése
	}
	
	/**
	 * 
	 * @param unknown_type $event
	 * @return unknown_type
	 * @Trigger(eventType="AjaxOnClick", senderType="PDiv", key="cartbtnlock")
	 */
	public function onCartClick($event){
		if (isset($this->custSF)){
			$this->custSF->clearCart();
			$cartBtn = SessionPeaHandler::getInstance()->getComponent("cartbtn");
			$cartBtn->content = "Kosár(".count($this->custSF->getCart()).") a kosár ürítéséhez kattintson ide.";
			EventHandler::sendEvent(new AjaxExportableEvent($cartBtn, "refresh"));
		}
	}

	/**
	 *
	 * @param AjaxOnFormSend $event
	 * @return void
	 *
	 * @Trigger(eventType="AjaxOnFormSend", senderType="PForm", key="loginfrmlock")
	 */
	public function loginSend($event){
		$username = $event->formData["uname"];
		$passwd = $event->formData["passwd"];
		$this->custSF = SessionPeaHandler::getInstance()->createInstance("CustomerSF", true);
		$this->custSF->login(md5($username.$passwd));
		$this->setListDvds();
	}

	/**
	 *
	 * @param unknown_type $event
	 * @return unknown_type
	 *
	 * @Trigger(eventType="AjaxOnClick", senderType="PDiv", key="dvdlistlock")
	 */
	public function dvdListBtnClick($event){
		$this->setListDvds();
	}

	/**
	 *
	 * @param unknown_type $event
	 * @return unknown_type
	 *
	 * @Trigger(eventType="AjaxOnClick", senderType="PDiv", key="ordstlock")
	 */
	public function ordersStateBtnClick($event){
		$ordlist = new PEntityList("95%");
		$ordlist->evenRowsCSSClass = "even";
		$ordlist->oddRowsCSSClass = "odd";
		$ordlist->headerCSSClass = "thead";
		if (!isset($this->custSF)){
			throw new Exception("Ehhez a funkcióhoz előbb, be kell jelentkezni.");
		}
		$ordlist->setEntities($this->custSF->getMyOrdersList());
		$ordlist->visibleFields = array("Rendelés dátuma" => "orderTime", "Sátusz" => "status",
										"Rendelt DVD-k" => "orderedDvds");
		$ordlist->translatorArray = array("after" => "Teljesítve", "before" => "Szállítás előtt", 
										  "in" => "Szállítás folyamatban", "need" => "Visszahívott");
		$ordlist->translatableFields = array("status");
		$ordlist->bind("main");
		$ordlist->activateEvents(array(PEntityList::$evtOnHeaderClick, PEntityList::$evtOnFieldClick));
		$ordlist->sessionPH = SessionPeaHandler::getInstance();
		SessionPeaHandler::getInstance()->addComponent($ordlist);
		EventHandler::sendEvent(new AjaxExportableEvent($ordlist));
	}

	/**
	 *
	 * @param unknown_type $event
	 * @return unknown_type
	 *
	 * @Trigger(eventType="AjaxOnClick", senderType="PDiv", key="reglock")
	 */
	public function regClick($event){
		$regFrom = new PForm("", "regform", "regformcss", "regfrmlock");
		//Kénytelen leszek minden mezőhöz külön csinálni egy egy editor mezőt, mert a view-os dolog még nem müxik :
		$nameTF = new PTextField("name", "", 40);
		$addressTF = new PTextField("address", "", 40);
		$phoneTF = new PTextField("phone", "", 40);
		$usernameTF = new PTextField("username", "", 40);
		$passwdTF = new PTextField("passwd", "", 40);
		$passwdagainTF = new PTextField("passwdagain", "", 40);
		$btn = new PButton("Regisztráció");
		$btn->type = "submit";
		$content = array(new PDiv("Név:"), $nameTF, new PDiv("Cím:"), $addressTF,
		new PDiv("Telefonszám:"), $phoneTF, new PDiv("Felhasználói név:"),
		$usernameTF, new PDiv("Jelszó:"), $passwdTF, 
		new PDiv("Jelszó mégegyszer:"), $passwdagainTF, new PDiv(""), $btn);
		$regFrom->content = $content;
		$regFrom->activateEvents(PForm::$evtOnSend);
		$regFrom->bind("main");
		SessionPeaHandler::getInstance()->addComponent($regFrom);
		EventHandler::sendEvent(new AjaxExportableEvent($regFrom));
	}

	/**
	 *
	 * @param unknown_type $event
	 * @return unknown_type
	 *
	 * @Trigger(eventType="AjaxOnFormSend", senderType="PForm", key="regfrmlock")
	 */
	public function regSendForm($event){
		$name = $event->formData["name"];
		$address = $event->formData["address"];
		$phone = $event->formData["phone"];
		$username = $event->formData["username"];
		$passwd = $event->formData["passwd"];
		$passwdagain = $event->formData["passwdagain"];

		if (isset($name) && isset($address) && isset($phone) && isset($username) && isset($passwd) &&
		isset($passwdagain)){
			if ($passwd == $passwdagain){
				$this->custSF = SessionPeaHandler::getInstance()->createInstance("CustomerSF", true);
				$this->custSF->registrate($name, $address, $phone, $username, $passwd);
				$div = new PDiv("<h1>Sikeres regisztráció.</h1>");
				$div->bind("main");
				EventHandler::sendEvent(new AjaxExportableEvent($div));
			}else{
				throw new Exception("A két jelszó mezőnek meg kell egyeznie.");
			}
		}else{
			throw new Exception("Minden mezőt ki kell tölteni.");
		}
	}

}
?>