<?php

namespace App\Exports\Receipts;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use App\Models\Accident;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithDrawings;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use App\Models\User as UserORM;

class ReceiptCityReport implements FromView, ShouldAutoSize, WithEvents, WithDrawings, WithStyles
{
    public function __construct($fromDate = null, $toDate = null, $province = null)
    {
        $this->fromDate = $fromDate ?? null;
        $this->toDate = $toDate ?? null;
        $this->province = $province ?? null;
    }

    public function view(): View
    {

        $fromDate = $this->fromDate ?? null;
        $toDate = $this->toDate ?? null;
        $province = $this->province_id ?? null;

        $data['list'] = UserORM::When($fromDate, function ($query) use ($fromDate, $toDate) {
            return $query->whereBetween('created_at', [$fromDate, $toDate]);
        })
        ->when($province, function ($query) use ($province) {
            return $query->whereIn('province_id', $province);
        })
        ->get();
        
        return view('excel.user-list', [
            'array' => $data,
            'fromDate' => $fromDate,
            'toDate' => $toDate
        ]);


        $province = $this->province ?? null;
        $fromDate = $this->fromDate ?? \Carbon\Carbon::now()->subDays(7);
        $toDate = $this->toDate ?? \Carbon\Carbon::now();
        
        $data = \App\Models\Accident::whereBetween('accident_date', [$fromDate . ' 00:00:00', $toDate . ' 23:59:59'])
                                    ->select(DB::raw('COUNT(*) as all_items'), DB::raw('COUNT(CASE WHEN status = 2 OR status=3 THEN 1 END) as tedad_vosol_shode'), DB::raw('sum(CASE WHEN status = 2 OR status=3 THEN deposit_amount END) as majmoee_vosol_shode'),'province_fa', DB::raw('SUM(deposit_amount) as sum'), 'city_fa')
                                    ->when($province, function ($query, $province) {
                                        return $query->where('province_id', $province);
                                    })
                                    ->groupBy('city_id')
                                    ->get();
        return view('excel.Receipt.City', [
            'data' => $data,
        ]);
    }

    /**
     * @return array
    */
    public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function (AfterSheet $event) {
                $event->sheet->getDelegate()->setRightToLeft(true);
            },
        ];
    }

    public function drawings()
    {
        $drawing = new Drawing();
        $drawing->setName('Logo');
        $drawing->setDescription('This is my logo');
        $drawing->setPath(public_path('/dist/logo.png'));
        $drawing->setWidth(50);
        $drawing->setHeight(50);
        $drawing->setOffsetX(5);
        $drawing->setOffsetY(5);
        $drawing->setCoordinates('A1');
        
        $drawing2 = new Drawing();
        $drawing2->setName('Logo');
        $drawing2->setDescription('This is my logo');
        $drawing2->setPath(public_path('/dist/141icon.png'));
        $drawing2->setWidth(50);
        $drawing2->setHeight(50);
        $drawing2->setOffsetX(5);
        $drawing2->setOffsetY(5);
        $drawing2->setCoordinates('B1');
        return [$drawing, $drawing2];
    }

    public function styles(Worksheet $sheet)
    {
        return [
            // Style the first row as bold text.
            'A:BA' =>    [
                'font' => [
                    'name' => 'B Nazanin'
                ]
            ],
        ];
    }
}
