{% extends 'base.html' %}

{% block headersupplement %}
  <script>
    function updatepasswordchanged() {
      document.getElementById('passwordchanged').value=1;
    }

    function getJsonPassword() {
      jQuery.noConflict();
      $.getJSON('/dashboard4/configurazione',
                function(data) {
		  console.log(data);
                  $('#clear').val(data.newPassword);
                  $('#password1').val(data.newPassword);
                  $('#password2').val(data.newPassword);
                  $('#password1').get(0).type='text';
                  $('#password2').get(0).type='text';
              });
    }
  </script>

  <script>
    function setPasswordField() {
      if( $('#password_change_enabled').is(":checked"))
    {
       $('#password1').attr('readonly',false);
       $('#password2').attr('readonly',false);
       $('#UpdatePWD').attr('disabled',false);
       $('#UpdatePWD').click(getJsonPassword);
     } else {
       $('#password1').attr('readonly',true);
       $('#password2').attr('readonly',true);
       $('#UpdatePWD').attr('disabled',true);
       $('#UpdatePWD').click(false);
     }
   };
  </script>
  <script>
    $(document).ready(function() { setPasswordField(); });
  </script>

  <script>
    $(function () {
      $('updatepwd').popover({
        container: 'body'
      });
    });
  </script>

  <script>
    function UpdateNome() {
      if (document.getElementById("nome").value.length == 0 ) {
        document.getElementById("nome").value = document.getElementById("mail").value;
      }
    }
  </script>

  <script>
    function copyToClipboard() {
      elem = document.getElementById("password1");
      elem.select();
      document.execCommand('copy');
    }
  </script>

{% endblock %}

{% block top %}
  <p>
    utente che sta effettuando gli aggiornamenti {{ utenteautorizzato.mail }}@{{ utenteautorizzato.domain.nome }}
  </p>
  {% if user %}
    <p>
      si stanno effettuando aggiornamenti su: {{ user.mail }}@{{ user.domain.nome }}
    </p>
  {% endif %}
{% endblock %}

{% block body %}

<div>Pannello Controllo utente - gestione</div>
<div></div>

<div>
  <div class='row'>
    {% if formemail.errors %}
       <p>Rilevati Errori, impossibile proseguire.</p>
       {% for f in formemail %}
         {% for e in f.errors %}
           <div class="alter alter-danger">
             <strong>{{f.label|escape }}: {{ e|escape }}</strong>
           </div>
         {% endfor %}
       {% endfor %}
       {% for e in formemail.non_field_errors %}
         <div class="alter alter-danger">
           <strong>{{ e|escape }}</strong>
         </div>
       {% endfor %}
     {% endif %}
   </div>
</div>

