<?php

namespace App\Exports;

use App\Models\VerifiedData;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\Exportable;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithMapping;

class VerifiedDataExport implements FromCollection, WithHeadings,WithColumnFormatting,WithMapping

{
    use Exportable;

    protected $from_date;
    protected $to_date;

    function __construct($from_date,$to_date) {
            $this->from_date = $from_date;
            $this->to_date = $to_date;
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        $query = VerifiedData::select("unique_id","name","phone","q_id","email","created_at");
            if ($this->from_date != '' && $this->to_date != '') {
                $query=$query->whereDate('created_at', '>=', $this->from_date)
                ->whereDate('created_at', '<=', $this->to_date);
            }
            $data=$query->orderBy('created_at', 'desc')->get();
        return $data;
    }

    /**
     * Write code on Method
     *
     * @return response()
     */
    public function headings(): array
    {
        return ["Unique ID", "Name", "Phone","Q-ID","Email","Created At"];
    }

    public function map($formdata): array
    {
        return [
            // $formdata->id,
            $formdata->unique_id,
            $formdata->name,
            $formdata->phone,
            $formdata->q_id,
            $formdata->email,
            Date::dateTimeToExcel($formdata->created_at)
        ];
    }
    
    public function columnFormats(): array
    {
        return [
            'A' => NumberFormat::FORMAT_TEXT,
            'B' => NumberFormat::FORMAT_TEXT,
            'C' => NumberFormat::FORMAT_TEXT,
            'D' => NumberFormat::FORMAT_TEXT,
            'E' => NumberFormat::FORMAT_TEXT,
            'F' => NumberFormat::FORMAT_DATE_DATETIME,
        ];
    }
}
