<?php

namespace App\Http\Controllers\OwnerPanel;

use App\Http\Controllers\Controller;
use App\Models\User;;

use App\Models\CurrencyMaster;
use App\Models\CarWashOwner;
use App\Models\WashDetail;
use Illuminate\Http\Request;
use Auth;
use Carbon\Carbon;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\View;
use DB;

class OwnerController extends Controller
{
    public function index()
    {
        $data['page_title'] = "Dashboard";
        $data['page_sub_title'] = "Statistics Overview";
        // return view('owner.dashboard');
        View::share('data', $data);
        $data['cards'] = $this->card_data();
        // dd($data);
        return view('owner.dashboard', $data);
    }

    public function profile(Request $request)
    {
        $userid = Auth::user()->id;
        // echo $id;exit;
        $record = User::findorfail($userid);
        $dataBag['details'] = $record;
        // echo "<pre>";print_r($dataBag['details']);exit;
        return view('owner.edit', $dataBag);
    }

    public function updateprofile(Request $request)
    {
        $userid = Auth::user()->id;
        $request->validate([
            'name' => 'required',
            'email' => 'required|email|unique:users,email,' . $userid,
            // 'phone' => 'numeric|nullable',
            // 'password' => [
            //     'required',
            //     'string',
            //     'min:6',             // must be at least 6 characters in length
            //     'regex:/[a-z]/',      // must contain at least one lowercase letter
            //     'regex:/[A-Z]/',      // must contain at least one uppercase letter
            //     'regex:/[0-9]/',      // must contain at least one digit
            //     'regex:/[!@#$%^&*()]/', // must contain a special character
            // ]
        ], [
            'name.required' => 'Please enter a name.',
            'email.required' => 'Please enter an email.',
            'email.email' => 'Please enter an email of correct format.',
            'email.unique' => 'This Email-id already exist, Try Another.',
            // 'password.required' => 'Please enter a password.',
            // 'password.regex' => 'Password must contain atleast one lowercase letter, one uppercase letter, one digit & a special character.',
            // 'phone.numeric' => 'Phoneno. should be numeric.',
        ]);
        $name = $request->input('name');
        $email = $request->input('email');
        $phone = $request->input('phone');
        $dt = Carbon::now();
        /*update users table starts*/
        $data = User::findOrFail($userid);
        if ($request->input('password') != '') {
            $password = Hash::make($request->input('password'));
            $record = $data->update([
                'name' => $name,
                'email' => $email,
                'password' => $password,
                'phone' => $phone,
                'isd_code' => $request->isd_code,
                'updated_at' => $dt->toDayDateTimeString()
            ]);
        } else {
            $record = $data->update([
                'name' => $name,
                'email' => $email,
                'phone' => $phone,
                'isd_code' => $request->isd_code,
                'updated_at' => $dt->toDayDateTimeString()
            ]);
        }

        /*update users table ends*/
        return back()->with('success', 'Your profile been updated successfully.')->with('msg_class', 'alert alert-success');
    }

