<?php

namespace App\Http\Livewire\Backend;

use App\Models\OrderManagement\Order;
use App\Models\SettingManagement\Status;
use Illuminate\Support\Facades\DB;
use Livewire\Component;

class DashboardChartComponent extends Component
{
    public $monthLabels = [];
    public $monthValues = [];

    public function mount(){
        $statuses = [
            Status::DELIVERED,
            Status::PAID,
        ];
        $orders = Order::select(DB::raw('SUM(total) as revenue'), DB::raw('Month(created_at) as month'))
                        ->whereIn('status_code',$statuses)
                        ->whereYear('created_at', date('Y'))
                        ->groupBy(DB::raw('Month(created_at)'))
                        ->pluck('revenue', 'month');


        foreach ($orders->keys() as $month_number) {
            $this->monthLabels[] = date('F', mktime(0, 0, 0, $month_number, 1));
        }
        $this->monthValues = $orders->values()->toArray();
//        $this->monthValues = $orders->map(function ($row){
//                                        return format_amount($row,true);
//                                    })->values()->toArray();
      //  dd($this->monthValues,$orders->values()->toArray(),$this->monthLabels);
    }

    public function render(){
        return view('livewire.backend.dashboard-chart-component');
    }
}
