<?php

class login extends main_controller{
    function __construct(){
        $this->mod = $this->model("login_model");
    }
    
    function init(){
        //$this->_get_loged();
        $this->setCss( array("init.css") );
        $this->setJs( array( "init.js") );
        
        $datax = array();
        $datax['main'] = $this->_obtener_main();
        $datax['titulo']= "Administracion";
        
        $this->set_layout("login_main.php");
        
        $this->render($datax);
    }
    

    
    function _obtener_main(){
        $data['fecha'] = $this->get_fecha();
        $data['modulo'] =  "";
        
        return $this->view("login", $data);
    }
    
    function x_send_login(){
        
        $cod = $_POST['cod'];
        $pass = $_POST['pass'];
        $ret = $this->mod->send_login($cod, $pass);
        
        if ($ret){
            $_SESSION['USER'] = $ret[0];
            echo json_encode($ret[0]);
        }
        else{
            echo json_encode(array());
        }
        
    }
    
    function logout(){
        unset( $_SESSION['USER'] );
        js_redirect("/clientes/main");
        
    }
    
    function x_recordar_clave(){
                
        $email = $_POST['email'];
                
        $ret = $this->mod->recordar_clave($email);
                
        if ($ret){
            
            require_once('general/plugin/phpmailer/class.phpmailer.php');
            $mail = new PHPMailer();

            $mail->IsSMTP(); // telling the class to use SMTP
            $mail->Host       = "smtp.gmail.com"; // SMTP server
            $mail->CharSet   = 'utf-8';                  // enable SMTP authentication
            //$mail->SMTPDebug  = 2;
            $mail->SMTPAuth   = true;                  // enable SMTP authentication
            $mail->Host       = "smtp.gmail.com"; // sets the SMTP server
            $mail->Port       = 465;                    // set the SMTP port for the GMAIL server
            $mail->SMTPSecure = "ssl";
            $mail->Username   = ""; // SMTP account username
            $mail->Password   = "";        // SMTP account password

            $mail->SetFrom('debonline.sac@gmail.com', 'Recuperar Clave');

            $mail->AddReplyTo("debonline.sac@gmail.com","Recuperar Clave");
            
            $mail->Subject    = "Recordar Password";
            
            //enviar mail
            $key = hash_hmac(ENC_TYPE, $ret['cod'] . "--".  $ret['email'], ENC_KEY_SECRET);
            $link = 'localhost/clientes/' . "recordar-password/get/". $key;
            $mail->AltBody    = "Recordar password aoypf: " . $link; 
            
            $body = ' 
            <html> 
            <head> 
               <title>Cambiar Contraseña</title> 
            </head> 
            <body> 
            <h1>Hola</h1> 
            <p> 
            <b>Solicitaste un cambio de contraseña. Ingresa al siguiente link para cambiar tu contraseña.</b>
            </p> 
            <p> 
            <b><a href="'.$link.'">Cambiar Contraseña</a></b>
            </p>
            <p> 
            <b>Si no visualisas el link anterior, copia y pega este codigo en tu navegador.</b>
            </p>
            <p> 
            '.$link.'
            </p>
            </body> 
            </html> 
            ';
            
            $mail->MsgHTML($body);
            $address = $ret['email'];
            //$address = 'fpretell@hotmail.com';
            $mail->AddAddress($address, "Debo");
            
            if(!$mail->Send()) {
                echo 0;
            } else {
                echo $ret['email'];
            }
                        
        }
        else{
            echo 0;
        }
        
    }


}