<?php
	require_once( $GLOBALS["site_root"] . "/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/ServiceTypes.inc.php" );

	class JobServices extends Widget {
		protected $aServiceTypes = array();
		protected $aServiceTypeUnits = array();
		protected $aServiceTypeUnitMinLengths = array();

		protected $aSelServiceTypes = array();

		public function clearServiceTypes() {
			$this->aServiceTypes = array();
		}

		public function clearSelectedServiceTypes() {
			$this->aSelServiceTypes = array();
		}

		public function refreshValueFromArray( $arr = null ) {
			if (!isset( $arr )) $arr = $this->aP;
			$qid = sprintf( "%s_q_" , $this->getId() );
			$uid = sprintf( "%s_unit_" , $this->getId() );
			$lid = sprintf( "%s_loc_" , $this->getId() );
			$a = array();
			foreach ($arr as $k => $v) {
				if (strpos($k,$qid)!==false) {
					$id = (int)str_replace( $qid , "" , $k );
					if (!isset( $a[$id] )) $a[$id]=array();
					$a[$id]["quantity"]=$v;
				}
				if (strpos($k,$uid)!==false) {
					$id = (int)str_replace( $uid , "" , $k );
					if (!isset( $a[$id] )) $a[$id]=array();
					$a[$id]["unit"]=$v;
				}
				if (strpos($k,$lid)!==false) {
					$id = (int)str_replace( $lid , "" , $k );
					if (!isset( $a[$id] )) $a[$id]=array();
					$a[$id]["loc"]=$v;
				}
			}
			foreach ($a as $k => $v) {
				if (!isset( $v["quantity"] )) $v["quantity"]=0;
				if (!strlen( trim( $v["quantity"] ))) $v["quantity"]=0;
				$a[$k]=$v;
			}
			$this->aSelServiceTypes = $a;
			return $this->aSelServiceTypes;
		}

		public function getValue() {
			return $this->aSelServiceTypes;
		}

		public function setServiceType( $servicetype_id , $servicetype_unit = "" , $servicetype_quantity = "" ) {
			$this->aSelServiceTypes[$servicetype_id]=array(
				"unit" => $servicetype_unit ,
				"quantity" => $servicetype_quantity );
		}

		public function unsetServiceType( $servicetype_id ) {
			if (!isset( $this->aSelServiceTypes[$servicetype_id] )) return;
			unset( $this->aSelServiceTypes[$servicetype_id] );
		}
		public function getSelectedServiceTypes() { return array_keys( $this->aSelServiceTypes ); }

		protected function prepareJavascript() {
			$js = "";
			$js.="aServiceTypes=".json_encode( $this->aServiceTypes ).";\n";
			$js.="aServiceTypeUnits=".json_encode( $this->aServiceTypeUnits ).";\n";
			$js.="aServiceTypeMinLengths=".json_encode( $this->aServiceTypeUnitMinLengths ).";\n";
			$js.="aXrefServiceTypeUnits=".json_encode( $this->aXrefServiceTypeUnits ).";\n";
			FNTHPage::setPageFullInit( "JobServices_js" , $js );
		}
		
		protected function generateLangDefs() {
			fo3_add_js_langdef( "Choose" );
			fo3_add_js_langdef( "quotepage_error_Service_size_too_small" );
			fo3_add_js_langdef( "quotepage_error_Service_size_too_small2" );
			fo3_add_js_langdef( "Minimal_service_size_needed" );
			fo3_add_js_langdef( "interpret_location" );
			fo3_add_js_langdef( "Location_empty" );
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$stm = new ServiceTypesModel();
			$this->aServiceTypes = $stm->getServiceTypeOptions( false );
			$this->aServiceTypeUnits = $stm->getServiceTypeUnitOptions( true );
			$this->aServiceTypeUnitMinLengths = $stm->getServiceTypeUnitMinimalLengths( true );
			$this->aXrefServiceTypeUnits = $stm->getServiceTypeUnits();
			$this->prepareJavascript();
			$this->generateLangDefs();

			FNTHPage::addUniquePageFullInit( sprintf( "InitJobServices('%s',%s);\n",
				$this->getId(),
				json_encode( $this->aSelServiceTypes ) ) );
			$this->setParam( "servicetypeunits" , $this->aServiceTypeUnits );
			$this->setParam( "servicetypes" , $this->aServiceTypes );
			$this->setParam( "xref_units" , $this->aXrefServiceTypeUnits );
			$this->setParam( "sel_servicetypes" , $this->aSelServiceTypes );
			return parent::render();
		}

	}
?>
