<?php

namespace App\Http\Controllers\AdminPanel;

use App\Http\Controllers\Controller;
// use App\Services\CarSearchService;
use Illuminate\Http\Request;
use App\Models\CarOwnerCar;
use App\Models\CarOwner;
use App\Models\User;

class SearchCarController extends Controller
{
    protected $carsearchService;

    // public function __construct(CarSearchService $carsearchService)
    // {
    //     $this->carsearchService = $carsearchService;
    // }
    public function __construct()
    {
    }

    /*public function index(Request $request)
    {
        if ($request->ajax()) {
            $cwo_lists = $this->carsearchService->getCarWashOwner();
            $iTotalRecords = $cwo_lists->count();

            $iDisplayLength = isset($_REQUEST['length']) ? intval($_REQUEST['length']) : 0;
            $iDisplayLength = $iDisplayLength < 0 ? $iTotalRecords : $iDisplayLength;
            $iDisplayStart = isset($_REQUEST['start']) ? intval($_REQUEST['start']) : 0;
            $sEcho = isset($_REQUEST['draw']) ? intval($_REQUEST['draw']) : 0;
            $rowNumber = $iDisplayStart + 1;

            foreach ($cwo_lists as $t) {

                $actionColumnHtml = '<td class="product-action">
                <span class="action-edit"><i class="feather icon-edit"></i></span>
                <span class="action-delete"><i class="feather icon-trash"></i></span>
                </td>';
                if ($t->status == 1) {
                    $status = '<div class="chip  chip-success">
                    <div class="chip-body">
                        <div class="chip-text">Active</div>
                    </div>
                </div>';
                } else {

                    $status = '<div class="chip  chip-danger">
                                <div class="chip-body">
                                    <div class="chip-text">Inactive</div>
                                </div>
                            </div>';
                }



                $records["data"][] = array(
                    // $rowNumber,
                    '',
                    '<div class="product-name">' . $t->acc_id . '</div>',
                    '<div class="product-name">' . $t->company . '</div>',
                    '<div class="custom-icon-center">' . $t->phone . '</div>',
                    '<div class="custom-icon-center">' . $t->name . '</div>',
                    '<div class="custom-icon-center">' . $t->bemail . '</div>',
                    '<div class="custom-icon-center">' . $t->bphone . '</div>',
                    '<div class="custom-icon-center">' . $t->email . '</div>',
                    $status,
                    '<div class="custom-icon-center">' . $t->reg_date . '</div>',
                    '<span class="custom-btn">' . $actionColumnHtml . '</span>'

                );
                $rowNumber++;
            }

            $records["draw"] = $sEcho;
            $records["recordsTotal"] = $iTotalRecords;
            $records["recordsFiltered"] = $iTotalRecords;
            return json_encode($records);
        }
        return view('carsearch.list');
    }*/

    public function index(Request $request)
    {
        // echo "search car";exit;
        $dataBag = array();
        $dataBag['chMenu'] = 'searchCarList';
        $car_registration_no = $request->input('car_registration_no');
        // $data = CarOwnerCar::select('co_car.id','co_car.car_model','co_car.car_registration_no','co_master.co_fname','co_master.co_lname','co_master.co_phone','users.email','wash_details.date_washed','co_master.isd_code')
        // ->join('co_master','co_master.id','=','co_car.co_id')
        // ->join('users','users.id','=','co_master.user_id')
        // ->leftJoin('wash_details','wash_details.car_id','=','co_car.id')
        // // ->where('co_car.car_registration_no', $car_registration_no)
        // ->where('co_car.car_registration_no', 'like', '%' . $car_registration_no . '%')
        // ->orderBy('co_car.id', 'desc');

        $data = CarOwnerCar::select('wash_details.id', 'wash_details.car_id', 'wash_details.entry_date', 'wash_details.wash_type_id', 'wash_details.body_type', 'wash_details.amount', 'wash_details.date_washed', 'wash_details.time_washed', 'wash_details.status as wash_status', 'wash_details.payment_status', 'wash_details.payment_details', 'cwo_master.cwo_name', 'cwo_master.cwo_company', 'co_master.co_fname', 'co_master.co_lname', 'co_master.co_phone', 'users.email', 'co_car.car_registration_no', 'currency_masters.symbol', 'wash_details.entry_time', 'co_master.isd_code', 'wash_details.driver_name', 'wash_details.driver_isd_code', 'wash_details.driver_phone', 'wash_details.total_amount', 'wash_details.is_free_wash', 'wash_details.driver_email', 'wash_details.wash_in', 'wash_details.wash_out')
        ->join('co_master','co_master.id','=','co_car.co_id')
        ->join('users','users.id','=','co_master.user_id')
        ->leftJoin('wash_details','wash_details.car_id','=','co_car.id')
        ->leftJoin('cwo_master', 'cwo_master.id', '=', 'wash_details.cwo_id')
        ->leftJoin('currency_masters', 'currency_masters.id', '=', 'cwo_master.currency_id')
        ->where('co_car.car_registration_no', 'like', '%' . $car_registration_no . '%')
        ->orderBy('wash_details.id', 'desc');

        $dataCnt = $data->count();
        $data = $data->paginate(10);
        $dataBag['allRecords'] = $dataCnt;
        $dataBag['records'] = $data;
        // echo "<pre>";print_r($dataBag['records']->toArray());exit;
        return view('carsearch.list', $dataBag);
        
    }


    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}
