<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "sub_categories".
 *
 * @property int $id
 * @property int $main_category_id
 * @property string|null $image_path
 * @property string $arabic_name
 * @property string $english_name
 *
 * @property Items[] $items
 * @property MainCategories $mainCategory
 */
class SubCategories extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'sub_categories';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['main_category_id', 'arabic_name', 'english_name'], 'required'],
            [['main_category_id'], 'integer'],
            [['image_path'], 'string', 'max' => 500],
            [['arabic_name', 'english_name'], 'string', 'max' => 200],
            [['main_category_id'], 'exist', 'skipOnError' => true, 'targetClass' => MainCategories::className(), 'targetAttribute' => ['main_category_id' => 'id']],
            [['image_path'], 'safe'],
            [['image_path'], 'file', 'extensions'=>'jpg, png, jpeg'],
            [['status'], 'string'],
            [['status'], 'default','value'=> 'Active'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'main_category_id' => Yii::t('app', 'Main Category'),
            'image_path' => Yii::t('app', 'Image'),
            'arabic_name' => Yii::t('app', 'Arabic Name'),
            'english_name' => Yii::t('app', 'English Name'),
            'status' => Yii::t('app', 'Status'),
        ];
    }

    /**
     * Gets query for [[Items]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getItems()
    {
        return $this->hasMany(Items::className(), ['sub_category_id' => 'id'])
        ->andOnCondition(['status' => 'Active']);
    }

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