<?php

class FormWithEmail extends CallbackForm

{
    public $email;
    public $sendFile;


    public function __construct(string $name, string $phone, string $email, array $sendFile)
    {
        parent::__construct($name, $phone);
        $this->email = $email;
        $this->sendFile = $sendFile;
    }

    public function validate(): bool
    {

        if (!empty($this->email) && !filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
            return false;
        }
        if ($this->sendFile['size'] > 5242880) {
            echo "Недопустимо большой размер файла";
            return false;
        }
        if (!empty($this->sendFile['name']) && $this->sendFile['type'] !== 'application/pdf') {
            echo "Используйте файл с расширением *.pdf";
            return false;
        }
        return parent::validate();
    }

    public function uploadFiles(): void
    {
        $uploadDirectory = dirname(__DIR__, 1) . '/file/';
        if (!file_exists($uploadDirectory)) {
            mkdir($uploadDirectory, 0777);
            $way = $uploadDirectory . time() . $this->sendFile['name'];
            move_uploaded_file($this->sendFile['tmp_name'], $way);
        } else {
            $way = $uploadDirectory . time() . $this->sendFile['name'];
            move_uploaded_file($this->sendFile['tmp_name'], $way);
        }
    }

    public function send()
    {
        parent::send();
        if (!empty($this->email)) {
            echo '<br>';
            echo 'Email: ' . $this->email;
        }
    }
}