<?php
	require_once( $GLOBALS["site_root"]."/includes/pages/BackendPopup.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTextbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTextarea.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminCheckbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminRadio.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSubmit.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminDatetime.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminJobRecipients.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminJobQuotes.inc.php" );
	

	require_once( $GLOBALS["site_root"]."/includes/widgets/MultiLangPairSelector.inc.php" );

	require_once( $GLOBALS["site_root"]."/includes/models/Job.inc.php" );
		

	class AdminJobEditPage extends BackendPopup {
		protected $jm;

		protected $job_version = "3.0";
		protected $job_title;
		protected $poster_name;
		protected $poster_email;
		protected $poster_phone;
		protected $poster_cellular;

		protected $job_quote_deadline;
		protected $job_deadline;
		protected $job_description;
		protected $job_mainaspects;
		protected $job_recipients;

		protected $job_languagepairs;
		protected $npublic_comment;
		protected $job_submit;

		public function __construct( $id = "AdminJobEditPage" , $parent = null ) {
			parent::__construct( $id , $parent );
			if (!AdminUserModel::isAdminLoggedIn()) return;
			$this->jm = new JobModel();

			$this->job_title = new AdminTextbox( "job_title" , $this );
			$this->ctstamp = new AdminDatetime( "ctstamp" , $this );
			$this->job_languagepairs = new MultiLangPairSelector( "job_languagepairs" , $this );
			$this->job_description = new AdminTextarea( "job_description" , $this );
			$this->npublic_comment = new AdminTextarea( "npublic_comment" , $this );

			$this->revoked_at = new AdminDatetime( "revoked_at" , $this );
			$this->closed_at = new AdminDatetime( "closed_at" , $this );

			$this->poster_acknowledged = new AdminCheckbox( "poster_acknowledged" , $this );
			$this->admin_acknowledged = new AdminCheckbox( "admin_acknowledged" , $this );
			$this->is_proofread = new AdminCheckbox( "is_proofread" , $this );

			$this->job_recipients = new AdminJobRecipients( "job_recipients" , $this );
			$this->job_quotes = new AdminJobQuotes( "job_quotes" , $this );

			$this->job_submit = new AdminSubmit( "job_submit" , $this );
		}

		public function modifyGetPost( &$g , &$p ) {
			if (!AdminUserModel::isAdminLoggedIn()) return parent::modifyGetPost( $g , $p );
			switch ($g["mode"]) {
			case "modify":
				$this->job_id = $g["uid"];
				$this->job = $this->jm->getJobFromId( $this->job_id , true );
				$this->job_version = $this->job["job_version"];
			}
			// common
			$this->job_title = new AdminTextbox( "job_title" , $this );
			$this->job_languagepairs = new MultiLangPairSelector( "job_languagepairs" , $this );
			$this->job_quote_deadline = new AdminDatetime( "job_quote_deadline" , $this );

			switch ($this->job_version) {
			case 2.0:
				break;
			case 3.0:
				$this->job_deadline = new AdminDatetime( "job_deadline" , $this );
				break;
			}
		}

		protected function initializeWidgets() {
			$lang = new JobModel();

			// common
			$this->job_title->setTitle( __("jobs_Title") );
			$this->job_title->setCSSAttribute( "width" , "700px" );
			$this->job_title->setShowTitle( true );

			$this->ctstamp->setTitle( __("jobspage_Posted_at" ) );
			$this->ctstamp->setShowTitle( true );

			$this->job_quote_deadline->setTitle( __("quotepage_Offer_quote_deadline") );
			$this->job_quote_deadline->setShowTitle( true );

			$this->revoked_at->setTitle( __("jobspage_Revoked_at" ) );
			$this->revoked_at->setShowTitle( true );
			
			$this->closed_at->setTitle( __("jobspage_Closed_at" ) );
			$this->closed_at->setShowTitle( true );

			$this->poster_acknowledged->setTitle( __("jobs_Acknowledged" ) );
			$this->poster_acknowledged->setShowTitle( true );
			
			$this->admin_acknowledged->setTitle( __("jobs_Admin_acknowledged" ) );
			$this->admin_acknowledged->setShowTitle( true );
			
			$this->is_proofread->setTitle( __("jobs_Proofread" ) );
			$this->is_proofread->setShowTitle( true );

			$this->job_description->setTitle( __("quotepage_Offer_description") );
			$this->job_description->setShowTitle( true );
			$this->job_description->setAttribute( "rows" , 4 );
			$this->job_description->setWidth( 560 );
			
			$this->npublic_comment->setTitle( __("quotepage_Not_public_comment") );
			$this->npublic_comment->setShowTitle( true );
			$this->npublic_comment->setAttribute( "rows" , 2 );
			$this->npublic_comment->setWidth( 560 );

			switch ($this->job_version) {
			case 2.0:
				break;
			case 3.0:
				$this->job_deadline->setTitle( __("quotepage_Offer_deadline") );
				$this->job_deadline->setShowTitle( true );
				break;
			}

			// submit gomb
			switch ($this->aG["mode"]) {
			case "new":
				$this->job_submit->setValue( __( "New","attr" ) );
				$this->setParam( "title" , __("jobs_New_job") );
				break;
			case "modify":
				$this->job_submit->setValue( __( "Modify","attr" ) );
				$title = sprintf( "#%d - %s" , $this->job_id , $this->job["job_title"] );
				$this->setParam( "title" , $title );
				break;
			case "delete":
				$this->job_submit->setValue( __( "Delete","attr" ) );
				$this->setParam( "title" , __("jobs_Delete_job" ) );
				break;
			}
			FNTHPage::setPageTitle( $this->getParam( "title" ) );
			// load datas
			switch ($this->aG["mode"]) {
			case "modify":
			case "delete":
				$this->loadDatas( $this->aG["uid"] );
				break;
			}
			$this->setParam( "job_version" , $this->job_version );
		}
		protected function prepareSelectedLanguages( $langs ) {
			FNTH_da( $langs );
		}

		protected function loadDatas( $job_id ) {
			$this->ctstamp->setValue( substr( $this->job["ctstamp"] , 0 , 19 ) );
			$this->job_title->setValue( $this->job["job_title"] );
			$this->job_languagepairs->setLanguagePairs( $this->job["job_languages"] );
			$this->job_quote_deadline->setValue( $this->job["job_quote_deadline"] );
			$this->job_description->setValue( $this->job["job_description"] );
			$this->npublic_comment->setValue( $this->job["npublic_comment"] );

			if (isset( $this->job["revoked_at"] )) $this->revoked_at->setValue( $this->job["revoked_at"] );
			if (isset( $this->job["closed_at"] )) $this->closed_at->setValue( $this->job["closed_at"] );

			if (isset( $this->job_recipients ))
				$this->job_recipients->setValue( $this->job["job_recipients"] );

			$this->poster_acknowledged->setChecked( $this->job["poster_acknowledged"]=="t" );
			$this->admin_acknowledged->setChecked( $this->job["admin_acknowledged"]=="t" );
			$this->is_proofread->setChecked( $this->job["is_proofread"]=="t" );

			switch ($this->job_version) {
			case 3.0:
				$this->job_deadline->setValue( $this->job["job_deadline"] );
				break;
			}
		}

		protected function saveDatas( $job_id = 0 ) {
			$jm = new JobModel();
			$datas = array();
			$datas["ctstamp"] = $this->ctstamp->getValue();
			if (strlen( $datas["ctstamp"] )==16) $datas["ctstamp"].=":00";
			$datas["job_title"] = $this->job_title->getValue();
			$datas["job_description"] = $this->job_description->getValue();
			$datas["npublic_comment"] = $this->npublic_comment->getValue();
			$datas["job_quote_deadline"] = $this->job_quote_deadline->getValue();

			$datas["revoked_at"]=$this->revoked_at->getValue();
			$datas["is_revoked"]=(strlen(trim($datas["revoked_at"])));
			$datas["closed_at"]=$this->closed_at->getValue();
			$datas["is_closed"]=(strlen(trim($datas["closed_at"])));

			$datas["poster_acknowledged"]=$this->poster_acknowledged->isChecked();
			$datas["admin_acknowledged"]=$this->admin_acknowledged->isChecked();
			$datas["is_proofread"]=$this->is_proofread->isChecked();
			$datas["job_recipients"]=$this->job_recipients->getValue();

			switch ($this->job_version) {
			case 3.0:
				$datas["job_deadline"] = $this->job_deadline->getValue();
			}
		
			if (!$job_id) {
				$job_id = $jm->addJob( $datas );
			} else {
				$jm->updateJob( $job_id , $datas );
			}
			$jm->setJobLanguages( $job_id , $this->job_languagepairs->getValue() );
		}

		protected function deleteDatas( $uid = 0 ) {
			$jm = new JobModel();
			$jm->deleteJob( $uid );
		}

		public function handleEvents() {
			if (!$this->isLoggedIn()) return parent::handleEvents();
			if (!isset( $this->aP[$this->job_submit->getId()] )) return parent::handleEvents();
			$this->refreshChildValuesFromArray();

			switch ($this->aG["mode"]) {
			case "new":$this->saveDatas( 0 );break;
			case "modify":$this->saveDatas( $this->aG["uid"] );break;
			case "delete":$this->deleteDatas( $this->aG["uid"] );break;
			}
			$this->closeAndRefreshParent();
		}

		public function render() {
			if (!$this->isLoggedIn()) return parent::render();
			$this->initializeWidgets();
			return parent::render();
		}
	}
?>
