<?php

namespace common\modules\event\models;


use common\models\Event;
use common\models\EventEquipmentType;
use common\models\EventEquipmentTypeAssignment;
use common\modules\event\manager\EventManager;
use common\modules\event\models\timetable\TimeTableMonth;
use common\modules\event\models\timetable\TimeTableMonthDay;
use customerapi\models\available\EventInterval;
use DateTime;
use Exception;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;

/**
 * @property string $sourceDateString
 */
class EventEquipmentTypeForm extends Model
{

    public $equipmentTypeList = [];
    public $idEquipmentType;
    public $idEvent;
    public $count = null;
    public $event = null;

    public $assignedEquipmentTypes;

    private $equipmentType = null;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['count','idEvent','idEquipmentType'],'required'],
            ['count','integer'],
            [['idEvent',], 'validateIdEvent'],
            [['idEquipmentType',], 'validateEquipment'],
        ];
    }

    public function validateEquipment(){

        $this->equipmentType = EventEquipmentType::find()->andWhere(['id'=> $this->idEquipmentType])->one();
        if ( !isset($this->equipmentType)){
            $this->addError("idEquipmentType","Hibás Felszerelés típus");
        }
    }

    public function validateIdEvent(){
        $this->loadEvent();
        if ( !isset($this->event)){
            $this->addError("idEvent","Esemény nem található");
        }
    }

    public function loadEvent(){
        $this->event = Event::find()->andWhere(['id'=> $this->idEvent])->one();
    }

    public function loadAssignedEquipment(){
        $this->assignedEquipmentTypes = new ActiveDataProvider(
            [
                'query' =>  EventEquipmentTypeAssignment::find()->andWhere(['id_event' => $this->event->id]),
                'pagination' => false,
//                'sort' => [
//                    'defaultOrder' => [
//                        'created_at' => SORT_DESC,
//                        'title' => SORT_ASC,
//                    ]
//                ],
            ]
        );

    }

    /**
     * @throws Exception
     */
    public function save(){
        $result = $this->validate();
        if ( $result === false ){
            return false;
        }

        // delete assignments with the same type
        EventEquipmentTypeAssignment::deleteAll(
            [
                'id_event' => $this->event->id,
                'id_event_equipment_type' => $this->equipmentType->id
            ]
        );

        if ( $this->count > 0 ){
            $assignment = new EventEquipmentTypeAssignment();
            $assignment->id_event = $this->event->id;
            $assignment->id_event_equipment_type = $this->equipmentType->id;
            $assignment->count =   $this->count;
            $assignment->save(false);
        }

        return true;
    }

   public function attributeLabels(){
        return [
            "count" => "Mennyiség"
        ];
   }



}
