<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>استانی</title>

</head>

<body dir="rtl" style="text-align: center;">

<table>
    <thead>
    <tr>
        <th colspan="25" style="background-color: #DAEEF3;text-align: center;border-right: 1px solid black;font-weight:bolder;">
            تاریخ دریافت گزارش: {{verta()->now()->format('Y/m/d H:i')}}
        </th>

    </tr>
    <tr>
        <th colspan="25" style="background-color: #DAEEF3;text-align: center;border-right:1px solid black;font-weight:bolder;"></th>
    </tr>
    <tr>
        <th colspan="26" style="background-color: #DAEEF3;text-align: center;border-right:1px solid black;font-weight:bolder;font-size: 13;">
            تعداد و مجموع فعالیت های روزانه و جاری راهداری ثبت شده از تاریخ
            {{verta($fromDate)->format('Y/n/j')}}
            تا
            تاریخ
            {{verta($toDate)->format('Y/n/j')}}
        </th>
    </tr>

    <tr>
        <th rowspan="3" style="border: 1px solid black;text-align: center;background-color: #EBF1DE;">اداره کل</th>
        <th colspan="24" style="border: 1px solid black;text-align: center;background-color: #EBF1DE;">فعالیت های انجام شده</th>
    </tr>

    <tr>
        @foreach($subItems as $subItem)
            <th colspan="2" style="border:1px solid black;text-align:center;background-color:#C4D79B">{{ $subItem->sub_item_str }}</th>
        @endforeach
    </tr>

    <tr>
        @foreach($subItems as $subItem)
            <th style="background-color: #FDE9D9;border: 1px solid black;text-align: center;">تعداد</th>
            <th style="background-color: #FDE9D9;border: 1px solid black;text-align: center;">مجموع ({{ $subItem->sub_item_unit }})</th>
        @endforeach
    </tr>

    </thead>

    <tbody>
    @php
        // محاسبه مقدار c و s برای هر ساب‌آیتم در کل کشور
        $countTotalsForProvince = [];
        $sumTotalsForProvince = [];

        foreach ($subItems as $subItem) {
            $countTotalsForProvince[$subItem['sub_item']] = collect($data)->where('ci', -1)->where('t', $subItem['sub_item'])->sum('c');
            $sumTotalsForProvince[$subItem['sub_item']] = collect($data)->where('ci', -1)->where('t', $subItem['sub_item'])->sum('s');
        }
    @endphp

    {{-- ردیف کل کشور --}}
    <tr>
        <td style="border:1px solid black;text-align: center;">کل کشور</td>
        @foreach($subItems as $subItem)
            <td style="border:1px solid black;text-align: center;">{{ $countTotalsForProvince[$subItem['sub_item']] ?? '-' }}</td>
            <td style="border:1px solid black;text-align: center;">{{ $sumTotalsForProvince[$subItem['sub_item']] ?? '-' }}</td>
        @endforeach
    </tr>

    {{-- ردیف‌های مربوط به هر استان --}}
    @foreach($edarateShahri as $edareShahri)
        @php
            $countTotalsForProvince = [];
            $sumTotalsForProvince = [];
            foreach ($subItems as $subItem) {
                $countTotalsForProvince[$subItem['sub_item']] = collect($data)->where('ci', $edareShahri['id'])->where('t', $subItem['sub_item'])->sum('c');
                $sumTotalsForProvince[$subItem['sub_item']] = collect($data)->where('ci', $edareShahri['id'])->where('t', $subItem['sub_item'])->sum('s');
            }
        @endphp
        <tr>
            <td style="border:1px solid black;text-align: center;">{{ $edareShahri['name_fa'] }}</td>
            @foreach($subItems as $subItem)
                <td style="border:1px solid black;text-align: center;">{{ $countTotalsForProvince[$subItem['sub_item']] ?? '-' }}</td>
                <td style="border:1px solid black;text-align: center;">{{ $sumTotalsForProvince[$subItem['sub_item']] ?? '-' }}</td>
            @endforeach
        </tr>
    @endforeach
    </tbody>
</table>
</body>
</html>