<?php


namespace frontend\models;


use common\models\Card;
use common\models\Customer;
use common\models\Fingerprint;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;

class FingerprintSearch extends Fingerprint
{

    public $id_card;

    public $customer;
    public $card;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[ 'id_card'], 'integer'],
            [[ 'id_card'], 'required']
        ];
    }

    /**
     * @param $params
     * @return ActiveDataProvider
     * @throws NotFoundHttpException
     */
    public function search($params){

        $this->load($params);
        $this->validate();


        $query = Fingerprint::find();

        /** @var Card $card */
        $card = Card::findOne($this->id_card);

        if ( !isset($card)){
            throw new NotFoundHttpException('Card not found:' . $this->id_card);
        }
        $this->card = $card;

        $customer = Customer::findOne($card->customer->id_customer);

        if ( !isset($customer)){
            throw new NotFoundHttpException('Customer not found');
        }


        $this->customer = $customer;

        $query->andWhere(['id_customer' => $customer->id_customer]);
        return new ActiveDataProvider([
            'query' => $query,
        ]);
}

}
