{{/*
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="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 sequence.
                        </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 sequence 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 sequence.</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 sequence 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 sequence.</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 sequence 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 sequence.</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 sequence as a task to a team member.</td>
                    </tr>
                    <tr>
                        <td>
                            <button class="btn btn-success" disabled>
                                <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 sequence.</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="modal fade" id="deleteTestSequenceModal">
    <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 Sequence</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 sequence? This is irreversible!</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" id="buttonDeleteTestsequenceConfirmed">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">Revert Test Sequence</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 sequence 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> Duplicate Test Sequence</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 sequence up to the currently shown version.
                    The test sequence duplicate needs a unique name.</p>
                <input class="form-control" id="inputDuplicateTestSequenceName"
                       placeholder="Enter name for test sequence 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="tab-card card" id="tabTestsequences">
    <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-sm-inline"> Back</span>
            </button>
            {{ if .LoggedIn }}
            <button class="btn btn-danger mr-2" id="buttonDelete" data-toggle="modal"
                    data-target="#deleteTestSequenceModal">
                <i class="fa fa-trash-o" aria-hidden="true"></i>
                <span class="d-none d-sm-inline"> Delete</span>
            </button>
            <button class="btn btn-primary mr-2" id="buttonDuplicate" formaction="duplicate" disabled data-toggle="tooltip" data-placement="bottom" title="" data-original-title="This feature will be available in a future version">
                    <!-- data-toggle="modal" data-target="#duplicateModal" >-->
                <i class="fa fa-clone" aria-hidden="true"></i>
                <span class="d-none d-sm-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-sm-inline"> History</span>
            </button>
            {{ if .LoggedIn }}
            {{ if lt .TestSequenceVersion.VersionNr (len .TestSequence.SequenceVersions) }}
            <button class="btn btn-primary mr-2" 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-sm-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-sm-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-sm-inline"> Assign</span>
            </button>
            {{ with .TestSequenceVersion }}
            {{ if .Cases }}
            <button class="btn btn-success" id="buttonExecute" >
                <i class="fa fa-play" aria-hidden="true"></i>
                <span class="d-none d-sm-inline"> Start</span>
            </button>
            {{ end }}
            {{ end }}
        </div>
    </nav>
    <div class="row tab-side-bar-row">
        <div class="col-md-9 p-3">
            {{ if ne .TestSequenceVersion.VersionNr (index .TestSequence.SequenceVersions 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 sequence
            </div>
            {{ end }}
            <h4 class="mb-3">{{ .TestSequence.Name }}</h4>
            {{ with .TestSequenceVersion }}
            <div class="form-group">
                <label><strong>Test Sequence Description</strong></label>
                <p class="text-muted">
                    {{ with .Description }}
                        {{ . }}
                    {{ else }}
                        No Description
                    {{ end }}
                </p>
            </div>
            <div class="form-group">
                <label><strong>Test Sequence Preconditions</strong></label>
                <p class="text-muted">
                    {{ with .Preconditions }}
                        {{ . }}
                    {{ else }}
                        No Conditions
                    {{ end }}
                </p>
            </div>
            <div class="form-group">
                <label><strong>Test Cases</strong></label>
                {{ if .Cases }}
                    <ul class="list-group">
                        {{ range .Cases }}
                        <li class="list-group-item">{{ .Name }}</li>
                        {{ end }}
                    </ul>
                {{ 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">
                <div class="form-group">
                    <div class="form-group">
                        <p class="text-muted" id="inputSUTVersion">
                            <abbr title="The first version of the system under test which can be tested by this version of the test sequence">
                                <strong>From SUT-Version</strong>
                            </abbr>
                            {{ .SequenceInfos.SUTVersionFrom }}
                        </p>
                        <p class="text-muted" id="inputSUTVersionTo">
                            <abbr title="The last version of the system under test which can be tested by this version of the test sequence">
                                <strong>To SUT-Version</strong>
                            </abbr>
                            {{ .SequenceInfos.SUTVersionTo }}
                        </p>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label><strong>Estimated Test Duration</strong></label>
                <p class="text-muted">
                    {{ if and (eq .SequenceInfos.DurationHours 0) (eq .SequenceInfos.DurationMin 0) }}
                        No Test Duration
                    {{ else }}
                        {{ if gt .SequenceInfos.DurationHours 0 }}
                            {{ .SequenceInfos.DurationHours }}
                             {{ if eq .SequenceInfos.DurationHours 1 }} Hour {{ else }} Hours {{ end }}
                        {{ end }}
                        {{ if gt .SequenceInfos.DurationMin 0 }}
                             {{ .SequenceInfos.DurationMin }}
                            {{ if eq .SequenceInfos.DurationMin 1 }}
                                Minute
                            {{ else }}
                                Minutes
                            {{ end }}
                        {{ end }}
                    {{ end }}
                </p>
            </div>
            <div class="form-group">
                <label><strong>Number of Test Cases</strong></label>
                <p class="text-muted">
                    {{ len .Cases }}
                </p>
            </div>
            {{ end }}
            {{ with .TestSequence }}
            <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 type='text/javascript'>
        $.getScript("/static/js/util/common.js");

        $("#buttonRevertConfirmed").click(revertTestSequence);

        // Reverts to selected older version of a testsequence.
        function revertTestSequence() {
            $('#revertModal').on('hidden.bs.modal', revertAjaxRequest).modal('hide');
        }

        function revertAjaxRequest(e) {
            e.preventDefault();

            /* Send the data using post with element ids*/
            var posting = $.ajax({
                url: window.location.pathname + "/update?fragment=true",
                type: "PUT",
                data: getRevertData()
            });

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

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

        function getRevertData() {
            return {
                //{{ with .TestSequenceVersion }}
                version: "{{ .VersionNr }}",
                inputCommitMessage: "Revert test case to version {{ .VersionNr }}",
                inputTestSequenceDescription:"{{ .Description }}",
                inputTestSequencePreconditions: "{{ .Preconditions }}",
                //{{ end }}
                //{{ with .TestSequence }}
                inputTestSequenceName: "{{ .Name }}"
                //{{ end }}
            }
        }
    </script>
    <script src="/static/js/testsequences.js"></script>
</div>
{{end}}