    protected function card_data()
    {
        $cwo_id = Auth::user()->cwo_id;
        // echo $cwo_id;exit;
        $today = date('Y-m-d');  
        /*$totalCarWashed = WashDetail::join('wash_categories', 'wash_categories.id', '=', 'wash_details.wash_type_id')
            ->where('wash_categories.cwo_id', $cwo_id)
            ->where('wash_details.status', 3)
            ->count();*/

        $getCurrency = CarWashOwner::leftJoin('currency_masters', 'currency_masters.id', '=', 'cwo_master.currency_id')->select('currency_masters.code', 'currency_masters.symbol')->where('cwo_master.id', $cwo_id)->get();
        // echo "<pre>";print_r($getCurrency->toArray());exit;

        /*$today = date('Y-m-d');

        $totalRevenue = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('wash_categories', 'wash_categories.id', '=', 'wash_details.wash_type_id')
            ->join('cwo_master', 'cwo_master.id', '=', 'wash_categories.cwo_id')
            ->where('wash_categories.cwo_id', $cwo_id)
            ->where('wash_details.status', 3)
            ->get();
        // echo "<pre>";print_r($totalRevenue->toArray());exit;
        $totalRevenue = $totalRevenue[0]->total_revenue != '' ? $totalRevenue[0]->total_revenue : 0;

        $todayRevenue = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('wash_categories', 'wash_categories.id', '=', 'wash_details.wash_type_id')
            ->join('cwo_master', 'cwo_master.id', '=', 'wash_categories.cwo_id')
            ->where('wash_categories.cwo_id', $cwo_id)
            ->where('wash_details.status', 3)
            ->whereDate('wash_details.date_washed', '=', $today)
            ->get();
        // echo "<pre>";print_r($todayRevenue->toArray());exit;
        $todayRevenue = $todayRevenue[0]->total_revenue != '' ? $todayRevenue[0]->total_revenue : 0;*/

        $todayTotalCarWashed = WashDetail::where('cwo_id', $cwo_id)
            ->whereDate('date_washed', '=', $today)
            // ->where('status', 3)
            ->count(); 

        $monthToDateTotalCarWashed = WashDetail::where('cwo_id', $cwo_id)
            ->whereYear('date_washed', Carbon::now()->year)
            ->whereMonth('date_washed', Carbon::now()->month)
            // ->where('status', 3)
            ->count(); 

        $yearToDateTotalCarWashed = WashDetail::where('cwo_id', $cwo_id)
            ->whereYear('date_washed', Carbon::now()->year)
            // ->where('status', 3)
            ->count();  

        $todayTotalRevenue = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            ->whereDate('wash_details.date_washed', '=', $today)
            ->where('wash_details.payment_status', 1)
            ->get();
        // echo "<pre>";print_r($todayTotalRevenue->toArray());exit;
        $todayTotalRevenue = $todayTotalRevenue[0]->total_revenue != '' ? $todayTotalRevenue[0]->total_revenue : 0;  

        $mtdTotalRevenue = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            ->whereYear('wash_details.date_washed', Carbon::now()->year)
            ->whereMonth('wash_details.date_washed', Carbon::now()->month)
            ->where('wash_details.payment_status', 1)
            ->get();
        $mtdTotalRevenue = $mtdTotalRevenue[0]->total_revenue != '' ? $mtdTotalRevenue[0]->total_revenue : 0;    

        $ytdTotalRevenue = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            ->whereYear('wash_details.date_washed', Carbon::now()->year)
            ->where('wash_details.payment_status', 1)
            ->get();
        $ytdTotalRevenue = $ytdTotalRevenue[0]->total_revenue != '' ? $ytdTotalRevenue[0]->total_revenue : 0;    

        /***************** today starts *********************/
        $todayRevenueInCash = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            ->where('wash_details.payment_details', 'cash')
            ->whereDate('wash_details.date_washed', '=', $today)
            ->get();
        // echo "<pre>";print_r($todayRevenueInCash->toArray());exit;
        $todayRevenueInCash = $todayRevenueInCash[0]->total_revenue != '' ? $todayRevenueInCash[0]->total_revenue : 0;

        $todayRevenueInCard = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            // ->where('wash_details.payment_details', 'card')
            ->where('wash_details.payment_details', 'like', '%' . 'card' . '%')
            ->whereDate('wash_details.date_washed', '=', $today)
            ->get();
        // echo "<pre>";print_r($todayRevenueInCard->toArray());exit;
        $todayRevenueInCard = $todayRevenueInCard[0]->total_revenue != '' ? $todayRevenueInCard[0]->total_revenue : 0;

        $todayRevenueInContract = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            ->where('wash_details.payment_details', 'like', '%' . 'contract' . '%')
            ->whereDate('wash_details.date_washed', '=', $today)
            ->get();
        // echo "<pre>";print_r($todayRevenueInContract->toArray());exit;
        $todayRevenueInContract = $todayRevenueInContract[0]->total_revenue != '' ? $todayRevenueInContract[0]->total_revenue : 0;

        // $todayRevenueInCash = $todayRevenueInCash + $todayRevenueInContract;
        /******************* today ends ************************/

        /**************** month to date starts ****************/
        $one_month_ago = date('Y-m-d', strtotime('-1 month', strtotime($today)));
        $monthAgoRevenueInCash = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            ->where('wash_details.payment_details', 'cash')
            // ->whereDate('wash_details.date_washed', '>=', $one_month_ago)
            // ->whereDate('wash_details.date_washed', '<=', $today)4
            ->whereYear('wash_details.date_washed', Carbon::now()->year)
            ->whereMonth('wash_details.date_washed', Carbon::now()->month)
            ->get();
        $monthAgoRevenueInCash = $monthAgoRevenueInCash[0]->total_revenue != '' ? $monthAgoRevenueInCash[0]->total_revenue : 0; 
        
        $monthAgoRevenueInCard = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            // ->where('wash_details.payment_details', 'card')
            ->where('wash_details.payment_details', 'like', '%' . 'card' . '%')
            // ->whereDate('wash_details.date_washed', '>=', $one_month_ago)
            // ->whereDate('wash_details.date_washed', '<=', $today)
            ->whereYear('wash_details.date_washed', Carbon::now()->year)
            ->whereMonth('wash_details.date_washed', Carbon::now()->month)
            ->get();
        $monthAgoRevenueInCard = $monthAgoRevenueInCard[0]->total_revenue != '' ? $monthAgoRevenueInCard[0]->total_revenue : 0;

        $monthAgoRevenueInContract = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            ->where('wash_details.payment_details', 'like', '%' . 'contract' . '%')
            // ->whereDate('wash_details.date_washed', '>=', $one_month_ago)
            // ->whereDate('wash_details.date_washed', '<=', $today)
            ->whereYear('wash_details.date_washed', Carbon::now()->year)
            ->whereMonth('wash_details.date_washed', Carbon::now()->month)
            ->get();
        $monthAgoRevenueInContract = $monthAgoRevenueInContract[0]->total_revenue != '' ? $monthAgoRevenueInContract[0]->total_revenue : 0;     

        // $monthAgoRevenueInCash = $monthAgoRevenueInCash + $monthAgoRevenueInContract;
        /**************** month to date ends *******************/

        /**************** year to date starts ****************/
        $one_year_ago = date('Y-m-d', strtotime('-1 year', strtotime($today)));
        $yearAgoRevenueInCash = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            ->where('wash_details.payment_details', 'cash')
            // ->whereDate('wash_details.date_washed', '>=', $one_year_ago)
            // ->whereDate('wash_details.date_washed', '<=', $today)
            ->whereYear('wash_details.date_washed', Carbon::now()->year)
            ->get();
        $yearAgoRevenueInCash = $yearAgoRevenueInCash[0]->total_revenue != '' ? $yearAgoRevenueInCash[0]->total_revenue : 0; 
        
        $yearAgoRevenueInCard = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            // ->where('wash_details.payment_details', 'card')
            ->where('wash_details.payment_details', 'like', '%' . 'card' . '%')
            // ->whereDate('wash_details.date_washed', '>=', $one_year_ago)
            // ->whereDate('wash_details.date_washed', '<=', $today)
            ->whereYear('wash_details.date_washed', Carbon::now()->year)
            ->get();
        $yearAgoRevenueInCard = $yearAgoRevenueInCard[0]->total_revenue != '' ? $yearAgoRevenueInCard[0]->total_revenue : 0; 

        $yearAgoRevenueInContract = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            ->where('wash_details.payment_details', 'like', '%' . 'contract' . '%')
            // ->whereDate('wash_details.date_washed', '>=', $one_year_ago)
            // ->whereDate('wash_details.date_washed', '<=', $today)
            ->whereYear('wash_details.date_washed', Carbon::now()->year)
            ->get();
        $yearAgoRevenueInContract = $yearAgoRevenueInContract[0]->total_revenue != '' ? $yearAgoRevenueInContract[0]->total_revenue : 0;  

        // $yearAgoRevenueInCash = $yearAgoRevenueInCash + $yearAgoRevenueInContract;
        /**************** year to date ends *******************/ 

        /*$card_data = [
            [
                'title' => 'Total Car Wash',
                'count' => $totalCarWashed,
                'icon' => 'icon-truck',
                'color' => '#1e89c8'
            ],
            [
                'title' => 'Total Revenue',
                // 'count' => $getCurrency[0]->symbol . $totalRevenue,
                'count' => $getCurrency[0]->symbol . formatCurrency($totalRevenue),
                'icon' => 'icon-credit-card',
                'color' => '#e58e95'
            ],
            [
                'title' => 'Revenue Today',
                // 'count' => $getCurrency[0]->symbol . $todayRevenue,
                'count' => $getCurrency[0]->symbol . formatCurrency($todayRevenue),
                'icon' => 'icon-credit-card',
                'color' => '#5dbaac'

            ],
            [
                'title' => 'Average Wash Time',
                'count' => 0,
                'icon' => 'icon-clock',
                'color' => '#df3657'
            ],

        ];*/

        $card_data = [
            
            [
                'title' => 'Cash',
                'count' => $getCurrency[0]->symbol . formatCurrency($todayRevenueInCash),
                'icon' => 'icon-credit-card',
                'color' => '#e58e95'
            ],
            [
                'title' => 'Card',
                'count' => $getCurrency[0]->symbol . formatCurrency($todayRevenueInCard),
                'icon' => 'icon-credit-card',
                'color' => '#90afd0'
            ],
            [
                'title' => 'Contract',
                'count' => $getCurrency[0]->symbol . formatCurrency($todayRevenueInContract),
                'icon' => 'icon-credit-card',
                'color' => '#df3657'
            ],
            [
                'title' => 'Total',
                'count' => $getCurrency[0]->symbol . formatCurrency($todayTotalRevenue),
                'icon' => 'icon-credit-card',
                'color' => '#1e89c8'
            ],
            [
                'title' => 'Cash',
                // 'count' => $getCurrency[0]->symbol . $totalRevenue,
                'count' => $getCurrency[0]->symbol . formatCurrency($monthAgoRevenueInCash),
                'icon' => 'icon-credit-card',
                'color' => '#ffa730'
            ],
            [
                'title' => 'Card',
                // 'count' => $getCurrency[0]->symbol . $totalRevenue,
                'count' => $getCurrency[0]->symbol . formatCurrency($monthAgoRevenueInCard),
                'icon' => 'icon-credit-card',
                'color' => '#5dbaac'
            ],
            [
                'title' => 'Contract',
                // 'count' => $getCurrency[0]->symbol . $totalRevenue,
                'count' => $getCurrency[0]->symbol . formatCurrency($monthAgoRevenueInContract),
                'icon' => 'icon-credit-card',
                'color' => '#1e89c8'
            ],
            [
                'title' => 'Total',
                // 'count' => $getCurrency[0]->symbol . $totalRevenue,
                'count' => $getCurrency[0]->symbol . formatCurrency($mtdTotalRevenue),
                'icon' => 'icon-credit-card',
                'color' => '#df3657'
            ],
            [
                'title' => 'Cash',
                'count' => $getCurrency[0]->symbol . formatCurrency($yearAgoRevenueInCash),
                'icon' => 'icon-credit-card',
                'color' => '#e58e95'

            ],
            [
                'title' => 'Card',
                'count' => $getCurrency[0]->symbol . formatCurrency($yearAgoRevenueInCard),
                'icon' => 'icon-credit-card',
                'color' => '#90afd0'

            ],
            [
                'title' => 'Contract',
                'count' => $getCurrency[0]->symbol . formatCurrency($yearAgoRevenueInContract),
                'icon' => 'icon-credit-card',
                'color' => '#df3657'

            ],
            [
                'title' => 'Total',
                'count' => $getCurrency[0]->symbol . formatCurrency($ytdTotalRevenue),
                'icon' => 'icon-credit-card',
                'color' => '#1e89c8'

            ],
            [
                'title' => 'Today',
                'count' => $todayTotalCarWashed,
                'icon' => 'icon-truck',
                'color' => '#df3657'

            ],
            [
                'title' => 'Month To Date',
                'count' => $monthToDateTotalCarWashed,
                'icon' => 'icon-truck',
                'color' => '#ffa730'

            ],
            [
                'title' => 'Year To Date',
                'count' => $yearToDateTotalCarWashed,
                'icon' => 'icon-truck',
                'color' => '#5dbaac'

            ],

        ];

        return $card_data;
    }
}
