<?php

namespace App\Exports\Sheets;

use App\Models\Fpdx\FormAnswerModel;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithTitle;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class FormAnswerSheet implements FromQuery, WithTitle, WithMapping, WithHeadings
{

    private $question;


    public function __construct($question)
    {
        $this->question = $question;
    }


    public function headings(): array
    {
        return [
            '#',
            'User',
            'Type',
            'Answer',
            'Date'
        ];
    }


    public function map($answer): array
    {
        return [
            '#',
            $answer->uid,
            $answer->type,
            $answer->answer,
            $answer->created_at,
        ];
    }


    public function query()
    {
        return FormAnswerModel::query()->where('question_id', $this->question->id);
    }

    /**
     * @return string
     */
    public function title(): string
    {
        return sprintf('第%s题', $this->question->sort);
    }


    public function columnFormats(): array
    {
        return [
            'E' => NumberFormat::FORMAT_DATE_DDMMYYYY,
        ];
    }
}
