<?php

namespace App\Http\Controllers\AdminPanel;

use App\Http\Controllers\Controller;
use App\Models\CarOwner;
use App\Models\CarWashOwner;
use App\Models\WashDetail;
use App\Models\CwoSubscription;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
use Carbon\Carbon;
use DB;

class AdminController extends Controller
{


    public function index(Request $request)
    {

        $data['page_title'] = "Dashboard";
        $data['page_sub_title'] = "Statistics Overview";
        $data['home_url'] = route('admin.dashboard');
        View::share('data', $data);

        $data['cards'] = $this->card_data();
        // dd($data);
        return view('admin.dashboard', $data);
    }

    protected function card_data()
    {
        $cwo_count = CarWashOwner::count();
        $co_count = CarOwner::count();
        $total_revenue = WashDetail::select(DB::raw('SUM(total_amount) as total_revenue'))->where('status', 3)->get();
        $today = date("Y-m-d");
        // $thirty_days_ago = date('Y-m-d', strtotime('-30 days', strtotime($today)));
        // $year_ago = date('Y-m-d', strtotime('-365 days', strtotime($today)));
        $total_car_washed_today = WashDetail::whereDate('date_washed', $today)->where('status', 3)->count();
        $total_car_washed_this_month = WashDetail::whereYear('date_washed', Carbon::now()->year)->whereMonth('date_washed', Carbon::now()->month)->where('status', 3)->count();
        $total_car_washed_this_year = WashDetail::whereYear('date_washed', Carbon::now()->year)->where('status', 3)->count();
        // echo "<pre>";print_r($total_car_washed_this_year);exit;

        $total_subscription_amount_this_month = CwoSubscription::select(DB::raw('SUM(amount) as total_subscription_amount_this_month'))->whereYear('start_date', Carbon::now()->year)->whereMonth('start_date', Carbon::now()->month)->where('status', 1)->get();
        // echo $total_subscription_amount_this_month;exit;
        $total_subscription_amount_this_year = CwoSubscription::select(DB::raw('SUM(amount) as total_subscription_amount_this_year'))->whereYear('start_date', Carbon::now()->year)->where('status', 1)->get();
        // echo $total_subscription_amount_this_year;exit;
        $card_data = [
            [
                'title' => 'Car Wash Owners',
                'count' => $cwo_count,
                'icon' => 'icon-users',
                'color' => '#1e89c8'
            ],
            [
                'title' => 'Car Owners',
                'count' => $co_count,
                'icon' => 'icon-users',
                'color' => '#e58e95'
            ],
            [
                'title' => 'Subscription Paid This Month',
                // 'count' => formatCurrency($total_subscription_amount_this_month[0]['total_subscription_amount_this_month']),
                'count' => $total_subscription_amount_this_month[0]['total_subscription_amount_this_month'] != '' ? formatCurrency($total_subscription_amount_this_month[0]['total_subscription_amount_this_month']) : 0,
                'icon' => 'icon-credit-card',
                'color' => '#90afd0'

            ],
            [
                'title' => 'Subscription Paid This Year',
                // 'count' => formatCurrency($total_subscription_amount_this_year[0]['total_subscription_amount_this_year']),
                'count' => $total_subscription_amount_this_year[0]['total_subscription_amount_this_year'] != '' ? formatCurrency($total_subscription_amount_this_year[0]['total_subscription_amount_this_year']) : 0,
                'icon' => 'icon-credit-card',
                'color' => '#df3657'
            ],
            // [
            //     'title' => 'R Total Revenue Generated',
            //     'count' => formatCurrency($total_revenue[0]['total_revenue']),
            //     'icon' => 'icon-credit-card',
            //     'color' => '#90afd0'

            // ],
            [
                'title' => 'Cars Washed Today',
                'count' => $total_car_washed_today,
                'icon' => 'icon-truck',
                'color' => '#df3657'
            ],
            [
                'title' => 'Cars Washed This Month',
                'count' => $total_car_washed_this_month,
                'icon' => 'icon-truck',
                'color' => '#ffa730'
            ],
            [
                'title' => 'Cars Washed This Year',
                'count' => $total_car_washed_this_year,
                'icon' => 'icon-truck',
                'color' => '#5dbaac'
            ],

        ];

        return $card_data;
    }
}
