<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Mail\MyTestMail;
use Illuminate\Support\Facades\Mail;
use App\MyLog;
use App\Models\Booking;

class BookingController extends Controller
{
    public function index(Request $request) 
    {
        $bookings = Booking::
                    leftJoin('appointments','bookings.appointment_id','=','appointments.id')
                    ->leftJoin('customers','customers.id','=','bookings.customer_id')
                    ->select('bookings.*','appointments.appointment_name','customers.name','customers.phone','customers.email')
                    ->paginate(15);
        
        if($request->page == ''){
            return redirect('booking?page='.$bookings->lastPage());
        }else{
            return view('admin/booking',['bookings'=>$bookings,'title'=>'AR Clean Bookings']);
        }
    }
    
    public function insert(Request $request){
        date_default_timezone_set("Asia/Jakarta");
        $updatedAt = date('Y-m-d H:i:s');
        
        $id = $request->id;
        $custId = $request->cust_id;
        $appointment_date = $request->date;
        $appointment_id = $request->time_sheet;

        if(isset($request->approval)){
            $approved = 1;
        }else{
            $approved=0;
        }
        
        if($id == null){
            $values = array(
                'customer_id'=>$custId,
                'appointment_id'=>$appointment_id,
                'appointment_date'=>$appointment_date,
                'status'=>$approved,
                'source'=>1
            );
            Booking::insert($values);
            
        }else{
            $values = array(
                'appointment_id'=>$appointment_id,
                'appointment_date'=>$appointment_date,
                'status'=>$approved,
                'updated_at'=>$updatedAt
            );
            Booking::where('id',$id)->update($values);
        }
        
        return redirect('booking');
        
    }
    
    public function insertWeb(Request $request){
        date_default_timezone_set("Asia/Jakarta");
    
        $customerValues = array(
            'name'=>$request->name,
            'phone'=>$request->phone,
            'email'=>$request->email,
            'address'=>$request->address
        );
        $insertedCustomerId = DB::table('customers')->insertGetId($customerValues);
        
        $bookingValues = array(
            'customer_id'=>$insertedCustomerId,
            'appointment_id'=>$request->time_sheet,
            'appointment_date'=>$request->date,
            'source'=>2
        );
        $insertedBookingId = DB::table('bookings')->insertGetId($bookingValues);
        
        $services = $request->ar_product;
        foreach($services as $service){
            $itemValues = array('booking_id'=>$insertedBookingId,'service_id'=>$service);
            DB::table('booking_items')->insert($itemValues);
        }
            
        //Mail section
        
        //Send mail to admin
    	$details = [
            'title' => 'Mail from website by-arsolution.com',
            'body' => 'Pengunjung '.$request->name.' telah melakukan booking. Silahkan periksa Mail Inbox anda',
            'subject'=>'Pesan dari pelanggan',
            'mail_template'=>'my_mail'
        ];
       
        //Mail::to('simetris_id@yahoo.co.id')->send(new \App\MyTestMail($details));
        Mail::to('info@by-arsolution.com')->send(new \App\MyTestMail($details));
        //Mail::to('by.ar.solution@gmail.com')->send(new \App\MyTestMail($details));
        //End of mail section

        $pesan = 'Pesan terkirim.
                Team AR-Clean akan segera menghubungi anda!
                Terima Kasih';
                     
        $request->session()->put('message',$pesan);
        $request->session()->put('source','/');
        return redirect('message-box');
    }
    
    public function create(){
        date_default_timezone_set("Asia/Jakarta");
        $userBookings = null;

        $bookings = DB::select('select bookings.*,appointments.appointment_name from bookings left join appointments on appointments.id=bookings.appointment_id where bookings.appointment_date="'.date('Y-m-d').'"');
        $appointments = DB::table('appointments')->get();
        $customers = DB::table('customers')->get();
        return view('admin/booking_create',['userBookings'=>$userBookings,'bookings'=>$bookings,'appointments'=>$appointments,'customers'=>$customers]);
    }

