{% extends "layout.html" %}
{% from "macros.html" import render_field, form_tag %}

{% block content %}
  <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
    <script type="text/javascript">
        var app = angular.module('MyApp', [])
        app.config(['$interpolateProvider', function($interpolateProvider) {
        $interpolateProvider.startSymbol('{a');
        $interpolateProvider.endSymbol('a}'); }]);

        app.controller('MyController', function ($scope, $window) {
            $scope.externalids = {{grant_data_list | safe}};
            $scope.grant_number = ''
            $scope.url = ''
            $scope.relationship = ''
            $scope.grant_type = ''
            $scope.grant_type_list = [{'label': 'Agricola', 'value': 'agr'}, {'label': 'Archival Resource Key Identifier', 'value': 'ark'},
            {'label': 'ArXiv', 'value': 'arxiv'}, {'label': 'Amazon Standard Identification Number', 'value': 'asin'},
            {'label': 'ASIN top-level domain', 'value': 'asin-tld'}, {'label': 'AuthenticusID', 'value': 'authenticusid'},
            {'label': 'Bibcode', 'value': 'bibcode'}, {'label': 'Chinese Biological Abstracts', 'value': 'cba'},
            {'label': 'Ciência-IUL Identifier', 'value': 'cienciaiul'}, {'label': 'CiteSeer', 'value': 'cit'},
            {'label': 'CiteExplore submission', 'value': 'ctx'}, {'label': 'German National Library identifier', 'value': 'dnb'},
            {'label': 'Digital object identifier', 'value': 'doi'}, {'label': 'Scopus Identifier', 'value': 'eid'},
            {'label': 'EThOS Peristent ID', 'value': 'ethos'}, {'label': 'grant number', 'value': 'grant_number'},
            {'label': 'Handle', 'value': 'handle'}, {'label': 'Chinese Biological Abstracts', 'value': 'cba'},
            {'label': 'NHS Evidence', 'value': 'hir'}, {'label': 'International Standard Book Number', 'value': 'isbn'},
            {'label': 'International Standard Serial Number. Includes print and electronic ISSN', 'value': 'issn'},
            {'label': 'Jahrbuch über die Fortschritte der Mathematik', 'value': 'jfm'}, {'label': 'JSTOR abstract', 'value': 'jstor'},
            {'label': 'KoreaMed Unique Identifier', 'value': 'kuid'}, {'label': 'Mathematical Reviews', 'value': 'mr'},
            {'label': 'Online Computer Library Center', 'value': 'oclc'}, {'label': 'Open Library', 'value': 'ol'},
            {'label': 'Office of Scientific and Technical Information', 'value': 'osti'}, {'label': 'Other identifier type', 'value': 'other-id'},
            {'label': 'Patent number', 'value': 'pat'}, {'label': 'Protein Data Bank identifier', 'value': 'pdb'},
            {'label': 'PubMed Central article number', 'value': 'pmc'}, {'label': 'PubMed Unique Identifier', 'value': 'pmid'},
            {'label': 'Request for Comments', 'value': 'rfc'}, {'label': 'Research Resource IDentifier', 'value': 'rrid'},
            {'label': 'Non-standard ID from work data source', 'value': 'source-work-id'}, {'label': 'Social Science Research Network', 'value': 'ssrn'},
            {'label': ' URI', 'value': 'uri'}, {'label': 'URN', 'value': 'urn'}, {'label': 'Web of Science™ identifier', 'value': 'wosuid'},
            {'label': 'Zentralblatt MATH', 'value': 'zbl'}];
            $scope.grant_relationship_list = [{'label': 'Part Of', 'value': 'PART_OF'}, {'label': 'Self', 'value': 'SELF'}];

            $scope.Add = function () {
                //Add the new item to the Array.
                var externalid = {};
                externalid.grant_number = $scope.grant_number;
                externalid.grant_type = $scope.grant_type;
                externalid.grant_url = $scope.grant_url;
                externalid.grant_relationship = $scope.grant_relationship;
                $scope.externalids.push(externalid);
            };

            $scope.Remove = function (index) {
                //Find the record using Index from Array.
                if ($window.confirm("Do you want to delete")) {
                    //Remove the item from Array using Index.
                    $scope.externalids.splice(index, 1);
                }
            }
        });
    </script>
  {% call form_tag(form) %}
<div class="row">
  <div class="col-md-9 col-sm-8 col-xs-12">
    <h1>{{tile or ("Education" if section_type == "EDU" else "Funding" if section_type == "FUN" else "Peer Review" if
      section_type == "PRR" else "Work" if section_type == "WOR" else "Researcher Url" if section_type == "RUR" else
      "Other Name" if section_type == "ONR" else "Keyword" if section_type == "KWR" else "Employment")}}</h1>
  </div>
