<?php

class Colaborador extends Conectar {
    
    public function get_colaboradores() {
        $conectar = parent::conexion();
        parent::set_names();

        $sql = "SELECT c.*, p.nombre_prof 
                FROM tm_colaborador c 
                LEFT JOIN tm_profesion p ON c.id_prof = p.id_prof 
                WHERE c.estado_colab = 1";

        $stmt = $conectar->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
        }

    public function get_colaborador_x_id($id_colab) {
            $conectar = parent::conexion();
            parent::set_names();

            $sql = "SELECT * FROM tm_colaborador WHERE id_colab = ?";
            $stmt = $conectar->prepare($sql);
            $stmt->execute([$id_colab]);
            return $stmt->fetch(PDO::FETCH_ASSOC);
        }

    public function delete_colaborador($id_colab) {
            $conectar = parent::conexion();
            parent::set_names();

            $sql = "UPDATE tm_colaborador SET estado_colab = 0 WHERE id_colab = ?";
            $stmt = $conectar->prepare($sql);
            $stmt->execute([$id_colab]);
        }

    public function insert_or_update_colaborador($id_colab, $id_prof, $rut, $nombre, $email, $estado, $pass) {
            $conectar = parent::conexion();
            parent::set_names();

            $clave_plana = $pass;

            if (empty($id_colab)) {
                // Insertar nuevo
                $sql = "INSERT INTO tm_colaborador (
                            id_prof, rut_colab, nombre_colab, email_colab, estado_colab, pass_colab
                        ) VALUES (?, ?, ?, ?, ?, ?)";
                $stmt = $conectar->prepare($sql);
                $stmt->execute([
                    $id_prof,
                    $rut,
                    $nombre,
                    $email,
                    $estado,
                    password_hash($pass, PASSWORD_DEFAULT)
                ]);

                return [
                    "email" => $email,
                    "pass" => $clave_plana
                ];
            } else {
                if (!empty($pass)) {
                    // Actualizar con nueva contraseña
                    $sql = "UPDATE tm_colaborador 
                            SET id_prof=?, rut_colab=?, nombre_colab=?, email_colab=?, estado_colab=?, pass_colab=? 
                            WHERE id_colab=?";
                    $stmt = $conectar->prepare($sql);
                    $stmt->execute([
                        $id_prof,
                        $rut,
                        $nombre,
                        $email,
                        $estado,
                        password_hash($pass, PASSWORD_DEFAULT),
                        $id_colab
                    ]);

                    return [
                        "email" => $email,
                        "pass" => $clave_plana
                    ];
                } else {
                    // Actualizar sin modificar contraseña
                    $sql = "UPDATE tm_colaborador 
                            SET id_prof=?, rut_colab=?, nombre_colab=?, email_colab=?, estado_colab=? 
                            WHERE id_colab=?";
                    $stmt = $conectar->prepare($sql);
                    $stmt->execute([
                        $id_prof,
                        $rut,
                        $nombre,
                        $email,
                        $estado,
                        $id_colab
                    ]);

                    return [
                        "email" => $email
                    ];
                }
            }
        }

    public function get_colaborador_select() {
            $conectar = parent::conexion();
            parent::set_names();

            $sql = "SELECT id_colab, nombre_colab, rut_colab, id_prof FROM tm_colaborador WHERE estado_colab = 1 ORDER BY nombre_colab ASC";
            $stmt = $conectar->prepare($sql);
            $stmt->execute();

            return $stmt->fetchAll(PDO::FETCH_ASSOC);
            }

        } 

