{{/*
This file is part of SystemTestPortal.
Copyright (C) 2017  Institute of Software Technology, University of Stuttgart

SystemTestPortal is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

SystemTestPortal is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with SystemTestPortal.  If not, see <http://www.gnu.org/licenses/>.
*/}}

{{define "tab-content"}}
<div class="modal fade" id="deleteModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><i class="fa fa-trash"></i> Delete Test Case</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span>&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <p>Do you really want to delete the test case? This is irreversible!</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" id="buttonDeleteConfirmed">Delete</button>
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="revertModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><i class="fa fa-undo"></i> Revert Test Case</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span>&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <p>Do you really want to revert the test case to this version?</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="buttonRevertConfirmed">Revert</button>
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="duplicateModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><i class="fa fa-clone"></i> Name Test Case Duplicate</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span>&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <p>This duplicates the current test case up to the currently shown version.
                    The test case duplicate needs a unique name.</p>
                <input class="form-control" id="inputDuplicateTestCaseName"
                       placeholder="Enter name for test case duplicate" required>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="buttonDuplicateConfirmed">Duplicate</button>
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="helpModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Tools</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span>&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <table class="help-table">
                    <tr>
                        <td colspan="2">
                            This shows the details of a test case.
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2"><h5>Buttons</h5></td>
                    </tr>
                    <tr>
                        <td>
                            <button class="btn btn-secondary">
                                <i class="fa fa-long-arrow-left" aria-hidden="true"></i>
                                <span class="d-none d-sm-inline"> Back</span>
                            </button>
                        </td>
                        <td>Get back to the test case list.</td>
                    </tr>
                    {{ if .LoggedIn }}
                    <tr>
                        <td>
                            <button class="btn btn-danger" >
                                <i class="fa fa-trash-o" aria-hidden="true"></i>
                                <span class="d-none d-sm-inline"> Delete</span>
                            </button>
                        </td>
                        <td>Open a confirm dialog before deleting the current test case.</td>
                    </tr>
                    <tr>
                        <td>
                            <button class="btn btn-primary"  formaction="duplicate">
                                <i class="fa fa-clone" aria-hidden="true"></i>
                                <span class="d-none d-sm-inline"> Duplicate</span>
                            </button>
                        </td>
                        <td>Duplicate the test case with history up to the currently selected version.</td>
                    </tr>
                    {{ end }}
                    <tr>
                        <td>
                            <button class="btn btn-primary" formaction="history">
                                <i class="fa fa-history" aria-hidden="true"></i>
                                <span class="d-none d-sm-inline"> History</span>
                            </button>
                        </td>
                        <td>Show a list with all previous versions of this test case.</td>
                    </tr>
                    {{ if .LoggedIn }}
                    <tr>
                        <td>
                            <button class="btn btn-primary" formaction="revert">
                                <abbr title="Reverts the test case to the selected version">
                                    <i class="fa fa-undo" aria-hidden="true"></i>
                                    <span class="d-none d-sm-inline">Revert</span>
                                </abbr>
                            </button>
                        </td>
                        <td>Revert the test case to the selected version.</td>
                    </tr>
                    <tr>
                        <td>
                            <button class="btn btn-primary" formaction="edit">
                                <i class="fa fa-pencil-square-o" aria-hidden="true"></i>
                                <span class="d-none d-sm-inline"> Edit</span>
                            </button>
                        </td>
                        <td>Edit the current test case.</td>
                    </tr>
                    {{ end }}
                    <tr>
                        <td>
                            <button class="btn btn-primary" disabled>
                                <i class="fa fa-user-plus" aria-hidden="true"></i>
                                <span class="d-none d-sm-inline"> Assign</span>
                            </button>
                        </td>
                        <td>Assign this test case as a task to a team member.</td>
                    </tr>
                    <tr>
                        <td>
                            <button class="btn btn-success">
                                <i class="fa fa-play" aria-hidden="true"></i>
                                <span class="d-none d-sm-inline"> Start</span>
                            </button>
                        </td>
                        <td>Start the execution of the current test case.</td>
                    </tr>
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<div class="tab-card card" id="tabTestCases">
    <nav class="navbar navbar-light action-bar p-3">
    <div class="input-group flex-nowrap">
        <button class="btn btn-secondary mr-2" id="buttonBack">
            <i class="fa fa-long-arrow-left" aria-hidden="true"></i>
            <span class="d-none d-md-inline"> Back</span>
        </button>
        {{ if .LoggedIn }}
        <button class="btn btn-danger mr-2" id="buttonDelete" data-toggle="modal"
                data-target="#deleteModal">
            <i class="fa fa-trash-o" aria-hidden="true"></i>
            <span class="d-none d-md-inline"> Delete</span>
        </button>
        <button class="btn btn-primary mr-2" id="buttonDuplicate" formaction="duplicate"
                data-toggle="modal" data-target="#duplicateModal">
            <i class="fa fa-clone" aria-hidden="true"></i>
            <span class="d-none d-md-inline"> Duplicate</span>
        </button>
        {{ end }}
        <button class="btn btn-primary mr-2" id="buttonHistory" formaction="history">
            <i class="fa fa-history" aria-hidden="true"></i>
            <span class="d-none d-md-inline"> History</span>
        </button>
        {{ if .LoggedIn }}
        {{ if ne .TestCaseVersion.VersionNr (index .TestCase.TestCaseVersions 0).VersionNr }}
        <button class="btn btn-primary mr-2" id="buttonRevert" formaction="revert" data-toggle="modal"
                data-target="#revertModal">
            <abbr title="Reverts the test case to the selected version">
                <i class="fa fa-undo" aria-hidden="true"></i>
                <span class="d-none d-md-inline">Revert</span>
            </abbr>
        </button>
        {{ else }}
        <button class="btn btn-primary mr-2" id="buttonEdit" formaction="edit">
            <i class="fa fa-pencil-square-o" aria-hidden="true"></i>
            <span class="d-none d-md-inline"> Edit</span>
        </button>
        {{ end }}
        {{ end }}
        <button class="btn btn-primary mr-2" id="buttonAssign" disabled data-toggle="tooltip" data-placement="bottom"
                title="" data-original-title="This feature will be available in a future version">
            <i class="fa fa-user-plus" aria-hidden="true"></i>
            <span class="d-none d-md-inline"> Assign</span>
        </button>
        {{ with .TestCaseVersion }}
        {{ if .Steps }}
        <button class="btn btn-success" id="buttonExecute">
            <i class="fa fa-play" aria-hidden="true"></i>
            <span class="d-none d-md-inline"> Start</span>
        </button>
        {{ end }}
        {{ end }}
    </div>
