<?php

namespace backend\models;

use Yii;
use backend\models\Book;
use backend\models\Chapter;

/**
 * This is the model class for table "comment".
 *
 * @property integer $id
 * @property integer $chapterId
 * @property string $name
 * @property string $content
 * @property string $datetime
 * @property integer $status
 */
class Comment extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'comment';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['chapterId', 'bookId','name', 'content', 'datetime', 'status'], 'required'],
            [['chapterId','bookId', 'status'], 'integer'],
            [['datetime'], 'safe'],
            [['name'], 'string', 'max' => 50],
            [['content'], 'string', 'max' => 250]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'bookId' => 'Book',
            'chapterId' => 'Chapter',
            'name' => 'Name',
            'content' => 'Comment',
            'datetime' => 'Date Time',
            'status' => 'Status',
        ];
    }

    public static function itemAlias($type,$code=NULL) {
        $_items = array(
            'status' => array(
                '0' => 'Inactive',
                '1' => 'Active',
            ),
        );
        if (isset($code))
            return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
        else
            return isset($_items[$type]) ? $_items[$type] : false;
    }

    public static function  bookName($bookId)
    {
        $book = Book::findOne($bookId);
        //var_dump($book);exit;
        if(count($book)>0)
            return $book->title;
        else
            return '';
    }

    public static function  chapterName($chapterId)
    {
        $chapter = Chapter::findOne($chapterId);
        if(count($chapter)>0)
            return $chapter->title;
        else
            return '';
    }
}
