{% extends "repo_master.html" %}

{% block title %}Edit - {{
    repo.namespace + '/' if repo.namespace }}{{ repo.name }}{% endblock %}
{% set tag = "home" %}
{% block header %}
<script type="text/javascript" nonce="{{ g.nonce }}" src="{{
  url_for('static', filename='vendor/codemirror/codemirror.js') }}?version={{ g.version}}"></script>
<link rel="stylesheet"  nonce="{{ g.nonce }}" href="{{
  url_for('static', filename='vendor/codemirror/codemirror.css') }}?version={{ g.version}}">
{% endblock%}

{% block repo %}

<div class="card">
  <div class="card-header">
      <ol class="breadcrumb p-0 bg-transparent mb-0">
      <li><a href="{{ url_for('ui_ns.view_tree',
            repo=repo.name,
            username=username,
            namespace=repo.namespace,
            identifier=branchname)
      }}"><span class="fa fa-random"></span>&nbsp; {{ branchname }}</a></li>{%
    for file in filename.split('/') %}
      {% set path = '/'.join(filename.split('/')[:loop.index]) %}
      {% if loop.index != loop.length %}<li><a
      href="{{ url_for('ui_ns.view_file',
            repo=repo.name,
            username=username,
            namespace=repo.namespace,
            identifier=branchname,
            filename=path)}}"
        ><span class="fa fa-folder"></span>&nbsp; {{ file }}</a>
        </li>{%
        elif file
        %}<li class="active"><span class="fa {%
            if output_type == 'tree' %}fa-folder{%
            else %}fa-file{%
            endif %}"></span>&nbsp; {{ file }}</li>{%
        endif %}
    {% endfor %}
    </ol>
  </div>
  <div class="card-block p-a-0">
    <form action="{{ url_for(
          'ui_ns.edit_file',
          repo=repo.name,
          username=username,
          namespace=repo.namespace,
          branchname=branchname, filename=filename) }}"
          method="post">
    {{ form.csrf_token }}

    <textarea id="textareaCode" name="content">{{ data }}</textarea>

    <div id="commit_block" class="p-a-2">
      <fieldset class="form-group">
        <label for="email"><strong>Committing as</strong></label>
        <div>
          <select id="email" name="email" class="c-select">
            {% for email in user.emails %}
            <option value="{{ email.email }}" {%
                if form.email.data == email.email %}selected{%
                endif %}>{{ email.email }}</option>
            {% endfor %}
          </select>
          {{ user.default_email | avatar(16) | safe }}
          {{ user.fullname or user.user }}
        </div>
      </fieldset>
      <fieldset class="form-group">
        <label for="branch"><strong>Branch</strong></label>
        <div>
          <span id="branch"><select name="branch" class="c-select">
            {% for branch in g.branches %}
            <option value="{{ branch }}" {%
                if form.branch.data == branch or branchname == branch %}selected{%
                endif %}>{{ branch }}</option>
            {% endfor %}
          </select></span>
          <input type="radio" name="braction" value="existing" checked />Existing branch
          <input type="radio" name="braction" value="new" />New branch
        </div>
      </fieldset>
      <fieldset class="form-group">
        <label for="commit_title"><strong>Commit Title</strong></label>
        <input type="text" class="form-control" id="commit_title" name="commit_title"
            placeholder="Update {{ filename }}"
            value="{{ form.commit_title.data if form.commit_title.data else
            'Update %s' % filename }}" />
      </fieldset>
      <fieldset class="form-group">
        <label for="commit_message"><strong>Commit Description</strong></label>
        <textarea rows="5" class="form-control" type="text" id="commit_message"
            name="commit_message" placeholder="An optional description of the change">{%
            if repo.settings['Enforce_signed-off_commits_in_pull-request'] %}Signed-off-by: {{
            user.user or user.fullname }} <{{ user.default_email }}>{%
            endif %}
            {{- form.commit_message.data if form.commit_message.data -}}
        </textarea>

      </fieldset>
      <div>
        <input type="submit" class="btn btn-primary" value="Commit changes">
        <input type="button" class="btn btn-secondary" value="Cancel" class="button cancel_btn">
      </div>
    </div>

    </form>
  </div><!-- end cardblock-->
</div> <!-- end card -->

{% endblock %}

{% block jscripts %}
{{ super() }}
<script type="text/javascript" nonce="{{ g.nonce }}">
    var myCodeMirror = CodeMirror.fromTextArea(document.getElementById("textareaCode"), {
    lineNumbers: true
  });
</script>
<script type="text/javascript" nonce="{{ g.nonce }}">

var lbranch = $('#branch').html();

$("input[name=braction]:radio").change(function (e) {
  if ($(e.currentTarget).val() == 'new'){
    $('#branch').html('<input type="text" placeholder="Branch name" name="branch">');
  } else {
    $('#branch').html(lbranch);
  }
});
</script>
{% endblock %}
