<?php

class Api_pago {
    private $db;
    public function __construct()
    {
        $this->db = new Database;
    }

    public function index(){
        $codigo=$_SESSION['name'];
        //$codigo=$_COOKIE["comp"];
        $this->db->query('SELECT * FROM pagos Where codigo=:codigo ORDER BY id desc');
        $this->db->bind(':codigo', $codigo); 
        $result = $this->db->resultSet();
        return $result;
    }

    //register new user
    public function guardar($data){
        $codigo=$_SESSION['name'];
        //$codigo=$_COOKIE["comp"];
        $this->db->query('INSERT INTO pagos (codigo,factura,monto,impuesto,forma_pago,referencia,fecha) VALUES (:codigo,:factura,:monto,:impuesto,:forma_pago,:referencia,:fecha)');
        $this->db->bind(':codigo', $codigo);
        $this->db->bind(':factura', $data->factura);
        $this->db->bind(':monto', $data->monto);
        $this->db->bind(':impuesto', $data->impuesto);
        //$this->db->bind(':total', $data->total);
        $this->db->bind(':forma_pago', $data->forma_pago);
        $this->db->bind(':referencia', $data->referencia);
        $this->db->bind(':fecha', $data->fecha);
        $resx=$this->db->execute();
        if($resx){
            $array["res"] = "ok";
            $array["msg"] = "Registro guardado...";
            return $array;
            //return $res;
        }else{
            $array["res"] = "error";
            $array["msg"] = $_SESSION['error'];
            return $array;
            //print_r($res);
            //return $res;
        }
    }
     public function actualizar($data){
        $this->db->query('UPDATE usuarios SET nombres=:nombres,apellidos=:apellidos,telefono=:telefono,correo=:correo where id=:id');
        $this->db->bind(':id', $data->id);
        $this->db->bind(':nombres', $data->nombres);
        $this->db->bind(':apellidos', $data->apellidos);
        $this->db->bind(':telefono', $data->telefono);
        $this->db->bind(':correo', $data->correo);
        $resx=$this->db->execute();
        if($resx){
            $array["res"] = "ok";
            $array["msg"] = "Registro guardado...";
            return $array;
            //return $res;
        }else{
            $array["res"] = "error";
            $array["msg"] = $_SESSION['error'];
            return $array;
            //print_r($res);
            //return $res;
        }
    }
    public function login($email, $password){

        $this->db->query('SELECT * FROM profesores where usuario = :email');
        $this->db->bind(':email', $email);

        $row = $this->db->single();
        //var_dump($row);
        $hash_password = $row->password;

        if(password_verify($password, $hash_password)){
            return $row;
        }else{
            return false;
        }
    }

    public function getUserById($id){
        $this->db->query('SELECT * FROM user WHERE id = :id');
        $this->db->bind(':id', $id);

        $row = $this->db->single();

        return $row;
    }


    public function eliminar($id){
        $this->db->query('DELETE FROM pagos WHERE id=:id');
        $this->db->bind(':id', $id->id);
        $resx=$this->db->execute();
        if($resx){
            $array["res"] = "ok";
            $array["msg"] = "Registro Eliminadoo...";
            return $array;
            //return $res;
        }else{
            $array["res"] = "error";
            $array["msg"] = $_SESSION['error'];
            return $array;
            //print_r($res);
            //return $res;
        }
    }

}