</nav>
    <div class="row tab-side-bar-row">
        <div class="col-md-9 p-3">
            {{ if ne .TestCaseVersion.VersionNr (index .TestCase.TestCaseVersions 0).VersionNr }}
            <div class="alert alert-warning alert-dismissible">
                <a class="close" data-dismiss="alert" aria-label="close">&times;</a>
                <strong>Warning!</strong> You are viewing an older version of the test case
            </div>
            {{ end }}
            <h4 class="mb-3">{{ .TestCase.Name }}</h4>
            {{ with .TestCaseVersion }}
            <div class="form-group">
                <label><strong>Test Case Description</strong></label>
                <p class="text-muted">
                    {{ with .Description }}
                    {{ . }}
                    {{ else }}
                    No Description
                    {{ end }}
                </p>
            </div>
            <div class="form-group">
                <label><strong>Test Case Preconditions</strong></label>
                <p class="text-muted">
                    {{ with .Preconditions }}
                    {{ . }}
                    {{ else }}
                    No Conditions
                    {{ end }}
                </p>
            </div>
            <div class="form-group">
                <label><strong>Test Steps</strong></label>
                {{ if .Steps }}
                <ol class="list-group" id="testStepsAccordion" data-children=".li">
                    {{ range $index, $element := .Steps }}
                    <li class="list-group-item">
                        <a data-toggle="collapse" data-parent="#testStepsAccordion" href="#testStepsAccordion{{ $index }}"
                           aria-expanded="true" aria-controls="testStepsAccordion{{ $index }}">
                            {{ .Action }}
                        </a>
                        <div id="testStepsAccordion{{ $index }}" class="collapse" role="tabpanel">
                            <p class="mb-3">
                                {{ .ExpectedResult }}
                            </p>
                        </div>
                    </li>
                    {{ end }}
                </ol>
                {{ else }}
                <p class="text-muted">No Test Cases</p>
                {{ end }}
            </div>
        </div>
        <div class="col-md-3 p-3 tab-side-bar">
            <div class="form-group">
                <label><strong>Applicability</strong></label>
                <div class="form-group">
                    <div class="col-2 col-sm-1 col-md-12 col-lg-12">
                        <label for="inputTestCaseSUTVariants">
                            <strong>Variants</strong>
                        </label>
                    </div>
                    <div class="col-10 col-sm-11 col-md-12 col-lg-12">
                        <select class="custom-select mb-2" id="inputTestCaseSUTVariants"></select>
                    </div>
                    <div class="col-2 col-sm-1 col-md-12 col-lg-12">
                        <label for="inputTestCaseSUTVersions">
                            <strong>Versions</strong>
                        </label>
                    </div>
                    <div class="form-group col-10 col-sm-11 col-md-12 col-lg-12">
                        <div class="form-control" id="inputTestCaseSUTVersions">
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label><strong>Estimated Test Duration</strong></label>
                <p class="text-muted">
                    {{ if and (eq .DurationHours 0) (eq .DurationMin 0) }}
                    No Test Duration
                    {{ else }}
                    {{ if gt .DurationHours 0 }}
                    {{ .DurationHours }}
                    {{ if eq .DurationHours 1 }} Hour {{ else }} Hours {{ end }}
                    {{ end }}
                    {{ if gt .DurationMin 0 }}
                    {{ .DurationMin }}
                    {{ if eq .DurationMin 1 }}
                    Minute
                    {{ else }}
                    Minutes
                    {{ end }}
                    {{ end }}
                    {{ end }}
                </p>
            </div>
            <div class="form-group">
                <label><strong>Number of Test Steps</strong></label>
                <p class="text-muted">
                    {{ len .Steps }}
                </p>
            </div>
            {{ end }}
            {{ with .TestCase }}
            <div class="form-group">
                <label><strong>Labels</strong></label>
                <p>
                    {{ range .Labels }}
                    <span class="badge badge-pill" style="background-color: {{ .Labelcolor }};">
                            {{ .Labelname}}
                        </span>
                    {{ else }}
                    <span class="text-muted">No Labels</span>
                    {{ end }}
                </p>
            </div>
            {{ end }}
        </div>
    </div>
    <script src="/static/js/testcases.js"></script>
    <script src="/static/js/sut-versions-show.js"></script>
    <script>
        $.getScript("/static/js/util/common.js");

        // Send a request to get the sut variants and versions
        var urlSeg = window.location.pathname.split("/");
        var url = urlSeg[0] + "/" + urlSeg[1] + "/" + urlSeg[2] + "/" + urlSeg[3] + "/" + urlSeg[4] + "/" + "json";

        // Request test case to get the sut versions from it
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", url, true);
        xmlhttp.send();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState === 4 && this.status === 200) {
                testcase = JSON.parse(this.responseText);
                tcvn = ({{ .TestCaseVersion.VersionNr }});
                tclength = (testcase.TestCaseVersions).length;
                testCaseVersionIndex = tcvn - tclength;
                testCaseVersionData = testcase.TestCaseVersions[testCaseVersionIndex].Variants;

                // Fill the drop down menu with the variants
                fillVariants(testCaseVersionData, "#inputTestCaseSUTVariants");
                // Update the version list with the versions of the selected variant
                updateVersionShowList();
            }
        };

        // BUTTON HANDLERS
        // Handler for the revert button of older test case versions
        $("#buttonRevertConfirmed").click(function () {

            $('#revertModal').on('hidden.bs.modal', function (e) {
                e.preventDefault();

                /* Send the data using post with element ids*/
                var posting = $.ajax({
                    url: window.location.pathname + "/update?fragment=true",
                    type: "PUT",
                    data: {
                        version: "{{ .TestCaseVersion.VersionNr }}",
                        inputCommitMessage: "Revert test case to version {{ .TestCaseVersion.VersionNr }}",
                        inputTestCaseName: "{{ .TestCase.Name }}",
                        inputTestCaseDescription: "{{ .TestCaseVersion.Description }}",
                        inputTestCasePreconditions: "{{ .TestCaseVersion.Preconditions }}",
                        inputHours: "{{ .TestCaseVersion.DurationHours }}",
                        inputMinutes: "{{ .TestCaseVersion.DurationMin }}"
                    }
                });

                /* Alerts the results */
                posting.done(function (response) {
                    var url = urlify(window.location.pathname)
                        .takeFirstSegments(4)
                        .appendSegment(posting.getResponseHeader("newName"))
                        .toString();

                    $('#tabarea').empty().append(response);
                    history.pushState('data', '', url);
                }).fail(function (response) {
                    $("#modalPlaceholder").empty().append(response.responseText);
                    $('#errorModal').modal('show');
                });
            }).modal('hide');
        });

        $('#buttonDuplicateConfirmed').off('click').click(function() {

            $('#duplicateModal').off('click').one('hidden.bs.modal', function (e) {
                e.preventDefault();

                var url = window.location.pathname + "/duplicate?fragment=true";

                var posting = $.post(url, {
                    inputTestCaseName: $('#inputDuplicateTestCaseName').val(),
                    version: "{{ .TestCaseVersion.VersionNr }}"
                });

                posting.done(function (r) {
                    url = urlify(window.location.pathname)
                        .takeFirstSegments(4)
                        .appendSegment(posting.getResponseHeader("newName"))
                        .toString();

                    $('#tabTestCases').empty().append(r);
                    history.pushState('data', '', url);
                }).fail(function (r) {
                    $("#modalPlaceholder").empty().append(r.responseText);
                    $('#errorModal').modal('show');
                });
            }).modal('hide');
        });
    </script>

</div>
{{ end }}