<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Billing;
use App\Models\Booking;
use App\Models\Customer;


class CustomerController extends Controller
{
    public function index(Request $request) 
    {
        $customers = Customer::paginate(15);
        if($request->page==''){
            return redirect('customer?page='.$customers->lastPage());
        }else{
            return view('admin/customer',['customers'=>$customers,'title'=>'AR Clean Customers']);
        }
    }
    
    public function delete(Request $request){
        $countBillings = Billing::where('customer_id',$request->id)->count();
        if($countBillings > 0){
            return response()->json([
                'success' => false,
                'message' => "Data customer tidak dapat di hapus. Karena digunakan pada database billings.",
                'data'    => ""
            ]);
            exit();
        }

        $countBookings = Booking::where('customer_id',$request->id)->count();
        if($countBookings > 0){
            return response()->json([
                'success' => false,
                'message' => "Data customer tidak dapat di hapus. Karena digunakan pada database bookings.",
                'data'    => ""
            ]);
            exit();
        }
        
        Customer::where('id',$request->id)->delete();

        return response()->json([
            'success' => true,
            'message' => "Berhasil",
            'data'    => $request->id
        ]);
    }
    
    public function getCustomer(Request $request){
        $customer = Customer::where('id',$request->id)->first();

        return response()->json([
            'success' => true,
            'message' => 'Detail Data Post',
            'data'    => $customer
        ]);
    }
    
    public function store(Request $request){
        date_default_timezone_set("Asia/Jakarta");

        $id = $request->id;

        $values = array(
            'name'=>$request->name,
            'address'=>$request->address,
            'phone'=>$request->phone,
            'email'=>$request->email,
            'updated_at'=>date('Y-m-d H:i:s')
        );
        
        if($id === '' || $id === null){
            //Insert data
            $insertedId = Customer::insertGetId($values);
            //$insertedId = 101;
            $values['id'] = $insertedId;

            return response()->json([
                'success' => true,
                'message' => "Insert data berhasil",
                'data'    => $values
            ]);

        }else{
            //Update data
            $values['id'] = $id;
            Customer::where('id',$id)->update($values);

            return response()->json([
                'success' => true,
                'message' => "Update data berhasil",
                'data'    => $values
            ]);
            
        }
        
    }
    
    public function livesearch(Request $request){
        $customers = '';
        if($request->q != ''){
            $customers = DB::table('customers')->where('name','LIKE','%'.$request->q.'%')->get();
        }
        return response()->json(['status'=>'sukses','data'=>$customers]);
    }
}
