{% extends 'layout.twig' %}

{% import 'messagesysteme.twig' as msgSyst %}

{% block content %}
	<div class="row">
		<div class="col col-md-12">
        <div style="overflow:auto;">

			<div class="box">
				<div class="box-header">
					<h3 class="box-title">{{ trad.admin_overload }}</h3>
				</div>
				<!-- /.box-header -->
				<div class="box-body no-padding">
					<p>{{ trad.explication_champs_additionnels }}</p>

					
					<table class="table table-striped">
						<tr>
							<th>{{trad.match_area}}</th>
							<th>{{trad.fieldname}}</th>
							<th>{{trad.fieldtype}}</th>
							<th>{{trad.champ_obligatoire}}</th>
							<th>{{trad.affiche_dans_les_vues}}</th>
							<th>{{trad.affiche_dans_les_mails}}</th>
							<th>{{trad.champ_confidentiel}}</th>
							<th>{{trad.envoy_mail_specifique}}*(1)(2)</th>
							<th colspan="2">{{trad.action}}</th>
						</tr>

						<form method="post" action="admin.php?p=admin_overload" >
							<input type="hidden" name="p" value="admin_overload" />
							<input type="hidden" name="action" value="add" />
							<tr>
								<td>
									<select name="id_area" size="1">
										{% for domaine in domaines %}
											<option value="{{ domaine.id }}">{{ domaine.nom }}</option>
										{% endfor %}
									</select>
								</td>
								<td><input type="text" name="fieldname" size="20" /></td>
								<td>
									<select name="fieldtype" size="1">
										<option value="text">{{ trad.type_text }}</option>
										<option value="numeric">{{ trad.type_numeric }}</option>
										<option value="checkbox">{{ trad.type_checkbox }}</option>
										<option value="textarea">{{ trad.type_area }}</option>
										<option value="list">{{ trad.type_list }}</option>
									</select>
								</td>
								<td><input type="checkbox" id="obligatoire" name="obligatoire" value="y" /></td>
								<td><input type="checkbox" id="affichage" name="affichage" value="y" /></td>
								<td><input type="checkbox" id="overload_mail" name="overload_mail" value="y" /></td>
								<td><input type="checkbox" id="confidentiel" name="confidentiel" value="y" /></td>
								<td><div><input type="text" name="mail_spec" size="20" /></div></td>
								<td colspan="2"><input type="submit" name="submit" value="{{ trad.add }}" class="btn bg-green" /></td>
							</tr>
						</form>

						{% for champAdd in champsAdd %}
							
							<tr>
								<form method="post" action="?p=admin_overload">
									<input type="hidden" name="p" value="admin_overload" />
									<input type="hidden" name="id_overload" value="{{champAdd.id}}" />
									<input type="hidden" name="action" value="change" />
									<td>{{champAdd.domaine}}</td>
									<td><input type="text" name="fieldname" value="{{ champAdd.nom }}"></td>
									<td>
										<select name="fieldtype" size="1">
											<option value="text" {% if champAdd.type == 'text' %} selected {% endif %}>{{ trad.type_text }}</option>
											<option value="numeric" {% if champAdd.type == 'numeric' %} selected {% endif %}>{{ trad.type_numeric }}</option>
											<option value="checkbox" {% if champAdd.type == 'checkbox' %} selected {% endif %}>{{ trad.type_checkbox }}</option>
											<option value="textarea" {% if champAdd.type == 'textarea' %} selected {% endif %}>{{ trad.type_area }}</option>
											<option value="list" {% if champAdd.type == 'list' %} selected {% endif %}>{{ trad.type_list }}</option>
										</select>
										{% if champAdd.type == 'list' or champAdd.type == 'checkbox' %}
											<br>{{ trad.Liste_des_champs |raw }}<br>
											<input type="text" name="fieldlist" value="{{ champAdd.liste }}" size="50" />
										{% endif %}
									</td>
									<td><input type="checkbox" id="obligatoire" name="obligatoire" value="y"  {% if champAdd.obligatoire == 'y' %} checked {% endif %}></td>
									<td><input type="checkbox" id="affichage" name="affichage" value="y"  {% if champAdd.affichagevue == 'y' %} checked {% endif %}></td>
									<td><input type="checkbox" id="overload_mail" name="overload_mail" value="y"  {% if champAdd.affichagemail == 'y' %} checked {% endif %}></td>
									<td><input type="checkbox" id="confidentiel" name="confidentiel" value="y"  {% if champAdd.confidentiel == 'y' %} checked {% endif %}></td>
									<td><div><input type="text" name="mail_spec" size="20" value="{{ champAdd.mail_spec }}" /></div></td>
									<td><button type="submit" class="btn btn-info btn-md"><i class="icon fa fa-check"></i></button></td>
								</form>	
								<form method="post" id="supChampAdd{{champAdd.id}}" action="?p=admin_overload">
									<input type="hidden" name="p" value="admin_overload" />
									<input type="hidden" name="id_overload" value="{{champAdd.id}}" />
									<input type="hidden" name="action" value="delete" />
									<td><button type="button" class="btn btn-danger btn-md" data-bs-toggle="modal" data-bs-target="#modal-warning" data-texte="{{trad.confirm_del}} : {{champAdd.nom}}" data-idsup="{{ champAdd.id }}"><i class="icon fa fa-trash"></i></button></td>
									
								</form>	
							</tr>
						{% endfor %}
					</table>
					<p>(1) {{ trad.cas_fonctionnalite_mail_actif }}</p>
					<p>(2) {{ trad.envois_mail_spec_exp }}</p>
				</div>
			</div>
		</div>
        </div>
	</div>

	<!-- popup modal -->
	<div class="modal fade" id="modal-warning" style="display: none;" aria-hidden="true">
		<form action="?p=admin_overload" id="formmodal">
		  <div class="modal-dialog">
			<div class="modal-content bg-warning">
			  <div class="modal-header">
				<h4 class="modal-title">{{trad.confirm_del}}</h4>
				<button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
				  <span aria-hidden="true">×</span>
				</button>
			  </div>
			  <div class="modal-body">
				<span class="modaltexte" id="modaltexte"></span><span class="modalidsup" id="modalidsup" hidden></span>
			  </div>
			  <div class="modal-footer justify-content-between">
				  <button type="button" class="btn btn-outline-dark" data-bs-dismiss="modal">{{trad.NO}}</button>
				  <button type="submit" id="liensup" class="btn btn-outline-dark">{{trad.YES}}</button>
			  </div>
			</div><!-- /.modal-content -->
		  </div><!-- /.modal-dialog -->
		</form>
	  </div><!-- /.modal -->
{% endblock %}

{% block javascript %}
	<script>
		// Modal
		$('#modal-warning').on('show.bs.modal', function (event) {
			var button = $(event.relatedTarget) // Button that triggered the modal
			var idasup = button.data('idsup')// Extract info from data-* attributes
			var texte = button.data('texte')
		    // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
		    // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
		    var modal = $(this)
		    modal.find('.modalidsup').text(idasup)
			modal.find('.modaltexte').text(texte)
		})

		$(function(){
			$('#formmodal').submit(function(e) {
			  e.preventDefault()
			  var $form = $(this)
			  var v=document.getElementById('modalidsup').innerHTML
			  $.post($form.attr('action'), $form.serialize())
			  .done(function(data) {
				$('#html').html(data);
				$('#modal-warning').modal('hide');
				//document.location.href='?p=admin_overload&action=delete&id_overload='+v+';
				document.getElementById("supChampAdd"+v).submit(); 
			  })
			  .fail(function() {
				alert('Une erreur est survenue : GRRJS')
			  })
			})
		})
	</script>
{% endblock %}