{% extends "layout.html" %}
{% set active_page = "directory" %}

{% block title %}{{ directory.name }}{% endblock title %}

{% block body %}

    <script>

        function swapToForm(elem, fields, formAction, inputName) {

            let form = document.createElement("form");
            form.setAttribute("action", formAction);

            for (let i in fields) {

                let hiddenInput = document.createElement("input");
                hiddenInput.setAttribute("type", "hidden");
                hiddenInput.setAttribute("value", fields[i].value);
                hiddenInput.setAttribute("name", fields[i].name);
                form.appendChild(hiddenInput);
            }

            let input = document.createElement("input");
            input.setAttribute("class", "form-control");
            input.setAttribute("type", "text");
            input.setAttribute("name", inputName);
            input.value = elem.innerHTML;
            form.appendChild(input);

            elem.parentNode.insertBefore(form, elem);
            elem.remove();

            input.focus();
            input.addEventListener("focusout", function () {
                form.submit();
            });

            return input;
        }

        function modifyVal(optId, key) {
            swapToForm(document.getElementById("val-" + optId), [
                {name: "id", value: optId},
                {name: "key", value: key},
                {name: "dir_id", value: {{ directory.id }}}
            ], "/directory/{{ directory.id }}/update_opt", "value");
        }

        function modifyDisplayName() {
            swapToForm(document.getElementById("display-name"), [],
                "/directory/{{ directory.id }}/update", "name");
        }

        function modifyPath() {
            swapToForm(document.getElementById("path"), [],
                "/directory/{{ directory.id }}/update", "path");
        }

    </script>

    <div class="container">

        <div class="card">

            <div class="card-header">Summary</div>
            <div class="card-body">

                <table class="info-table">
                    <tr onclick="modifyDisplayName()">
                        <th style="width: 20%">Display name</th>
                        <td>
                            <pre id="display-name" title="Click to update">{{ directory.name }}</pre>
                        </td>
                    </tr>

                    <tr onclick="modifyPath()">
                        <th style="width: 20%">Path</th>
                        <td>
                            <pre id="path" title="Click to update">{{ directory.path }}</pre>
                        </td>
                    </tr>

                    <tr>
                        <th style="width: 20%">Enabled</th>
                        <td>
                            <form action="/directory/{{ directory.id }}/update"
                                  style="display: inline;margin-left: 6px;">
                                <input type="hidden" name="enabled" value="{{ "0" if directory.enabled else "1" }}">
                                <button class="btn btn-sm {{ "btn-danger" if directory.enabled else "btn-success" }}">
                                    <i class="far {{ "fa-check-square" if directory.enabled else "fa-square" }}"></i>
                                    {{ "Disable" if directory.enabled else "Enable" }}
                                </button>
                            </form>
                        </td>
                    </tr>

                    <tr>
                        <th>Thumbnail cache size</th>
                        <td>
                            <pre>{{ tn_size_formatted }} ({{ tn_size }} bytes)</pre>
                        </td>
                    </tr>
                </table>
            </div>

        </div>

        <div class="card">
            <div class="card-header">Actions</div>
            <div class="card-body">

                <div class="d-flex">

                    <form action="/task/add" class="p-2">
                        <input type="hidden" value="1" name="type">
                        <input type="hidden" value="{{ directory.id }}" name="directory">
                        <button class="btn btn-primary" href="/task/">
                            <i class="fas fa-book"></i> Generate index
                        </button>
                    </form>

                    <form action="/task/add" class="p-2">
                        <input type="hidden" value="2" name="type">
                        <input type="hidden" value="{{ directory.id }}" name="directory">
                        <button class="btn btn-primary" href="/task/">
                            <i class="far fa-images"></i> Generate thumbnails
                        </button>
                    </form>

                    <div class="dropdown p-2">
                        <button class="btn dropdown-toggle btn-danger" data-toggle="dropdown">Action</button>

                        <div class="dropdown-menu">
                            <a class="dropdown-item" href="/directory/{{ directory.id }}/del">Delete directory</a>
                            <a class="dropdown-item" href="/directory/{{ directory.id }}/reset">Reset to default settings</a>
                        </div>
                    </div>
                </div>


            </div>
        </div>

        <div class="card">
            <div class="card-header">Options <a href="https://github.com/simon987/Simple-Incremental-Search-Tool/blob/master/config.py#L1-L13" style="float:right">Learn more <i
                    class="fas fa-external-link-alt"></i></a></div>
            <div class="card-body">
                <table class="info-table table-striped table-hover">
                    <thead>
                    <tr>
                        <th>Option</th>
                        <th>Value</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for option in directory.options %}

                        <tr>
                            <td style="width: 30%"><span>{{ option.key }}</span></td>
                            <td onclick="modifyVal({{ option.id }}, '{{ option.key }}')" title="Click to update">
                                <pre id="val-{{ option.id }}">{{ option.value }}</pre>
                            </td>
                        </tr>

                    {% endfor %}

                    </tbody>
                </table>

            </div>
        </div>
    </div>


{% endblock body %}