<!-- templates/stock/report.html -->
{% extends "base.html" %}
{% load static %}

{% block content %}
<div class="min-h-screen bg-gray-100 py-6">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <!-- Header -->
        <div class="mb-8">
            <h1 class="text-3xl font-bold text-gray-900">
                Report
            </h1>
            <p class="mt-2 text-sm text-gray-700">
                Pilih rentang tanggal untuk melihat laporan
            </p>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-4 gap-6 items-start">
            <!-- Date Range Form -->
            <div class="md:col-span-2 bg-white rounded-lg shadow p-6 h-auto max-h-screen flex flex-col overflow-hidden">
                <form method="get" class="space-y-4">
                    {% if error %}
                    <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative" role="alert">
                        <span class="block sm:inline">{{ error }}</span>
                    </div>
                    {% endif %}

                    <div class="grid grid-cols-1 md:grid-cols-2 gap-10">
                        <div>
                            <label for="start_date" class="block text-sm font-medium text-gray-700">
                                Tanggal Awal
                            </label>
                            <input type="date" name="start_date" id="start_date" value="{{ start_date }}" required
                                class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
                        </div>

                        <div>
                            <label for="end_date" class="block text-sm font-medium text-gray-700">
                                Tanggal Akhir
                            </label>
                            <input type="date" name="end_date" id="end_date" value="{{ end_date }}" required
                                class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500">
                        </div>
                    </div>

                    <div class="flex justify-end">
                        <button type="submit"
                            class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
                            Tampilkan Laporan
                        </button>
                    </div>
                </form>
            </div>

            {% if has_results %}
            <!-- Results Table -->
            <div class="bg-white rounded-lg shadow overflow-hidden md:col-span-4">
                <div class="px-4 py-5 sm:p-6">
                    <!-- Table Controls -->
                    <div class="mb-4 flex justify-end space-x-2">
                        <button onclick="exportExcel()"
                            class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 no-print">
                            <svg class="h-5 w-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                    d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
                            </svg>
                            Export Excel
                        </button>
                        <button onclick="printPDF()"
                            class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-green-600 hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500 no-print">
                            <svg class="h-4 w-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                    d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z">
                                </path>
                            </svg>
                            Export PDF
                        </button>
                    </div>

                    <h2 class="text-xl font-medium text-gray-800 my-2">Laporan Pengeluaran</h2>
                    <!-- Table -->
                    <div class="overflow-x-auto">
                        <table class="min-w-full divide-y divide-gray-200">
                            <thead>
                                <tr class="bg-gray-50">
                                    <th
                                        class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Tanggal
                                    </th>
                                    <th
                                        class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Produk
                                    </th>
                                    <th
                                        class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Kategori
                                    </th>
                                    <th
                                        class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Supplier
                                    </th>
                                    <th
                                        class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Qty
                                    </th>
                                    <th
                                        class="text-right px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Harga
                                    </th>
                                    <th
                                        class="text-right px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Subtotal
                                    </th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-gray-200">
                                {% if stocks %}
                                {% for stock in stocks %}
                                <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100">
                                    <td class="px-6 py-3 whitespace-nowrap text-sm text-gray-900">
                                        {{ stock.date_in|date:"d/m/Y"}}
                                    </td>
                                    <td class="px-6 py-3 whitespace-nowrap text-sm text-gray-900">
                                        {{ stock.product }}
                                    </td>
                                    <td class="px-6 py-3 whitespace-nowrap text-sm text-gray-900">
                                        {{ stock.category }}
                                    </td>
                                    <td class="px-6 py-3 whitespace-nowrap text-sm text-gray-900">
                                        {{ stock.supplier }}
                                    </td>
                                    <td class="px-6 py-3 whitespace-nowrap text-sm text-gray-900">
                                        {{ stock.stock_in }}
                                    </td>
                                    <td class="price text-right px-6 py-3 whitespace-nowrap text-sm text-gray-900"
                                        data-value="{{ stock.product.price }}">
                                        {{ stock.price}}
                                    </td>
                                    <td class="subtotal text-right px-6 py-3 whitespace-nowrap text-sm text-gray-900"
                                        data-value="{{ stock.subtotal}}">
                                        {{ stock.subtotal }}
                                    </td>
                                </tr>
                                {% endfor %}
                                {% else %}
                                <tr>
                                    <td class="text-center text-sm py-3" colspan="100%">No products found for
                                        the selected date range.</td>
                                </tr>
                                {% endif %}
                            </tbody>
                            <tfoot>
                                <tr class="bg-gray-50">
                                    <td colspan="6"
                                        class="text-right px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900 text-right">
                                        Total:
                                    </td>
                                    <td class="total text-right px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900"
                                        data-value="{{ stock_total }}">
                                        {{ stock_total }}
                                    </td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
                <div class="px-4 py-5 sm:p-6">
                    <h2 class="text-xl font-medium text-gray-800 my-2">Laporan Pemasukan</h2>

                    <!-- Table -->
                    <div class="overflow-x-auto">
                        <table class="min-w-full divide-y divide-gray-200">
                            <thead>
                                <tr class="bg-gray-50">
                                    <th
                                        class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Tanggal
                                    </th>
                                    <th
                                        class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Kode Transaksi
                                    </th>
                                    <th
                                        class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Nama Produk
                                    </th>
                                    <th
                                        class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Kategori
                                    </th>
                                    <th
                                        class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Employee
                                    </th>
                                    <th
                                        class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Customer
                                    </th>
                                    <th
                                        class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Qty
                                    </th>
                                    <th
                                        class="text-right px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Price
                                    </th>
                                    <th
                                        class="text-right px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Subtotal
                                    </th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-gray-200">
                                {% if transaction_items %}
                                {% for item in transaction_items %}
                                <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100">
                                    <td class="px-6 py-3 whitespace-nowrap text-sm text-gray-900">
                                        {{ item.updated_at|date:"d/m/Y" }}
                                    </td>
                                    <td class="px-6 py-3 whitespace-nowrap text-sm text-gray-900">
                                        {{ item.transaction_id }}
                                    </td>
                                    <td class="px-6 py-3 whitespace-nowrap text-sm text-gray-900">
                                        {{ item.product }}
                                    </td>
                                    <td class="px-6 py-3 whitespace-nowrap text-sm text-gray-900">
                                        {{ item.category }}
                                    </td>
                                    <td class="px-6 py-3 whitespace-nowrap text-sm text-gray-900">
                                        {{ item.employee }}
                                    </td>
                                    <td class="px-6 py-3 whitespace-nowrap text-sm text-gray-900">
                                        {{ item.customer }}
                                    </td>
                                    <td class="px-6 py-3 whitespace-nowrap text-sm text-gray-900">
                                        {{ item.qty }}
                                    </td>
                                    <td class="price text-right px-6 py-3 whitespace-nowrap text-sm text-gray-900"
                                        data-value="{{ item.unit_price }}">
                                        Rp {{ item.unit_price }}
                                    </td>
                                    <td class="subtotal text-right px-6 py-3 whitespace-nowrap text-sm text-gray-900"
                                        data-value="{{ item.subtotal }}">
                                        Rp {{ item.subtotal }}
                                    </td>
                                </tr>
                                {% endfor %}
                                {% else %}
                                <tr>
                                    <td class="text-center text-sm py-3" colspan="100%">No transactions found for
                                        the selected date range.</td>
                                </tr>
                                {% endif %}
                            </tbody>
                            <tfoot>
                                <tr class="bg-gray-50">
                                    <td colspan="8"
                                        class="text-right px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900 text-right">
                                        Total:
                                    </td>
                                    <td class="total text-right px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900"
                                        data-value="{{ transaction_total }}">
                                        {{ transaction_total }}
                                    </td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>

                <div class="px-4 py-5 flex justify-end sm:p-6">
                    <table class="border-collapse text-right">
                        <tfoot>
                            <tr>
                                <th class="px-4 py-2 text-sm font-medium text-gray-500">Total Pengeluaran:</th>
                                <td class="total px-4 py-2 text-sm font-medium text-gray-900"
                                    data-value="{{ stock_total }}">
                                    {{ stock_total }}
                                </td>
                            </tr>
                            <tr>
                                <th class="px-4 py-2 text-sm font-medium text-gray-500">Total Pemasukan:</th>
                                <td class="total px-4 py-2 text-sm font-medium text-gray-900"
                                    data-value="{{ transaction_total }}">
                                    {{ transaction_total }}
                                </td>
                            </tr>
                            <tr>
                                <th class="px-4 py-2 text-sm font-medium text-gray-500">Saldo:</th>
                                <td class="total px-4 py-2 text-sm font-medium text-gray-900"
                                    data-value="{{ balance }}">
                                    {{ balance }}
                                </td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
            {% endif %}
        </div>
    </div>
