<?php

namespace App\Exports\Export;

use App\Models\FormEntry;
use Maatwebsite\Excel\Concerns\FromCollection;

//use Maatwebsite\Excel\Concerns\WithDrawings;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithStyles;

//use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class DashboardFormEntriesExport implements FromCollection, WithHeadings, WithMapping, WithStyles
{
    protected $search;
    protected $sortDirection;
    protected $sortColumn;
    protected $count;

    public function __construct($search, $sortColumn, $sortDirection)
    {
        $this->search = $search;
        $this->sortColumn = $sortColumn ?? 'created_at';
        $this->sortDirection = $sortDirection ?? 'desc';
    }

    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        $sitePosReport = FormEntry::query();

        $sitePosReport->select('form_name', 'form_surname', 'form_company', 'form_email', 'form_phone', 'status', 'created_at');

        $sitePosReport->where('is_deleted', '=', 'false');

        if ($this->search) {
            $sitePosReport->where(function ($query) {
                $query->where('form_name', 'like', '%' . $this->search . '%')
                    ->orWhere('form_surname', 'like', '%' . $this->search . '%')
                    ->orWhere('form_company', 'like', '%' . $this->search . '%')
                    ->orWhere('form_email', 'like', '%' . $this->search . '%')
                    ->orWhere('form_phone', 'like', '%' . $this->search . '%');
            });
        }

        $this->count = $sitePosReport->count();
        $sitePosReport->orderBy($this->sortColumn, $this->sortDirection);

        return $sitePosReport->get();
    }


    /**
     * @return array
     */
    public function headings(): array
    {
        return [
            'İsim',
            'Soyisim',
            'Firma',
            'Eposta',
            'Telefon',
            'Status',
            'Tarih',
        ];
    }

    /**
     * @param mixed $row
     * @return array
     */
    public function map($row): array
    {
        return [
            $row->form_name,
            $row->form_surname,
            $row->form_company,
            $row->form_email,
            $row->form_phone,
            $row->status,
            date('d.m.Y', strtotime($row->created_at))
        ];
    }

    /**
     * @param Worksheet $sheet
     * @return void
     */
    public function styles(Worksheet $sheet)
    {
        $numOfRows = $this->count;
        $totalRow = $numOfRows + 2;

        //chnaged color of header
        $sheet->mergeCells("A{$totalRow}:G{$totalRow}");
        $sheet->getStyle("G{$totalRow}")->getFont()->setBold(true);

        //text color white and background color dark gray
        $sheet->getStyle('A1:G1')->getFill()->setFillType('solid')->getStartColor()->setARGB('FF333333');
        $sheet->getStyle('A1:G1')->getFont()->getColor()->setARGB('FFFFFFFF');
        $sheet->getStyle('A1:G1')->getFont()->setBold(true);
    }

//    public function drawings()
//    {
//        $drawing = new Drawing();
//        $drawing->setName('Logo');
//        $drawing->setDescription('Pionr');
//        $drawing->setPath(public_path('assets/images/tahsildar.png'));
//        $drawing->setHeight(90);
//        //insert the image in the first row
//        $drawing->setCoordinates('G1');
//        $drawing->setOffsetX(110);
//        $drawing->setOffsetY(10);
//
//        return $drawing;
//    }


}
