{% extends "layouts/base.html" %}

{% block title %} Stacks {% endblock %}

<!-- Specific Page CSS goes HERE  -->
{% block stylesheets %}
    <style>
.card-row.card-columns {
    justify-content: flex-start;
}
</style>
{% endblock stylesheets %}

{% block content %}

<main class="content">

    {% include 'includes/navigation.html' %}
        <div class="preloader bg-soft flex-column justify-content-center align-items-center">
            <img class="loader-element animate__animated animate__jackInTheBox" src="/static/assets/img/brand/light.svg" height="90" alt="Volt logo">
        </div>

    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center py-4">
        <div class="d-block mb-4 mb-md-0">
            <nav aria-label="breadcrumb" class="d-none d-md-inline-block">
                <ol class="breadcrumb breadcrumb-dark breadcrumb-transparent">
                    <li class="breadcrumb-item"><a href="#"><span class="fas fa-home"></span></a></li>
                    <li class="breadcrumb-item"><a href="#">Stacks</a></li>
                    <li class="breadcrumb-item active" aria-current="page">List</li>
                </ol>
            </nav>
            <h2 class="h4">All Stacks</h2>
            <p class="mb-0">List all stacks for deploy</p>
        </div>
        <div class="btn-toolbar mb-2 mb-md-0">
            <div class="btn-group">
            <div>
                <button onclick="changeView('table')" class="btn btn-outline-gray" title="change table view">
                <i class="fa fa-table" aria-hidden="true"></i>
                </button>
            </div>
            </div>
        </div>
    </div>
    <div class="table-settings mb-4">
        <div class="row align-items-center justify-content-between">
            <div class="col col-md-6 col-lg-3 col-xl-4">
                <div class="input-group" style="height: 0; overflow: hidden;">
                    <input type="text" autocomplete="off">
                </div>
                <div class="input-group">
                    <span class="input-group-text" id="basic-addon2"><span class="fas fa-search"></span></span>
                    <input type="text" class="form-control" id="search" onkeyup="search()" placeholder="Search" aria-label="Search" aria-describedby="basic-addon2" autocomplete="off">
                </div>
            </div>
            </div>
            </div>



