<?php namespace App\Controllers\Presupuestos;


use App\Models\Collection;

use App\Models\Presupuestos\PresupuestoPreimpresionesModel;


class Presupuestopreimpresiones extends \App\Controllers\BaseResourceController
{

    protected $modelName = PresupuestoPreimpresionesModel::class;
    protected $format = 'json';

    protected static $singularObjectName = 'Presupuesto preimpresion';
    protected static $singularObjectNameCc = 'presupuestoPreimpresion';
    protected static $pluralObjectName = 'Presupuestos preimpresion';
    protected static $pluralObjectNameCc = 'presupuestosPreimpresion';

    protected static $controllerSlug = 'presupuesto-preimpresiones';

    protected static $viewPath = 'themes/vuexy/form/presupuestos/';

 

    public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
    }
 

    public function edit($requestedId = null)
    {

        if ($requestedId == null) :
            return;
        endif;

        $postData = $this->request->getJSON();
        $tarifas = array_column($postData->datos, 'tarifa_id');
        if(count($tarifas)>0){
            $this->model->deleteServiciosNotInArray($requestedId, $tarifas);
        }
        else{
            $this->model->deleteAllServicios($requestedId);
        }

        if(count($tarifas)>0){
            $this->model->updateTarifas($requestedId, $postData->datos);
        }

        $newTokenHash = csrf_hash();
        $csrfTokenName = csrf_token();
        $data = [
            $csrfTokenName => $newTokenHash
        ];
        
        return $this->respond($data);
    }

    public function update($requestedId = null)
    {

        if ($requestedId == null) :
            return;
        endif;

        $postData = $this->request->getJSON();
        $tarifas = array_column($postData->datos, 'tarifa_id');
        $result = [];

        if(count($tarifas)>0){
            foreach ($tarifas as $tarifa){
                $values = $this->model->getPrecioTarifa($tarifa);
                array_push($result, $values);
            }
        }
        
        $newTokenHash = csrf_hash();
        $csrfTokenName = csrf_token();
        $data = [
            'lines' => $result,
            $csrfTokenName => $newTokenHash
        ];
        
        return $this->respond($data);
    }

    public function getServiciosPreimpresion($datos_tarifas)
    {
        $result = [];
        $model = model('App\Models\Presupuestos\Presupuestopreimpresiones');
        
        if (count($datos_tarifas) > 0) {
            foreach ($datos_tarifas as $tarifa) {
                $values = $model->getPrecioTarifa($tarifa);
                array_push($result, $values[0]);
            }
        }
        return $result;
    }


    public function datatable()
    {
        if ($this->request->isAJAX()) {
            $reqData = $this->request->getPost();

            $tarifa_preimpresion_id = $reqData['tarifa_preimpresion_id'] ?? 0;
            
            $newTokenHash = csrf_hash();
            $csrfTokenName = csrf_token();

            $values = $this->model->getPrecioTarifa($tarifa_preimpresion_id);

            $data = [
                'values' => $values,
                $csrfTokenName => $newTokenHash
            ];

            return $this->respond($data);

        } else {
            return $this->failUnauthorized('Invalid request', 403);
        }
    }

    public function getRowValues()
    {
        if ($this->request->isAJAX()) {
            $reqData = $this->request->getPost();

            $tarifa_preimpresion_id = $reqData['tarifa_preimpresion_id'] ?? 0;
            
            $newTokenHash = csrf_hash();
            $csrfTokenName = csrf_token();

            $values = $this->model->getPrecioTarifa($tarifa_preimpresion_id);

            $data = [
                'values' => $values,
                $csrfTokenName => $newTokenHash
            ];

            return $this->respond($data);

        } else {
            return $this->failUnauthorized('Invalid request', 403);
        }
    }

    public function cargar()
    {
        if($this->request->isAJAX()) {
            $presupuesto_id = $this->request->getGet('presupuesto_id') ?? null;

            $rows = $this->model->getResource($presupuesto_id)->get()->getResultObject();

            $newTokenHash = csrf_hash();
            $csrfTokenName = csrf_token();
            $data = [
                'rows' => $rows,
                $csrfTokenName => $newTokenHash
            ];

            return $this->respond($data);
        } else {
            return $this->failUnauthorized('Invalid request', 403);
        }
    }
}
