<?php
class M_sys_user extends MY_Model
{

    public $id ;
    public $id_profil;
    public $mot_de_passe;
    public $statut;
    public $id_personnel;

	public function get_data( /*$code_str*/)

    { 
        return $this->db->select("usr.*, pers_str.*, ssp.*, s.libelle_structure")
            ->from($this->get_db_table() . ' as usr')
            ->join('personnel as pers_str', 'pers_str.id_personnel = usr.id_personnel')
            ->join('sys_type_profil as ssp', 'ssp.id_type_profil = usr.id_profil')
            ->join('structure as s', 's.id_structure = pers_str.id_structure')
            // ->where('pers_str.id_structure', $_SESSION['id_structure'])
            ->get()
            ->result();

    }

	public function get_db_table()
    {
		return 'sys_user';
	}

    public function get_db_table_pk()
    {
        return 'id';
    }

    public function change_etat($id,$statut)
    {
        return $this->db->query( 'UPDATE sys_user SET statut = "'.$statut.'" WHERE id_personnel ="'.$id.'"');
    }

    public function get_info_mail($id)
    {
        return $this->db->select("pers.*")
                    ->from('sys_user as usr')
                    ->join('personnel as pers', 'pers.id_personnel = usr.id_personnel')
                    ->where('id', $id)
                    ->get()
                    ->result();
    }

    public function get_tel_by_id($id)
    {
        $tel = $this->db->select("pers.tel_personnel")
            ->from('sys_user as usr')
            ->join('personnel as pers', 'pers.id_personnel = usr.id_personnel')
            ->where('id', $id)
            ->get()
            ->row();
        return $tel->tel_personnel;
    }

    public function get_email_by_id($id)
    {
        $email = $this->db->select("pers.email_personnel")
            ->from('sys_user as usr')
            ->join('personnel as pers', 'pers.id_personnel = usr.id_personnel')
            ->where('id', $id)
            ->get()
            ->row();
        return $email->email_personnel;
    }

    public function pw_reset($id,$pw)
    {
        $this->db->set('mot_de_passe', $pw)
                 ->where('id', $id)
                 ->update('sys_user');

        
        
        if ($this->db->trans_status() === FALSE) 
        {
            $status = 'error';
            $result = 'Erreur lors de la réinitialisation.';
        }
        else
        {
            $status = 'success';
            $result = 'Mot de passe réinitialisé avec succées.';
        }
        
        $d = array();
        $d['id'] = $id;
        $d['status'] = $status;
        $d['message'] = $result;

        return $d;
    }
}
