<?php

namespace app\models;

use Yii;
use yii\db\ActiveRecord;
use yii\db\Expression;
use yii\behaviors\TimestampBehavior;
/**
 * This is the model class for table "hasil_voting".
 *
 * @property int $id_voting
 * @property int $id_user
 * @property int $pilihan
 * @property int $status_voting
 * @property string|null $created_at
 * @property int|null $created_by
 * @property string|null $updated_at
 * @property int|null $updated_by
 * @property int|null $active
 *
 * @property TUser $user
 * @property MVoting $voting
 */
class HasilVoting extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'hasil_voting';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id_voting', 'id_user', 'pilihan', 'status_voting'], 'required'],
            [['id_voting', 'id_user', 'pilihan', 'status_voting', 'created_by', 'updated_by', 'active'], 'integer'],
            [['created_at', 'updated_at'], 'safe'],
            [['id_voting', 'id_user'], 'unique', 'targetAttribute' => ['id_voting', 'id_user']],
            [['id_user'], 'exist', 'skipOnError' => true, 'targetClass' => TUser::className(), 'targetAttribute' => ['id_user' => 'id']],
            [['id_voting'], 'exist', 'skipOnError' => true, 'targetClass' => MVoting::className(), 'targetAttribute' => ['id_voting' => 'id_voting']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id_voting' => 'Id Voting',
            'id_user' => 'Id User',
            'pilihan' => 'Pilihan',
            'status_voting' => 'Status Voting',
            'created_at' => 'Created At',
            'created_by' => 'Created By',
            'updated_at' => 'Updated At',
            'updated_by' => 'Updated By',
            'active' => 'Active',
        ];
    }

    public function behaviors()
    {
       return [
           'timestamp' => [
               'class' => TimestampBehavior::className(),
               'attributes' => [
                   ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                   ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
               ],
               'value' => new Expression('NOW()'),
           ],

       ];
    }

    public function beforeSave($insert)
    {
        if (!parent::beforeSave($insert)) {
            return false;
        }
        if(ActiveRecord::EVENT_BEFORE_INSERT){
            $this->created_by = Yii::$app->user->identity->id;
            $this->updated_by = Yii::$app->user->identity->id;
        }else if(ActiveRecord::EVENT_BEFORE_UPDATE){
            $this->updated_by = Yii::$app->user->identity->id;
        }

        return true;
    }

    /**
     * Gets query for [[User]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(TUser::className(), ['id' => 'id_user']);
    }

    /**
     * Gets query for [[Voting]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getVoting()
    {
        return $this->hasOne(MVoting::className(), ['id_voting' => 'id_voting']);
    }
}
