<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;
use App\Models\Sponsor;
use App\Models\Studentdetails;
use App\Models\User;
use Carbon\Carbon;

class SponsorController extends Controller
{
   public function create()
{
    
    $students = StudentDetails::all();

  
    $studentIdsWithSponsors = Sponsor::pluck('student_id')->flatten()->unique()->map(function ($item) {
        return is_array($item) ? $item : json_decode($item, true);
    })->flatten()->filter()->unique()->values();
    $uncommonStudentIds = $students->pluck('id')->diff($studentIdsWithSponsors);
    $uncommonStudents = $students->whereIn('id', $uncommonStudentIds);
    return view('sponsor.create', ['students' => $uncommonStudents]);
}


    // public function store(Request $request)
    // {
    //     // Validate the incoming request
    //     $validatedData = $request->validate([
    //         'fullName' => 'required|string',
    //         'gender' => 'required|in:male,female,other',
    //         'dob' => 'required|date_format:d/m/Y',
    //         'address' => 'required|string',
    //         'city' => 'required|string',
    //         'state' => 'required|string',
    //         'zip' => 'required|string',
    //         'country' => 'required|string',
    //         'phone' => 'required|string',
    //         'email' => 'required|email',
    //         'pan' => 'nullable|string',
    //     ]);

    //     //dd( $validatedData);
    //     $sponsor = Sponsor::create($validatedData);

        
    //     return redirect()->route('sponsor.list')->with('success', 'Sponsor registration successful!');
    // }


//     public function store(Request $request)
// {
//     dd($request->all());
//     // Validate the incoming request
//     $validatedData = $request->validate([
//         'fullName' => 'required|string',
//         'gender' => 'required|in:male,female,other',
//         'dob' => 'required|date_format:d/m/Y',
//         'address' => 'required|string',
//         'city' => 'required|string',
//         'state' => 'required|string',
//         'zip' => 'required|string',
//         'country' => 'required|string',
//         'phone' => 'required|string',
//         'email' => 'required|email',
//         'pan' => 'nullable|string',
//     ]);

//     // Create a new user
//     $user = new User();
//     $user->name = $validatedData['fullName']; // Assuming 'fullName' is the name field for the user
//     $user->email = $validatedData['email'];
//     $user->password = Hash::make($validatedData['phone']); // Using phone number as password, ensure it's hashed
//     $user->phone = $validatedData['phone'];
//     $user->role = 0;
//     $user->save();

//     // Now create a sponsor record
//     $sponsor = Sponsor::create($validatedData);

//     return redirect()->route('sponsor.list')->with('success', 'Sponsor registration successful!');
// }



public function store(Request $request)
{
   // dd($request->all());
    // Validate the incoming request
    $validatedData = $request->validate([
        'fullName' => 'required|string',
        'gender' => 'required|in:male,female,other',
        'dob' => 'required|date_format:d/m/Y',
        'address' => 'required|string',
        'city' => 'required|string',
        'state' => 'required|string',
        'zip' => 'required|string',
        'country' => 'required|string',
        'phone' => 'required|string',
        'email' => 'required|email',
        'pan' => 'nullable|string',
        'selected_students' => 'array', // Validate selected_students as an array
      // 'selected_students.*' => 'exists:students,id', // Ensure each selected student ID exists in the students table
    ]);

    // Create a new user
    $user = new User();
    $user->name = $validatedData['fullName']; // Assuming 'fullName' is the name field for the user
    $user->email = $validatedData['email'];
    $user->password = Hash::make($validatedData['phone']); // Using phone number as password, ensure it's hashed
    $user->phone = $validatedData['phone'];
    $user->role = 0;
    $user->save();
    
    // Now create a sponsor record
    $sponsor = new Sponsor();
    $sponsor->fullName = $validatedData['fullName'];
    $sponsor->gender = $validatedData['gender'];
    $sponsor->dob = Carbon::createFromFormat('d/m/Y', $validatedData['dob'])->toDateString();
    $sponsor->address = $validatedData['address'];
    $sponsor->city = $validatedData['city'];
    $sponsor->state = $validatedData['state'];
    $sponsor->zip = $validatedData['zip'];
    $sponsor->country = $validatedData['country'];
    $sponsor->phone = $validatedData['phone'];
    $sponsor->email = $validatedData['email'];
    $sponsor->pan = $validatedData['pan'];
    $sponsor->student_id = implode(',', $validatedData['selected_students']);
    $sponsor->user_id = $user->id;
//dd($user->id);
   // dd(json_encode($sponsor->student_id)) ;// Serialize the array before storing
   // $sponsor->user_id = $user->id; // Associate the sponsor with the user
    $sponsor->save();
    
    return redirect()->route('sponsor.list')->with('success', 'Sponsor registration successful!');
}




    public function sponserlist()
{
    $sponsors = Sponsor::all(); // Retrieve all sponsors from the database

    return view('sponsor.list', compact('sponsors')); // Pass sponsors data to the view
}

    public function edit($id)
    {
        $sponsor = Sponsor::findOrFail($id);
         $students = Studentdetails::all();

        return view('sponsor.edit', compact('sponsor', 'students'));
    }

   public function update(Request $request, $id)
{
    // Validate the incoming request
    $validatedData = $request->validate([
        'fullName' => 'required|string',
        'gender' => 'required|in:male,female,other',
        'dob' => 'required|date_format:d/m/Y',
        'address' => 'required|string',
        'city' => 'required|string',
        'state' => 'required|string',
        'zip' => 'required|string',
        'country' => 'required|string',
        'phone' => 'required|string',
        'email' => 'required|email',
        'pan' => 'nullable|string',
        'selected_students' => 'array', // Validate selected_students as an array
        // 'selected_students.*' => 'exists:students,id', // Ensure each selected student ID exists in the students table
    ]);

    // Find the sponsor record to update
    $sponsor = Sponsor::findOrFail($id);

    // Update sponsor details
    $sponsor->fullName = $validatedData['fullName'];
    $sponsor->gender = $validatedData['gender'];
    $sponsor->dob = Carbon::createFromFormat('d/m/Y', $validatedData['dob'])->toDateString();
    $sponsor->address = $validatedData['address'];
    $sponsor->city = $validatedData['city'];
    $sponsor->state = $validatedData['state'];
    $sponsor->zip = $validatedData['zip'];
    $sponsor->country = $validatedData['country'];
    $sponsor->phone = $validatedData['phone'];
    $sponsor->email = $validatedData['email'];
    $sponsor->pan = $validatedData['pan'];
   $sponsor->student_id = implode(',', $validatedData['selected_students']);

    // Save the updated sponsor details
    $sponsor->save();

    // You may handle user update if needed

    return redirect()->route('sponsor.list')->with('success', 'Sponsor details updated successfully!');
}

public function destroy($id)
{
    // Find the sponsor by ID
    $sponsor = Sponsor::findOrFail($id);

    // Delete the sponsor
    $sponsor->delete();

    // Redirect back with success message
    return redirect()->back()->with('success', 'Sponsor deleted successfully.');
}



}
