<?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/AdminDate.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminNumeric.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminButton.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSubmit.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminCouponType.inc.php" );

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

	class AdminCouponCampaignEditPage extends BackendPopup {
		protected $cm;

		protected $cc_name;
		protected $cc_avail_from;
		protected $cc_avail_until;
		protected $cc_coupon_type;
		protected $cc_freedays;

		protected $cc_submit;

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

			$this->cc_name = new AdminTextbox( "cc_name" , $this );
			$this->cc_avail_from = new AdminDate( "cc_avail_from" , $this );
			$this->cc_avail_until = new AdminDate( "cc_avail_until" , $this );
			$this->cc_coupon_type = new AdminCouponType( "cc_coupon_type" , $this );
			$this->cc_freedays = new AdminNumeric( "cc_freedays" , $this );

			$this->cc_submit = new AdminSubmit( "cc_submit" , $this );
		}
		
		protected function initializeWidgets() {
			if (!AdminUserModel::isAdminLoggedIn()) return;
		
			$this->cc_name->setTitle( __("articles_Name") );
			$this->cc_name->setWidth( 310 );	// ( "width" , "350px" );
			$this->cc_name->setShowTitle( true );

			$this->cc_avail_from->setTitle( __("coupons_From" ) );
			$this->cc_avail_from->setShowTitle( true );

			$this->cc_avail_until->setTitle( __("coupons_Until" ) );
			$this->cc_avail_until->setShowTitle( true );

			$this->cc_coupon_type->setTitle( __("coupons_Type" ) );
			$this->cc_coupon_type->setShowTitle( true );

			$this->cc_freedays->setTitle( __("coupons_freedays") );
			$this->cc_freedays->setShowTitle( true );

			// submit gomb
			switch ($this->aG["mode"]) {
			case "new":
				$this->setParam("title" , __("coupons_New_coupon_campaign" ));
				$this->cc_submit->setValue( __( "New","attr" ) );
				break;
			case "modify":
				$this->setParam("title" , __("coupons_Modify_coupon_campaign" ));
				$this->cc_submit->setValue( __( "Modify","attr" ) );
				break;
			case "delete":
				$this->setParam("title" , __("coupons_Delete_coupon" ));
				$this->cc_submit->setValue( __( "Delete","attr" ) );
				break;
			}
			// load datas
			switch ($this->aG["mode"]) {
			case "modify":
			case "delete":
				$this->loadDatas( $this->aG["uid"] );
				break;
			}
		}
		
		public function modifyGetPost( &$g , &$p ) {
			parent::modifyGetPost( $g , $p );
			$this->initializeWidgets();
		}

		protected function validateForm() {
			$valid = true;
			$cc_coupon_type = $this->cc_coupon_type->getValue();
			if (!strlen( $cc_coupon_type )) {
				$valid = false;
				$this->warningMessage( "no_coupon_type" , __("coupons_No_coupon_type" ) );
			}

			// cc_name
			$cc_name = $this->cc_name->getValue();
			if (!strlen( trim( $cc_name ))) {
				$valid = false;
				$this->warningMessage( "no_cc_name" , __("coupons_No_coupon_campaign_name" ) );
			}
			return $valid;
		}


		protected function loadDatas( $uid ) {
			$datas = $this->cm->getCouponCampaignFromId( $uid );
			$this->cc_name->setValue( $datas["cc_name"] );
			$this->cc_coupon_type->setValue( $datas["cc_coupon_type"] );
			$this->cc_avail_from->setValue( $datas["cc_avail_from"] );
			$this->cc_avail_until->setValue( $datas["cc_avail_until"] );
			$this->cc_freedays->setValue( $datas["cc_freedays"] );
		}

		protected function saveDatas( $uid = 0 ) {
			$datas = array();
			$datas["cc_name"] = $this->cc_name->getValue();
			$datas["cc_coupon_type"] = $this->cc_coupon_type->getValue();
			$datas["cc_avail_from"]=$this->cc_avail_from->getValue();
			$datas["cc_avail_until"]=$this->cc_avail_until->getValue();
			$datas["cc_freedays"]=$this->cc_freedays->getValue();
			if (!$uid)
				$uid = $this->cm->addCouponCampaign( $datas );
			else
				$this->cm->updateCouponCampaign( $uid , $datas );
		}

		protected function deleteDatas( $uid = 0 ) {
			$this->cm->deleteCouponCampaign( $uid );
		}

		public function handleEvents() {
			if (!$this->isLoggedIn()) return parent::handleEvents();

			$this->cc_submit->refreshValueFromArray();
			if ($this->cc_submit->isSubmitted()) {
				$this->refreshChildValuesFromArray();
				$valid = $this->validateForm();
				if ($valid) {
					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();
				}
			}
			return parent::handleEvents();
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "coupons_No_coupon_name" );
		}

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