{% extends "base.html" %}

{% block content %}
<div class="container mt-5">
    <div class="card mb-4">
        <div class="card-header">
            <h1 class="card-title">{{ tender.title }}</h1>
        </div>
        <div class="card-body">
            <p><strong>Company:</strong> {{ tender.company.name }}</p>
            <p><strong>Description:</strong></p>
            <p>{{ tender.description }}</p>
            <p><strong>Deadline:</strong> {{ tender.deadline }}</p>
            <p><strong>Status:</strong> <span class="badge bg-info">{{ tender.get_status_display }}</span></p>
        </div>
        <div class="card-footer">
            <a href="{% url 'tender_list' tender.company.id %}" class="btn btn-secondary">Back to Tenders</a>
            <a href="{% url 'update_tender' tender.id %}" class="btn btn-primary">Update</a>
            <a href="{% url 'delete_tender' tender.id %}" class="btn btn-danger">Delete</a>
        </div>
    </div>

    <div class="card mb-4">
        <div class="card-header">
            <h2 class="h4">Workflow</h2>
        </div>
        <div class="card-body">
            {% if request.user.is_authenticated and request.user.groups.all|length > 0 %}
                {% if 'Head of Bids' in request.user.groups.all.0.name or 'Bid Administrator' in request.user.groups.all.0.name %}
                    {% if next_status %}
                        <p>The current step is complete. Ready to move to the next step.</p>
                        <a href="{% url 'update_tender_status' tender.id next_status.0 %}" class="btn btn-success">Approve and move to: {{ next_status.1 }}</a>
                    {% else %}
                        <p>This tender is in the final workflow step.</p>
                    {% endif %}
                {% endif %}
            {% else %}
                <p>You do not have permission to advance the workflow.</p>
            {% endif %}
        </div>
    </div>

    <div class="card mb-4">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h2 class="h4">Bids</h2>
            <a href="{% url 'create_bid' tender.id %}" class="btn btn-primary">Create Bid</a>
        </div>
        <div class="card-body">
            {% if bids %}
                <ul class="list-group">
                    {% for bid in bids %}
                        <li class="list-group-item d-flex justify-content-between align-items-center">
                            <div>
                                <span>{{ bid.company.name }} - ${{ bid.amount }}</span>
                                {% with approval=bid.approval_set.first %}
                                    {% if approval %}
                                        <span class="badge bg-{% if approval.status == 'approved' %}success{% elif approval.status == 'rejected' %}danger{% else %}secondary{% endif %}">{{ approval.get_status_display }}</span>
                                    {% else %}
                                        <span class="badge bg-secondary">Pending</span>
                                    {% endif %}
                                {% endwith %}
                            </div>
                            <div>
                                <a href="{% url 'approve_bid' bid.id %}" class="btn btn-sm btn-outline-primary">Review</a>
                                {% if request.user.membership_set.first.company == bid.company %}
                                    <a href="{% url 'update_bid' bid.id %}" class="btn btn-sm btn-outline-secondary">Update</a>
                                    <a href="{% url 'delete_bid' bid.id %}" class="btn btn-sm btn-outline-danger">Delete</a>
                                {% endif %}
                            </div>
                        </li>
                    {% endfor %}
                </ul>
            {% else %}
                <p>No bids yet.</p>
            {% endif %}
        </div>
    </div>

    <div class="card mb-4">
        <div class="card-header">
            <h2 class="h4">Documents</h2>
        </div>
        <div class="card-body">
            <form method="post" enctype="multipart/form-data" class="mb-4">
                {% csrf_token %}
                {{ doc_form.as_p }}
                <button type="submit" name="submit_document" class="btn btn-primary">Upload Document</button>
            </form>

            {% if documents %}
                <ul class="list-group">
                    {% for doc in documents %}
                        <li class="list-group-item d-flex justify-content-between align-items-center">
                            <a href="{{ doc.file.url }}" target="_blank">{{ doc.file.name }}</a>
                            <small class="text-muted">by {{ doc.uploaded_by.username }} on {{ doc.uploaded_at|date:"Y-m-d" }}</small>
                        </li>
                    {% endfor %}
                </ul>
            {% else %}
                <p>No documents yet.</p>
            {% endif %}
        </div>
    </div>

    <div class="card">
        <div class="card-header">
            <h2 class="h4">Notes</h2>
        </div>
        <div class="card-body">
            <form method="post" class="mb-4">
                {% csrf_token %}
                {{ note_form.as_p }}
                <button type="submit" name="submit_note" class="btn btn-primary">Add Note</button>
            </form>

            {% if notes %}
                <ul class="list-group">
                    {% for note in notes %}
                        <li class="list-group-item">
                            <p class="mb-1">{{ note.note }}</p>
                            <small class="text-muted">by {{ note.user.username }} on {{ note.created_at|date:"Y-m-d" }}</small>
                        </li>
                    {% endfor %}
                </ul>
            {% else %}
                <p>No notes yet.</p>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
