{% extends '@MauticCore/Default/content.html.twig' %}

{% block headerTitle 'Deal Stages' %}

{% block actions %}
    {% if canEditSettings %}
        {% set actionReturnUrl = path('mautic_dealflow_stage_index', {'pipelineId': selectedPipeline.getId()}) %}
        <a
            class="btn btn-primary"
            href="{{ path('mautic_dealflow_stage_new', {'pipelineId': selectedPipeline.getId(), 'returnUrl': actionReturnUrl}) }}"
            data-toggle="ajaxmodal"
            data-target="#MauticSharedModal"
            data-header="New Deal Stage"
            data-href="{{ path('mautic_dealflow_stage_new', {'pipelineId': selectedPipeline.getId(), 'modal': 1, 'contentOnly': 1, 'returnUrl': actionReturnUrl}) }}"
        >
            <i class="ri-add-line"></i>
            New Stage
        </a>
    {% endif %}
{% endblock %}

{% block content %}
    {{ include('@DealFlow/_styles.html.twig') }}
    {{ include('@DealFlow/Deal/_nav.html.twig') }}

    {% if pipelines|length > 1 %}
        <form class="form-inline mb-lg" method="get">
            <div class="form-group">
                <label class="mr-sm" for="dealflow_stage_pipeline">Pipeline</label>
                <select class="form-control" id="dealflow_stage_pipeline" name="pipelineId" onchange="this.form.submit()">
                    {% for pipeline in pipelines %}
                        <option value="{{ pipeline.getId() }}" {% if selectedPipeline and pipeline.getId() == selectedPipeline.getId() %}selected{% endif %}>
                            {{ pipeline.getName() }}
                        </option>
                    {% endfor %}
                </select>
            </div>
        </form>
    {% endif %}

    <table class="table table-hover dealflow-admin-table">
        <thead>
        <tr>
            <th>Name</th>
            <th>Slug</th>
            <th>Type</th>
            <th>Probability</th>
            <th>Sort</th>
            <th>Active</th>
            <th>Order</th>
            <th></th>
        </tr>
        </thead>
        <tbody>
        {% for item in items %}
            <tr>
                <td>{{ item.getName() }}</td>
                <td>{{ item.getSlug() }}</td>
                <td>{{ item.getStageType() }}</td>
                <td>{{ item.getProbability() }}%</td>
                <td>{{ item.getSortOrder() }}</td>
                <td>{{ item.isPublished() ? 'Yes' : 'No' }}</td>
                <td>
                    {% if canEditSettings %}
                        <div class="btn-group btn-group-xs">
                            <form method="post" action="{{ path('mautic_dealflow_stage_move', {'objectId': item.getId(), 'direction': 'up', 'pipelineId': selectedPipeline.getId()}) }}" style="display:inline-block;">
                                <input type="hidden" name="_token" value="{{ csrf_token('dealflow_stage_move_' ~ item.getId() ~ '_up') }}">
                                <button class="btn btn-default" type="submit" {% if loop.first %}disabled{% endif %}>Up</button>
                            </form>
                            <form method="post" action="{{ path('mautic_dealflow_stage_move', {'objectId': item.getId(), 'direction': 'down', 'pipelineId': selectedPipeline.getId()}) }}" style="display:inline-block;">
                                <input type="hidden" name="_token" value="{{ csrf_token('dealflow_stage_move_' ~ item.getId() ~ '_down') }}">
                                <button class="btn btn-default" type="submit" {% if loop.last %}disabled{% endif %}>Down</button>
                            </form>
                        </div>
                    {% endif %}
                </td>
                <td>
                    {% if canEditSettings %}
                        <a href="{{ path('mautic_dealflow_stage_edit', {'objectId': item.getId(), 'pipelineId': selectedPipeline.getId()}) }}">Edit</a>
                        <span class="text-muted">|</span>
                        <a href="{{ path('mautic_dealflow_stage_delete', {'objectId': item.getId(), 'pipelineId': selectedPipeline.getId()}) }}" onclick="return confirm('Are you sure you want to delete this item? This action cannot be undone.')">Delete</a>
                    {% endif %}
                </td>
            </tr>
        {% else %}
            <tr>
                <td colspan="8" class="text-muted">No stages configured.</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
{% endblock %}
