<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Models\Meeting;
use App\Models\Project;
use App\Models\Employee;
use App\Models\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ClientMeeting extends Controller
{
    public function index(Request $request)
    {
        // Get the authenticated user's client_id
        $user = Auth::user();
        $client_id = $user->client_id; // Get the client_id of the logged-in user
    
        // Get the filters from the request
        $projectId = $request->input('project_id');
        $startDate = $request->input('start_date');
    
        // Start the query for meetings, filtering by client_id
        $meetings = Meeting::with('project', 'client')
                            ->where('client_id', $client_id); // Filter by client_id
    
        // Apply project filter if provided
        if ($projectId) {
            $meetings->where('project_id', $projectId);
        }
    
        // Apply start date filter if provided
        if ($startDate) {
            $meetings->whereDate('start_time', $startDate);
        }
    
        // Get the filtered meetings
        $meetings = $meetings->get();
    
        // Fetch other necessary data
        $employees = Employee::all();
        $clients = Client::all();
        $projects = Project::all();
    
        // Return the view with filtered meetings and other data
        return view('client.meetings.index', compact('meetings', 'employees', 'clients', 'projects'));
    }
    
}
