<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Appointment;
use Carbon\Carbon;
use App\Models\AppointmentBookingDetail;

class AppointmentController extends Controller
{
    public function index(){

        $educationAppointments = Appointment::where('date', '>=', Carbon::today())->where('service_type', '1')->get();
        $visaAppointments = Appointment::where('date', '>=', Carbon::today())->where('service_type', '2')->get();
        // foreach($educationAppointments as $appointment){
        //     $startTime = Carbon::createFromFormat('H:i', $appointment->start_time);
        //     $date = Carbon::createFromFormat('H:i', $appointment->start_time);
        //     dd($date);
        // }
        return view('appointment', compact('educationAppointments', 'visaAppointments'));

    }

    public function get_appointment_by_date(Request $request){

        $dateTime = $request->date;

        $date_parts = explode(" ", $dateTime);
        $month_number = date_parse($date_parts[1])['month'];
        $carbon = Carbon::createFromDate($date_parts[3], $month_number, $date_parts[2]);
        $date = $carbon->format('Y-m-d');

        $type_id = $request->id;
        $appointments_all = Appointment::whereDate('date',$date)->where(['service_type' => $type_id,'is_booked' => false,'status' => 1])->get();
        $old_date = Carbon::createFromFormat('Y-m-d', $date);
        $formated_date = $old_date->format('M d, Y');
        $currentTime = Carbon::now();
        $appointments = [];
        foreach ($appointments_all as $appointment) {
            $start_time = explode(" ", $appointment->start_time);
            $start_time = $start_time[0];
            $appointmentDate = Carbon::createFromFormat('Y-m-d H:i', $appointment->date.' '.$start_time);
            if ($appointmentDate->gt($currentTime)) {
                array_push($appointments, $appointment);
            }
        }

        return response()->json(['appointment' => $appointments,'formated_date' => $formated_date]);
    }

    public function form_submit(Request $request){

        $request->validate([
            'name' => 'required',
            'email' => 'required|email',
            'phone' => 'required',
        ]);
        $appointment_id = $request->get('appointment_id');

        $appointment = Appointment::findorfail($appointment_id);
        $appointment_detail = new AppointmentBookingDetail();
        $appointment_detail->name = $request->get('name');
        $appointment_detail->email = $request->get('email');
        $appointment_detail->phone = $request->get('phone');
        $appointment_detail->notes = $request->get('notes');
        $appointment_detail->appointment_id = $appointment_id;
        $email = $request['email'];
        $name = $request['name'];
        $phone = $request['phone'];

        if($appointment_detail->save()){

            $date = Carbon::createFromFormat('Y-m-d', $appointment->date);
            $formated_date = $date->format('M d, Y');
            $appointment->is_booked = true;
            $appointment->save();
            $subject = 'Appointment Booked Successfully.';
            dispatch(function() use ($name,$email,$phone,$subject,$formated_date,$appointment) {

                \Mail::send('appointment_confirmed', array(

                'full_name' =>$name,

                'email' =>$email,

                'date' => $formated_date,

                'start_time' => $appointment['start_time'],

                'end_time' => $appointment['end_time'],

                'phone' =>$phone,

                'subject' =>$subject


               ), function($message) use ($subject,$email,$name){
                        // $subject=($service!= '') ? 'Enquiry for '.$service : 'Contact/Feedback';
                       $message->subject($subject);
                    //    $message->from('admin@eteducation.com.au', 'Admin');
                       $message->to($email, $name)->subject($subject);
                     // $message->cc('extratechweb@gmail.com', 'Extratech')->subject($subject);
                    // $message->cc('admin@eteducation.com.au', 'Extratech')->subject($subject);

               });


            });

            dispatch(function() use ($name,$email,$phone,$subject,$formated_date,$appointment) {

                \Mail::send('appointment_confirmed_for_admin', array(

                'full_name' =>$name,

                'email' =>$email,

                'date' => $formated_date,

                'start_time' => $appointment['start_time'],

                'end_time' => $appointment['end_time'],

                'phone' =>$phone,

                'subject' =>$subject

               ), function($message) use ($subject){
                        // $subject=($service!= '') ? 'Enquiry for '.$service : 'Contact/Feedback';
                       $message->subject($subject);
                    //    $message->to($email, $name)->subject($subject);
                      $message->to('mahesh@extratechs.com.au', 'Extratech')->subject($subject);
                    // $message->cc('admin@eteducation.com.au', 'Extratech')->subject($subject);

               });


            });
        }

        return response()->json(['appointment_detail' => $appointment_detail,'appointment' => $appointment,'formated_date' => $formated_date],200);

    }
}