<?php

declare(strict_types=1);

namespace app\controllers;

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

use app\models\Request;
use app\models\Account;

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');

        // Запись поставщика
        Account::writeSupplier($request['name'], $request['phon'], $request['mail'], $file = UploadedFile::getInstance(new Request($request), 'file'));

        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->tempName, ['fileName' => $file->name])
            ->send();

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