@extends('layouts.admin.app')

@section('title', translate('Product Profit/Loss'))

@section('content')
    <div class="content container-fluid">
        <div class="page-header">
            <h1 class="page-header-title">
                <span class="page-header-icon">
                    <i class="tio-chart-line-up"></i>
                </span>
                <span>{{ translate('Product Profit/Loss') }}</span>
            </h1>
        </div>

        <div class="card mb-3">
            <div class="card-body">
                <form action="{{ route('admin.report.product-profit-loss') }}" method="get">
                    <div class="row g-3">
                        <div class="col-md-3">
                            <input type="text" name="search" value="{{ request('search') }}" class="form-control" placeholder="{{ translate('Search product name') }}">
                        </div>
                        <div class="col-md-3">
                            <select class="form-control" name="filter" id="product-profit-filter">
                                <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-md-3 {{ request('filter') === 'custom' ? '' : 'd-none' }}" id="product-profit-from-wrapper">
                            <input type="date" name="from" value="{{ request('from') }}" class="form-control">
                        </div>
                        <div class="col-md-3 {{ request('filter') === 'custom' ? '' : 'd-none' }}" id="product-profit-to-wrapper">
                            <input type="date" name="to" value="{{ request('to') }}" class="form-control">
                        </div>
                    </div>
                    <div class="row g-3 mt-1">
                        <div class="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-md-2">
                            <button class="btn btn--primary btn-block" type="submit">{{ translate('Filter') }}</button>
                        </div>
                        <div class="col-md-2">
                            <a href="{{ route('admin.report.product-profit-loss') }}" class="btn btn--reset btn-block">{{ translate('Reset') }}</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        @if($summary['missing_delivery_cost_orders'] > 0 || $summary['missing_return_cost_orders'] > 0 || $summary['missing_cod_charge_orders'] > 0 || $summary['missing_purchase_cost_items'] > 0)
            <div class="alert alert-soft-warning">
                <strong>{{ translate('Attention:') }}</strong>
                {{ translate('Some product profit/loss values are still missing source cost data.') }}
                <ul class="mb-0 mt-2">
                    <li>{{ translate('Delivered orders missing actual courier delivery cost') }}: {{ $summary['missing_delivery_cost_orders'] }}</li>
                    <li>{{ translate('Returned orders missing actual return courier cost') }}: {{ $summary['missing_return_cost_orders'] }}</li>
                    <li>{{ translate('COD orders missing actual COD charge') }}: {{ $summary['missing_cod_charge_orders'] }}</li>
                    <li>{{ translate('Products missing purchase cost history') }}: {{ $summary['missing_purchase_cost_items'] }}</li>
                </ul>
            </div>
        @endif

        <div class="card">
            <div class="card-header border-0">
                <h4 class="card-title">{{ translate('Product Profit/Loss Table') }} <span class="badge badge-soft-secondary">{{ $rows->total() }}</span></h4>
            </div>
            <div class="table-responsive">
                <table class="table table-borderless table-thead-bordered table-nowrap card-table">
                    <thead class="thead-light">
                    <tr>
                        <th>{{ translate('sl') }}</th>
                        <th>{{ translate('Product Name') }}</th>
                        <th class="text-center">{{ translate('Total Sold Quantity') }}</th>
                        <th class="text-right">{{ translate('Total Selling Amount') }}</th>
                        <th class="text-right">{{ translate('Total Purchase Cost') }}</th>
                        <th class="text-right">{{ translate('Allocated Actual Courier Delivery Cost') }}</th>
                        <th class="text-right">{{ translate('Allocated Actual COD Charge') }}</th>
                        <th class="text-right">{{ translate('Allocated Actual Return Charge') }}</th>
                        <th class="text-right">{{ translate('Net Profit / Loss') }}</th>
                    </tr>
                    </thead>
                    <tbody>
                    @forelse($rows as $key => $row)
                        <tr>
                            <td>{{ $key + $rows->firstItem() }}</td>
                            <td>{{ $row['product_name'] }}</td>
                            <td class="text-center">{{ $row['total_sold_quantity'] }}</td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($row['total_selling_amount']) }}</td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($row['total_purchase_cost']) }}</td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($row['allocated_actual_courier_delivery_cost']) }}</td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($row['allocated_actual_cod_charge']) }}</td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($row['allocated_actual_return_charge']) }}</td>
                            <td class="text-right font-weight-bold">{{ \App\CentralLogics\Helpers::format_currency($row['net_profit_or_loss']) }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="9" class="text-center py-4">{{ translate('No product-wise profit/loss data found') }}</td>
                        </tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
            @if($rows->hasPages())
                <div class="card-footer">
                    {!! $rows->links() !!}
                </div>
            @endif
        </div>
    </div>
@endsection

@push('script_2')
    <script>
        "use strict";
        $('#product-profit-filter').on('change', function () {
            const isCustom = $(this).val() === 'custom';
            $('#product-profit-from-wrapper, #product-profit-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
