<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/SystemArticle.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/MyUser.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Job.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/JobQuote.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Constants.inc.php" );

	class QuoteOfJobHandler extends Widget {

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
		}

		protected function checkRights() {
			$retval = array();
			$this->model_myuser = new MyUserModel();
			$this->model_job = new JobModel();
			$this->model_jobquote = new JobQuoteModel();
			$this->user_id = $this->model_myuser->getLoggedinUserId();
			// nincs is bejelentkezve
			if (!$this->user_id) {
				$retval["error"] = __("globalerror_User_has_to_login_to_reach_functionality" );
				return $retval;
			}
			// nincs job_quote_id
			$this->job_quote_id = $this->aP["job_quote_id"];
			if (!$this->job_quote_id) {
				$retval["error"] = __("globalerror_bad_request");
				return $retval;
			}
			$this->job_quote = $this->model_jobquote->getJobQuoteFromJobQuoteId( $this->job_quote_id );
			// nincs job
			$this->job_id = $this->job_quote["job_id"];
			if (!$this->job_id) {
				$retval["error"] = __("globalerror_bad_request");
				return $retval;
			}
			$this->job = $this->model_job->getJobFromId( $this->job_id , true );
			// nem az ove a job
			if (!$this->model_job->isJobOwnedByUser( $this->job_id , $this->user_id , $this->job )) {
				$retval["error"] = __("qojpage_Job_not_owned_by_user");
				return $retval;
			}
			// barmit csinalhat vele
			return $retval;
		}
		
		protected function renderDialog( $job_quote_id , $type ) {
			$sa = new SystemArticle( "system_article" , $this , "sys_".$type );
			$sa->setOnlyBody( true );

			$cm = new ConstantsModel( $sa , $GLOBALS["language"] );
			$cm->addSystemParametersToTemplate();
			$cm->addJobQuoteConstantsToTemplate( $this->job_quote_id , "job_quote" , $this->job_quote );
			$cm->addJobConstantsToTemplate( $this->job_id , "job" , $this->job );

			return $sa->render();
		}

		protected function renderDialogButtons( $job_quote_id , $type ) {
			switch ($type) {
			case "dlg_accept_job_quote":
				return array(
					__("Cancel") => false,
					__("qojpage_Accept_job_quote") =>
						array(	"ajaxevent" => "accept_job_quote",
								"job_quote_id" => $job_quote_id )
				);
			case "dlg_decline_job_quote":
				return array(
					__("Cancel") => false,
					__("qojpage_Decline_job_quote") =>
						array(	"ajaxevent" => "decline_job_quote",
								"job_quote_id" => $job_quote_id )
				);
			}
		}

		public function handleEvents() {
			if (!isset( $this->aP["inajax"] )) return parent::handleEvents();
			if (!isset( $this->aP["ajaxevent"] )) return parent::handleEvents();
			if (!isset( $this->aP["job_quote_id"] )) return parent::handleEvents();

			$retval = array();
			switch ($this->aP["ajaxevent"]) {
			case "accept_job_quote":
			case "decline_job_quote":
				$retval = $this->checkRights();
				if (isset( $retval["error"] )) break;	// nem sikerult a mutatvany
				// tenyleges submit tortenik
				$accept = ($this->aP["ajaxevent"]=="accept_job_quote");
				$comment = $this->aP["jqcomment"];
				$this->model_jobquote->acceptJobQuote( $this->job_quote_id , $accept , $comment );
				// elkuldjuk a refresht
				$retval["evaluate"]="document.location.href=document.location.href;";
				// $retval["evaluate"]=sprintf( "alert( '%s(%d)' );",$this->aP["ajaxevent"] , $this->aP["job_quote_id"] );
				break;	
			case "dlg_accept_job_quote":
			case "dlg_decline_job_quote":
				$retval = $this->checkRights();
				if (isset( $retval["error"] )) break;	// nem sikerult a mutatvany
				// megkrealjuk a dialogot + buttonokat
				$retval["dlg"]=$this->renderDialog( $this->job_quote_id , $this->aP["ajaxevent"] );
				$retval["buttons"]=$this->renderDialogButtons( $this->job_quote_id , $this->aP["ajaxevent"] );
				switch ($this->aP["ajaxevent"]) {
				case "dlg_accept_job_quote":
					$retval["dlg_ttl"]=__("qojpage_Accept_job_quote");
					break;
				case "dlg_decline_job_quote":
					$retval["dlg_ttl"]=__("qojpage_Decline_job_quote");
				}
				break;
			default:
				return parent::handleEvents();
			}
			FNTH_echo_json_response( $retval );
			fo3_exit();
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			return parent::render();
		}

	}


?>
