@extends('layouts.app')

@section('title', 'Report')

@section('content')

<div class="row">
    <div class="col-12">
        <div class="card card-transparent card-block card-stretch card-height border-none">
            <div class="card-body p-0 mt-lg-2 mt-0 d-flex flex-wrap align-items-center justify-content-between">
                <div>
                    <h3 class="mb-1">Business Reports</h3>
                </div>
            </div>
        </div>
    </div>
</div>


{{-- ========================================================= --}}
{{-- SUMMARY CARDS --}}
{{-- ========================================================= --}}

<div class="row">

    {{-- Total Orders --}}
    <div class="col-lg-4 col-md-6">
        <div class="card card-block card-stretch card-height">
            <div class="card-body">
                <div class="d-flex align-items-center mb-4 card-total-sale">
                    <div class="icon iq-icon-box-2 bg-info-light">
                        <i class="ri-shopping-cart-2-line"></i>
                    </div>

                    <div>
                        <p class="mb-2">Total Orders</p>
                        <h4>{{ $totalOrders }}</h4>
                    </div>
                </div>

                <div class="iq-progress-bar mt-2">
                    <span class="bg-info iq-progress progress-1" data-percent="78"></span>
                </div>
            </div>
        </div>
    </div>


    {{-- Total Customers --}}
    <div class="col-lg-4 col-md-6">
        <div class="card card-block card-stretch card-height">
            <div class="card-body">
                <div class="d-flex align-items-center mb-4 card-total-sale">
                    <div class="icon iq-icon-box-2 bg-primary-light">
                        <i class="ri-user-3-line"></i>
                    </div>

                    <div>
                        <p class="mb-2">Total Customers</p>
                        <h4>{{ $totalCustomers }}</h4>
                    </div>
                </div>

                <div class="iq-progress-bar mt-2">
                    <span class="bg-primary iq-progress progress-1" data-percent="65"></span>
                </div>
            </div>
        </div>
    </div>


    {{-- Total Products --}}
    <div class="col-lg-4 col-md-6">
        <div class="card card-block card-stretch card-height">
            <div class="card-body">
                <div class="d-flex align-items-center mb-4 card-total-sale">
                    <div class="icon iq-icon-box-2 bg-warning-light">
                        <i class="ri-archive-2-line"></i>
                    </div>

                    <div>
                        <p class="mb-2">Total Products</p>
                        <h4>{{ $totalProducts }}</h4>
                    </div>
                </div>

                <div class="iq-progress-bar mt-2">
                    <span class="bg-warning iq-progress progress-1" data-percent="70"></span>
                </div>
            </div>
        </div>
    </div>


    {{-- Total Profit --}}
    <div class="col-lg-4 col-md-6">
        <div class="card card-block card-stretch card-height">
            <div class="card-body">
                <div class="d-flex align-items-center mb-4 card-total-sale">
                    <div class="icon iq-icon-box-2 bg-success-light">
                        <i class="ri-money-dollar-circle-line"></i>
                    </div>

                    <div>
                        <p class="mb-2">Total Profit</p>
                        <p class="mb-2">This Month</p>

                        <h4>
                            Rs. {{ number_format($totalProfit, 2) }}
                        </h4>
                    </div>
                </div>

                <div class="iq-progress-bar mt-2">
                    <span class="bg-success iq-progress progress-1" data-percent="82"></span>
                </div>
            </div>
        </div>
    </div>


    {{-- Pending Payments --}}
    <div class="col-lg-4 col-md-6">
        <div class="card card-block card-stretch card-height">
            <div class="card-body">
                <div class="d-flex align-items-center mb-4 card-total-sale">
                    <div class="icon iq-icon-box-2 bg-danger-light">
                        <i class="ri-bank-card-line"></i>
                    </div>

                    <div>
                        <p class="mb-2">Pending Payments</p>

                        <h4>
                            Rs. {{ number_format($pendingPayments, 2) }}
                        </h4>
                    </div>
                </div>

                <div class="iq-progress-bar mt-2">
                    <span class="bg-danger iq-progress progress-1" data-percent="35"></span>
                </div>
            </div>
        </div>
    </div>


    {{-- Low Stock --}}
    <div class="col-lg-4 col-md-6">
        <div class="card card-block card-stretch card-height">
            <div class="card-body">
                <div class="d-flex align-items-center mb-4 card-total-sale">
                    <div class="icon iq-icon-box-2 bg-secondary-light">
                        <i class="ri-alert-line"></i>
                    </div>

                    <div>
                        <p class="mb-2">Low Stock Items</p>
                        <h4>{{ $lowStockItems }}</h4>
                    </div>
                </div>

                <div class="iq-progress-bar mt-2">
                    <span class="bg-secondary iq-progress progress-1" data-percent="20"></span>
                </div>
            </div>
        </div>
    </div>

