<?php


namespace backend\models;


use common\models\Card;
use yii\base\Model;

class CustomerInactivateForm  extends Model{

    public $inactivateDate;

    public $timestampInactivate;

    public $activeCardCount;

    public $message ;


    public function rules()
    {
        return [
            [[ 'inactivateDate'   ], 'required'],
            [[ 'inactivateDate', ], 'date'  , 'timestampAttribute' => 'timestampInactivate' ,'timestampAttributeFormat' => 'yyyy-MM-dd'  ],

        ];
    }


    public function loadActiveCardCount(){
        $this->activeCardCount = Card::find()->andWhere(
            ['status' => Card::STATUS_ACTIVE]
        )->count();
    }

    public function inactivate(){
        if ( !$this->validate()){
            return false;
        }
        Card::updateAll(
            [
                'status' => Card::STATUS_INACTIVE,
                'inactivated_at' => $this->timestampInactivate
            ],
            [
                'status' => Card::STATUS_ACTIVE
            ]
        );
        Card::updateFlagStatus();
        \Yii::$app->session->setFlash ( 'success', 'Kártyák inaktiválva' );
        $this->message =  $this->activeCardCount ." kártya inkatválva a köveztekző dátummal: " . $this->inactivateDate ;
        return true;
    }

}
