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

class DashboardController extends CI_Controller
{
    public function __construct()
{
    parent::__construct();

    $this->load->model('LoginModel');
    $this->load->library('session');
    $this->load->helper('url');

    $method = $this->router->fetch_method();

    // allow login methods
    $allowed = ['index', 'check_login'];

    if (!in_array($method, $allowed) && !$this->session->userdata('user_id')) {
        redirect('login');
        exit;
    }
}

    public function index()
    {
//         error_reporting(E_ALL);
// ini_set('display_errors', 1);

        // $data['title'] = "Gold Invoice";

    // $this->load->view('layouts/header',$data);
    // $this->load->view('dashboard/index');
    // $this->load->view('dashboard/invoice');
    $this->load->view('dashboard/login');
    // $this->load->view('dashboard/third');
    // $this->load->view('layouts/footer');

    }

    public function dashboard(){
        $data['images'] = $this->LoginModel->get_images();
        
        $this->load->view('dashboard/invoice', $data);
    }

    public function imageList(){
        $this->load->view('dashboard/upload_gold_images');
    }


    public function check_login()
    {


        $username = $this->input->post('username');
        $password = $this->input->post('password');



        $user = $this->LoginModel->get_user($username);



        if($user)
        {


            if(password_verify($password,$user->password))
            {


                $session_data = array(

                    'user_id'  => $user->id,
                    'username' => $user->username,
                    'role'     => $user->role,
                    'login'    => TRUE

                );


                $this->session->set_userdata($session_data);



                redirect('dashboard');


            }
            else
            {

                $this->session->set_flashdata(
                    'error',
                    'Invalid Password'
                );


                redirect('login');

            }



        }
        else
        {


            $this->session->set_flashdata(
                'error',
                'User not found'
            );


            redirect('login');


        }


    }

    public function logout()
    {

        $this->session->sess_destroy();

        redirect('login');

    }

    public function upload_gold_images()
{
    $config['upload_path']   = './uploads/gold/';
    $config['allowed_types'] = 'jpg|jpeg|png';
    $config['max_size']      = 2048;

    $this->load->library('upload', $config);

    $data = [];

    // =========================
    // BIS 916
    // =========================
    if (!empty($_FILES['img_916']['name'])) {

        $_FILES['file']['name']     = $_FILES['img_916']['name'];
        $_FILES['file']['type']     = $_FILES['img_916']['type'];
        $_FILES['file']['tmp_name'] = $_FILES['img_916']['tmp_name'];
        $_FILES['file']['error']    = $_FILES['img_916']['error'];
        $_FILES['file']['size']     = $_FILES['img_916']['size'];

        $this->upload->initialize($config);

        if ($this->upload->do_upload('file')) {
            $data['img_916'] = $this->upload->data('file_name');
        }
    }

    // =========================
    // MACHINE
    // =========================
    if (!empty($_FILES['img_machine']['name'])) {

        $_FILES['file']['name'] = $_FILES['img_machine']['name'];
        $_FILES['file']['tmp_name'] = $_FILES['img_machine']['tmp_name'];

        $this->upload->initialize($config);

        if ($this->upload->do_upload('file')) {
            $data['img_machine'] = $this->upload->data('file_name');
        }
    }

    // =========================
    // MANUAL
    // =========================
    if (!empty($_FILES['img_manual']['name'])) {

        $_FILES['file']['name'] = $_FILES['img_manual']['name'];
        $_FILES['file']['tmp_name'] = $_FILES['img_manual']['tmp_name'];

        $this->upload->initialize($config);

        if ($this->upload->do_upload('file')) {
            $data['img_manual'] = $this->upload->data('file_name');
        }
    }

    // =========================
    // NON KDM
    // =========================
    if (!empty($_FILES['img_non_kdm']['name'])) {

        $_FILES['file']['name'] = $_FILES['img_non_kdm']['name'];
        $_FILES['file']['tmp_name'] = $_FILES['img_non_kdm']['tmp_name'];

        $this->upload->initialize($config);

        if ($this->upload->do_upload('file')) {
            $data['img_non_kdm'] = $this->upload->data('file_name');
        }
    }

    // SAVE TO DB (optional)
    $this->db->insert('gold_images', $data);

    $this->session->set_flashdata('success', 'Images uploaded successfully');

    redirect('dashboard');
}

}