</div>


{{-- ========================================================= --}}
{{-- GENERATE REPORT --}}
{{-- ========================================================= --}}

<div class="row">
    <div class="col-12">

        <div class="card card-block card-stretch card-height">

            <div class="card-header d-flex justify-content-between">

                <div class="header-title">
                    <h4 class="card-title">Generate Report</h4>
                </div>

            </div>


            <div class="card-body">

                <form method="GET"
                      action="{{ route('report.index') }}"
                      id="reportForm">

                    <div class="row">

                        {{-- Report Type --}}
                        <div class="col-lg-3 col-md-6">

                            <div class="form-group">

                                <label>Report Type</label>

                                <select name="report_type"
                                        id="reportType"
                                        class="form-control">

                                    <option value="sales"
                                        {{ request('report_type', 'sales') == 'sales' ? 'selected' : '' }}>
                                        Sales Report
                                    </option>

                                    <option value="inventory"
                                        {{ request('report_type') == 'inventory' ? 'selected' : '' }}>
                                        Inventory Report
                                    </option>

                                    <option value="customer"
                                        {{ request('report_type') == 'customer' ? 'selected' : '' }}>
                                        Customer Report
                                    </option>

                                    <option value="payment"
                                        {{ request('report_type') == 'payment' ? 'selected' : '' }}>
                                        Payment Report
                                    </option>

                                    <option value="order"
                                        {{ request('report_type') == 'order' ? 'selected' : '' }}>
                                        Order Report
                                    </option>

                                </select>

                            </div>

                        </div>


                        {{-- From Date --}}
                        <div class="col-lg-2 col-md-6">

                            <div class="form-group">

                                <label>From Date</label>

                                <input type="date"
                                       name="from_date"
                                       class="form-control"
                                       value="{{ request('from_date') }}">

                            </div>

                        </div>


                        {{-- To Date --}}
                        <div class="col-lg-2 col-md-6">

                            <div class="form-group">

                                <label>To Date</label>

                                <input type="date"
                                       name="to_date"
                                       class="form-control"
                                       value="{{ request('to_date') }}">

                            </div>

                        </div>


                        {{-- Customer --}}
                        <div class="col-lg-2 col-md-6">

                            <div class="form-group">

                                <label>Customer</label>

                                <select name="customer_id"
                                        id="customer_id"
                                        class="form-control">

                                    <option value="">
                                        All Customers
                                    </option>

                                    @foreach($customers as $customer)

                                        <option value="{{ $customer->customer_id }}"
                                            {{ request('customer_id') == $customer->customer_id ? 'selected' : '' }}>

                                            {{ $customer->customer_name }}

                                        </option>

                                    @endforeach

                                </select>

                            </div>

                        </div>


                        {{-- Export Format --}}
                        <div class="col-lg-3 col-md-6">

                            <div class="form-group">

                                <label>Export Format</label>

                                <select name="export_format"
                                        id="exportFormat"
                                        class="form-control">

                                    <option value="">
                                        Select Format
                                    </option>

                                    <option value="pdf"
                                        {{ request('export_format') == 'pdf' ? 'selected' : '' }}>
                                        PDF
                                    </option>

                                    <option value="excel"
                                        {{ request('export_format') == 'excel' ? 'selected' : '' }}>
                                        Excel
                                    </option>

                                    <option value="print"
                                        {{ request('export_format') == 'print' ? 'selected' : '' }}>
                                        Print
                                    </option>

                                </select>

                            </div>

                        </div>

                    </div>


                    {{-- Buttons --}}
                    <div class="d-flex">

                        <button type="submit"
                                class="btn btn-primary mr-2"
                                id="generateReportBtn">

                            <i class="ri-file-chart-line mr-1"></i>

                            Generate Report

                        </button>


                        <button type="reset"
                                class="btn border"
                                id="resetReportBtn">

                            <i class="ri-refresh-line mr-1"></i>

                            Reset

                        </button>

                    </div>

                </form>

            </div>

        </div>

    </div>