<form name='useredit' method='post'>

  {% csrf_token %}
  <div class='row'>'
    <div class='col'>
      <label for='mail'>Email Address/Account</label>
      <input type='text' class='form-control' id='mail' name='mail' placeholder='account email' value='{{ formemail.mail.value }}' {% if not 'EDITUSERNEW' in securitylist %} readonly {% endif %} onchange="UpdateNome()">

    </div>
    <div class='col'>
      <label for='domain'>Dominio</label>
      <input type='text' class='form-control' id='dominio_domain' name='dominio_domain' value='{{ formemail.dominio_domain.value }}' readonly>
      <input type='hidden' name='dominio' value='{{ user.domain_id }}'>
    </div>
  </div>

  <div class='form-group'>
    <label for='nome'>Nome completo utente</label>
    <input type='text' class='form-control' id='nome' name='nome' placeholder='nome utente' value='{{ formemail.nome.value }}' {% if not 'EDITUSERINFO' in securitylist %} readonly {% endif %}>
  </div>

  <div class='row form-group' id='updatepwd'>
    {% if 'USERCHANGEPASSWORD' in securitylist %}
        <div class='col form-check-inline'>
          <label for='password_change_enabled' class='form-check-label'>Cambio password abilitato: </label>
          <input type='checkbox' class='form-check-input' id='password_change_enabled' name='password_change_enabled' {% if formemail.password_change_enabled.value %} checked {% endif %} onclick="setPasswordField()">
        </div>
    {% else %}
      <input type='checkbox' style="opacity:0; position:absolute; left:9999px;" id='password_change_enabled' name='password_change_enabled' {% if formemail.password_change_enabled.value %} checked {% endif %}> 
    {% endif %}

    <div class='col'>
      <label for='password1'  {% if 'USERSHOWPASSWORD' in securitylist %} data-toggle='tooltip' data-html='true' title='{{ formemail.clear.value }}' {% endif %}>Password</label>
      <input type='password' class='form-control' id='password1' name='password1' value='{{ formemail.clear.value }}' placeholder='digita la password' {% if not 'USEREDITPASSWORD' in securitylist %} readonly {% endif %} {% if not formemail.password_change_enabled.value %} readonly {% endif %} oninput='updatepasswordchanged()' data-toggle='popover' title='{{ mail_password_message }}'>
   </div>
   <div class='col'>
      <label for='password2'>Ripetere</label>
      <input type='password' class='form-control' id='password2' name='password2' value='{{ formemail.clear.value }}' placeholder='ripeti la password' {% if not 'USEREDITPASSWORD' in securitylist %} readonly {% endif %} {% if not formemail.password_change_enabled.value  %} readonly {% endif %} oninput='updatepasswordchanged()' data-toggle='popover' title='{{ mail_password_message }}'>

      <input type='hidden' id='passwordchanged' name='passwordchanged' value='{{ formemail.passwordchanged.value }}'>
    </div>

    {% if 'USERSHOWPASSWORD' in securitylist %}
      <div class='col'>
        <label for='buttonshare'>Copia Password</label>
        <button type='button' class='form-control btn btn-secondary' id='buttonshare' onclick='copyToClipboard();'>Clipboard</button>
      </div>
    {% endif %}

    {% if 'USEREDITPASSWORD' in securitylist %}
    <div class='col'><!-- richiesta nuova password e relativo aggiornamento -->
	    <label for='UpdatePWD'>Genera Password</label>
	    <button type='button' class='form-control btn btn-secondary' name='UpdatePWD' id='UpdatePWD' 
		    {% if formemail.password_change_enabled.value == 1 %}> 
		    {% else %} disabled>
		    {% endif %} Password</button>
    </div>
    {% endif %}

    {% if 'USERSHOWPASSWORD' in securitylist %}
      <div class='col'>
        <label for='sendInfoToAdmin'>Invia Info ad Admin</label>
	<button type='submit' class='form-control btn btn-secondary' name='sendInfoToAdmin' id='sendInfoToAdmin' >Invia</button>
      </div>
    {% endif %}
      <div class='col'>
        <label for='sendInfoToMe'>Invia Info a me</label>
	<button type='submit' class='form-control btn btn-secondary' name='sendInfoToMe' id='sendInfoToMe' >Invia</button>
      </div>

  </div>

  <div class='row form-group'>
    <!-- fino a che il sistema non provvedera' a inviare email
    <div class='col'>
      <label for='rigenerapassword'>Password Casuale</label>
      <input type='checkbox' class='form-control' id='rigenerapassword' name='rigenerapassword' {% if not 'USEREDITPASSWORD' in securitylist %} readonly {% endif %}>
    </div>
    <div class='col'>
      <label for='invioparametri'>Invia Parametri</label>
      <input type='checkbox' class='form-control' id='inviaparametri' name='inviaparametri'>
    </div>

    <div class='col'>
      <label for='emailtest'>Invia Mail di test</label>
      <input type='checkbox' class='form-control' id='emailtest' name='emailtest'>
    </div>
    -->
  </div>

  <hr class='half-rule'>

  <div class='row form-group'>
    {% if 'USEREDITENABLED' in securitylist %}
      <div class='col form-inline'>
        <label class='form-check-label' for='enabled'>Account Abilitato:&nbsp;&nbsp;</label>
        <input type='checkbox' class='form-check-input' id='enabled' name='enabled' {% if formemail.enabled.value %} checked {% endif %}>
      </div>
    {% endif %}
    {% if 'USERTOBEDELETED' in securitylist and 'USERTOBERECOVERED' in securitylist %}
      <div class='col form-inline'>
        <label class='tobedeleted'>Da Eliminare:&nbsp;&nbsp;</label>
        <input type='checkbox' class='form-check-input' id='tobedeleted' name='tobedeleted' {% if formemail.tobedeleted.value %} checked {% endif %}>
      </div>
    {% elif 'USERTOBEDELETED' in securitylist and formemail.tobedelete.value %}
       <div class='col form-inline'>
        <label class='tobedeleted'>Da Eliminare:&nbsp;&nbsp;</label>
        <input type='checkbox' class='form-check-input' id='tobedeleted' name='tobedeleted' {% if formemail.tobedeleted.value %} checked {% endif %}>
      </div>
     {% elif 'USERTOBEDELETED' in securitylist %}
       <div class='col form-inline'>
        <label class='tobedeleted'>Da Eliminare:&nbsp;&nbsp;</label>
        <input type='checkbox' class='form-check-input' id='tobedeleted' name='tobedeleted' {% if formemail.tobedeleted.value %} checked {% endif %}>
      </div>
  {% endif %}

    <hr class='half-rule'>

  </div>

  {% if 'EDITUSEROPTIONS' in securitylist %}
    <div class='form-group'>
      <div class='form-inline'>
        <label class='form-input-label' for='forward'>Forward email a (questa opzione supera le regole della webmail):  </label>
	<input type='text' clas=form-control' id='forward' name='forward' value="{{ formemail.forward.value }}" size=64></input>
      </div>
    </div>
    <div class='row form-group'> 
      <div class='col form-inline'>
        <label class='form-check-label' for='pop3'>Pop3:&nbsp;&nbsp;</label>
        <input type='checkbox' class='form-check-input' id='pop3' name='pop3' {% if formemail.pop3.value %} checked {% endif %}>
      </div>

      <div class="col form-inline">
        <label class='form-check-label' for='imap'>Imap:&nbsp;&nbsp;</label>
        <input type='checkbox' class='form-check-input' id='imap' name='imap' {% if formemail.imap.value %} checked {% endif %}>
      </div>

      <div class="col form-inline">
        <label class='form-check-label' for='smtp'>Smtp:&nbsp;&nbsp;</label>
        <input type='checkbox' class='form-check-input' id='smtp' name='smtp' {% if formemail.smtp.value %}checked{% endif %}>
      </div>

      <div class="col form-inline">
        <label class='form-check-label' for='smtp_relay'>Relay:&nbsp;&nbsp;</label>
        <input type='checkbox' class='form-check-input' id='smtp_relay' name='smtp_relay' {% if formemail.smtp_relay.value %} checked {% endif %}>
      </div>

      <div class='col form-inline'>
        <label for='webmail'>Webmail:&nbsp;&nbsp;</label>
        <input type='checkbox' class='form-check-input' id='webmail' name='webmail' {% if formemail.webmail.value %} checked {% endif %}>
      </div>
        
      <div class='col form-inline'>
        <label for='nextcloud'>Nextcloud:&nbsp;&nbsp;</label>
        <input type='checkbox' class='form-check-input' id='nextcloud' name='nextcloud' {% if formemail.nextcloud.value %} checked {% endif %}>
      </div>
      <div class='col form-inline'>
        <label for='nextcloud_pwd'>Nextcloud Pass Sync:&nbsp;&nbsp;</label>
        <input type='checkbox' class='form-check-input' id='nextcloud_pwd' name='nextcloud_pwd' {% if formemail.nextcloud_pwd.value %} checked {% endif %}>
      </div>

    </div>

    <div class='row form-group'> 
      <div class='col form-inline'>
        <label for='spf'>Spf:&nbsp;&nbsp;</label>
        <input type='checkbox' class='form-check-input' id='spf' name='spf' {% if formemail.spf.value %} checked {% endif %}>
      </div>
      <div class='col form-inline'>
        <label for='antivirus'>Antivirus:&nbsp;&nbsp;</label>
        <input type='checkbox' class='form-check-input' id='antivirus' name='antivirus' {% if formemail.antivirus.value %} checked {% endif %}>
      </div>
      <div class='col form-inline'>
        <label for='blacklist'>blacklist:&nbsp;&nbsp;</label>
        <input type='checkbox' class='form-check-input' id='blacklist' name='blacklist' {% if formemail.blacklist.value %} checked {% endif %}>
      </div>
      <div class='col form-inline'>
        <label for='sieve'>sieve:&nbsp;&nbsp;</label>
        <input type='checkbox' class='form-check-input' id='sieve' name='sieve' {% if formemail.sieve.value %} checked {% endif %}>
      </div>
      <div class='col form-inline'>
        <label for='sms' class='form-inline'>Sms:&nbsp;&nbsp;</label>
        <input type='checkbox' class='form-check-input' id='sms' name='sms' {% if formemail.sms.value %} checked {% endif %}>
      </div>
      <div class='col form-inline'>
        <label for='enabled' class='form-inline'>SendaFile:&nbsp;&nbsp;</label>
        <input type='checkbox' class='form-check-input' id='sendafile' name='sendafile' {% if formemail.sendafile.value %} checked {% endif %} >
      </div>
    </div>
    {% endif %}
    <hr class='half-rule'>

  {% if 'EDITUSEROPTIONS' in securitylist %}
    <div class='row form-group'>
      <div class='col form-inline'>
        <label class='form-check-label' for='livello'>Livello:&nbsp;&nbsp;</label>
        <select class='form-control' id='securitylevel' name='securitylevel'>
        {% for i in livello %} 
          <option value='{{ i.id }}' {% if i.id|stringformat:'s' == formemail.securitylevel.value|stringformat:'s' %} selected {% endif %}>{{ i.nome }}</option>
        {% endfor %}
        </select>
      </div>
    </div>

    <div class='row form-group'>
      <div class='col form-check-inline'>
	      <label class='form-check-label' for='lmtp'>Lmtp Server {% if user.domain.lmtp %}( Lmtp Attivo). {% endif %} </label>
        <input class='form-check-input' type='checkbox' id='lmtp' name='lmtp' {% if formemail.lmtp.value %} checked {% endif %}>
      </div>

      <div class='col form-group'>
        <label class='form-check-label' for='lmtp_server'>Lmtp Server: </label>
        <select id='lmtp_server' class='form-control' name='lmtp_server'>
          <option values=''></option>
          {% for i in lmtpserverlist %}
             <option value='{{ i.id }}' {% if i.id|stringformat:'s' == formemail.lmtp_server.value|stringformat:'s' %} selected {% endif %}>({{ i.id}}) {{ i.server }}</option>
          {% endfor %}
        </select>
      </div>

    <div class='col form-group'>
      <label class='form-check-label' for='mail_server'>Mail Server</label>
      <select id='mail_server' class='form-control' name='mail_server'>
	<option values=''></option>
        {% for i in mailserverlist %}
	  <option value='{{ i.id }}' {% if i.id|stringformat:'s' == formemail.mail_server.value|stringformat:'s' %} selected {% endif %}>({{ i.id }}) {{ i.server }}</option>
        {% endfor %}
      </select>
    </div>

    <div class='col form-group'>
	    <label class='form-check-label' for='sql_remote'>Sql Remote</label>
      <select id='sql_remote' class='form-control' name='sql_remote'>
	<option values=''></option>
        {% for i in sqlremotelist %}
	  <option value='{{ i.id }}' {% if i.id|stringformat:'s' == formemail.sql_remote.value|stringformat:'s' %} selected {% endif %}>({{ i.id }}) {{ i.server }}</option>
        {% endfor %}
      </select>
    </div>

    <div class='col select-wrapper'>
      <label class='form-check-label' for='mail_proxy'>Mail Proxy</label>
      <select id='mail_proxy' class='form-control' name='mail_proxy'>
	<option values=''></option>
        {% for i in mailproxylist %}
          <option value='{{ i.id }}' {% if i.id|stringformat:'s' == formemail.mail_proxy.value|stringformat:'s' %} selected {% endif %}>({{ i.id }}) {{ i.server }}</option>
        {% endfor %}
      </select>
    </div>
    </div>
  {% endif %}
  
  {% if 'EDITUSERINFO' in securitylist %}
    <div class='row'>
      <div class='col'>
        <label for='nota'>Note e appunti</label>
        <textarea class='form-control' rows='3' id='note' placeholder='note e appunti'>{{ formemail.nota.value }}</textarea>
      </div>
    </div>
    <p></p>
    <div class='row'>
      <div class='col'>
        <label for='quota'>Quota (in Mb):</label>
        <input type='text' class='form-control' id='mail_quota' name='mail_quota' value='{{ formemail.mail_quota.value }}' placeholder='0' readonly=''>
      </div>
      <div class='col'>
        <label for='warning_mail_quota'>Spazio Avviso (in Mb):</label>
        <input type='text' class='form-control' id='warning_mail_quota' name='warning_mail_quota' value='{{ user.warning_mail_quota }}' placeholder='{{ user.domain.warning_mail_quota }}' {%  if not 'EDITUSERWARNINGMAILQUOTA' in securitylist %} readonly='' {% endif %}>
      </div>
      <div class='col'>
        <label for='limit_mail_quota'>Spazio Limite (in Mb):</label>
        <input type='text' class='form-control' id='limit_mail_quota' name='limit_mail_quota' value='{{ user.limit_mail_quota }}' placeholder='{{ user.domain.limit_mail_quota }}' {%  if not 'EDITUSERWARNINGMAILQUOTA' in securitylist %} readonly='' {% endif %}>
      </div>
    </div>
    <p></p>
  <div class='row'>
    <div class='col'>
      <label for='mail_di_recupero'>Mail di recupero (Inserendo una mail personale, sarà possibile recuperare l'accesso in caso di smarrimento delle credenziali originali)</label>
      <input type='text' class='form-control' id='mail_di_recupero' name='mail_di_recupero' value='{{ formemail.mail_di_recupero.value }}' placeholder='mail di recupero password smarrite' {% if not 'EDITUSERINFO' in securitylist %} readonly='' {% endif %}>
    </div>
  </div>
  {% endif %}

  <br>

  <div class='form-group'>
    <div class='row'>
      <div class='col'>
              <label for='mail_who'>Invio Notifica Credenziali a (attenzione: Stiamo inviando informazioni preziose e sensibili riguardo questa mail):</label>
        <input type='text' class='form-control' id='mail_who' name='mail_who' value=''>
      </div>
    </div>
  </div>

  <br>

  <div class='form-group'>
    {% if mailinglist %}
      L'utente risulta iscritto alle seguenti liste di distribuzione del dominio:<br>
      {% for m in mailinglist %}
        {{ m.nome }}@{{ m.dominio.nome }}<br>
      {% endfor %}
    {% endif %}  
  </div>

  <br>
  <div class='btn-group' role='group' aria-label='conferme'>
    <button type='submit' class='btn btn-primary'>Conferma</button>
    <button type='reset' class='btn btn-secondary'>Reset</button>
    <button type='submit' class='btn btn-secondary' name="Ritorno">Ritorno (Senza salvare)</button>
  </div>

  <div class='row'>
    <div class='col'>
      {% if formemail.errors %}
        <p>Rilevati Errori, impossibile proseguire.</p>
        {% for f in formemail %}
          {% for e in f.errors %}
            <div class="alter alter-danger">
              <strong>{{f.label|escape }}: {{ e|escape }}</strong>
            </div>
          {% endfor %}
        {% endfor %}
        {% for e in formemail.non_field_errors %}
          <div class="alter alter-danger">
            <strong>{{ e|escape }}</strong>
          </div>
        {% endfor %}
      {% endif %}
    </div>
  </div>

</form>
</div>

{% endblock %}

