<?php

namespace app\models;

use Yii;
use yii\base\Model;

/**
 * This is the model class for table "bills".
 *
 * @property int $id
 * @property int $user_id
 * @property string $type
 * @property int $lot_id
 * @property User $user
 */
class Bills extends \yii\db\ActiveRecord {
    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return 'bills';
    }

    /*
         * without migrations
         * */

    public static function create(Bidding $bid) {
        $data = [
            'user_id' => Yii::$app->user->id,
            'type' => 'guarantee',
            'bid_id' => $bid->id,
            'payed' => 0,
            'auction_id' => $bid->auction_id,
        ];
        (new Bills($data))->save(false);
        // $data['type'] = 'registration';
        // (new Bills($data))->save(false);
        return true;
    }

    public static function types() {
        return
            [
                'guarantee' => Yii::t('app', 'Guaranteed payment'),
                'registration' => Yii::t('app', 'Registration fee'),
            ];
    }

    public static function payStatuses() {
        return
            [
                '0' => Yii::t('app','Нові'),
                '1' => Yii::t('app','Сплачено'),
                '2' => Yii::t('app','Не сплачено'),
            ];
    }

    public function rules() {
        return [
            [['user_id', 'type'], 'required'],
            [['user_id','auction_id' ,'bid_id'], 'integer'],
            [['type'], 'in', 'range' => array_keys(static::types())],
            [['payed'], 'in', 'range' => array_keys(static::payStatuses())],
        ];
    }

//    public function getLotName() {
//        return $this->hasOne(Lots::className(), );
//    }

    public function getBid() {
        return $this->hasOne(Bidding::className(), ['id' => 'bid_id']);
    }

    public function getAuction() {
        return $this->hasOne(Auctions::className(), ['id' => 'auction_id'])->via('bid');
    }

    public function getUser() {
        return $this->hasOne(User::className(), ['id' =>'user_id']);
    }

    public function getProfile() {
        return $this->hasOne(Profile::className(), ['user_id' => 'user_id']);
    }

    public function getRequisite() {
        return $this->hasOne(Requisites::className(), ['user_id' => 'user_id']);
    }
    public function attributeLabels() {
        return [
            'id' => Yii::t('app', 'ID'),
            'user_id' => Yii::t('app', 'User ID'),
            'type' => Yii::t('app', 'Bill type'),
            'bid_id' => Yii::t('app', 'Ставка №'),
            'payed' => Yii::t('app', 'Payed'),
            'auction_id' => Yii::t('app', 'ID Аукціонa'),
            'user_at_org' => Yii::t('app', 'Company full name'),
        ];
    }
}
