<?php

declare(strict_types=1);

namespace app\controllers;

use yii;
use yii\web\Controller;
use yii\web\UploadedFile;

use app\models\Request;

class SuppliersController extends Controller
{
    public function actionIndex()
    {
        return $this->renderPartial('/suppliers/index');
    }

    public function actionRequest()
    {
        return $this->renderPartial('/suppliers/request');
    }

    /**
     * @todo Сделать отправку только назначенным модераторам
     */
    public function actionRequestSend()
    {
        $request = yii::$app->request->post('Request') ?? yii::$app->request->get('Request');

        yii::$app->mail_system->compose()
            ->setFrom(yii::$app->params['mail']['system'])
            ->setTo(yii::$app->params['mail']['info'])
            ->setSubject('Регистрация поставщика')
            ->setHtmlBody($this->renderPartial('/mails/supplier', $request))
            ->attach(($file = UploadedFile::getInstance(new Request($request), 'file'))->tempName, ['fileName' => $file->name])
            ->send();

        return $this->renderPartial('/suppliers/requested');
    }
}
