<?php

class PlanillaController extends Controller
{
    public function filters()
    {
        return [
            'accessControl',
            [
                'CrugeAccessControlFilter',
            ],
        ];
    }

    public function accessRules()
    {
        return [
            [
                'allow',
                'actions' => ['update', 'descargar'],
                'users' => ['@'],
            ],
            [
                'deny',
                'users' => ['*'],
            ],
        ];
    }

    public function actionUpdate($id = null)
    {
        $model = $this->find($id);

        if (! isset($_POST['Asociado'])) {
            return $this->render('update', [
                'model' => $model,
                'id_asociado' => $id,
            ]);
        }

        $model->attributes = $_POST['Asociado'];
        $model->setScenario('carga_planilla_firmada');

        if (! $model->validate()) {
            return $this->render('update', [
                'model' => $model,
                'id_asociado' => $id,
            ]);
        }

        $imagen = CUploadedFile::getInstance($model, 'planilla');
        if ($imagen === null) {
            return $this->render('update', [
                'model' => $model,
            ]);
        }

        $ruta = vsprintf('%s%s.%s', [
            'images/planillas_firmadas/',
            $model->randomString(),
            $imagen->getExtensionName(),
        ]);

        if ($imagen->saveAs($ruta)) {
            $model->planilla_actualizada = true;
            $model->planilla_firmada = $ruta;
            $model->save();

            return $this->redirect([$id ? '/asociado/Asociado/admin' : '/cruge/ui/principal']);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    public function actionDescargar($id)
    {
        $model = $this->find($id);

        if (! $model->planilla_actualizada) {
            Yii::app()->user->setFlash(
                'info',
                "El asociado <strong>{$model->nombre()}</strong> no tiene planilla firmada cargada."
            );

            return $this->redirect(['/asociado/Asociado/admin']);
        }

        Yii::app()->getRequest()->sendFile(
            $model->cedula.'_'.$model->fechaingreso.'_'.$model->idasociado.'.'.$model->getExtensionFromPlanilla(),
            file_get_contents($model->planilla_firmada)
        );
    }

    private function find($id)
    {
        if (! $id) {
            $model = Asociado::model()->find([
                'condition' => 'cedula=:cedula AND blnborrado = false AND id_estatus in(1, 4)',
                'order' => 'idasociado DESC',
                'params' => [
                    'cedula' => Yii::app()->user->getUser()->cedula,
                ],
            ]);

            if (! $model) {
                throw new CHttpException(404, 'The requested page does not exist.');
            }

            return $model;
        }

        $model = Asociado::model()->find([
            'condition' => 'idasociado=:id AND blnborrado = false AND id_estatus in(1, 4)',
            'order' => 'idasociado DESC',
            'params' => [
                'id' => $id,
            ],
        ]);

        if (! $model) {
            throw new CHttpException(404, 'The requested page does not exist.');
        }

        return $model;
    }
}
