{% extends "kpi/base_kpi.html" %}

{% block title %}
    Example of scripted KPI report
{% endblock %}

{% block scripts %}
    <script type="text/javascript">
        var g_doc_pattern = /.*docs.google.com.*\/d\/(.+)\/.*/;

        $(document).ready(function () {

            function processTextArea() {
                readKpiScript($("#kpi_script").val(), "root_container");
            }

            var url_vars = getUrlVars();

            if ("url" in url_vars) {
                var url = url_vars["url"];
                $("#kpi_url").val(url);
                $("#source_link").attr("href", url);

                var pattern_match = g_doc_pattern.exec(url);
                if (pattern_match) {
                    var doc_id = pattern_match[1];
                    url = "https://docs.google.com/feeds/download/documents/export/Export?id=" + doc_id + "&exportFormat=txt";
                    console.log(url);
                }

                $.get(url,function (data) {
                    $("#kpi_script").val(data);
                    processTextArea();
                }).fail(function(){
                    $("#kpi_script").val("Error loading file from URL: " + url_vars["url"]);
                });
            } else {
                processTextArea();
            }

            $("#btn_update").click(function () {
                processTextArea();
            });
        });
    </script>
{% endblock %}


{% block content %}
    <div>KPI report can be configured by script or loaded from URL.</div>

    <form action="/kpi/script" method="get">
        <label for="kpi_url">URL:</label>
        <input id="kpi_url" style="width: 45em;" name="url"/>
        <input type="submit" value="Load">
        <span><a href="" id="source_link">View Source</a></span>
    </form>

    <label for="kpi_script"></label>
    <textarea id="kpi_script" style="width: 60em; height: 30em;">
Release Juno:
  Company Mirantis:
    - top 5 by commits in official-integrated
    - less than 50% by commits in stackalytics
  Group glance-group:
    User lzy-dev:
      - top 3 by commits
      - top 4 by reviews in glance
      - make 10 commits during 30 days
      - become core
    User apevec:
      - make 15 reviews in python-glanceclient
      - draft 2 blueprints
      - implement 7 blueprints
      - send 10 emails
    Company Mirantis:
      - top 5 by locs
</textarea>
    <button id="btn_update">Update</button>

    <div id="root_container"></div>
{% endblock %}
