@extends('common/main/layout')
@section('content')
<div class="container-fluid px-0">
  <div class="row mb-3">
    <div class="col">
      <h1 class="h2">@lang('db.reports') @lang('db.report_waypoint.quantity')@lang('system.title.total')</h1>
    </div>
  </div>
  @include('common/error-message')
  <div class="row">
    <div class="col-12 col-md">
    </div>
    <div class="col col-md p-2">
      <select id="search_user_id" class="form-select" name="user_id">
        <option value="all">すべて表示</option>
        @foreach ($users as $user)
          <option value="{{ $user->id }}" @if($params['user_id'] == $user->id) selected  @endif>{{ $user->name }}</option>
        @endforeach
      </select>
    </div>
    <div class="col-12 col-md p-2">
      <input type="month" id="search_start_at" class="form-control" name="start_at" placeholder="" value="{{ old('start_at', date('Y-m', strtotime($params['start_at']))) }}">
    </div>
  </div>
  <div class="row">
    <div class="col">
      <div class="table-responsive">
        <table class="table table-hover text-nowrap">
          <thead>
            <tr>
              <th class="text-start">日付</th>
              <th class="text-end">{{ config('const.UNITS.1') }}</th>
              <th class="text-end">{{ config('const.UNITS.2') }}</th>
              <th class="text-end">{{ config('const.UNITS.3') }}</th>
              <th class="text-end">{{ config('const.UNITS.4') }}</th>
              <th class="text-end">{{ config('const.UNITS.5') }}</th>
            </tr>
          </thead>
          <tbody>
            @php
              $sumArray['unit_1'] = 0;
              $sumArray['unit_2'] = 0;
              $sumArray['unit_3'] = 0;
              $sumArray['unit_4'] = 0;
              $sumArray['unit_5'] = 0;
            @endphp
            @foreach($totals as $total)
            <tr>
              <td class="text-start">
                {{ date('j日', strtotime($total->date)) }}
              </td>
              <td class="text-end">
                @if (!empty($total->sum_quantity_unit_1))
                  {{ number_format($total->sum_quantity_unit_1) }}&nbsp;{{ config('const.UNITS.1') }}
                  @php $sumArray['unit_1'] = $sumArray['unit_1'] + intval($total->sum_quantity_unit_1); @endphp
                @else
                  -
                @endif
              </td>
              <td class="text-end">
                @if (!empty($total->sum_quantity_unit_2))
                  {{ number_format($total->sum_quantity_unit_2) }}&nbsp;{{ config('const.UNITS.2') }}
                  @php $sumArray['unit_2'] = $sumArray['unit_2'] + intval($total->sum_quantity_unit_2); @endphp
                @else
                  -
                @endif
              </td>
              <td class="text-end">
                @if (!empty($total->sum_quantity_unit_3))
                  {{ number_format($total->sum_quantity_unit_3) }}&nbsp;{{ config('const.UNITS.3') }}
                  @php $sumArray['unit_3'] = $sumArray['unit_3'] + intval($total->sum_quantity_unit_3); @endphp
                @else
                  -
                @endif
              </td>
              <td class="text-end">
                @if (!empty($total->sum_quantity_unit_4))
                  {{ number_format($total->sum_quantity_unit_4) }}&nbsp;{{ config('const.UNITS.4') }}
                  @php $sumArray['unit_4'] = $sumArray['unit_4'] + intval($total->sum_quantity_unit_4); @endphp
                @else
                  -
                @endif
              </td>
              <td class="text-end">
                @if (!empty($total->sum_quantity_unit_5))
                  {{ number_format($total->sum_quantity_unit_5) }}&nbsp;{{ config('const.UNITS.5') }}
                  @php $sumArray['unit_5'] = $sumArray['unit_5'] + intval($total->sum_quantity_unit_5); @endphp
                @else
                  -
                @endif
              </td>
            </tr>
            @endforeach
            <tr class="table-light">
              <td class="text-center fw-bold">
                合計
              </td>
              <td class="text-end fw-bold">
                {{ number_format($sumArray['unit_1']) }}&nbsp;{{ config('const.UNITS.1') }}
              </td>
              <td class="text-end fw-bold">
                {{ number_format($sumArray['unit_2']) }}&nbsp;{{ config('const.UNITS.2') }}
              </td>
              <td class="text-end fw-bold">
                {{ number_format($sumArray['unit_3']) }}&nbsp;{{ config('const.UNITS.3') }}
              </td>
              <td class="text-end fw-bold">
                {{ number_format($sumArray['unit_4']) }}&nbsp;{{ config('const.UNITS.4') }}
              </td>
              <td class="text-end fw-bold">
                {{ number_format($sumArray['unit_5']) }}&nbsp;{{ config('const.UNITS.5') }}
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
<script>
    window.addEventListener('load', function() {
        document.getElementById('search_user_id').addEventListener('change', function() {
          let requests = {
            user_id: this.value
          }
          let url = getRequestUrl(requests);
          if (!url) return;

          window.location.href = url;
          return;
        }, false);
        document.getElementById('search_start_at').addEventListener('change', function() {
          let requests = {
            start_at: this.value
          }
          let url = getRequestUrl(requests);
          if (!url) return;

          window.location.href = url;
          return;
        }, false);
    }, false);
</script>
@endsection