<head>
  <title>PenText editor</title>
</head>

<body>
{{>main}}
</body>

<template name="main">
    <div class="top_menu box">
        <button class="edit_findings_button {{findingButtonActive}}" name="edit_findings">EDIT FINDINGS</button>
        <button class="edit_non_findings_button {{nonFindingButtonActive}}" name="edit_non_findings">EDIT NON-FINDINGS</button>
    </div>

    {{#if editModeIs "findings"}}
        <div class="box">{{> findings_settings}}</div>
        <div class="bottom_pane">
            <fieldset class="left_pane box">
                <legend>Findings</legend>
                {{> findings_pane}}
            </fieldset>
            {{#if findingsEditorActive}}
                <div class="middle_pane box">{{> findings_editor}}</div>
            {{/if}}
        </div>
    {{/if}}
    {{#if editModeIs "non_findings"}}
        <div class="box">{{> non_findings_settings}}</div>
        <div class="bottom_pane">
            <fieldset class="left_pane box">
                <legend>Non-Findings</legend>
                {{> findings_pane}}
            </fieldset>
            {{#if nonFindingsEditorActive}}
                <div class="middle_pane box">{{> non_findings_editor}}</div>
            {{/if}}
        </div>
    {{/if}}
</template>

<template name="findings_settings">
    <form class="update_findings_directory">
        <label for="findings_directory">Pentest Findings directory: </label>
        <input id="findings_directory"
               name="findings_directory"
               class="directory"
               value="{{findings_directory}}"/>
        <button type="submit">UPDATE</button>
    </form>
    <form class="update_findings_library_directory">
        <label for="findings_directory">Findings library directory: </label>
        <input id="findings_library_directory"
               name="library_findings_directory"
               class="directory"
               value="{{findings_library_directory}}"/>
        <button type="submit">UPDATE</button>
    </form>
</template>

<template name="non_findings_settings">
    <form class="update_non_findings_directory">
        <label for="non_findings_directory">Pentest Non-Findings directory: </label>
        <input id="non_findings_directory"
               name="non_findings_directory"
               class="directory"
               value="{{non_findings_directory}}"/>
        <button type="submit">UPDATE</button>
    </form>
    <form class="update_non_findings_library_directory">
        <label for="non_findings_library_directory">Non-Findings library directory: </label>
        <input id="non_findings_library_directory"
               name="non_findings_library_directory"
               class="directory"
               value="{{non_findings_library_directory}}"/>
        <button type="submit">UPDATE</button>
    </form>
</template>

<template name="finding">
    <div id="{{filename}}" class="search_result selectable box {{bordercolor}}" title="{{tooltip}}">
        <div class="search_result_title">
            <span>{{{filename}}}</span>
            {{#if typeIs "findings"}}
                {{> use_finding_button}}
            {{/if}}
            {{#if typeIs "library_findings"}}
                {{> use_library_finding_button }}
            {{/if}}
            {{#if typeIs "non_findings"}}
                {{> use_non_finding_button}}
            {{/if}}
            {{#if typeIs "library_non_findings"}}
                {{> use_library_non_finding_button }}
            {{/if}}
        </div>

        {{#if shouldShowContent}}
            <textarea>{{{raw}}}</textarea>
        {{/if}}
    </div>
</template>

<template name="use_finding_button">
    <button title="Use this finding in editor">&#9658;</button>
</template>

<template name="use_library_finding_button">
    <button title="Use this library finding to update the fields of a pentests finding in the editor">&#9658;</button>
</template>

<template name="use_non_finding_button">
    <button title="Use this non-finding in editor">&#9658;</button>
</template>

<template name="use_library_non_finding_button">
    <button title="Use this library non-finding to update the fields of a pentests non-finding in the editor">&#9658;</button>
</template>


<template name="findings_editor">
    {{#autoForm collection="Findings" doc=getSelectedFinding type="update" id="findingForm"}}
            <!--{{> afQuickField name="filename"}}-->
            {{> afQuickField name="id"}}
            {{> afQuickField name="threat_level" options="allowed"}}
            {{> afQuickField name="type"}}
            {{> afQuickField name="title"}}
            {{> afQuickField name="description"}}
            {{> afQuickField name="technical_description"}}
            {{> afQuickField name="impact"}}
            {{> afQuickField name="recommendation"}}
            <div>
                <button type="submit" class="btn btn-primary">UPDATE</button>
            </div>
    {{/autoForm}}
</template>

<template name="non_findings_editor">
    {{#autoForm collection="NonFindings" doc=getSelectedNonFinding type="update" id="nonfindingForm"}}
        <!--{{> afQuickField name="filename"}}-->
        {{> afQuickField name="id"}}
        {{> afQuickField name="title"}}
        {{> afQuickField name="description"}}
        <div>
            <button type="submit" class="btn btn-primary">UPDATE</button>
        </div>
    {{/autoForm}}
</template>

<template name="findings_pane">
    <div class="box">
        <button class="pentest {{isPentestTabActive}}">PENTEST</button>
        <button class="library {{isLibraryTabActive}}">LIBRARY</button>
    </div>
    <div>
        {{#if isPentestTabActive}}
            {{#if editModeIs "findings"}}
                {{> findings}}
            {{/if}}
            {{# if editModeIs "non_findings"}}
                {{> non_findings}}
            {{/if}}
        {{/if}}
        {{#if isLibraryTabActive}}
            {{# if editModeIs "findings"}}
                {{> library_findings}}
            {{/if}}

            {{# if editModeIs "non_findings"}}
                {{> library_non_findings }}
            {{/if}}
        {{/if}}
    </div>
</template>


<template name="findings">
    <div class="search_pane">
        {{> EasySearch.Input attributes=properties index=FindingsIndex timeout="200" count=10000}}

        {{#EasySearch.Each index=FindingsIndex}}
            {{> finding}}
        {{/EasySearch.Each}}

        {{#EasySearch.IfNoResults index=FindingsIndex}}
            <div class="no-results">No results found!</div>
        {{/EasySearch.IfNoResults}}
    </div>
</template>
<template name="library_findings">
    <div class="search_pane">
        {{> EasySearch.Input attributes=properties index=LibraryFindingsIndex timeout="200" count=10000}}

        {{#EasySearch.Each index=LibraryFindingsIndex}}
            {{> finding}}
        {{/EasySearch.Each}}

        {{#EasySearch.IfNoResults index=LibraryFindingsIndex}}
            <div class="no-results">No results found!</div>
        {{/EasySearch.IfNoResults}}
    </div>
</template>

<template name="non_findings">
    <div class="search_pane">
        {{> EasySearch.Input attributes=properties index=NonFindingsIndex timeout="200" count=10000}}

        {{#EasySearch.Each index=NonFindingsIndex}}
            {{> finding}}
        {{/EasySearch.Each}}

        {{#EasySearch.IfNoResults index=NonFindingsIndex}}
            <div class="no-results">No results found!</div>
        {{/EasySearch.IfNoResults}}
    </div>
</template>
<template name="library_non_findings">
    <div class="search_pane">
        {{> EasySearch.Input attributes=properties index=LibraryNonFindingsIndex timeout="200" count=10000}}

        {{#EasySearch.Each index=LibraryNonFindingsIndex}}
            {{> finding}}
        {{/EasySearch.Each}}

        {{#EasySearch.IfNoResults index=LibraryNonFindingsIndex}}
            <div class="no-results">No results found!</div>
        {{/EasySearch.IfNoResults}}
    </div>
</template>