<?php namespace App\Controllers\Presupuestos;


use App\Controllers\GoBaseResourceController;

use App\Models\Collection;


use App\Models\Presupuestos\PresupuestoAcabadosModel;


 
class Presupuestoacabados extends \App\Controllers\GoBaseResourceController
{

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

    protected static $singularObjectName = 'Presupuesto acabado';
    protected static $singularObjectNameCc = 'presupuestoAcabado';
    protected static $pluralObjectName = 'Presupuestos acabado';
    protected static $pluralObjectNameCc = 'presupuestosAcabado';

    protected static $controllerSlug = 'presupuesto-acabados';

    protected static $viewPath = 'themes/backend/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');
        $tirada = $postData->tirada ?? 0;
        $POD = $postData->POD ?? 0;
        $result = [];

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

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

            $tarifa_acabado_id = $reqData['tarifa_acabado_id'] ?? 0;
            $tirada = $reqData['tirada'] ?? 0;
            $POD = $reqData['POD'] ?? 0;
            
            $newTokenHash = csrf_hash();
            $csrfTokenName = csrf_token();

            $values = $this->model->getPrecioTarifa($tarifa_acabado_id, $tirada, $POD);

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

            return $this->respond($data);

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


}
