<?php /** @noinspection PhpUnused */


namespace frontend\controllers;


use common\models\Card;
use common\models\Customer;
use common\models\Fingerprint;
use frontend\models\FingerprintCreate;
use frontend\models\FingerprintSearch;
use Throwable;
use Yii;
use yii\db\StaleObjectException;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\Response;

class FingerprintController extends Controller
{


    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['index'],
                'rules' => [
                    // allow authenticated users
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                    // everything else is denied
                ],
            ],

        ];
    }

    /**
     * @return string
     * @throws NotFoundHttpException
     */
    public function actionIndex()
    {
        $searchModel = new FingerprintSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'card' => $searchModel->card,
            'customer' => $searchModel->customer,
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * @param $id_card
     * @return string|Response
     * @throws NotFoundHttpException
     */
    public function actionCreate($id_card)
    {

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

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

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

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


        $model = new FingerprintCreate();
        $model->id_customer = $customer->id_customer;

        /** @noinspection NotOptimalIfConditionsInspection */
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index', 'FingerprintSearch[id_card]' => $card->id_card]);
        }
        return $this->render('create', [
            'card' => $card,
            'model' => $model,
        ]);

    }


    /**
     * @param $id
     * @return Response
     * @throws NotFoundHttpException
     * @throws Throwable
     * @throws StaleObjectException
     */
    public function actionDelete($id)
    {

        $model = Fingerprint::findOne($id);

        if (!isset($model)) {
            throw new NotFoundHttpException();
        }


        /** @var Customer $customer */
        $customer = Customer::findOne($model->id_customer);

        $model->delete();

        return $this->redirect([
            'index',
            'FingerprintSearch[id_card]' => $customer->id_customer_card
        ]);

    }

}
