@extends('layouts.admin.app')

@section('title', translate('Income Statement'))

@section('content')
    <div class="content container-fluid">
        <div class="page-header">
            <h1 class="page-header-title">
                <span class="page-header-icon">
                    <i class="tio-receipt"></i>
                </span>
                <span>{{ translate('Income Statement') }}</span>
            </h1>
        </div>

        <div class="card mb-3">
            <div class="card-body">
                <form action="{{ route('admin.report.income-statement') }}" method="get">
                    <div class="row g-3">
                        <div class="col-sm-6 col-md-3">
                            <select class="form-control" name="filter" id="income-filter-select">
                                <option value="all_time" {{ request('filter', 'all_time') === 'all_time' ? 'selected' : '' }}>{{ translate('All Time') }}</option>
                                <option value="this_year" {{ request('filter') === 'this_year' ? 'selected' : '' }}>{{ translate('This Year') }}</option>
                                <option value="previous_year" {{ request('filter') === 'previous_year' ? 'selected' : '' }}>{{ translate('Previous Year') }}</option>
                                <option value="this_month" {{ request('filter') === 'this_month' ? 'selected' : '' }}>{{ translate('This Month') }}</option>
                                <option value="this_week" {{ request('filter') === 'this_week' ? 'selected' : '' }}>{{ translate('This Week') }}</option>
                                <option value="custom" {{ request('filter') === 'custom' ? 'selected' : '' }}>{{ translate('Custom') }}</option>
                            </select>
                        </div>
                        <div class="col-sm-6 col-md-3">
                            <select name="store_id" data-placeholder="{{ translate('Select store') }}" class="js-data-example-ajax form-control">
                                @php($selectedStore = request('store_id') ? \App\Models\Store::find(request('store_id')) : null)
                                @if($selectedStore)
                                    <option value="{{ $selectedStore->id }}" selected>{{ $selectedStore->name }}</option>
                                @else
                                    <option value="">{{ translate('All Stores') }}</option>
                                @endif
                            </select>
                        </div>
                        <div class="col-sm-6 col-md-2 {{ request('filter') === 'custom' ? '' : 'd-none' }}" id="income-from-wrapper">
                            <input type="date" name="from" value="{{ request('from') }}" class="form-control">
                        </div>
                        <div class="col-sm-6 col-md-2 {{ request('filter') === 'custom' ? '' : 'd-none' }}" id="income-to-wrapper">
                            <input type="date" name="to" value="{{ request('to') }}" class="form-control">
                        </div>
                        <div class="col-sm-6 col-md-1">
                            <button class="btn btn--primary btn-block" type="submit">{{ translate('Go') }}</button>
                        </div>
                        <div class="col-sm-6 col-md-1">
                            <a href="{{ route('admin.report.income-statement') }}" class="btn btn--reset btn-block">{{ translate('Reset') }}</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        @if($totals['orders_missing_delivery_cost'] > 0 || $totals['orders_missing_return_cost'] > 0 || $totals['orders_missing_cod_charge'] > 0 || $totals['items_missing_purchase_cost_count'] > 0)
            <div class="alert alert-soft-warning">
                <strong>{{ translate('Attention:') }}</strong>
                {{ translate('Some report values are still missing source data.') }}
                <ul class="mb-0 mt-2">
                    <li>{{ translate('Delivered orders missing actual courier delivery cost') }}: {{ $totals['orders_missing_delivery_cost'] }}</li>
                    <li>{{ translate('Returned orders missing actual return courier cost') }}: {{ $totals['orders_missing_return_cost'] }}</li>
                    <li>{{ translate('COD orders missing actual COD charge') }}: {{ $totals['orders_missing_cod_charge'] }}</li>
                    <li>{{ translate('Products missing purchase cost history') }}: {{ $totals['items_missing_purchase_cost_count'] }}</li>
                </ul>
            </div>
        @endif

        <div class="row g-3 mb-3">
            <div class="col-sm-6 col-lg-3">
                <div class="card h-100"><div class="card-body"><h6>{{ translate('Total Sales Amount') }}</h6><h3>{{ \App\CentralLogics\Helpers::format_currency($totals['sales_amount']) }}</h3></div></div>
            </div>
            <div class="col-sm-6 col-lg-3">
                <div class="card h-100"><div class="card-body"><h6>{{ translate('Total Purchase Cost') }}</h6><h3>{{ \App\CentralLogics\Helpers::format_currency($totals['purchase_cost']) }}</h3></div></div>
            </div>
            <div class="col-sm-6 col-lg-3">
                <div class="card h-100"><div class="card-body"><h6>{{ translate('Total Actual Courier Delivery Cost') }}</h6><h3>{{ \App\CentralLogics\Helpers::format_currency($totals['actual_delivery_cost']) }}</h3></div></div>
            </div>
            <div class="col-sm-6 col-lg-3">
                <div class="card h-100"><div class="card-body"><h6>{{ translate('Total Actual COD Charge') }}</h6><h3>{{ \App\CentralLogics\Helpers::format_currency($totals['actual_cod_charge']) }}</h3></div></div>
            </div>
            <div class="col-sm-6 col-lg-3">
                <div class="card h-100"><div class="card-body"><h6>{{ translate('Gross Profit') }}</h6><h3>{{ \App\CentralLogics\Helpers::format_currency($totals['gross_profit']) }}</h3></div></div>
            </div>
        </div>

        <div class="card mb-3">
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-borderless mb-0">
                        <tbody>
                        <tr><th>{{ translate('Total Sales Amount') }}</th><td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($totals['sales_amount']) }}</td></tr>
                        <tr><th>{{ translate('Total Purchase Cost') }}</th><td class="text-right">(-) {{ \App\CentralLogics\Helpers::format_currency($totals['purchase_cost']) }}</td></tr>
                        <tr><th>{{ translate('Total Actual Courier Delivery Cost') }}</th><td class="text-right">(-) {{ \App\CentralLogics\Helpers::format_currency($totals['actual_delivery_cost']) }}</td></tr>
                        <tr><th>{{ translate('Total Actual Return Courier Cost') }}</th><td class="text-right">(-) {{ \App\CentralLogics\Helpers::format_currency($totals['actual_return_cost']) }}</td></tr>
                        <tr><th>{{ translate('Total Actual COD Charge') }}</th><td class="text-right">(-) {{ \App\CentralLogics\Helpers::format_currency($totals['actual_cod_charge']) }}</td></tr>
                        <tr class="border-top"><th>{{ translate('Gross Profit') }}</th><td class="text-right font-weight-bold">{{ \App\CentralLogics\Helpers::format_currency($totals['gross_profit']) }}</td></tr>
                        <tr><th>{{ translate('Total Operating Expenses') }}</th><td class="text-right">(-) {{ \App\CentralLogics\Helpers::format_currency($totals['operating_expenses']) }}</td></tr>
                        <tr class="border-top"><th>{{ translate('Net Profit / Loss') }}</th><td class="text-right font-weight-bold">{{ \App\CentralLogics\Helpers::format_currency($totals['net_profit_or_loss']) }}</td></tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="card">
            <div class="card-header border-0">
                <h4 class="card-title">{{ translate('Operating Expense Breakdown') }}</h4>
            </div>
            <div class="table-responsive">
                <table class="table table-borderless table-thead-bordered mb-0">
                    <thead class="thead-light">
                    <tr>
                        <th>{{ translate('Category') }}</th>
                        <th class="text-right">{{ translate('Amount') }}</th>
                    </tr>
                    </thead>
                    <tbody>
                    @forelse($expense_breakdown as $row)
                        <tr>
                            <td>{{ translate($row['label']) }}</td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($row['amount']) }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="2" class="text-center py-4">{{ translate('No operating expense found for this period') }}</td>
                        </tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection

@push('script_2')
    <script>
        "use strict";
        $('#income-filter-select').on('change', function () {
            const isCustom = $(this).val() === 'custom';
            $('#income-from-wrapper, #income-to-wrapper').toggleClass('d-none', !isCustom);
        });

        $(document).ready(function () {
            $('.js-data-example-ajax').select2({
                ajax: {
                    url: '{{ url('/') }}/admin/store/get-stores',
                    data: function(params) {
                        return {
                            q: params.term,
                            all: true,
                            page: params.page,
                            @if(config('module.current_module_id'))
                            module_id: {{ config('module.current_module_id') }},
                            @endif
                        };
                    },
                    processResults: function(data) {
                        return { results: data };
                    }
                }
            });
        });
    </script>
@endpush
