<?php

namespace app\models;

use Yii;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "bidding".
 *
 * @property integer $id
 * @property integer $auction_id
 * @property integer $user_id
 * @property integer $status
 */
class Bidding extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public $cnt;

    public static function tableName(){
        return 'bidding';
    }

    /**
     * @inheritdoc
     */
    public function rules(){
        return [
            [['auction_id', 'user_id', 'status'], 'required'],
            [['auction_id', 'user_id', 'org_id', 'status','file_id'], 'integer'],
            [['auctionName','fileName','created_at'], 'safe'],
            [['special_price'], 'number', 'integerOnly' => false],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels(){
        return [
            'id' => Yii::t('app', 'ID'),
            'auction_id' => Yii::t('app', 'Auction ID'),
            'user_id' => Yii::t('app', 'User ID'),
            'org_id'    =>  Yii::t('app', 'Org ID'),
            'status' => Yii::t('app', 'Status'),
            'auctionName'   =>  Yii::t('app', 'Auction ID'),
            'fileName'   =>  Yii::t('app', 'FileName ID'),
            'created_at'    =>  Yii::t('app', 'BiddingDate ID'),
            'date_update' => Yii::t('app', 'Date Update'),
            'special_price' => Yii::t('app', 'Спеціальна ставка'),
        ];
    }

    public function CreateBid($insert){
        $model = new Bidding();
        $model->setAttributes([
            'auction_id' => $insert['auction_id'],
            'user_id' => $insert['user_id'],
            'org_id'    => $insert['org_id'],
            'file_id'    => $insert['file_id'],
            'created_at' =>  date("Y-m-d H:i:s"),
            'special_price' => $insert['special_price'],
            'type' => 'guarantee'
        ]);
        return $model->save(false) && Bills::create($model);
    }

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

    public function getLot(){
        return $this->hasOne(Lots::className(), ['id' => 'lot_id'])->via('auction');
    }

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

    public function getBills(){
        return $this->hasMany(Bills::className(), ['user_id' => 'user_id']);
    }    

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

    public function getAuctionName(){
        return ArrayHelper::getValue($this->auction,'name');
        //return $this->auction->name;
    }

    public function getFile(){
        return $this->hasOne(Files::className(), ['id' => 'file_id']);
    }

    public function getFileName(){
        return $this->file->name;
    }





}
