<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Auth extends CI_Controller
{
    public function login()
    {
        check_already_login();  // kode akses login diambil dari helper
        $this->load->view('login');
    }
    public function proses()
    {
        $post = $this->input->post(null, TRUE);
        if (isset($post['login'])) {
            $this->load->model('user_m');
            $query = $this->user_m->login($post);
?>
            <link rel="stylesheet" href="<?= base_url() ?>/assets/dist/sweetalert2/sweetalert2.css">
            <link rel="stylesheet" href="<?= base_url() ?>/assets/dist/sweetalert2/animate.min.css">
            <script src="<?= base_url() ?>/assets/dist/sweetalert2/sweetalert2.all.js"></script>
            <script src="<?= base_url() ?>/assets/dist/sweetalert2/sweetalert2.all.min.js"></script>

            <body></body>
            <?php
            if ($query->num_rows() > 0) {
                $row = $query->row();
                $params = array(
                    'userid' => $row->id_user,
                    'level' => $row->tipe_user,
                );
                $this->session->set_userdata($params);
            ?>
                <script>
                    Swal.fire({
                        icon: 'success',
                        title: 'SELAMAT BEKERJA',
                        text: ' Semoga, Pekerjaan hari ini Lebih baik dari Usaha hari Kemarin',
                        showClass: {
                            popup: 'animate__animated animate__bounce'
                        },
                        hideClass: {
                            popup: 'animate__animated animate__bounceOut'
                        }
                    }).then((result) => {
                        window.location = '<?= site_url('dashboard') ?>';

                    })
                </script>

            <?php
            } else {
            ?>

                <script>
                    Swal.fire({
                        icon: 'error',
                        title: 'Login Gagal',
                        text: ' Username / Password anda salah ?',
                        showClass: {
                            popup: 'animate__animated animate__bounce'
                        },
                        hideClass: {
                            popup: 'animate__animated animate__bounceOut'
                        }
                    }).then((result) => {

                        window.location = '<?= site_url('auth/login') ?>';

                    })
                </script>

        <?php
            }
        }
    }
    public function logout()
    {
        $params = array('userid', 'level');
        $this->session->unset_userdata($params);
        ?>
        <link rel="stylesheet" href="<?= base_url() ?>/assets/dist/sweetalert2/sweetalert2.css">
        <script src="<?= base_url() ?>/assets/dist/sweetalert2/sweetalert2.all.js"></script>
        <script src="<?= base_url() ?>/assets/dist/sweetalert2/sweetalert2.all.min.js"></script>

        <body></body>
        <script>
            Swal.fire({
                icon: 'success',
                title: 'Terimakasih',
                text: ' ,Tetap Semangat Bekerja ya...',
            }).then((result) => {

                window.location = '<?= site_url('auth/login') ?>';

            })
        </script>
<?php
    }
}
