<?php

namespace App\Exports\SafetyAndPrivacy;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use App\Models\SafetyAndPrivacy as SafetyAndPrivacyModel;
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 Hekmatinasser\Verta\Verta;

class SafetyAndPrivacyExport implements FromView, ShouldAutoSize, WithEvents, WithDrawings, WithStyles
{
    public function __construct($id, $info_id, $step, $fromDate, $toDate, $province_id)
    {
        $this->id = $id;
        $this->info_id = $info_id;
        $this->step = $step;
        $this->fromDate = $fromDate;
        $this->toDate = $toDate;
        $this->province_id = $province_id;
    }

    public function view(): View
    {
        $fromDate = $this->fromDate ? $this->fromDate . ' 00:00:00': null;
        $toDate = $this->toDate ?  $this->toDate . ' 23:59:59': null;

        if (auth()->user()->hasPermissionTo('show-safety-and-privacy-operator-cartable')) {

            $data = SafetyAndPrivacyModel::when($this->id, function($query, $id){
                        return $query->where('id', $id);
                    })
                    ->when($this->info_id, function($query, $info_id){
                        return $query->where('info_id', $info_id);
                    })
                    ->when($this->step, function($query, $step){
                        return $query->where('step', $step);
                    })
                    ->when(($fromDate && $toDate), function($query) use($fromDate, $toDate){
                        return $query->whereBetween('activity_date_time', [$fromDate, $toDate]);
                    })
                    ->when($this->province_id, function($query, $province_id){
                        return $query->where('province_id', $province_id);
                    })
                    ->select('id', 'info_id', 'info_fa',
                            'created_at', 'activity_date_time', 'step', 'step_fa', 'province_fa', 
                            'edare_shahri_name', 'judiciary_document_upload_date', 'action_picture_document_upload_date')
                    ->orderBy('province_id')
                    ->orderBy('info_id')
                    ->get();


        } elseif (auth()->user()->hasPermissionTo('show-safety-and-privacy-operator-cartable-province')) {
            if (is_null(auth()->user()->province_id)) {
                return response()->json([
                    'message' => 'استانی برای شما در سامانه ثبت نشده است!'
                ], 400);
            }

            $data = SafetyAndPrivacyModel::where('province_id', auth()->user()->province_id)
                    ->when($this->id, function($query, $id){
                        return $query->where('id', $id);
                    })
                    ->when($this->info_id, function($query, $info_id){
                        return $query->where('info_id', $info_id);
                    })
                    ->when($this->step, function($query, $step){
                        return $query->where('step', $step);
                    })
                    ->when(($fromDate && $toDate), function($query) use($fromDate, $toDate){
                        return $query->whereBetween('activity_date_time', [$fromDate, $toDate]);
                    })
                    // ->when($this->province_id, function($query, $province_id){
                    //     return $query->where('province_id', $province_id);
                    // })
                    ->select('id', 'info_id', 'info_fa',
                            'created_at', 'activity_date_time', 'step', 'step_fa', 'province_fa', 
                            'edare_shahri_name', 'judiciary_document_upload_date', 'action_picture_document_upload_date')
                    ->orderBy('province_id')
                    ->orderBy('info_id')
                    ->get();
        } elseif (auth()->user()->hasPermissionTo('show-safety-and-privacy-operator-cartable-edarate-shahri')) {
            if (is_null(auth()->user()->edarate_shahri_id)) {
                return response()->json([
                    'message' => 'اداره ای برای شما در سامانه ثبت نشده است!'
                ], 400);
            }

            // $data = SafetyAndPrivacyModel::where('edare_shahri_id', auth()->user()->edarate_shahri_id)
            //                      ->selectRaw($selectRaw);


            $data = SafetyAndPrivacyModel::where('edare_shahri_id', auth()->user()->edarate_shahri_id)
                    ->when($this->id, function($query, $id){
                        return $query->where('id', $id);
                    })
                    ->when($this->info_id, function($query, $info_id){
                        return $query->where('info_id', $info_id);
                    })
                    ->when($this->step, function($query, $step){
                        return $query->where('step', $step);
                    })
                    ->when(($fromDate && $toDate), function($query) use($fromDate, $toDate){
                        return $query->whereBetween('activity_date_time', [$fromDate, $toDate]);
                    })
                    ->select('id', 'info_id', 'info_fa',
                            'created_at', 'activity_date_time', 'step', 'step_fa', 'province_fa', 
                            'edare_shahri_name', 'judiciary_document_upload_date', 'action_picture_document_upload_date')
                    ->orderBy('province_id')
                    ->orderBy('info_id')
                    ->get();                                 
        }


        // $data = SafetyAndPrivacyModel::when($this->province, function ($query, $province) {
        //                             return $query->where('province_id', $province);
        //                         })
        //                         ->whereBetween('activity_date_time', [$fromDate, $toDate])
                                // ->orderBy('province_id')
                                // ->orderBy('info_id')
        //                         ->get();

        return view('excel.SafetyAndPrivacy.SafetyAndPrivacyList', [
            'data' => $data,
            'fromFa' => $fromDate ?? null,
            'toFa' => $toDate ?? null
        ]);
    }

    /**
     * @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'
                ]
            ],
        ];
    }
}