    public function delete(Request $request){
        $items = DB::table('booking_items')->where('booking_id',$request->booking_id);

        if($items != null){
            DB::table('booking_items')->where('booking_id',$request->booking_id)->delete();
        }
        $result = DB::table('bookings')->where('id',$request->booking_id)->delete();

        $request->session()->put('message','Data berhasil di hapus.');
        $request->session()->put('source','booking');
        return redirect('message-box');
    }
    
    public function edit(Request $request){
        date_default_timezone_set("Asia/Jakarta");
        
        $appointments = DB::table('appointments')->get();
        $bookings = DB::select('select bookings.*,appointments.appointment_name from bookings left join appointments on appointments.id=bookings.appointment_id where bookings.appointment_date="'.date('Y-m-d').'"');
        $userBookings = Booking::leftJoin('customers','customers.id','=','bookings.customer_id')
            ->select('bookings.*','customers.name')
            ->where('bookings.id',$request->id)
            ->get();
        $customers = DB::table('customers')->get();

        return view('admin/booking_create',['userBookings'=>$userBookings,'appointments'=>$appointments,'bookings'=>$bookings,'customers'=>$customers]);
    }
    
    public function update(Request $request){
        date_default_timezone_set("Asia/Jakarta");
        $updatedAt = date('Y-m-d H:i:s');

        $values = array(
            'appointment_date'=>$request->date,
            'customer'=>$request->name,
            'appointment_id'=>$request->time_sheet,
            'updated_at'=>$updatedAt
        );
        $result = Booking::where('id',$request->booking_id)->update($values);
    }
    
    public function approve(Request $request){
        date_default_timezone_set("Asia/Jakarta");
        $updatedAt = date('Y-m-d H:i:s');

        $values = array('status'=>1,'updated_at'=>$updatedAt);
        $result = Booking::where('id',$request->booking_id)->update($values);
        
        $request->session()->put('message','Appointments approved!');
        $request->session()->put('source','booking');
        return redirect('message-box');
    }
    
    public function show(Request $request){
        date_default_timezone_set("Asia/Jakarta");
        $appointments = DB::table('appointments')->get();
        $bookings = DB::select('select bookings.*,appointments.appointment_name from bookings left join appointments on appointments.id=bookings.appointment_id where bookings.appointment_date="'.$request->date.'"');
        $appointments = ['appointments'=>$appointments,'bookings'=>$bookings,'request_date'=>$request->date];
       
        return Response()->json($appointments);
    }
    
    public function form(Request $request){
        $services = DB::table('services')->get();
        return view('admin/booking_form',['selected_service'=>$request->data,'source'=>'web','services'=>$services]);
    }
    
    public function saveCustomer(Request $request){
        $name = $request->cust_name;
        $bookingId = $request->booking_id;
        
        $customerValues = array('name'=>$name);
        $insertedId = DB::table('customers')->insertGetId($customerValues);
        
        $bookingValues = array('customer_id'=>$insertedId);
        Booking::where('id',$bookingId)->update($bookingValues);
        
        $request->session()->put('message','Data customer berhasil di simpan.');
        $request->session()->put('source','booking');
        return redirect('message-box');
    }
    
    public function createBilling(Request $request){
        $booking = Booking::where('id',$request->id)->first();
        
        $billingValues = array(
            'booking_id'=>$booking->id,
            'customer_id'=>$booking->customer_id
        );
        $insertedBillingId = DB::table('billings')->insertGetId($billingValues);
        
        $bookingItems = DB::table('booking_items')
            ->leftJoin('services','services.id','=','booking_items.service_id')
            ->select('booking_items.*','services.name','services.cost')
            ->where('booking_id',$booking->id)
            ->get();
        
        foreach($bookingItems as $item){
            $billingItemValues = array(
                'billing_id'=>$insertedBillingId,
                'description'=>$item->name,
                'service_id'=>$item->service_id,
                'amount'=>$item->cost
            );
            DB::table('billing_items')->insert($billingItemValues);
        }
        
        return redirect('billing');
    }
}