</div>

<!-- Print Styles -->
<style>
    @media print {
        .no-print {
            display: none !important;
        }

        body {
            background-color: white;
        }

        .shadow {
            box-shadow: none !important;
        }

        .max-w-7xl {
            max-width: none !important;
        }

        @page {
            margin: 2cm;
        }
    }
</style>

<!-- Script untuk Export Excel -->
<script>
    function exportExcel() {
        const startDate = document.querySelector('input[name="start_date"]').value;
        const endDate = document.querySelector('input[name="end_date"]').value;

        if (!startDate || !endDate) {
            Swal.fire({
                icon: 'error',
                title: 'Error',
                text: 'Silakan pilih tanggal awal dan akhir terlebih dahulu'
            });
            return;
        }

        window.location.href = `?export=excel&start_date=${startDate}&end_date=${endDate}`;
    }
</script>

<!-- Script untuk Mencetak PDF -->
<script>
    function printPDF() {
        const startDate = document.getElementById('start_date').value;
        const endDate = document.getElementById('end_date').value;

        if (!startDate || !endDate) {
            alert('Silakan pilih tanggal awal dan akhir terlebih dahulu');
            return;
        }

        const baseUrl = "{% url 'report_pdf' %}";
        const pdfUrl = `${baseUrl}?start_date=${startDate}&end_date=${endDate}`;
        window.open(pdfUrl, '_blank');
    }
</script>
{% endblock %}