<?php
class Dashboard extends CI_controller
{


    public function __construct()
    {
        parent::__construct();
        chek_session();
        $this->load->model('Model_dashboard');
    }


    function index()
    {
        $data['box'] = $this->box();
        $data['graph'] = $this->Model_dashboard->graph_stok();
        $data['kategori'] = $this->Model_dashboard->graph_kategori();
        $data['laris'] = $this->Model_dashboard->barang_laris();
        $this->template->load('Template/template', 'Dashboard/lihat_dashboard', $data);
        // var_dump($this->session->userdata());
        // die;
    }

    function form_update(){
        $this->template->load('Template/template', 'form_update');
	}
    public function box()
    {
        $box = [
            [
                'box'         => 'light-blue',
                'total'       => $this->Model_dashboard->total('barang'),
                'title'       => 'Total Barang',
                'link'        => 'Barang',
                'icon'        => 'cubes'
            ],
            [
                'box'         => 'olive',
                'total'       => $this->Model_dashboard->total('kategori'),
                'title'       => 'Kategori Barang',
                'link'        => 'kategori',
                'icon'        => 'list'
            ],
            [
                'box'         => 'yellow-active',
                'total'       =>  $this->Model_dashboard->total_penjualan()->total,
                'title'       => 'Total Penjualan',
                'link'        => 'laporandetail',
                'icon'        => 'shopping-cart'
            ],
            [
                'box'         => 'red',
                'total'       => $this->Model_dashboard->total_stok()->total,
                'title'       => 'Total Stok Barang',
                'link'        => 'stok',
                'icon'        => 'retweet'
            ],
        ];
        $info_box = json_decode(json_encode($box), FALSE);
        return $info_box;
    }

    public function update_password()
    {
        // Pastikan POST request terkirim
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            // Ambil data password dari form
            $password = $this->input->post('password');
            $confirm_password = $this->input->post('confirm_password');

            // Validasi bahwa kedua password sesuai
            if ($password == $confirm_password) {
                // Jika sesuai, update password di database
                $id_operator = $this->session->userdata('id'); // Sesuaikan dengan session operator Anda
                $this->db->where('id_operator', $id_operator);
                $this->db->update('operator', ['password' => md5($password)]);

                // Set pesan alert berhasil
                $alert_message = array(
                    'class' => 'alert-success',
                    'message' => 'Password Sukses diupdate!'
                );

                // Set session untuk menampilkan alert
                $this->session->set_flashdata('alert_message', $alert_message);

                // Redirect ke halaman lain atau tampilkan pesan sukses
                redirect('dashboard/form_update');
            } else {
                // Jika password tidak cocok, set pesan alert gagal
                $alert_message = array(
                    'class' => 'alert-danger',
                    'message' => 'Password and Confirm Tidak Sama!'
                );

                // Set session untuk menampilkan alert
                $this->session->set_flashdata('alert_message', $alert_message);

                // Redirect kembali ke halaman update password
                redirect('dashboard/form_update');
            }
        }
    }
}