</div>
    <div class="row" ng-app="MyApp" ng-controller="MyController">
      <div class="col-md-6 col-sm-6 col-xs-12">
        {% if section_type in ["RUR", "ONR", "KWR"] %}
          {{ render_field(form.display_index) }}
          {{ render_field(form.visibility) }}
        {% else %}
          {% if section_type == "WOR" %}
            {{ render_field(form.work_type) }}
            {{ render_field(form.title) }}
            {{ render_field(form.subtitle) }}
            {{ render_field(form.translated_title) }}
            {{ render_field(form.translated_title_language_code) }}
          {% else %}
            {{ render_field(form.org_name) }}
            {{ render_field(form.city) }}
            {{ render_field(form.state) }}
            {{ render_field(form.disambiguated_id) }}
          {% endif %}
          {{ render_field(form.country) }}
        {% endif %}
        {% if section_type == "FUN" or section_type == "PRR" or section_type == "WOR"%}
        <input type="button" class="btn btn-danger" ng-click="Add()"
               value="External Identifier Info (Atleast add one info)"/><br>
        <div ng-repeat="m in externalids">
          <strong>{{"External ID Type" if section_type != "FUN" else "Funding Grant Type"}}</strong><br>
          <select ng-model="selectedGrantvalue" class="form-control" name="grant_type">
            <option value="">-- Select --</option>
            <option ng-repeat="gtl in grant_type_list" ng-selected="m.grant_type == gtl.value"
                    value="{a gtl.value a}">{a gtl.label a}
            </option>
          </select>
          <strong>{{"External ID" if section_type != "FUN" else "Funding Grant Number"}}</strong><br>
          <input type="text" class="form-control" name="grant_number" value="{a m.grant_number a}"/><br>
          <strong>{{"External ID Url" if section_type != "FUN" else "Funding Grant Url"}}</strong><br>
          <input type="text" class="form-control" name="grant_url" value="{a m.grant_url a}"/><br>
          <strong>{{"External ID Relationship" if section_type != "FUN" else "Funding Grant Relationship"}}</strong><br>
          <select ng-model="selectedItemvalue" class="form-control" name="grant_relationship">
            <option value="">-- Select --</option>
            <option ng-repeat="grl in grant_relationship_list" ng-selected="m.grant_relationship == grl.value"
                    value="{a grl.value a}">{a grl.label a}
            </option>
          </select>
          <input type="button" class="btn btn-danger" ng-click="Remove($index)" value="Remove"/><br>
        </div>
        {% endif %}
      </div>
      <div class="col-md-6 col-sm-6 col-xs-12">
        {% if section_type == "WOR" %}
          {{ render_field(form.journal_title) }}
          {{ render_field(form.short_description) }}
          {{ render_field(form.citation_type) }}
          {{ render_field(form.citation) }}
          {{ render_field(form.publication_date) }}
          {{ render_field(form.url) }}
          {{ render_field(form.language_code) }}
        {% elif section_type == "PRR" %}
          {{ render_field(form.reviewer_role) }}
          {{ render_field(form.review_url) }}
          {{ render_field(form.review_type) }}
          {{ render_field(form.review_group_id) }}
          {{ render_field(form.subject_external_identifier_type) }}
          {{ render_field(form.subject_external_identifier_value) }}
          {{ render_field(form.subject_external_identifier_url) }}
          {{ render_field(form.subject_external_identifier_relationship) }}
          {{ render_field(form.subject_container_name) }}
          {{ render_field(form.subject_type) }}
          {{ render_field(form.subject_title) }}
          {{ render_field(form.subject_subtitle) }}
          {{ render_field(form.subject_translated_title) }}
          {{ render_field(form.subject_translated_title_language_code) }}
          {{ render_field(form.subject_url) }}
          {{ render_field(form.review_completion_date) }}
          {{ render_field(form.disambiguation_source) }}
        {% elif section_type == "RUR" %}
          {{ render_field(form.url_name) }}
          {{ render_field(form.url_value) }}
        {% elif section_type in ["ONR", "KWR"] %}
          {{ render_field(form.content) }}
        {% else %}
          {% if section_type == "FUN" %}
            {{ render_field(form.funding_title) }}
            {{ render_field(form.funding_translated_title) }}
            {{ render_field(form.translated_title_language) }}
            {{ render_field(form.funding_type) }}
            {{ render_field(form.funding_subtype) }}
            {{ render_field(form.funding_description) }}
            {{ render_field(form.total_funding_amount) }}
            {{ render_field(form.total_funding_amount_currency) }}
          {% else %}
            {{ render_field(form.department) }}
            {{ render_field(form.role) }}
          {% endif %}
          {{ render_field(form.start_date) }}
          {{ render_field(form.end_date) }}
          {{ render_field(form.disambiguation_source) }}
        {% endif %}
        <button type="submit" class="btn btn-primary" value="Save">Save</button>
        <a role="button" class="btn btn-default" href="{{_url}}">Cancel</a>
      </div>
    </div>
  {% endcall %}
{% endblock %}
