<?php

class Login extends Controller {

    public function __construct() {
        session_start();
        $this->loginModel = $this->logic("Login_model");
    }

    public function index() {
        $data['title'] = "Login";
        $this->display("login/login", $data);
    }

    public function signup() {
        $data['title'] = "Register";
        $this->display("login/signup", $data);
    }

    public function process_login() {
        $email = $_POST['email'];
        $password = $_POST['password'];

        $user = $this->loginModel->login($email, $password);

        if ($user) {
            $_SESSION['user'] = $user;
            header("Location: " . APP_PATH . "employees");
        } else {
            echo "<script>alert('Email atau Password salah');window.location='" . APP_PATH . "login';</script>";
        }
    }

    public function process_signup() {
        if ($this->loginModel->register($_POST)) {
            echo "<script>alert('Registrasi berhasil');window.location='" . APP_PATH . "login';</script>";
        } else {
            echo "<script>alert('Gagal daftar, email sudah dipakai');window.location='" . APP_PATH . "login/signup';</script>";
        }
    }

    public function logout() {
        session_start();
        session_destroy();
        header("Location: " . APP_PATH . "login");
    }
}