</div>


{{-- ========================================================= --}}
{{-- MONTHLY SALES OVERVIEW --}}
{{-- ========================================================= --}}

<div class="row report-section">

    <div class="col-lg-12">

        <div class="card card-block card-stretch card-height">

            <div class="card-header d-flex justify-content-between">

                <div class="header-title">

                    <h4 class="card-title">
                        Monthly Sales Overview
                    </h4>

                </div>

            </div>


            <div class="card-body">

                <div class="table-responsive mt-4">

                    <table class="table table-striped table-hover mb-0 report-table">

                        <thead>

                            <tr>
                                <th>Month</th>
                                <th>Profit</th>
                            </tr>

                        </thead>


                        <tbody>

                            @forelse($monthlyProfit as $month)

                                <tr>

                                    <td>
                                        {{ $month->month_name }}
                                    </td>

                                    <td>
                                        Rs. {{ number_format($month->profit, 2) }}
                                    </td>

                                </tr>

                            @empty

                                <tr>

                                    <td colspan="2"
                                        class="text-center">

                                        No sales data available.

                                    </td>

                                </tr>

                            @endforelse

                        </tbody>

                    </table>

                </div>

            </div>

        </div>

    </div>

</div>


{{-- ========================================================= --}}
{{-- TOP SELLING PRODUCTS --}}
{{-- ========================================================= --}}

<div class="row report-section">

    <div class="col-12">

        <div class="card card-block card-stretch card-height">

            <div class="card-header d-flex justify-content-between">

                <div class="header-title">

                    <h4 class="card-title">
                        Top Selling Products
                    </h4>

                </div>

            </div>


            <div class="card-body">

                <div class="table-responsive">

                    <table class="table table-striped table-hover mb-0 report-table">

                        <thead>

                            <tr>

                                <th>Item</th>
                                <th>Units Sold</th>
                                <th>Current Stock</th>

                            </tr>

                        </thead>


                        <tbody>

                            @forelse($topSellingProducts as $product)

                                <tr>

                                    <td>
                                        {{ $product->item_name }}
                                    </td>

                                    <td>
                                        {{ $product->units_sold }}
                                    </td>

                                    <td>
                                        {{ $product->item_quantity }}
                                    </td>

                                </tr>

                            @empty

                                <tr>

                                    <td colspan="3"
                                        class="text-center">

                                        No sales data available.

                                    </td>

                                </tr>

                            @endforelse

                        </tbody>

                    </table>

                </div>

            </div>

        </div>

    </div>

</div>


{{-- ========================================================= --}}
{{-- LOW STOCK PRODUCTS --}}
{{-- ========================================================= --}}

<div class="row report-section">

    <div class="col-12">

        <div class="card card-block card-stretch card-height">

            <div class="card-header d-flex justify-content-between">

                <div class="header-title">

                    <h4 class="card-title">
                        Low Stock Products
                    </h4>

                </div>

            </div>


            <div class="card-body">

                <div class="table-responsive">

                    <table class="table table-striped table-hover mb-0 report-table">

                        <thead>

                            <tr>

                                <th>Item</th>
                                <th>Category</th>
                                <th>Current Stock</th>
                                <th>Minimum Stock</th>

                            </tr>

                        </thead>


                        <tbody>

                            @forelse($lowStockProducts as $product)

                                <tr>

                                    <td>
                                        {{ $product->item_name }}
                                    </td>

                                    <td>
                                        {{ $product->category->category_name ?? 'N/A' }}
                                    </td>

                                    <td>
                                        {{ $product->item_quantity }}
                                    </td>

                                    <td>
                                        {{ $product->minimum_stock }}
                                    </td>

                                </tr>

                            @empty

                                <tr>

                                    <td colspan="4"
                                        class="text-center">

                                        No low stock items.

                                    </td>

                                </tr>

                            @endforelse

                        </tbody>

                    </table>

                </div>

            </div>

        </div>

    </div>

