<?php namespace App\Controllers\Presupuestos;


use App\Models\Collection;

use App\Models\Presupuestos\PresupuestoServiciosExtraModel;


class Presupuestoserviciosextra extends \App\Controllers\BaseResourceController
{

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

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

    protected static $controllerSlug = 'presupuesto-servicios-extra';

    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 getServiciosExtra($datos_tarifas)
    {
        $result = [];
        $model = model('App\Models\Presupuestos\PresupuestoServiciosExtraModel');
        
        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_extra_id = $reqData['tarifa_extra_id'] ?? 0;
            
            $newTokenHash = csrf_hash();
            $csrfTokenName = csrf_token();

            $values = $this->model->getPrecioTarifa($tarifa_extra_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_extra_id = $reqData['tarifa_extra_id'] ?? 0;
            
            $newTokenHash = csrf_hash();
            $csrfTokenName = csrf_token();

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

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

            return $this->respond($data);

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

}
