<?php
require_once "./Controller/Database.php";
require_once "./Controller/Setting.php";
require_once "./Controller/Recitven.php";

class Unidades extends BD {

    private $PARM;
    private $RECITVEN;
    function __construct() {
        $this->PARM = new Setting();
        $this->RECITVEN = new Recitven();
    }

    public function GET() {
        $this->Permission();
        if($this->id==0){
            if($this->admin){
                $query = $this->consult("SELECT * FROM vorganizaciones");
                $query->execute();    
            } else {
                $query = $this->consult("SELECT * FROM vorganizaciones WHERE userid=?");
                $query->execute([$this->userid]);    
            }
            $result = $query->fetchAll(PDO::FETCH_ASSOC);
            $this->data = array(
                'result' => $result,
                'sectores' => $this->PARM->sectores(),
            );
            $this->httpHeaders = 200;
        } else {
            $query = $this->consult("SELECT * FROM vunidades WHERE organizacion=?");
            $query->execute([$this->id]);
            $this->data = $query->fetchAll(PDO::FETCH_ASSOC);
            $this->httpHeaders = 200;
        }
    }

    public function POST() {
        $this->Permission();
        $input = $this->input;
        $dat = array(
            ':organizacion' => $input->organizacion,
            ':nombre' => strtoupper($input->nombre),
            ':created_by' => $this->userid,
        ); 
        $query = $this->consult('INSERT INTO unidades (organizacion,nombre,created_by,created_at) 
            VALUES (:organizacion,:nombre,:created_by,now()) returning id as id');
        if($query->execute($dat)){
            $unidad = $query->fetchAll(PDO::FETCH_ASSOC)[0]['id'];
            $dat = array(
                ':unidad' => $unidad,
                ':cedula' => $input->responsable_cedula,
                ':nacionalidad' => $input->responsable_nacionalidad,
                ':created_by' => $this->userid,
            ); 
            $query = $this->consult('INSERT INTO responsables_unidad (unidad,cedula,created_by,created_at,nacionalidad) 
            VALUES (:unidad,:cedula,:created_by,now(),:nacionalidad)');
            $query->execute($dat);
            $this->httpHeaders = 200;
            $this->CreateUser($input->responsable_cedula,$this->userid);
        } 
    }

    public function PUT() {
        $this->Permission();
        $input = $this->input;
        $dat = array(
            ':nombre' => strtoupper($input->nombre),
            ':edited_by' => $this->userid,
            ':id' => $this->id,
        ); 
        $query = $this->consult('UPDATE unidades SET nombre=:nombre,
            edited_by=:edited_by, updated_at=now() WHERE id=:id');
        if($query->execute($dat)){
            $dat = array(
                ':unidad' => $this->id,
                ':cedula' => $input->responsable_cedula,
                ':nacionalidad' => $input->responsable_nacionalidad,
                ':edited_by' => $this->userid,
            ); 
            $query = $this->consult('UPDATE responsables_unidad SET cedula=:cedula, nacionalidad=:nacionalidad,
                edited_by=:edited_by, updated_at=now() WHERE unidad=:unidad');
            $query->execute($dat);
            $this->httpHeaders = 200;    
            $this->CreateUser($input->responsable_cedula,$this->userid);    }
    }

    public function DEL() {
        $this->Permission();
        $query = $this->consult('UPDATE unidades SET edited_by=?, deleted_at=now() WHERE id=?');
        if($query->execute([$this->userid,$this->input->id])){ 
            $this->httpHeaders = 200; 
        }
    }

    public function Cedula() {
        $this->Permission();
        $input = $this->input;
        $result = $this->RECITVEN->Cedula($input->nacionalidad,$input->cedula);
        if($result['status']){
            $this->data = $result;
            $this->httpHeaders = 200;
        } else {
            $this->httpHeaders = 204;
        }  
    }

    private function CreateUser($cedula,$userid) {
        $query = $this->consult("SELECT * FROM auth.users WHERE cedula=?");
        $query->execute([$cedula]);
        $result = $query->fetchAll(PDO::FETCH_ASSOC);
        if(!$result){
            $query = $this->consult("INSERT INTO auth.users (name, lastname, email, activo, cedula)
            SELECT nombres, apellidos, email, true, cedula FROM personal 
            WHERE cedula=? returning id as id");
            $query->execute([$cedula]);
            $result = $query->fetchAll(PDO::FETCH_ASSOC);
            $id = $result[0]['id'];
        } else {
            $id = $result[0]['id'];
        }
        $query = $this->consult("SELECT * FROM auth.role_user WHERE activo AND role=3 AND userid=?");
        $query->execute([$id]);
        $result = $query->fetchAll(PDO::FETCH_ASSOC);
        if(!$result) {
            $query = $this->consult('INSERT INTO auth.role_user ("userid","role","activo",created_by,created_at) 
            VALUES (?,?,?,?,now())');
            $query->execute([$id,3,1,$userid]);     
        }
    }

}