<?php 


 
configModule("document","Documents")
->add("head_left","Entête Gauche","textarea")
->add("head_right","Entête Droite","textarea")
->add("pied","Pied","textarea")
;


configModule("remiseticket","Remise Ticket")
->addInput("remise","Remise","{{_data_.remise.valeur}}",function(){
	?>
	<select class="form-control avs" ng-model="_data_.remise.valeur">
		<option>Valeurs de remise fix</option>
		<option>Définir un interval de remise</option>
	</select>
	<?php
})
->addInput("valeurremise","Valeurs de Remise","",function(){
	?>
		<table class="table table-bordered" style="background:#efefef;width:100%">
			<tr>
				<th>%</th>
				<th style="width:10px;"></th>
			</tr>
			<tr ng-repeat="elm in _data_.valeurremise">
				<td style="padding:2px;"><input style="height:30px;text-align: right;" type="number" class="form-control" ng-model="elm.val" placeholder="% Remise"></td>
				<td style="padding:2px;"><button class="btn btn-sm" ng-click="_data_.valeurremise.splice($index,1)"><span class="glyphicon glyphicon-trash"></span></button></td>
			</tr>
			<tr>
				<td colspan="2">
					<center>
						<button class="btn btn-xs btn-primary" ng-click="_data_.valeurremise=_data_.valeurremise||[];_data_.valeurremise.push({val:0})">Ajouter</button>
					</center>
				</td>
			</tr>
		</table>
	<?php
})
->addInput("intervalremise","Interval de Remise","de {{_data_.intervalremise.min}}% - à {{_data_.intervalremise.max}}% ",function(){
	?>
	<div style="display: flex;">
		<div class="form-group avs" style="margin-right: 5px;">
			<label style="display:block" class="control-label">% Minimum</label>
			<input type="number" style="text-align: right;" class="form-control avs" ng-model="_data_.intervalremise.min" placeholder="Minimum">
		</div>
		<div class="form-group avs">
			<label style="display:block" class="control-label">% Maximum</label>
			<input type="number" style="text-align: right;" class="form-control avs" ng-model="_data_.intervalremise.max" placeholder="Maximum">
		</div>
	</div>
	<?php
})
;



configModule("timbre","Timbre Fiscal")
->add("prix","Prix Timbre Fiscal","number")
;

configModule("smsing","SMSing")
->add("expediteur","Expéditeur","text")
->add("message","Message","textarea")
;




function configModule($name,$titre){
	return configModule::get($name,$titre);
} 
class configModule{
	static $list = [];
	var $name = "";
	var $titre = "";
	function __construct($name,$titre){
		$this->name = $name;
		$this->titre = $titre;
		self::$list[$name] = $this;
	}	
	static function get($name,$titre){
		if(isset(self::$list[$name])) return self::$list[$name];
		return new self($name,$titre);
	}
	var $configs = [];
	function add($name,$titre,$type,$args=[],$default = null){
		$args["as"] = "add";
		$args["type"] = $type;
		$args["titre"] = $titre;
		$args["default"] = $default;
		$this->configs[$name] = $args;
		return $this;
	}
	function addInput($name,$titre,$showtpl,$inputtpl){
		if(is_callable($showtpl)){
			ob_start();
			$showtpl();
			$showtpl=ob_get_clean();
		}
		if(is_callable($inputtpl)){
			ob_start();
			$inputtpl();
			$inputtpl=ob_get_clean();
		}
		$args["as"] = "addInput";
		$args["inputtpl"] = $inputtpl;
		$args["showtpl"] = $showtpl;
		$args["titre"] = $titre;
		$this->configs[$name] = $args;
		return $this;
	}
	function getHtml($varname,$widgetname){
		ob_start();
		?>
	<rowconfig store-widget="<?= $widgetname ?>"  ng-init="<?= $varname ?>.<?= $this->name ?> = <?= $varname ?>.<?= $this->name ?> || {}">
		<titre>
			<div style="display: flex;align-items: flex-start;">
				<span class="glyphicon glyphicon-menu-right" style="margin-top: 2px;font-weight: normal !important;color:#aaa;"></span>
				<div  style="flex:1;margin-left: 10px;">
					<?= $this->titre ?>
				</div>
			</div>
		</titre>
		<edit>
		<div style="background: #e4e4e4;">
			<md-switch ng-model="<?= $varname ?>.<?= $this->name ?>.active" aria-label="active" style="margin: 0;padding: 22px 12px;">
			 <?= $this->titre ?>
			</md-switch>
		</div>
		<div ng-show="<?= $varname ?>.<?= $this->name ?>.active">
<?php
		foreach ($this->configs as $name => $args) {
			if ($args["as"] == "add") {
				switch ($args["type"]) {
					case 'select':
					?>
					<rowconfig-input type="select" options="<?= json_encode($args["options"]) ?>" ng-model="<?= $varname ?>.<?= $this->name ?>.<?= $name ?>"><?= $args["titre"] ?></rowconfig-input>
					<?php
					break;
					case 'textarea':
					?>
					<rowconfig-input type="textarea" options="<?= json_encode($args["options"]) ?>" ng-model="<?= $varname ?>.<?= $this->name ?>.<?= $name ?>"><?= $args["titre"] ?></rowconfig-input>
					<?php
					break;
					default:
						?>
						<rowconfig-input ng-model="<?= $varname ?>.<?= $this->name ?>.<?= $name ?>"><?= $args["titre"] ?></rowconfig-input>
						<?php
					break;
				}
			}elseif($args["as"] == "addInput"){
?>	
				<rowconfig>
					<titre>
						<div style="display: flex;align-items: flex-start;">
							<span class="glyphicon glyphicon-menu-right" style="margin-top: 2px;font-weight: normal !important;color:#aaa;"></span>
							<div style="flex:1;margin-left: 10px;">
								<?= $args["titre"] ?>
							</div>
						</div>
					</titre>
					<edit>
						<?= $args["inputtpl"] ?>
					</edit>
					<show>
					 	<?= $args["showtpl"] ?>
					</show>
				</rowconfig>
<?php
			}
		}
?>
		</div>
		</edit>
		<show>
			<label class="label label-success" ng-show="<?= $varname ?>.<?= $this->name ?>.active">Activé</label>
			<label class="label label-danger" ng-hide="<?= $varname ?>.<?= $this->name ?>.active">Desactivé</label>
		</show>
	</rowconfig>
<?php
		$code=ob_get_clean();
		echo preg_replace("/_data_/i", "$varname.$this->name", $code);
	}
	static function getAllHtml($varname,$widgetname){
		foreach (self::$list as $elm) {
			$elm->getHtml($varname,$widgetname);
		}
	}
}