{% extends 'default.html' %}

{% set active_page = "setting" %}

{% block title %}
  Settings
{% endblock %}

{% block default_content %}

  <div class="col-md-8 offset-md-2 pb-3">
    <!-- Change email -->
    <div class="card">
      <form method="post" enctype="multipart/form-data">
        <input type="hidden" name="form-name" value="update-email">
        {{ change_email_form.csrf_token }}

        <div class="card-body">
          <div class="card-title">
            Change Email Address
          </div>
          <div class="form-group">
            <label class="form-label">Email</label>

            <!-- Not allow user to change email if there's a pending change -->
            {{ change_email_form.email(class="form-control", value=current_user.email, readonly=pending_email != None) }}
            {{ render_field_errors(change_email_form.email) }}

            {% if pending_email %}
              <div class="mt-2">
                <span class="text-danger">Pending email change: {{ pending_email }}</span>
                <a href="{{ url_for('dashboard.resend_email_change') }}" class="btn btn-secondary btn-sm">Resend
                  confirmation email</a>
                <a href="{{ url_for('dashboard.cancel_email_change') }}" class="btn btn-secondary btn-sm">Cancel email
                  change</a>
              </div>
            {% endif %}
          </div>
          <button class="btn btn-primary">Change Email</button>
        </div>
      </form>
    </div>
    <!-- END Change email -->

    <!-- Change name & profile picture -->
    <div class="card">
      <form method="post" enctype="multipart/form-data">
        {{ form.csrf_token }}
        <input type="hidden" name="form-name" value="update-profile">

        <div class="card-body">
          <div class="card-title">
            Change Profile
          </div>
          <div class="form-group">
            <label class="form-label">Name</label>
            {{ form.name(class="form-control", value=current_user.name) }}
            {{ render_field_errors(form.name) }}
          </div>

          <div class="form-group">
            <div class="form-label">Profile picture</div>
            {{ form.profile_picture(class="form-control-file") }}
            {{ render_field_errors(form.profile_picture) }}
            {% if current_user.profile_picture_id %}
              <img src="{{ current_user.profile_picture_url() }}" class="profile-picture">
            {% endif %}
          </div>
          <button class="btn btn-primary">Update</button>
        </div>
      </form>
    </div>
    <!-- END change name & profile picture -->


    <div class="card">
      <div class="card-body">
        <div class="card-title">Multi-Factor Authentication (MFA)
          <div class="small-text mt-1 mb-3">
            Secure your account with Multi-Factor Authentication. <br>
            This requires having applications like Google Authenticator, Authy, FreeOTP, etc.
          </div>
        </div>
        {% if not current_user.enable_otp %}
          <a href="{{ url_for('dashboard.mfa_setup') }}" class="btn btn-outline-primary">Enable</a>
        {% else %}
          <a href="{{ url_for('dashboard.mfa_cancel') }}" class="btn btn-outline-danger">Cancel MFA</a>
        {% endif %}
      </div>
    </div>

    <div class="card">
      <div class="card-body">
        <div class="card-title">
          Change password
          <div class="small-text mt-1 mb-3">
            You will receive an email containing instructions on how to change password.
          </div>
        </div>
        <form method="post">
          <input type="hidden" name="form-name" value="change-password">
          <button class="btn btn-outline-primary">Change password</button>
        </form>
      </div>
    </div>

    <div class="card">
      <div class="card-body">
        <div class="card-title">Random Alias
          <div class="small-text mt-1 mb-3">Choose how to create your email alias by default</div>
        </div>
        <form method="post" class="form-inline">
          <input type="hidden" name="form-name" value="change-alias-generator">
          <select class="custom-select mr-sm-2" name="alias-generator-scheme">
            <option value="{{ AliasGeneratorEnum.word.value }}"
                {% if current_user.alias_generator == AliasGeneratorEnum.word.value %} selected {% endif %} >Based on
              Random {{ AliasGeneratorEnum.word.name.capitalize() }}</option>
            <option value="{{ AliasGeneratorEnum.uuid.value }}"
                {% if current_user.alias_generator == AliasGeneratorEnum.uuid.value %} selected {% endif %} >Based
              on {{ AliasGeneratorEnum.uuid.name.upper() }}</option>
          </select>
          <button class="btn btn-outline-primary">Update Preference</button>
        </form>
      </div>
    </div>

    <div class="card">
      <div class="card-body">
        <div class="card-title" id="notification">Newsletters
          <div class="small-text mt-1 mb-3">
            Every now and then we can send you an email
            to let you know about a new feature that might be useful to you.
          </div>
        </div>
        <form method="post">
          <input type="hidden" name="form-name" value="notification-preference">
          <div class="form-check">
            <input type="checkbox" id="notification" name="notification" {% if current_user.notification %}
                   checked {% endif %} class="form-check-input">
            <label for="notification">I want to receive your newsletter</label>
          </div>
          <button type="submit" class="btn btn-outline-primary">Submit</button>
        </form>
      </div>
    </div>


    <div class="card">
      <div class="card-body">
        <div class="card-title mb-3">Current Plan</div>

        {% if current_user.get_subscription() %}
          You are on the  {{ current_user.get_subscription().plan_name() }} plan. <br>
          <a href="{{ url_for('dashboard.billing') }}" class="btn btn-outline-primary">
            Manage Subscription
          </a>
        {% elif manual_sub %}
          You are on the Premium plan. The plan ends {{ manual_sub.end_at | dt }}.
        {% elif current_user.lifetime %}
          You have the lifetime licence.
        {% elif current_user.in_trial() %}
          You are in the trial period. The trial ends {{ current_user.trial_end | dt }}.
        {% else %}
          You are on the Free plan.
        {% endif %}
      </div>
    </div>


    <div class="card">
      <div class="card-body">
        <div class="card-title">Deleted Aliases
          <div class="small-text mt-1 mb-3" style="max-width: 40rem">
            When an alias is deleted, all its activities are deleted and no emails can be sent to it. <br>
            It is moved to another location and only used to check when new alias is created. <br>
            This check is necessary to avoid someone else accidentally taking this alias. <br>
            Because in this case, the other person might receive inadvertently information that belong to you. <br>
          </div>
        </div>
        <a href="{{ url_for('dashboard.deleted_alias_route') }}" class="btn btn-outline-primary">
          See deleted aliases
        </a>
      </div>
    </div>

    <div class="card">
      <div class="card-body">
        <div class="card-title">Export Data
          <div class="small-text mt-1 mb-3">
            You can download all aliases you have created on SimpleLogin along with other data.
          </div>
        </div>
        <form method="post">
          <input type="hidden" name="form-name" value="export-data">
          <button class="btn btn-outline-info">Export Data</button>
        </form>
      </div>
    </div>


    <div class="card">
      <div class="card-body">
        <div class="card-title">Delete Account
          <div class="small-text mt-1 mb-3">Please note that this operation is irreversible.
          </div>

        </div>
        <form method="post">
          <input type="hidden" name="form-name" value="delete-account">
          <span class="delete-account btn btn-outline-danger">Delete account</span>
        </form>
      </div>
    </div>

  </div>

{% endblock %}

{% block script %}
  <script>
    $(".delete-account").on("click", function (e) {
      notie.confirm({
        text: "All your data including your aliases will be deleted, " +
          "other people might not be able to reach you after, " +
          " please confirm.",
        cancelCallback: () => {
          // nothing to do
        },
        submitCallback: () => {
          $(this).closest("form").submit();
        }
      });
    });
  </script>
{% endblock %}