</div>


{{-- ========================================================= --}}
{{-- JAVASCRIPT --}}
{{-- ========================================================= --}}

<script>

document.addEventListener('DOMContentLoaded', function () {

    const form = document.getElementById('reportForm');

    const exportFormat = document.getElementById('exportFormat');

    const resetButton = document.getElementById('resetReportBtn');


    /*
    |--------------------------------------------------------------------------
    | Generate Report
    |--------------------------------------------------------------------------
    */

    form.addEventListener('submit', function (event) {

        const format = exportFormat.value;


        /*
        |--------------------------------------------------------------------------
        | No format selected
        |--------------------------------------------------------------------------
        */

        if (format === '') {

            event.preventDefault();

            alert('Please select an Export Format.');

            return;

        }


        /*
        |--------------------------------------------------------------------------
        | PRINT
        |--------------------------------------------------------------------------
        */

        if (format === 'print') {

            event.preventDefault();

            window.print();

            return;

        }


        /*
        |--------------------------------------------------------------------------
        | PDF
        |--------------------------------------------------------------------------
        |
        | Browser print dialog allows:
        | Print -> Save as PDF
        |
        */

        if (format === 'pdf') {

            event.preventDefault();

            window.print();

            return;

        }


        /*
        |--------------------------------------------------------------------------
        | EXCEL
        |--------------------------------------------------------------------------
        */

        if (format === 'excel') {

            event.preventDefault();

            exportToExcel();

            return;

        }

    });


    /*
    |--------------------------------------------------------------------------
    | Export Current Report Tables to Excel
    |--------------------------------------------------------------------------
    */

    function exportToExcel() {

        let tables = document.querySelectorAll('.report-table');

        if (tables.length === 0) {

            alert('No report data available to export.');

            return;

        }


        let excelContent = `
            <html>
            <head>
                <meta charset="UTF-8">
            </head>
            <body>

                <h2>Business Report</h2>

                <p>
                    Generated on:
                    ${new Date().toLocaleDateString()}
                </p>
        `;


        tables.forEach(function (table) {

            excelContent += table.outerHTML;

            excelContent += '<br><br>';

        });


        excelContent += `
            </body>
            </html>
        `;


        const blob = new Blob(
            [excelContent],
            {
                type: 'application/vnd.ms-excel'
            }
        );


        const url = URL.createObjectURL(blob);

        const link = document.createElement('a');

        link.href = url;

        link.download =
            'ZT_Stationery_Report_' +
            new Date().toISOString().slice(0, 10) +
            '.xls';


        document.body.appendChild(link);

        link.click();

        document.body.removeChild(link);

        URL.revokeObjectURL(url);

    }


    /*
    |--------------------------------------------------------------------------
    | Reset
    |--------------------------------------------------------------------------
    */

    resetButton.addEventListener('click', function () {

        setTimeout(function () {

            window.location.href =
                "{{ route('report.index') }}";

        }, 100);

    });

});

</script>


{{-- ========================================================= --}}
{{-- PRINT CSS --}}
{{-- ========================================================= --}}

<style>

@media print {

    body {
        background: #ffffff !important;
    }


    .sidebar,
    .iq-sidebar,
    .iq-top-navbar,
    .iq-navbar-custom,
    .navbar,
    header,
    footer,
    .no-print,
    #reportForm,
    button,
    select,
    input {
        display: none !important;
    }


    .card {
        border: 1px solid #ddd !important;
        box-shadow: none !important;
    }


    .card-header {
        background: #ffffff !important;
    }


    .report-section {
        display: block !important;
        width: 100% !important;
    }


    .report-table {
        width: 100% !important;
        border-collapse: collapse !important;
    }


    .report-table th,
    .report-table td {
        border: 1px solid #000 !important;
        padding: 8px !important;
    }


    h3,
    h4 {
        color: #000 !important;
    }

}

</style>


@endsection