<?php

class Discapacidad extends Conexion {
    /* =============================================
      ACTUALIZAR INFORMACION PERSONAL
      ============================================= */

    public function mdlInsertarActualizarDiscapacidad($datos) {
        $conectar = parent::conectar();
        parent::set_names();
        $sql_verificar = "Select count(*) as total from discapacidad where dis_personadbk = ?";
        $sql_verificar = $conectar->prepare($sql_verificar);
        $sql_verificar->bindValue(1, $datos["dis_personadbk"]);
        $sql_verificar->execute();
        $res = $sql_verificar->fetchAll();
        if (is_array($res) == true and count($res) <> 0) {
            foreach ($res as $tot) {
                $total = $tot["total"];
            }
        }
        if ($total == 0) {
            $sql = "
                    INSERT INTO " . $datos["tabla"] . " 
                    (discapacidadId,
                    porcentajeDiscapacidad,
                    numCarnetConadis,
                    tipoDiscapacidadId,
                    dis_personadbk) 
                    VALUES(?,?,?,?,?);
                    ";
            $sql = $conectar->prepare($sql);
            $sql->bindValue(1, $datos["discapacidadId"]);
            $sql->bindValue(2, $datos["porcentajeDiscapacidad"]);
            $sql->bindValue(3, $datos["numCarnetConadis"]);
            $sql->bindValue(4, $datos["tipoDiscapacidadId"]);
            $sql->bindValue(5, $datos["dis_personadbk"]);
            $sql->execute();
            return $resultado = $sql->fetchAll();
        } else {
            $sql = "UPDATE " . $datos["tabla"] . "  
                SET discapacidadId = ?,
                porcentajeDiscapacidad = ?,
                numCarnetConadis = ?,
                tipoDiscapacidadId = ? 
                WHERE dis_personadbk = ?";
            $sql = $conectar->prepare($sql);
            $sql->bindValue(1, $datos["discapacidadId"]);
            $sql->bindValue(2, $datos["porcentajeDiscapacidad"]);
            $sql->bindValue(3, $datos["numCarnetConadis"]);
            $sql->bindValue(4, $datos["tipoDiscapacidadId"]);
            $sql->bindValue(5, $datos["dis_personadbk"]);
            $sql->execute();
            return $resultado = $sql->fetchAll();
        }
    }

    public function Total_Discapacidad() {
        $conectar = parent::conectar();
        parent::set_names();
        $sql = "SELECT count(*) as discapacidad FROM discapacidad WHERE tipoDiscapacidadId != 89";
        $sql = $conectar->prepare($sql);
        $sql->execute();
        return $resultado = $sql->fetchAll();
    }

    public function mdlConsultarRegistro($personadbk) {
        $conectar = parent::conectar();
        parent::set_names();
        $sql = "SELECT count(*)as total FROM discapacidad WHERE dis_personadbk = ?";
        $sql = $conectar->prepare($sql);
        $sql->bindValue(1, $personadbk);
        $sql->execute();
        return $resultado = $sql->fetch();
    }

    public function GraficoDiscapacidad() {
        $conectar = parent::conectar();
        parent::set_names();
        $sql = "SELECT count(*) AS cantidad, catalogo.descripcion
                FROM discapacidad
                INNER JOIN catalogo ON catalogo.catalogodbk = discapacidad.discapacidadId
                GROUP BY catalogo.descripcion";
        $sql = $conectar->prepare($sql);
        $sql->execute();
        return $resultado = $sql->fetchAll();
    }

    public function GraficoTipoDiscapacidad() {
        $conectar = parent::conectar();
        parent::set_names();
        $sql = "SELECT count(*) AS cantidad, catalogo.descripcion
                FROM discapacidad
                INNER JOIN catalogo ON catalogo.catalogodbk = discapacidad.tipoDiscapacidadId
                GROUP BY catalogo.descripcion";
        $sql = $conectar->prepare($sql);
        $sql->execute();
        return $resultado = $sql->fetchAll();
    }

}

?>