@extends('backend.layouts.app')

@section('content')
<div class="row page-titles">
   <div class="col-md-5 align-self-center">
      <h4 class="text-themecolor">Reports Manager</h4>
   </div>
   <div class="col-md-7 align-self-center text-right">
      <div class="d-flex justify-content-end align-items-center">
         <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{{route('dashboard')}}">Dashboard</a></li>
            <li class="breadcrumb-item">Reports Manager</li>
            <li class="breadcrumb-item active">Leads distribution based on Members</li>
         </ol>
      </div>
   </div>
</div>
<div class="container-fluid">
    <div class="content-wrapper">
        @if (session('status'))
            <div class="alert alert-success alert-dismissible fade show" role="alert"><i class="far fa-check-circle"></i>
                {{ session('status') }}
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
        @endif
        @if (session('error'))
            <div class="alert alert-danger alert-dismissible fade show" role="alert"><i class="far fa-check-circle"></i>
                {{ session('error') }}
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
        @endif        

        <section class="content">
            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-body">
                            <h4>Leads distribution based on Members <p style="font-size: 10px; color:gray">This report shows monthly based count of leads distribution according to member for each area</p></h4>
                            <div class="form-inline">
                                <br>
                                <form action="{{ route('reports.leads.areas.members') }}" method="get" id="form">
    <div class="row">
        <!-- existing filters -->

        <!-- Date Range Type Filter -->
        <div class="mr-3 mb-2">
            <select name="period" class="form-control" onchange="this.form.submit()">
                <option value="daily" {{ request('period') == 'daily' ? 'selected' : '' }}>Daily</option>
                <option value="monthly" {{ request('period') == 'monthly' ? 'selected' : '' }}>Monthly</option>
                <option value="quarterly" {{ request('period') == 'quarterly' ? 'selected' : '' }}>Quarterly</option>
                <option value="yearly" {{ request('period') == 'yearly' ? 'selected' : '' }}>Yearly</option>
            </select>
        </div>

        <!-- Submit button remains if needed -->

    </div>
</form>
                                 {{-- <form action="{{ route('reports.leads.areas.members') }}" method="get" id="form">
                                        <div class="row">
                                            <!-- Dropdown for Area Filter -->
                                            <div class="mr-3 mb-2">
                                                <select name="sarea" class="form-control">
                                                    <option value="">Select  Area</option>
                                                    @foreach ($areas as $area)
                                                        <option value="{{ $area->id }}" {{ request('sarea') == $area->id ? 'selected' : '' }}>
                                                            {{ $area->code .' ( '. $area->name .')' }}
                                                        </option>
                                                    @endforeach
                                                </select>
                                            </div>

                                            <!-- Dropdown for Member Filter -->
                                            <div class="mr-3 mb-2">
                                                <select name="smember" class="form-control">
                                                    <option value="">Select  Member</option>
                                                    @foreach ($members as $member)
                                                        <option value="{{ $member->id }}" {{ request('smember') == $member->id ? 'selected' : '' }}>
                                                            {{ $member->business_name }}
                                                        </option>
                                                    @endforeach
                                                </select>
                                            </div>

                                            <!-- Submit Button and Reload Link -->
                                            <div class="mr-3 mb-2 d-flex">
                                                <button class="btn btn-sidebar btn btn-secondary w-100" type="submit">
                                                    <i class="fas fa-search fa-fw"></i>
                                                </button>
                                                <a href="{{ route('reports.leads.areas.members') }}" class="btn btn-secondary w-100" title="Reload">
                                                    <i class="fas fa-sync"></i>
                                                </a>
                                            </div>

                                        </div>
                                    </form> --}}
                            </div>
                            <br>
                            <div class="table-responsive">
                                <table id="airports" class="table table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th>S. No.</th>
                                            <th>Code</th> 
                                            <th>Name</th>                                             
                                            <th>Member Name</th>                                             
                                            <th>Total</th> 
                                            @foreach($months as $month)
                                                <th>{{ $month }}</th>
                                            @endforeach
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @if(count($reportData) > 0)
                                            @php  
                                                $i = 0;
                                                $col_total = array_fill_keys(array_merge(['total_leads'], $months), 0);
                                            @endphp
                                            @foreach ($reportData as $key => $list)
                                                <tr>
                                                    <td>{{ ++$i }}</td>
                                                    <td>{{ $list->code }}</td>
                                                    <td>{{ $list->name }}</td>
                                                    <td>{{ $list->business_name }}</td>
                                                    <td>{{ $list->total_leads }}</td>
                                                    @php
                                                        $col_total['total_leads'] += $list->total_leads;
                                                    @endphp

                                                    @foreach($months as $month)
                                                        @php
                                                            $value = $list->{$month} ?? 0;
                                                            $col_total[$month] += $value;
                                                        @endphp
                                                        <td>{{ $value }}</td>
                                                    @endforeach
                                                </tr>
                                            @endforeach
                                            <tr>
                                                <td colspan="4"><p style="font-weight: 700">Total</p></td>
                                                <td><strong>{{ $col_total['total_leads'] }}</strong></td>
                                                @foreach($months as $month)
                                                    <td><strong>{{ $col_total[$month] }}</strong></td>
                                                @endforeach
                                            </tr>
                                        @else
                                            <tr>
                                                <td colspan="12">
                                                    <h6><center>No Records Found</center></h6>
                                                </td>
                                            </tr>
                                        @endif
                                    </tbody>
                                </table>
                            </div>
                            
                        </div>                       
                    </div>
                </div>
            </div>
        </section>
    </div>
</div>
@stop