<?php
include_once(__DIR__."/../../core.php");
 
?><form action="" method="post" role="form" id="check">
	<input type="hidden" name="backto" value="record-edit">
	<input type="hidden" name="op" value="update">
	
	<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">
		<div class="clearfix"></div>
		</br>	
		<div class="col-md-4">				
		
		</div>
		<div class="col-md-4">				
			<div class="pull-right">								
				<div class="pull-left showcase sweet"> 		
				<button type="button" class="btn-app btn-danger" aria-label="Show SweetAlert2 success message" ><i class="fa fa-trash-o"></i> Elimina</span></button>		
				</div>				
			</div>	
		</div>
		
		<div class="col-md-4">
					
		</div>			
		<div class="clearfix"></div>
		
	</div>
	
	<div class="clearfix"></div>
	<!-- DATI -->
	<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">
		<div class="panel-heading">
			<h3 class="panel-title"><?php echo _("Dati") ?></h3>
		</div>
	
		<div class="panel-body">
			<div class="row">
				<div class="col-xs-12 col-md-9">
					{[ "type": "text", "label": "<?php echo _("Descrizione") ?>", "name": "descrizione", "required": 0, "class": "",  "value": "$descrizione$", "extra": "" ]}
				</div>
				<div class="col-xs-12 col-md-3">
					{[ "type": "select", "label": "Trascodifica XML", "name": "codice_modalita_pagamento_fe", "required": 0, "class": "", "values": "query=SELECT codice as id , descrizione as descrizione FROM fte_modalitapagamento", "value": "$codice_modalita_pagamento_fe$", "extra": "" ]}
				</div>
			</div>
		</div>
	</div>

	<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">
		<div class="panel-heading">
			<h3 class="panel-title"><?php echo _("Rate") ?></h3>
		</div>
	
		<div class="panel-body">
			<div class="data">
