<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "messages".
 *
 * @property integer $id
 * @property integer $user_id
 * @property string $notes
 * @property string $date
 * @property integer $status
 */
class Notification extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'messages';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'notes', 'date'], 'required'],
            [['user_id', 'status'], 'integer'],
            [['date'], 'safe'],
            [['notes'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'user_id' => Yii::t('app', 'User ID'),
            'notes' => Yii::t('app', 'Notes'),
            'date' => Yii::t('app', 'Date'),
            'status' => Yii::t('app', 'Status'),
        ];
    }
    public function fields()
    {
        return [
            'count_messages' => function () {
                $sql = Yii::$app->db->createCommand("SELECT count(*) as msg from messages WHERE status=0 AND user_id=:user_id");
                $sql->bindValue(':user_id', Yii::$app->user->identity->id);
                $result = $sql->queryOne();
                return $result['msg'];
            },
            'count_bids'  => function () {
                if(Yii::$app->user->can('member'))
                {
                    $sql = Yii::$app->db->createCommand("SELECT count(*) as bid from bidding WHERE readed=0 AND user_id=:user_id");
                }
                else
                {
                    $sql = Yii::$app->db->createCommand("SELECT count(*) as bid from bidding WHERE readed=0 AND org_id=:user_id");
                }
                $sql->bindValue(':user_id', Yii::$app->user->identity->id);
                $result = $sql->queryOne();
                return $result['bid'];
            },
        ];
    }
}
