<?php

namespace app\models;

use Yii;
use yii\helpers\Url;

/**
 * This is the model class for table "email_templates".
 *
 * @property string $id
 * @property string $template_name
 * @property string $dedicated_for
 * @property string $is_active
 * @property string $content
 */
class EmailTemplates extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'email_templates';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['template_name', 'dedicated_for', 'content', 'is_active'], 'required'],
            [['content'], 'string'],
            [['template_name', 'dedicated_for'], 'string', 'max' => 150],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'template_name' => Yii::t('app', 'Template Name'),
            'dedicated_for' => Yii::t('app', 'Dedicated For'),
            'content' => Yii::t('app', 'Content'),
            'is_active' => Yii::t('app', 'Is Active')
        ];
    }

    public function getVariables(){
        return $this->hasMany(EmailTemplatesVariables::className(), ['template_id' => 'id']);
    }

    public static function replaceVariablesCode(&$content, $code, $training_request, $interview = null, $training = null, $rejection = null, $user = null){
        switch($code){
            case '[TRE_FN]':
                $content = $training_request != null ? str_replace('[TRE_FN]', $training_request->full_name, $content) : $content;
            break;
            case '[TRA_CD]':
                $content = $training_request != null ? str_replace('[TRA_CD]', $training_request->create_date, $content) : $content;
            break;
            case '[TRA_SN]':
                $content = $training_request != null ? str_replace('[TRA_SN]', $training_request->serial_number, $content) : $content;
            break;
            case '[PAS_RT]':
                $content = $user != null ? str_replace('[PAS_RT]', Url::base(true) . '/users/reset-password?token=' . $user->reset_password, $content) : $content;
            break;
            case '[INT_DT]':
                $content = $interview != null ? str_replace('[INT_DT]', $interview->date, $content) : $content;
            break;
            case '[INT_BR]':
                $content = $interview != null ? str_replace('[INT_BR]', $interview->branch->branch . ' (' . $interview->branch->address . ')', $content) : $content;
            break;
            case '[TRA_SD]':
                $content = $training != null ? str_replace('[TRA_SD]', $training->start_date, $content) : $content;
            break;
            case '[TRA_ED]':
                $content = $training != null ? str_replace('[TRA_ED]', $training->end_date, $content) : $content;
            break;
            case '[TRA_BR]':
                $content = $training != null ? str_replace('[TRA_BR]', $training->branch->branch . ' (' . $training->branch->address . ')', $content) : $content;
            break;
            case '[TRA_RC]':
                $content = $rejection != null ? str_replace('[TRA_RC]', $rejection->rejection_causes, $content) : $content;
            break;
        }
    }
}