<?php
$values = "";
for($i=1; $i<=31; $i++){
	$values .='\"'.$i.'\": \"'.$i.'\"';
	if($i!=31) $values .=",";
}
$results = $dbo->fetchArray("SELECT * FROM `co_pagamenti` WHERE descrizione=".prepare($records[0]["descrizione"])." ORDER BY `num_giorni` ASC");
$cont = 1;
foreach($results as $result){
	echo '
				<div class="box box-success">
					<div class="box-header with-border">
						<h3 class="box-title">'.str_replace("_NUMBER_", $cont, _("Rata _NUMBER_")).'</h3>
						<a class=" btn-app btn-danger pull-right" onclick="';
	echo "if(confirm('"._("Eliminare questo elemento?")."')){ location.href='".$rootdir."/editor.php?id_module=".$id_module."&id_record=".$id_record."&op=delete_rata&id=" . $result['id'] . "'; }";
	echo '"><i class="fa fa-trash"></i> '._("Elimina").'</a>
					</div>
					<div class="box-body">
						<input type="hidden" value="'.$result['id'].'" name="id[]">
						<div class="row">
							<div class="col-xs-12 col-md-3">
								{[ "type": "text", "label": "'._("Percentuale").'", "name": "percentuale[]", "required": 0, "class": "inputmask-decimal", "value": "'.$result["prc"].'", "extra": "", "icon-after": "<i class=\"fa fa-percent\"></i>" ]}
							</div>

							<div class="col-xs-12 col-md-3">
								{[ "type": "select", "label": "'._("Scadenza").'", "name": "scadenza[]", "required": 0, "class": "", "values": "list=\"-1\":\"'._("Fine mese").'\",\"0\":\"'._("Distanza in giorni esatta").'\",\"1\":\"'._("Definisci giorno").'\"", "value": "';
	if($result["giorno"] == 0 || $result["giorno"] == -1) echo $result["giorno"];
	else echo 1;
	echo '", "extra": "" ]}
							</div>

							<div class="col-xs-12 col-md-3">
								{[ "type": "select", "label": "'._("Giorno").'", "name": "giorno[]", "required": 0, "class": "", "values": "list='.$values.'", "value": "';
	if($result["giorno"] != 0 && $result["giorno"] != -1) echo $result["giorno"];
	echo '", "extra": "';
	if($result["giorno"] == 0 || $result["giorno"] == -1) echo ' disabled';
	echo '" ]}
							</div>

							<div class="col-xs-12 col-md-3">
								{[ "type": "text", "label": "'._("Distanza in giorni").'", "name": "distanza[]", "required": 0, "class": "inputmask-integer", "value": "'.$result["num_giorni"].'", "extra": "" ]}
							</div>
						</div>
					</div>
				</div>';
	$cont++;
}
?>
			</div>
			<div class="pull-right">
				<button type="button" class="btn-app btn-primary" id="add"><i class="fa fa-plus"></i> <?php echo _("Aggiungi"); ?></button>
				<button type="submit" class="btn-app btn-success"><i class="fa fa-check"></i> <?php echo _("Salva"); ?></button>
			</div>
		</div>
	</div>

</form>

<div class="box box-warning box-solid text-center hide" id="wait">
	<div class="box-header with-border">
		<h3 class="box-title"><i class="fa fa-warning"></i> <?php echo _("Attenzione!"); ?></h3>
	</div>
	<div class="box-body">
		<p><?php echo _("Prima di poter continuare con il salvataggio è necessario che i valori percentuali raggiungano in totale il 100%"); ?>.</p>
	</div>
</div>

<form action="" method="post" role="form" id="form-delete">
	<input type="hidden" name="backto" value="record-list">
	<input type="hidden" name="op" value="delete">
</form>

<script>
$(document).ready(function(){
	$(document).on('click', '#add', function(){
		$(this).parent().parent().find('.data').append(
				'		<div class="box">' +
				'			<div class="box-header with-border">' +
				'				<h3 class="box-title"><?php echo _("Nuova rata") ?></h3>' +
				'			</div>' +
				'			<div class="box-body">' +
				'				<input type="hidden" value="" name="id[]">' +
				'				<div class="row">' +
				'					<div class="col-xs-12 col-md-3">' +
				'						<div class="form-group">' +
				'							<label for="percentuale"><?php echo _("Percentuale") ?></label>' +
				'							<div class="input-group">' +
				'								<input  type="text" class="form-control inputmask-decimal" name="percentuale[]" id="percentuale" value="0"> <span class="input-group-addon no-padding"><i class="fa fa-percent"></i></span>' +
				'							</div>' +
				'						</div>' +
				'					</div>' +
				'					<div class="col-xs-12 col-md-3">' +
				'						<div class="form-group">' +
				'							<label for="scadenza"><?php echo _("Scadenza") ?></label>' +
				'							<select class="form-control superselect " name="scadenza[]" id="scadenza" data-placeholder="- Seleziona un"opzione -">' +
				'								<option value="-1"><?php echo _("Fine mese") ?></option>' +
				'								<option value="0"><?php echo _("Distanza in giorni esatta") ?></option>' +
				'								<option value="1"><?php echo _("Definisci giorno") ?></option>' +
				'							</select>' +
				'						</div>' +
				'					</div>' +
				'					<div class="col-xs-12 col-md-3">' +
				'						<div class="form-group">' +
				'							<label for="giorno"><?php echo _("Giorno") ?></label>' +
				'							<select class="form-control superselect " name="giorno[]" id="giorno"  disabled  data-placeholder="- Seleziona un"opzione -" >' +
				'								<option value="1">1</option>' +
				'								<option value="2">2</option>' +
				'								<option value="3">3</option>' +
				'								<option value="4">4</option>' +
				'								<option value="5">5</option>' +
				'								<option value="6">6</option>' +
				'								<option value="7">7</option>' +
				'								<option value="8">8</option>' +
				'								<option value="9">9</option>' +
				'								<option value="10">10</option>' +
				'								<option value="11">11</option>' +
				'								<option value="12">12</option>' +
				'								<option value="13">13</option>' +
				'								<option value="14">14</option>' +
				'								<option value="15">15</option>' +
				'								<option value="16">16</option>' +
				'								<option value="17">17</option>' +
				'								<option value="18">18</option>' +
				'								<option value="19">19</option>' +
				'								<option value="20">20</option>' +
				'								<option value="21">21</option>' +
				'								<option value="22">22</option>' +
				'								<option value="23">23</option>' +
				'								<option value="24">24</option>' +
				'								<option value="25">25</option>' +
				'								<option value="26">26</option>' +
				'								<option value="27">27</option>' +
				'								<option value="28">28</option>' +
				'								<option value="29">29</option>' +
				'								<option value="30">30</option>' +
				'								<option value="31">31</option>' +
				'							</select>' +
				'						</div>' +
				'					</div>' +
				'					<div class="col-xs-12 col-md-3">' +
				'						<div class="form-group">' +
				'							<label for="rata"><?php echo _("Distanza in giorni") ?></label>' +
				'							<input  type="text" class="form-control inputmask-integer" name="distanza[]" id="rata" value="0">' +
				'						</div>' +
				'					</div>' +
				'				</div>' +
				'				<hr>' +
				'				</div>' +
				'			</div>' +
				'		</div>');
				start_superselect();
				start_inputmask();
	});

	$(document).on('change', '[name*=scadenza]', function(){
		if($(this).val() == 1) $(this).parent().parent().parent().parent().find('[name*=giorno]').prop("disabled", false);
		else $(this).parent().parent().parent().parent().find('[name*=giorno]').prop("disabled", true);
	});

	$('#check').submit(function(event) {
		var tot = 0;
		$(this).find('[name*=percentuale]').each(function(){
			tot += parseFloat($(this).val());
		});
		if(tot != 100) {
			$('#wait').removeClass("hide");
			event.preventDefault();
		}
	});
	$('.showcase.sweet button').on('click', () => {
		swal({
		  title: 'Sei sicuro?',
		  text: "Eliminare questo elemento!",
		  type: 'warning',
		  showCancelButton: true,
		  confirmButtonColor: '#3085d6',
		  cancelButtonColor: '#d33',
		  confirmButtonText: 'Conferma!'
		}).then((result) => {
		  if (result.value) {			
			{ $('#form-delete').submit(); }
		  }
		})
	})		
	
});
</script>