{% extends "layouts/base.html" %}
{% block title %}View Call Campaign{% endblock %}

{% block extra_css %}
<style>
    body {
        background: #f5f7fb;
    }

    .campaigns-container {
        /* padding: 32px 24px 80px; */
        max-width: 950px;
        margin: 0 auto;
    }

    .campaign-header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 16px;
        margin-bottom: 24px;
    }

    .campaign-header h2 {
        margin: 0 0 6px;
        font-size: 28px;
        font-weight: 700;
        color: #0f172a;
    }

    .campaign-header p {
        margin: 0;
        font-size: 15px;
        color: #475569;
    }

    .back-link {
        padding: 11px 18px;
        border-radius: 12px;
        text-decoration: none;
        border: 1px solid rgba(148, 163, 184, 0.32);
        color: #0f172a;
        font-weight: 600;
        display: inline-flex;
        align-items: center;
        gap: 8px;
        transition: all 0.2s ease;
        background: #fff;
    }

    .back-link:hover {
        color: #2563eb;
        border-color: rgba(37, 99, 235, 0.45);
        box-shadow: 0 10px 20px rgba(37, 99, 235, 0.18);
    }

    .meta-bar {
        background: #ffffff;
        border-radius: 16px;
        padding: 18px 22px;
        margin-bottom: 24px;
        display: flex;
        gap: 24px;
        align-items: center;
        box-shadow: 0 25px 50px rgba(15, 23, 42, 0.05);
        border: 1px solid rgba(148, 163, 184, 0.12);
    }

    .meta-item {
        display: flex;
        flex-direction: column;
        gap: 6px;
    }

    .meta-label {
        font-size: 12px;
        text-transform: uppercase;
        color: #94a3b8;
        letter-spacing: 0.08em;
        font-weight: 600;
    }

    .meta-value {
        font-size: 16px;
        font-weight: 600;
        color: #0f172a;
    }

    .table-card {
        background: #ffffff;
        border-radius: 18px;
        box-shadow: 0 35px 65px rgba(15, 23, 42, 0.06);
        overflow: scroll;
        border: 1px solid rgba(148, 163, 184, 0.12);
    }

    .table-title-bar {
        padding: 18px 24px;
        border-bottom: 1px solid rgba(148, 163, 184, 0.18);
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .table-title-bar h3 {
        margin: 0;
        font-size: 18px;
        font-weight: 600;
        color: #0f172a;
    }

    .data-table {
        width: 100%;
        border-collapse: separate;
        border-spacing: 0;
    }

    .data-table thead th {
        background: #f8fafc;
        padding: 16px 20px;
        text-align: left;
        font-size: 13px;
        letter-spacing: 0.04em;
        text-transform: uppercase;
        color: #64748b;
        border-bottom: 1px solid rgba(148, 163, 184, 0.18);
    }

    .data-table tbody tr {
        transition: transform 0.2s ease, box-shadow 0.2s ease;
    }

    .data-table tbody tr:hover {
        transform: translateY(-2px);
        box-shadow: 0 12px 24px rgba(15, 23, 42, 0.08);
    }

    .data-table tbody tr + tr td {
        border-top: 1px solid rgba(148, 163, 184, 0.12);
    }

    .data-table tbody td {
        padding: 18px 20px;
        font-size: 15px;
        color: #1f2937;
        background: #ffffff;
        vertical-align: middle;
    }

    .contact-name {
        font-weight: 600;
        color: #0f172a;
    }

    .campaign-status-pill {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        padding: 6px 14px;
        border-radius: 999px;
        font-size: 13px;
        font-weight: 600;
        background: rgba(14, 116, 144, 0.08);
        color: #0f172a;
    }

    .status-pending { background: rgba(245, 158, 11, 0.16); color: #b45309; }
    .status-in-progress { background: rgba(14, 165, 233, 0.16); color: #0369a1; }
    .status-completed { background: rgba(34, 197, 94, 0.16); color: #15803d; }
    .status-failed { background: rgba(249, 115, 22, 0.16); color: #c2410c; }

    .outcome-badge {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        padding: 6px 12px;
        border-radius: 999px;
        font-size: 12px;
        font-weight: 600;
        text-transform: capitalize;
    }

    .outcome-booking { background: rgba(34, 197, 94, 0.16); color: #15803d; }
    .outcome-callback { background: rgba(14, 165, 233, 0.16); color: #0369a1; }
    .outcome-do-not-call { background: rgba(239, 68, 68, 0.16); color: #dc2626; }
    .outcome-declined { background: rgba(249, 115, 22, 0.16); color: #c2410c; }
    .outcome-maybe { background: rgba(168, 85, 247, 0.16); color: #7c3aed; }
    .outcome-interested { background: rgba(59, 130, 246, 0.16); color: #2563eb; }
    .outcome-no-decision { background: rgba(148, 163, 184, 0.16); color: #64748b; }

    .filters-toolbar {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
        gap: 16px;
        padding: 16px 20px;
        background: #f8fafc;
        border-radius: 16px;
        border: 1px solid rgba(148, 163, 184, 0.18);
        margin-bottom: 20px;
    }

    .filters-toolbar .filter-input {
        display: flex;
        flex-direction: column;
        gap: 6px;
    }

    .filters-toolbar .filter-input label {
        font-size: 12px;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        color: #64748b;
        font-weight: 600;
    }

    .filters-toolbar .input-wrapper {
        position: relative;
        display: flex;
        align-items: center;
        background: #ffffff;
        border-radius: 12px;
        border: 1px solid #dbe3ee;
        padding: 10px 12px;
        box-shadow: 0 10px 24px rgba(15, 23, 42, 0.05);
        transition: border 0.2s ease, box-shadow 0.2s ease;
    }

    .filters-toolbar .input-wrapper:focus-within {
        border-color: #2563eb;
        box-shadow: 0 12px 26px rgba(37, 99, 235, 0.18);
    }

    .filters-toolbar .input-wrapper i {
        margin-right: 10px;
        color: #94a3b8;
        font-size: 16px;
    }

    .filters-toolbar .input-wrapper input,
    .filters-toolbar .input-wrapper select {
        flex: 1;
        border: none;
        background: transparent;
        outline: none;
        font-size: 14px;
        color: #0f172a;
    }

    .filters-toolbar .input-wrapper select {
        appearance: none;
        padding-right: 18px;
        cursor: pointer;
    }

    .filters-toolbar .input-wrapper.input-select {
        position: relative;
    }

    .filters-toolbar .input-wrapper.input-select::after {
        content: '▼';
        position: absolute;
        right: 12px;
        top: 50%;
        transform: translateY(-50%);
        color: #94a3b8;
        font-size: 10px;
        pointer-events: none;
    }

    .filters-toolbar .filter-action {
        display: flex;
        align-items: flex-end;
    }

    .filters-toolbar .btn {
        padding: 10px 20px;
        border-radius: 12px;
        border: 1px solid #dbe3ee;
        background: #ffffff;
        color: #0f172a;
        font-size: 14px;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.2s ease;
        display: flex;
        align-items: center;
        gap: 8px;
        box-shadow: 0 10px 24px rgba(15, 23, 42, 0.05);
    }

    .filters-toolbar .btn:hover {
        border-color: #2563eb;
        color: #2563eb;
        box-shadow: 0 12px 26px rgba(37, 99, 235, 0.18);
    }

    @media (max-width: 768px) {
        .filters-toolbar {
            grid-template-columns: 1fr;
        }
    }

    .audio-cell {
        display: flex;
        align-items: center;
        gap: 12px;
    }

    .audio-cell audio {
        border-radius: 12px;
        background: #f8fafc;
    }

    .audio-cell a {
        color: #2563eb;
        font-weight: 600;
        text-decoration: none;
    }

    .audio-cell a:hover {
        text-decoration: underline;
    }

    @media (max-width: 768px) {
        .campaigns-container {
            padding: 24px 18px 60px;
        }

        .campaign-header {
            flex-direction: column;
            align-items: stretch;
        }

        .meta-bar {
            flex-direction: column;
            align-items: flex-start;
        }

        .data-table thead {
            display: none;
        }

        .data-table,
        .data-table tbody,
        .data-table tr,
        .data-table td {
            display: block;
            width: 100%;
        }

        .data-table tbody tr {
            margin-bottom: 16px;
            border-radius: 16px;
            box-shadow: 0 12px 24px rgba(15, 23, 42, 0.08);
        }

        .data-table tbody td {
            padding: 14px 18px;
            position: relative;
        }

        .data-table tbody td::before {
            content: attr(data-label);
            display: block;
            margin-bottom: 6px;
            font-size: 12px;
            font-weight: 600;
            color: #94a3b8;
            text-transform: uppercase;
            letter-spacing: 0.06em;
        }

        .audio-cell {
            flex-direction: column;
            align-items: flex-start;
        }
    }
</style>
{% endblock %}
{% block content %}
<div class="campaigns-container mt-5">
    <div class="campaign-header">
        <div>
            <h2>{{ campaign[0] }}</h2>
            <p>Review call results and follow up with every contact in the campaign.</p>
        </div>
        <a href="{{ url_for('company.call_campaign') }}" class="back-link">
            <i class="fa-light fa-arrow-left"></i> Back to Campaigns
        </a>
    </div>

    <div class="meta-bar">
        <div class="meta-item">
            <span class="meta-label">Campaign Name</span>
            <span class="meta-value">{{ campaign[0] }}</span>
        </div>
        <div class="meta-item">
            <span class="meta-label">Created</span>
            <span class="meta-value">{{ campaign[1] }}</span>
        </div>
    </div>

    <div class="filters-toolbar">
        <div class="filter-input">
            <label for="filterContactName">Contact Name</label>
            <div class="input-wrapper">
                <i class="fa-light fa-magnifying-glass"></i>
                <input type="text" id="filterContactName" placeholder="Search contact name">
            </div>
        </div>
        <div class="filter-input">
            <label for="filterPhone">Phone</label>
            <div class="input-wrapper">
                <i class="fa-light fa-phone"></i>
                <input type="text" id="filterPhone" placeholder="Search phone number">
            </div>
        </div>
        <div class="filter-input">
            <label for="filterOutcome">Outcome</label>
            <div class="input-wrapper input-select">
                <i class="fa-light fa-filter"></i>
                <select id="filterOutcome">
                    <option value="all">All outcomes</option>
                    <option value="booking">Booking</option>
                    <option value="callback">Callback</option>
                    <option value="do_not_call">Do Not Call</option>
                    <option value="declined">Declined</option>
                    <option value="maybe">Maybe</option>
                    <option value="interested">Interested</option>
                    <option value="no_decision">No Decision</option>
                    <option value="none">Not Analyzed</option>
                </select>
            </div>
        </div>
        <div class="filter-action">
            <button id="refreshCalls" class="btn btn-secondary">
                <i class="fa-light fa-rotate"></i>
                <span>Refresh</span>
            </button>
        </div>
    </div>

    <div class="table-card">
        <div class="table-title-bar">
            <h3>Call Attempts</h3>
        </div>
        <table class="data-table" id="calls-table">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Contact Name</th>
                    <th>Phone</th>
                    <th>Status</th>
                    <th>Outcome</th>
                    <th>Attempts</th>
                    <th>Error</th>
                    <th>Transcript</th>
                    <th>Audio</th>
                </tr>
            </thead>
            <tbody>
                {% for call in calls %}
                <tr>
                    <td data-label="#">{{ loop.index }}</td>
                    <td data-label="Contact Name" class="contact-name" data-contact-name="{{ call[1] }}">{{ call[1] }}</td>
                    <td data-label="Phone" data-phone="{{ call[2] }}">{{ call[2] }}</td>
                    <td data-label="Status">
                        <span class="campaign-status-pill status-{{ call[3]|lower|replace(' ', '-') }}">
                            <i class="fa-solid fa-circle"></i>
                            {{ call[3] }}
                        </span>
                    </td>
                    <td data-label="Outcome" data-outcome="{{ (call[8] or 'none')|lower }}">
                        {% if call[8] %}
                        <span class="outcome-badge outcome-{{ call[8]|lower|replace('_', '-') }}">
                            {{ call[8]|replace('_', ' ')|title }}
                        </span>
                        {% else %}
                        <span class="outcome-badge outcome-no-decision" style="opacity: 0.6;">Not Analyzed</span>
                        {% endif %}
                    </td>
                    <td data-label="Attempts">{{ call[4] }}</td>
                    <td data-label="Error">{{ call[5] or '' }}</td>
                    <td data-label="Transcript">
                        <a href="{{ url_for('company.view_call_transcript', call_id=call[0]) }}">View</a>
                    </td>
                    <td data-label="Audio">
                        <div class="audio-cell">
                            {% if call[7] %}
                            <audio controls style="width: 200px;">
                                <source src="{{ url_for('static', filename=call[7].replace('static/', '')) }}" type="audio/mp3">
                                Your browser does not support the audio element.
                            </audio>
                            <!-- <a href="{{ url_for('static', filename=call[7].replace('static/', '')) }}" download>Download</a> -->
                            {% else %}
                            N/A
                            {% endif %}
                        </div>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    (function() {
        const tableBody = document.querySelector('#calls-table tbody');
        const filterContactName = document.getElementById('filterContactName');
        const filterPhone = document.getElementById('filterPhone');
        const filterOutcome = document.getElementById('filterOutcome');
        const refreshButton = document.getElementById('refreshCalls');

        const callRows = Array.from(document.querySelectorAll('#calls-table tbody tr')).map(row => {
            const cells = row.querySelectorAll('td');
            return {
                row: row,
                contactName: (cells[1]?.getAttribute('data-contact-name') || cells[1]?.textContent || '').trim().toLowerCase(),
                phone: (cells[2]?.getAttribute('data-phone') || cells[2]?.textContent || '').trim().toLowerCase(),
                outcome: (cells[4]?.getAttribute('data-outcome') || 'none').toLowerCase()
            };
        });

        function filterCalls() {
            const contactSearch = (filterContactName.value || '').trim().toLowerCase();
            const phoneSearch = (filterPhone.value || '').trim().toLowerCase();
            const outcomeFilter = (filterOutcome.value || 'all').toLowerCase();

            callRows.forEach(call => {
                let show = true;

                if (contactSearch && !call.contactName.includes(contactSearch)) {
                    show = false;
                }

                if (phoneSearch && !call.phone.includes(phoneSearch)) {
                    show = false;
                }

                if (outcomeFilter !== 'all') {
                    if (outcomeFilter === 'none') {
                        if (call.outcome !== 'none') {
                            show = false;
                        }
                    } else {
                        // Normalize outcome values for comparison
                        const normalizedOutcome = call.outcome.replace(/-/g, '_');
                        const normalizedFilter = outcomeFilter.replace(/-/g, '_');
                        if (normalizedOutcome !== normalizedFilter) {
                            show = false;
                        }
                    }
                }

                call.row.style.display = show ? '' : 'none';
            });
        }

        filterContactName.addEventListener('input', filterCalls);
        filterPhone.addEventListener('input', filterCalls);
        filterOutcome.addEventListener('change', filterCalls);
        refreshButton.addEventListener('click', () => {
            filterContactName.value = '';
            filterPhone.value = '';
            filterOutcome.value = 'all';
            filterCalls();
        });
    })();
</script>
{% endblock %}