<div class="card border-light shadow-sm table-wrapper table-responsive pt-0 extra-padding-top ">
    <div class="card-row card-columns">
    {% for stack in stacks %}
        {% if "aws" in stack.stack_name or "azure" in stack.stack_name or "gcp" in stack.stack_name or "custom" in stack.stack_name %}
            <a href="{{ url_for('.deploy_stack', stack_id=stack.id)}}" class="card-link">
            <div class="card border-primary border-1 rounded mb-4 m-4" style="max-width: 20rem; min-width: 20rem; min-height: 18rem;">
                <div class="card-header" title="Click to Deploy">
                    {% if stack.icon_path %}
                        <img src="{{ stack.icon_path }}" alt="Icono" width="40" height="40">
                    {% else %}
                        <img src="/static/assets/img/gallery/Providers/{% if 'aws' in stack.stack_name %}aws{% elif 'azure' in stack.stack_name %}azure{% elif 'gcp' in stack.stack_name %}gcp{% else %}custom{% endif %}.svg" alt="Icono" width="40" height="40">
                    {% endif %}
                        <h5 class="card-title limit_words">{{stack.stack_name}}</h5>
                </div>
                <div class="card-body text-primary">
                <p class="card-text" title="Click to Deploy">{{stack.description}}</p>
            </a>
                <div class="card-row card-columns d-flex justify-content-start">
                        <div class="limit_words" title="{{ stack.git_repo }}">
                        <span style="font-size: 12px; font-weight: bold;">Repo:</span>
                        <span class="font-weight-normal">{{ stack.git_repo.split('/')[-1] | replace('.git', '') }}</span>
                        <span class="copy-icon" onclick="copyToClipboard('{{ stack.git_repo | replace('.git', '') }}/tree/{{ stack.branch }}')" title="Copy to clipboard">
                            <i class="far fa-copy"></i>
                        </span>
                        </div>
                        <div class="limit_words" title="{{ stack.branch }}">
                        <span style="font-size: 12px; font-weight: bold;">Branch:</span>
                        <span class="font-weight-normal">
                            {{ stack.branch }}
                        </span>
                        </div>
                        <div title="Sack Details">
                            <span class="icon icon-sm">
                                <a title="Details Stack" class="dropdown-item" href="{{ url_for('.details_stack', stack_id=stack.id)}}"><span class="fas fa-info"></span></a>
                        </span>
                        </div>
                        
                    {% if "yoda" in  current_user.role or "darth_vader" in current_user.role %}
                        <div class="btn-group">
                            <button class="btn btn-link text-dark dropdown-toggle dropdown-toggle-split m-0 p-0" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                <span class="icon icon-sm">
                                    <span class="fas fa-ellipsis-h icon-dark"></span>
                                </span>
                                <span class="sr-only">Toggle Dropdown</span>
                            </button>
                            <div class="dropdown-menu">
                                <a class="dropdown-item" href="{{ url_for('.edit_stack', stack_id=stack.id)}}"><span class="fas fa-edit mr-2"></span>Edit</a>
                                <a class="dropdown-item" data-toggle="modal" data-target="#SyncModalCenter-{{stack.id}}"><span class="fas fa-sync mr-2"></span>sync</a>
                                <a class="dropdown-item text-danger" data-toggle="modal" data-target="#RemoveModalCenter-{{stack.stack_name}}"><span class="fas fa-trash-alt mr-2"></span>Remove</a>
                </div>
                </div>
                <!-- Modal sync-->
                <div class="modal fade" id="SyncModalCenter-{{stack.id}}">
                    <div class="modal-dialog modal-dialog-centered" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title" id="SyncModalLongTitle-{{stack.id}}">Sync Repo
                                                                                                {{stack.stack_name}}</h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div class="modal-body">
                                <p> <strong>This will sync the repository again,
                                    updating any changes to the stack variables and resources,
                                    that could affect all implementations deployed with this stack.
                                </strong></p>
                                <p><strong>Are you sure? </strong></p>
                            </div>
                            <div class="modal-footer">

                                <button type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>

                                <form action="{{ url_for('.resync_stack',view_mode='cards', stack_id=stack.id)}}" onsubmit="return checkForm(this);">
                                    <input type="submit" value="Sync Repo" name="stackButton" class="btn btn-danger">
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Modal Remove-->
                <div class="modal fade" id="RemoveModalCenter-{{stack.stack_name}}">
                    <div class="modal-dialog modal-dialog-centered" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title" id="SyncModalLongTitle-{{stack.stack_name}}">Remove Repo
                                                                                                {{stack.stack_name}}</h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div class="modal-body">
                                <p> <strong>This change will permanently delete the repository.
                                            could affect all deployed implementations.
                                </strong></p>
                                <p><strong>Are you sure? </strong></p>
                            </div>
                            <div class="modal-footer">

                                <button type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>

                                <form action="{{ url_for('.delete_stack', view_mode='cards', stack_name=stack.stack_name)}}">
                                    <input type="submit" value="Remove Repo" class="btn btn-danger">
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                    {% endif %}
                <!-- END Modal -->
                </div>
                        <img src="/static/assets/img/iac_type/{% if 'terragrunt' in stack.iac_type %}terragrunt{% elif 'tofu' in stack.iac_type %}tofu{% else %}tf{% endif %}.svg" alt="Icono" width="30" height="30" title="IaC Type">
                </div>
            </div>
        {% endif %}
    {% endfor %}
    </div>
</div>


                    
    {% include 'includes/footer.html' %}

</main>

{% endblock content %}

<!-- Specific Page JS goes HERE  -->
{% block javascripts %}
<script type="text/javascript">

function checkForm(form) // Submit button clicked
  {

    form.stackButton.disabled = true;
    form.stackButton.value = "Please wait...";
    return true;
  }

</script>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        var searchInput = document.getElementById('search');

        searchInput.addEventListener('keyup', function() {
            var searchTerm = searchInput.value.toLowerCase();
            var cards = document.getElementsByClassName('card');

            for (var i = 0; i < cards.length; i++) {
                var card = cards[i];
                if (card.textContent.toLowerCase().includes(searchTerm)) {
                    card.style.display = "";
                } else {
                    card.style.display = "none";
                }
            }
        });
    });
</script>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        var searchInput = document.getElementById('search');
        var savedSearchValue = localStorage.getItem('searchValue');

        if (savedSearchValue) {
            searchInput.value = savedSearchValue;
            filterCards(savedSearchValue);
        }

        searchInput.addEventListener('keyup', function () {
            var searchTerm = searchInput.value.toLowerCase();
            filterCards(searchTerm);

            // Save the search value to localStorage
            localStorage.setItem('searchValue', searchTerm);
        });

        function filterCards(searchTerm) {
            var cards = document.getElementsByClassName('card');

            for (var i = 0; i < cards.length; i++) {
                var card = cards[i];
                var cardText = card.textContent || card.innerText;

                if (cardText.toLowerCase().includes(searchTerm)) {
                    card.style.display = "";
                } else {
                    card.style.display = "none";
                }
            }
        }
    });
</script>

<script src="/static/assets/js/copy_clipboard.js"></script>
<script src="/static/assets/js/selector_stacks.js"></script>
{% endblock javascripts %}
