{% extends 'base.html' %}
{% block headersupplement %}
  <script>
    function UpdateHome() {
      if (document.getElementById("home").value.length == 0 ) {
        document.getElementById("home").value = document.getElementById("dominio").value;
      } 
    }
    function OnSync() {
      jQuery.noConflict();
      $.get('/dashboard4/domini/OnSync',
                function(data,status) {
		  alert("Data: " + data + "\nStatus: " + status);
              }
      );
    }
  </script>
{% endblock %}
{% load domini_tags %}

{% block top %}
      <p>
      utente che sta effettuando aggiornamenti: {{ utenteautorizzato.mail }}@{{ utenteautorizzato.domain.nome }}
      </p>
{% endblock %}

{% block body %}

<div>Gestione Amministrativa del Dominio</div>
<p>Dominio: {{ dominio.id }}: {{ dominio.nome }}</p>
<div>

<div>

  {% if checkpassword.errors %}
    <p>
      Sono stati rilevati degli errori e non e' possibile continuare
      {{ checkpassword.errors }}
    </p>
  {% endif %}


<form name='domainedit' method='post'>
  {% csrf_token %}

  {% if 'DOMINIEDITCAUSALI' in securitylist %}
  <div class='form-group'>
    <div class='row'>
      <div class='col'>
	      {% if display_last_causale %}
  	        Ultima Causale Registrata: {{ display_last_causale }} <br><p>
	      {% if dominio_storia %}
	      {% for ssl in dominio_storia %}
	      <p>{{ ssl.causale.nome }} {{ ssl.data }} {%endfor %}
	      {% endif %}
	      {% endif %} 
      </div>
    </div>
    <p></p>
    <div class='row'>
      <div class='col'>
        <select class='form-control' id='causale' name='causale'>
	  <option value="0">-- -- --</option>
          {% for i in causali %}
          <option value="{{ i.id }}" {% if i.id|stringformat:'s' == domainedit.causale.value|stringformat:'s' %} selected {% endif %}>{{ i.nome }}</option>
          {% endfor %}
        </select>
      </div>
      <div class='col'>
        <button type='submit' class='btn btn-secondary' name="confermacausale"  value="confermacausale">Conferma</button>
	<button type='button' class='btn btn-secondary' name='toggle' data-toggle='modal' data-target='#ListaUltimeCausaliRegistrate'>Ultime Causali Registrate</button>
        <button type='submit' class='btn btn-secondary' name='Ritorna'>Ritorna</button>
        <label for='rinnovo_automatico'>Rinnovo Automatico</label>
        <input type='checkbox' class='form-check-input' id='rinnovo_automatico' name='rinnovo_automatico' {% if domainedit.rinnovo_automatico.value %} checked {% endif %}>
      </div>
    </div>

    <!-- Modal -->
    <div id='ListaUltimeCausaliRegistrate' class='modal fade' role='dialog' tabindex='-1' aria-labelledby='Update' aria-hidden='true'>
      <div class='modal-dialog modal-lg' role='document'>

      <!-- inizio contenuto modale-->
      <div class='modal-content'>
        <div class='modal-header'>
          <h4>Lista Ultime Causali Utilizzate</h3>
        </div>

        <div>
          <ul class='list-group'>
          {% for lc in UltimaStoriaCausali %}
	  <li class='list-group-item'>{{ lc.data|DateFromUnixTimeStamp }} {{ lc.causale.nome }}</li>
          {% endfor %}
          </ul>
        </div>

        <div class='modal-footer'>
          <input type='button' class='btn btn-outline-dark' data-dismiss='modal' value='Chiudi'></button>
        </div>

      </div>
      <!-- fine del contenuto modale -->

      </div>
    </div>
  {% endif %}

  <div class='row'>&nbsp;</div>

  <div class='row'>
    <div class='col'>
	    <label for='dominio'>Dominio </label>
      <input type='text' class='form-control' id='dominio' name='dominio' placeholder='nome dominio (obbligatorio)' value='{{ domainedit.dominio.value }}' {% if not 'DOMINIEDITDOMAIN' in securitylist  or not domainedit.edit.value %}  readonly {% endif %} onchange='UpdateHome()'>
    </div>

    <div class='col'>
      <label for='public_email'>Email Pubblica</label>
      <input type='text' class='form-control' id='public_email' name='public_email' value='{{ domainedit.public_email.value }}' {% if not 'DOMINIEDITPUBLICEMAIL' in securitylist or not domainedit.edit.value %} readonly {% endif %}>
    </div>
  </div>
  <div class='row'>
    <div class='col'>
      <label for='proprietario'>Owner</label>
      <textarea name='proprietario' class='form-control' rows='5'>{{ domainedit.proprietario.value }}</textarea>
    </div>
  </div>
  <div class='row'>
    <div class='col'>
      <label for='contatti'>Contatti Email (Separare le email con spazi o virgole)</label>
      <input type='text' class='form-control' id='contatti' name='contatti' value='{{ domainedit.contatti.value }}'/>
    </div>
  </div>
  <br>
  <div class='row'>
    <div class='col'>
      <div class='form-group'>
	      <label for='target'>Target di Fatturazione</label>
	<select class='form-control' id='target' name='target'>
	{% for i in target %}
	<option value='{{ i.id }}' {% if i.id|stringformat:'s' == domainedit.target.value|stringformat:'s' %} selected {% endif %}>{{ i.nome }} - Max Size: {{ i.spazio }}</options>
        {% endfor %}
	</select>
      </div>
    </div>
  </div>
  <hr>
  Servizi assegnati al dominio:
  {% if  dsf %}
    <div class='row'> 
      <table class="table table-striped">
      <thead>
        <th>Id</th>
        <th>Servizio</th>
        <th>Attivazione</th>
      </thead>
      <tbody>
      {% for row in dsf %}
        <tr>
          <th scope="row">{{ row.servizio.id }}</th>
	  <td>{{ row.servizio.nome }}</td>
          <td>{{ row.attivazione }}</td>
          <td {% if row.disattivazione %}style="text-decoration:line-through red;" {% endif %}>{{ row.disattivazione }}</td>
        </tr>
      {% endfor %}
      </tbody>
      </table>
    </div>
  {% else %}
    Non sono stati registrati servizi!
  {% endif %}
  <hr>
  <div class='row form-group'>
    <div class='col'>
      <label for='dic'>Inizio Contratto</label>
      <input type='text' class='form-control' id='idc' placeholder='inizio' name='date_start_contract' value='{{ domainedit.date_start_contract.value | date:'d/m/Y' }}' {% if not 'DOMINIDATE' in securitylist and not domainedit.edit.value %} readonly {% endif %}>
    </div>
    <div class='col'>
      <label for='psc'>Prossima Scadenza Contratto</label>
      <input type='text' class='form-control' id='psc' placeholder='sc. contratto' name='date_expire_contract' value='{{ domainedit.date_expire_contract.value | date:'d/m/Y' }}' {% if not 'DOMINIDATE' in securitylist and not domainedit.edit.value %} readonly {% endif %}>
    </div>
    <div class='col'>
      <label for='psr'>Prossima Scadenza Registrazione</label>
      <input type='text' class='form-control' id='psr' placeholder='sc. registrazione' name='date_expire_registrar' value='{{ domainedit.date_expire_registrar.value | date:'d/m/Y' }}' {% if not 'DOMINIDATE' in securitylist and not domainedit.edit.value %} readonly='' {% endif %}>
    </div>
  </div>

  <div class='form-group'>
    <label for='home'>Home</label>
    <input type='text' class='form-control' id='home' name='home' value='{{ domainedit.home.value }}'>
  </div>

  {% if 'DOMINIEDITREGISTRAR' in securitylist %}
    <div class='form-group'>
      <label for='registrar'>Registrar</label> 
      <select class='form-control' id='registrar' name='registrar' {% if not domainedit.edit.value %} readonly {% endif %}>
        {% for i in registrar %}
	<option value="{{ i.id }}" {% if i.id|stringformat:'s'  == domainedit.registrar.value|stringformat:'s'  %} selected {% endif %}>{{ i.nome }} ({% if i.registrazione %}Gestito Da noi{% else %}Gestito dal cliente {% endif %})</option>
        {% endfor %}
      </select>
  {% endif %}

  <div class='form-group'>
    <label for='mail_server'>Mail Server</label> 
      <select class='form-control' id='mail_server' name='mail_server' {% if not domainedit.edit.value %} readonly {% endif %}>
        {% for i in mailserver %}
	<option value="{{ i.id }}" {% if i.id|stringformat:'s'  == domainedit.mail_server.value|stringformat:'s'  %} selected {% endif %}>
	{{ i.id }} {{ i.nome }} {{ i.nota }}</option>
        {% endfor %}
      </select>
  </div> 
  <div class='form-group'>
    <label for='lmtp_server'>Lmtp Server</label> 
      <select class='form-control' id='lmtp_server' name='lmtp_server' {% if not domainedit.edit.value %} readonly {% endif %}>
        {% for i in mailserver %}
	<option value="{{ i.id }}" {% if i.id|stringformat:'s'  == domainedit.lmtp_server.value|stringformat:'s'  %} selected {% endif %}>
	{{ i.id }} {{ i.nome }} {{ i.nota }}</option>
        {% endfor %}
      </select>
  </div> 

  <div class='form-group'>
    <label for='mail_server'>Mail Proxy</label> 
      <select class='form-control' id='mail_proxy' name='mail_proxy' {% if not domainedit.edit.value %} readonly {% endif %}>
        {% for i in mailserver %}
	<option value="{{ i.id }}" {% if i.id|stringformat:'s'  == domainedit.mail_proxy.value|stringformat:'s'  %} selected {% endif %}>
	{{ i.id }} {{ i.nome }} {{ i.nota }}</option>
        {% endfor %}
      </select>
  </div> 
  <div class='form-group'>
    <label for='mail_server'>Mail Database Server</label> 
      <select class='form-control' id='sql_remote' name='sql_remote' {% if not domainedit.edit.value %} readonly {% endif %}>
        {% for i in mailserver %}
	<option value="{{ i.id }}" {% if i.id|stringformat:'s'  == domainedit.sql_remote.value|stringformat:'s'  %} selected {% endif %}>
	{{ i.id }} {{ i.nome }} {{ i.nota }}</option>
        {% endfor %}
      </select>
  </div> 
  <div>
    <label for='OnSync'>Sincronizza Utenti</label>
    <button type='button' class='form-control btn btn-secondary' name='OnSync' id='OnSync' onclick="OnSync">Sincronizza Utenti</button>
  </div>

  <div class='row'>
    <div class='col text-center'>
      <label for='enabled'>attivo</label>
      <input type='checkbox' class='form-check-input' id='enabled' name='enabled' {% if domainedit.enabled.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label for='tobedeleted'>Dominio Impostato per la cancellazione</label>
      <input type='checkbox' class='form-check-input' id='tobedeleted' name='tobedeleted' {% if domainedit.tobedeleted.value %} checked {% endif %}{% if not domainedit.edit.value %} readonly {% endif %}>
    </div>
  </div>

  <br>

  <div class='row'>
    <div class='col text-center'>
      <label for='mail'>mail</label>
      <input type='checkbox' class='form-check-input' id='mail' name='mail' {% if domainedit.mail.value %} checked {% endif %}>
      </div>
    <div class='col text-center'>
      <label for='pop3'>Pop3</label>
      <input type='checkbox' class='form-check-input' id='pop3' name='pop3' {% if domainedit.pop3.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label for='imap'>Imap</label>
      <input type='checkbox' class='form-check-input' id='imap' name='imap' {% if domainedit.imap.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label for='smtp'>Smtp</label>
      <input type='checkbox' class='form-check-input' id='smtp' name='smtp' {% if domainedit.smtp.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label for='smtp_relay'>Relay</label>
      <input type='checkbox' class='form-check-input' id='smtp_relay' name='smtp_relay' {% if domainedit.smtp_relay.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label for='webmail'>Webmail</label>
      <input type='checkbox' class='form-check-input' id='webmail' name='webmail' {% if domainedit.webmail.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label for='nextcloud'>NextCloud</label>
      <input type='checkbox' class='form-check-input' id='nextcloud' name='nextcloud' {% if domainedit.nextcloud.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label for='nextcloud_pwd'>NextCloud Pwd Sync</label>
      <input type='checkbox' class='form-check-input' id='nextcloud_pwd' name='nextcloud_pwd' {% if domainedit.nextcloud_pwd.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label for='sendafile'>Send A File</label>
      <input type='checkbox' class='form-check-input' id='sendafile' name='sendafile' {% if domainedit.sendafile.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label for='sms'>Sms</label>
      <input type='checkbox' class='form-check-input' id='sms' name='sms' {% if domainedit.sms.value %} checked {% endif %}>
    </div>
  </div>
  <hr>
  <div class='row'>
    <div class='col text-center'>
      <label for='lmtp'>Lmtp</label>
      <input type='checkbox' class='form-check-input' id='lmtp' name='lmtp' {% if domainedit.lmtp.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label for='sieve'>Sieve</label>
      <input type='checkbox' class='form-check-input' id='sieve' name='sieve' {% if domainedit.sieve.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label for='antivirus'>Antivirus</label>
      <input type='checkbox' class='form-check-input' id='antivirus' name='antivirus' {% if domainedit.antivirus.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label for='spf'>Spf</label>
      <input type='checkbox' class='form-check-input' id='spf' name='spf' {% if domainedit.spf.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label for='blacklist'>Blacklist</label>
      <input type='checkbox' class='form-check-input' id='blacklist' name='blacklist' {% if domainedit.blacklist.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label for='ftp'>Ftp</label>
      <input type='checkbox' class='form-check-input' id='ftp' name='ftp' {% if domainedit.ftp.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label for='sql'>Sql</label>
      <input type='checkbox' class='form-check-input' id='sql' name='sql' {% if domainedit.sql.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label for='http'>Http</label>
      <input type='checkbox' class='form-check-input' id='http' name='http' {% if domainedit.http.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label for='proxy'>Proxy</label>
      <input type='checkbox' class='form-check-input' id='proxy' name='proxy' {% if domainedit.proxy.value %} checked {% endif %}>
    </div>
  </div>
  <hr>
  <div class='row'>
    <div class='col text-center'>
      <label for='Account'>Account di posta</label>
      <input type='number' class='form-control' id='max_account' name='max_account' value={{ domainedit.max_account.value }}>
    </div>
    <div class='col text-center'>
      <label for='max_alias'>Alias</label>
      <input type='number' class='form-control' id='max_alias' name='max_alias' value={{ domainedit.max_alias.value }}>
    </div>
    <div class='col text-center'>
      <label for='max_alias'>Liste</label>
      <input type='number' class='form-control' id='max_list' name='max_list' value={{ domainedit.max_list.value }}>
    </div>
  </div>
  <hr>
  <div class='row'>
    <div class='col text-center'>
      <label for='account_used'>Account Usati</label>
      <input type='text' class='form-control' id='account_used' name='account_used' value="{{ domainedit.account_used.value }}" readonly>
    </div>
    <div class='col text-center'>
      <label for='alias_used'>Alias Utilizzati</label>
      <input type='text' class='form-control' id='alias_used' name='alias_used' value="{{ domainedit.alias_used.value }}" readonly>
    </div>
    <div class='col text-center'>
      <label for='list_used'>Liste Utilizzate</label>
      <input type='test' class='form-control' id='list_used' name='list_used' value="{{ domainedit.list_used.value }}" readonly>
    </div>
  </div>
  <hr>

  Quote utilizzate (I valori sono espressi in Mega)
  <div class='row'>
    <div class='col'>
      <label for='mail_quota'>Mail Quota</label>
      <input type='text' class='form-control' id='mail_quota' name='mail_quota' value='{{ dominio.mail_quota }}' readonly>
    </div>
    <div class='col'>
      <label for='http_quota'>Http Quota</label>
      <input type='Text' class='form-control' id='http_quota' name='http_quota' value='{{ dominio.http_quota }}' readonly>
    </div>
    <div class='col'>
      <label for='ftp_quota'>Ftp Quota</label>
      <input type='Text' class='form-control' id='ftp_quota' name='ftp_quota'value='{{ dominio.ftp_quota }}' readonly>
    </div>
    <div class='col'>
      <label for='http_quota'>Sql Quota</label>
      <input type='Text' class='form-control' id='http_quota' name='sql_quota' value='{{ dominio.sql_quota }}' readonly>
    </div>
    <div class='col'>
      <label for='nextcloud_quota'>Nextcloud quota</label>
     <input type='text' class='form-control' id='Nextcloud_quota' name='nextcloud_quota' value='{{ dominio.nextcloud_quota }}' readonly>
    </div>
    <div class='col'>
     <label for='totale'>Totale</label>
     <input type='text' class='form-control' id='Totale' name='totale' value='{{ totale }}' readonly>
    </div>
 </div>

  <hr>

  <div class='row form-group'>
    <div class='col text-center'>
      <label for='warning_mail_quota'>Quota Allarme per Utente</label>
      <input type='number' class='form-control' id='warning_mail_quota' name='warning_mail_quota' value={{ domainedit.warning_mail_quota.value }}>
    </div>
    <div class='col'>
       <label class='form-check-label' for='warning_mail_quota_check'>Controllo Soglia</label>:
       <input type='checkbox' class='form-check-label' id='warning_mail_quota_check' name='warning_mail_quota_check' {% if domainedit.warning_mail_quota_check.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label class='form-check-label' for='update_mail_quota'>Aggiorna Quota Email</label>:
      <input type='checkbox' class='form-check-input'' id='update_mail_quota' name='update_mail_quota' {% if domainedit.update_mail_quota.value %} checked {% endif %}>
    </div>
    <div class='col text-center'>
      <label class=ìform-check-label' for='force_update_mail_quota'>Forza Aggiornamento</label>:
      <input type='checkbox' class='form-check-input' id='force_update_mail_quota' name='force_update_mail_quota' {% if domainedit.force_update_mail_quota.value %} checked {% endif %}>
    </div>
  </div>
  <hr>

    {% if ftplist %}
    Spazio FTP assegnato
    <table class='table table-striped table-hover'>
    <thead class='thead-dark'>
      <tr>
        <th>Utente</th>
        <th>Home</th>
	<th>Quota</th>
      </tr>
    </thead>
    <tbody>
    {% for f in ftplist %}
      <tr>
              <td>{{ f.utente }}</td>
              <td>{{ f.homedir }}</td>
	      <td>{{ f.quota }}</td>
     </tr>
    {% endfor %}
    </tbody>
  </table>
  {% endif %}


  <div class='row form-group'>
    <label for='nota'>Note e appunti</label>
    <textarea name='nota' class='form-control' rows='5'>{{ domainedit.nota.value }}</textarea>
  </div>

  {% if 'EDITUSEROPTIONS' in securitylist %}
    <label for='edit' class='form-check-label'>Editing Campi Protetti</label>:
    <input type='checkbox' class='form-check-input' id='edit' name='edit' {% if domainedit.edit.value %} checked {% endif %}>
  {% endif %}
  <div>
    <div class='row'>
      {% if domainedit.errors %}
         <div class='row'>
         Rilevati Errori, impossibile proseguire.
         </div>
         
         <div class='row'>
         {% for f in domainedit %}
           {% for e in f.errors %}
             <div class="alter alter-danger">
               <strong>{{f.label|escape }}: {{ e|escape }}</strong>
             </div>
           {% endfor %}
         {% endfor %}
         </div>

         <div class='row'>
         {% for e in domainedit.non_field_errors %}
           <div class="alter alter-danger">
             <strong>{{ e|escape }}</strong>
           </div>
         {% endfor %}
         </div>
       {% endif %}
     </div>
  </div>

  <hr>
  <div class='btn-group' role='group' aria-label='conferme'>
    <button type='submit' class='btn btn-primary active'>Conferma</button>
    <button type='reset' class='btn btn-secondary'>Reset</button>
   <button type='submit' class='btn btn-secondary' name='Ritorna'>Ritorna (Senza Salvare)</button>
  </div>
</form>
</div>

{% endblock %}
