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

class Dashboard extends CI_Controller {  

    public function __construct()  
    {  
        parent::__construct();
        
        date_default_timezone_set('Asia/Jakarta');

       // Cek apakah user sudah login
        if ($this->session->userdata('status') != "telah_login") {  
            redirect(site_url('login?alert=belum_login'), 'refresh');  
        }

        // Load models dan library yang diperlukan
        $this->load->model('M_data');        
        $this->load->model('Kategori_model'); 
        $this->load->model('Chat_model'); 
        $this->load->library('session');
        $this->load->model('Artikel_model');
    }

    // Login aksi
    public function login_aksi(){
            $username = $this->input->post('username');
            $password = md5($this->input->post('password'));

            $where = array(
                'pengguna_username' => $username,
                'pengguna_password' => $password,
                'pengguna_status' => 1
            );

            $cek = $this->db->get_where('pengguna', $where)->row();

            if ($cek) {
                $data_session = array(
                    'id' => $cek->pengguna_id,
                    'username' => $cek->pengguna_username,
                    'level' => $cek->pengguna_level,
                    'satker_id' => $cek->satker_id,
                    'status' => "telah_login"
                );

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

                redirect(base_url('dashboard'));
            } else {
                redirect(base_url('login?alert=gagal'));
            }
    }

    
    
    
    
    public function index() {
        // Data untuk dashboard (misalnya statistik)
        $data['jumlah_artikel']   = $this->M_data->get_data('artikel')->num_rows();
        $data['jumlah_kategori']  = $this->M_data->get_data('kategori')->num_rows();
        $data['jumlah_pengguna']  = $this->M_data->get_data('pengguna')->num_rows();
        $data['jumlah_halaman']   = $this->M_data->get_data('halaman')->num_rows();
        
        // Ambil data kategori per artikel (misalnya)
        $data['kategori'] = $this->Kategori_model->get_artikel_per_kategori();
        
        // Muat view; pastikan view header/footer sudah mendukung layout yang Anda inginkan
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_index', $data);
        $this->load->view('dashboard/v_footer');

    }

    public function fetch_messages() {
        // Ambil pesan dari database melalui model
        $messages = $this->Chat_model->get_messages();

        // Pastikan mengeluarkan header JSON
        header('Content-Type: application/json');
        echo json_encode($messages);
    }

    public function send_message() {
        // Pastikan response berupa JSON
        header('Content-Type: application/json');

        // Ambil data pesan dan ID user dari session
        $message = $this->input->post('message');
        $user_id = $this->session->userdata('id');

        // Validasi: pastikan pesan dan user_id tidak kosong
        if (!empty($message) && !empty($user_id)) {
            // Gunakan htmlspecialchars agar pesan aman untuk disimpan dan ditampilkan
            if ($this->Chat_model->insert_message($user_id, htmlspecialchars($message, ENT_QUOTES, 'UTF-8'))) {
                echo json_encode(['status' => 'success']);
            } else {
                echo json_encode(['status' => 'error', 'message' => 'Pesan gagal disimpan karena kesalahan database.']);
            }
        } else {
            echo json_encode(['status' => 'error', 'message' => 'Pesan atau ID pengguna kosong']);
        }
    }


    public function ganti_password()  
    {  
        // Load the change password view  
        $this->load->view('dashboard/v_header'); 
        $this->load->view('dashboard/v_ganti_password'); 
        $this->load->view('dashboard/v_footer'); 
    }  

    public function ganti_password_aksi()  
    {  
        // Form validation rules
        $this->form_validation->set_rules('password_lama', 'Password Lama', 'required');
        $this->form_validation->set_rules('password_baru', 'Password Baru', 'required|min_length[8]');
        $this->form_validation->set_rules('konfirmasi_password', 'Konfirmasi Password Baru', 'required|matches[password_baru]');

        // Check validation
        if ($this->form_validation->run() != false) {
            // Capture data from the form
            $password_lama = $this->input->post('password_lama');
            $password_baru = $this->input->post('password_baru');

            // Check the old password
            $where = array('pengguna_id' => $this->session->userdata('id'));

            // Retrieve user data from the database
            $user = $this->M_data->get_user($where); 

            // Verify the old password using MD5
            if ($user && md5($password_lama) === $user->pengguna_password) {
                // Update the password using MD5
                $data = array(
                    'pengguna_password' => md5($password_baru) // Hash the new password with MD5
                );
                $this->M_data->update_data($where, $data, 'pengguna');

                // Set flashdata for success notification
                $this->session->set_flashdata('alert', 'Password telah diubah!');
                redirect('dashboard/ganti_password');
            } else {
                // Set flashdata for error notification
                $this->session->set_flashdata('error', 'Maaf, password lama yang anda masukkan salah!');
                redirect('dashboard/ganti_password');
            }
        } else {
            // If validation fails, return to the change password form
            $this->ganti_password(); 
        }
    }

    public function keluar()  
    {  
        // Destroy user session  
        $this->session->sess_destroy();  
        redirect(site_url('login?alert=logout'), 'refresh');  
    }

    // CRUD KATEGORI ARTIKEL
    public function kategori()
    {
        $data['kategori'] = $this->M_data->get_data('kategori')->result();
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_kategori', $data);
        $this->load->view('dashboard/v_footer');
    }

    // Function to display the add category page
    public function tambah_kategori() 
    {
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_tambah_kategori'); 
        $this->load->view('dashboard/v_footer');
    }

    // Function untuk akses simpan kategori
    public function kategori_aksi()
    {
        // Set validation rules for category input
        $this->form_validation->set_rules('kategori', 'Kategori', 'required|trim|htmlspecialchars');

        // Check if validation is successful
        if ($this->form_validation->run() == TRUE) {
            // Get category data from input
            $kategori = $this->input->post('kategori', TRUE); 

            // Prepare data to be saved
            $data = array(
                'kategori_nama' => $kategori,
                'kategori_slug' => strtolower(url_title($kategori)) 
            );

            // Save category data to the kategori table
            $this->M_data->insert_data($data, 'kategori'); 

            // Set flashdata for success notification
            $this->session->set_flashdata('alert', 'Kategori berhasil ditambahkan!');

            // Redirect to the category page
            redirect('dashboard/kategori');
        } else {
            // If validation fails, return to the add category page
            $this->load->view('dashboard/v_header');
            $this->load->view('dashboard/v_tambah_kategori'); 
            $this->load->view('dashboard/v_footer');
        }
    }

    // Function untuk Edit Kategori
    public function kategori_edit($id) 
    { 
        $where = array( 
            'kategori_id' => $id 
        );

        // Prepare the data to be passed to the view
        $data['kategori'] = $this->M_data->edit_data($where,'kategori')->result();

        $this->load->view('dashboard/v_header'); 
        $this->load->view('dashboard/v_kategori_edit',$data); 
        $this->load->view('dashboard/v_footer'); 
    }

    // Function untuk Update Kategori
    public function kategori_update()
	{
		$this->form_validation->set_rules('kategori','Kategori','required');

		if($this->form_validation->run() != false){

			$id = $this->input->post('id');
			$kategori = $this->input->post('kategori');

			$where = array(
				'kategori_id' => $id
			);

			$data = array(
				'kategori_nama' => $kategori,
				'kategori_slug' => strtolower(url_title($kategori))
			);

			$this->M_data->update_data($where, $data,'kategori');

			redirect(base_url().'dashboard/kategori');
			
		}else{

			$id = $this->input->post('id');
			$where = array(
				'kategori_id' => $id
			);
			$data['kategori'] = $this->M_data->edit_data($where,'kategori')->result();
			$this->load->view('dashboard/v_header');
			$this->load->view('dashboard/v_kategori_edit',$data);
			$this->load->view('dashboard/v_footer');
		}
	}


    // Function untuk Hapus Kategori
    public function kategori_hapus($id)
	{
		$where = array(
			'kategori_id' => $id
		);

		$this->M_data->delete_data($where,'kategori');

		redirect(base_url().'dashboard/kategori');
	}
	// END CRUD KATEGORI ARTIKEL

    // CRUD KATEGORI PANDUAN

    public function kategori_panduan()
    {
        $data['kategori_panduan'] = $this->M_data->get_data('kategori_panduan')->result();
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_kategori_panduan', $data);
        $this->load->view('dashboard/v_footer');
    }

    public function tambah_kategori_panduan() 
    {
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_tambah_kategori_panduan'); 
        $this->load->view('dashboard/v_footer');
    }

    public function kategori_panduan_aksi()
    {
        // Set validation rules for category input
        $this->form_validation->set_rules('kategori_panduan', 'Kategori_panduan', 'required|trim|htmlspecialchars');

        // Check if validation is successful
        if ($this->form_validation->run() == TRUE) {
            // Get category data from input
            $kategori_panduan = $this->input->post('kategori_panduan', TRUE); 

            // Prepare data to be saved
            $data = array(
                'kategori_panduan_nama' => $kategori_panduan,
                'kategori_panduan_slug' => strtolower(url_title($kategori_panduan)) 
            );

            // Save category data to the kategori table
            $this->M_data->insert_data($data, 'kategori_panduan'); 

            // Set flashdata for success notification
            $this->session->set_flashdata('alert', 'Kategori Panduan berhasil ditambahkan!');

            // Redirect to the category page
            redirect('dashboard/kategori_panduan');
        } else {
            // If validation fails, return to the add category page
            $this->load->view('dashboard/v_header');
            $this->load->view('dashboard/v_tambah_kategori_panduan'); 
            $this->load->view('dashboard/v_footer');
        }
    }

    // Function untuk Edit kategori_panduan
    public function edit_kategori_panduan($id) 
    { 
        $where = array( 
            'kategori_panduan_id' => $id 
        );

        // Prepare the data to be passed to the view
        $data['kategori_panduan'] = $this->M_data->edit_data($where,'kategori_panduan')->result();

        $this->load->view('dashboard/v_header'); 
        $this->load->view('dashboard/v_edit_kategori_panduan',$data); 
        $this->load->view('dashboard/v_footer'); 
    }

    // Function untuk Update kategori_panduan
    public function update_kategori_panduan()
	{
		$this->form_validation->set_rules('kategori_panduan','kategori_panduan','required');

		if($this->form_validation->run() != false){

			$id = $this->input->post('id');
			$kategori_panduan = $this->input->post('kategori_panduan');

			$where = array(
				'kategori_panduan_id' => $id
			);

			$data = array(
				'kategori_panduan_nama' => $kategori_panduan,
				'kategori_panduan_slug' => strtolower(url_title($kategori_panduan))
			);

			$this->M_data->update_data($where, $data,'kategori_panduan');

			redirect(base_url().'dashboard/kategori_panduan');
			
		}else{

			$id = $this->input->post('id');
			$where = array(
				'kategori_panduan_id' => $id
			);
			$data['kategori_panduan'] = $this->M_data->edit_data($where,'kategori_panduan')->result();
			$this->load->view('dashboard/v_header');
			$this->load->view('dashboard/v_edit_kategori_panduan',$data);
			$this->load->view('dashboard/v_footer');
		}
	}

        // Function untuk Hapus Kategori
        public function hapus_kategori_panduan($id)
        {
            $where = array(
                'kategori_panduan_id' => $id
            );
    
            $this->M_data->delete_data($where,'kategori_panduan');
    
            redirect(base_url().'dashboard/kategori_panduan');
        }
    
    // END CRUD KATEGORI PANDUAN

    // CRUD KATEGORI LAYANAN

    public function kategori_layanan()
    {
        $data['kategori_layanan'] = $this->M_data->get_data('kategori_layanan')->result();
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_kategori_layanan', $data);
        $this->load->view('dashboard/v_footer');
    }

    public function tambah_kategori_layanan() 
    {
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_tambah_kategori_layanan'); 
        $this->load->view('dashboard/v_footer');
    }

    public function kategori_layanan_aksi()
    {
        // Set validation rules for category input
        $this->form_validation->set_rules('kategori_layanan', 'Kategori_layanan', 'required|trim|htmlspecialchars');

        // Check if validation is successful
        if ($this->form_validation->run() == TRUE) {
            // Get category data from input
            $kategori_layanan = $this->input->post('kategori_layanan', TRUE); 

            // Prepare data to be saved
            $data = array(
                'kategori_layanan_nama' => $kategori_layanan,
                'kategori_layanan_slug' => strtolower(url_title($kategori_layanan)) 
            );

            // Save category data to the kategori table
            $this->M_data->insert_data($data, 'kategori_layanan'); 

            // Set flashdata for success notification
            $this->session->set_flashdata('alert', 'Kategori layanan berhasil ditambahkan!');

            // Redirect to the category page
            redirect('dashboard/kategori_layanan');
        } else {
            // If validation fails, return to the add category page
            $this->load->view('dashboard/v_header');
            $this->load->view('dashboard/v_tambah_kategori_layanan'); 
            $this->load->view('dashboard/v_footer');
        }
    }

    // Function untuk Edit kategori_layanan
    public function edit_kategori_layanan($id) 
    { 
    $where = array( 
        'kategori_layanan_id' => $id 
    );

    // Prepare the data to be passed to the view
    $data['kategori_layanan'] = $this->M_data->edit_data($where,'kategori_layanan')->result();

    $this->load->view('dashboard/v_header'); 
    $this->load->view('dashboard/v_edit_kategori_layanan',$data); 
    $this->load->view('dashboard/v_footer'); 
    }

    // Function untuk Update kategori_layanan
    public function update_kategori_layanan()
    {
    $this->form_validation->set_rules('kategori_layanan','kategori_layanan','required');

    if($this->form_validation->run() != false){

        $id = $this->input->post('id');
        $kategori_layanan = $this->input->post('kategori_layanan');

        $where = array(
            'kategori_layanan_id' => $id
        );

        $data = array(
            'kategori_layanan_nama' => $kategori_layanan,
            'kategori_layanan_slug' => strtolower(url_title($kategori_layanan))
        );

        $this->M_data->update_data($where, $data,'kategori_layanan');

        redirect(base_url().'dashboard/kategori_layanan');
        
    }else{

        $id = $this->input->post('id');
        $where = array(
            'kategori_layanan_id' => $id
        );
        $data['kategori_layanan'] = $this->M_data->edit_data($where,'kategori_layanan')->result();
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_edit_kategori_layanan',$data);
        $this->load->view('dashboard/v_footer');
    }
    }

    // Function untuk Hapus Kategori
    public function hapus_kategori_layanan($id)
    {
        $where = array(
            'kategori_layanan_id' => $id
        );

        $this->M_data->delete_data($where,'kategori_layanan');

        redirect(base_url().'dashboard/kategori_layanan');
    }


    // END CRUD KATEGORI LAYANAN

    // CRUD KATEGORI RFC2350

    public function kategori_rfc2350()
    {
        $data['kategori_rfc2350'] = $this->M_data->get_data('kategori_rfc2350')->result();
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_kategori_rfc2350', $data);
        $this->load->view('dashboard/v_footer');
    }

    public function tambah_kategori_rfc2350() 
    {
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_tambah_kategori_rfc2350'); 
        $this->load->view('dashboard/v_footer');
    }

    public function kategori_rfc2350_aksi()
    {
        // Set validation rules for category input
        $this->form_validation->set_rules('kategori_rfc2350', 'Kategori_rfc2350', 'required|trim|htmlspecialchars');

        // Check if validation is successful
        if ($this->form_validation->run() == TRUE) {
            // Get category data from input
            $kategori_rfc2350 = $this->input->post('kategori_rfc2350', TRUE); 

            // Prepare data to be saved
            $data = array(
                'kategori_rfc2350_nama' => $kategori_rfc2350,
                'kategori_rfc2350_slug' => strtolower(url_title($kategori_rfc2350)) 
            );

            // Save category data to the kategori table
            $this->M_data->insert_data($data, 'kategori_rfc2350'); 

            // Set flashdata for success notification
            $this->session->set_flashdata('alert', 'Kategori rfc2350 berhasil ditambahkan!');

            // Redirect to the category page
            redirect('dashboard/kategori_rfc2350');
        } else {
            // If validation fails, return to the add category page
            $this->load->view('dashboard/v_header');
            $this->load->view('dashboard/v_tambah_kategori_rfc2350'); 
            $this->load->view('dashboard/v_footer');
        }
    }

    public function edit_kategori_rfc2350($id) 
    { 
        $where = array( 
            'kategori_rfc2350_id' => $id 
        );

        // Prepare the data to be passed to the view
        $data['kategori_rfc2350'] = $this->M_data->edit_data($where,'kategori_rfc2350')->result();

        $this->load->view('dashboard/v_header'); 
        $this->load->view('dashboard/v_edit_kategori_rfc2350',$data); 
        $this->load->view('dashboard/v_footer'); 
    }

    // Function untuk Update kategori_rfc2350
    public function update_kategori_rfc2350()
    {
        $this->form_validation->set_rules('kategori_rfc2350','kategori_rfc2350','required');

        if($this->form_validation->run() != false){

            $id = $this->input->post('id');
            $kategori_rfc2350 = $this->input->post('kategori_rfc2350');

            $where = array(
                'kategori_rfc2350_id' => $id
            );

            $data = array(
                'kategori_rfc2350_nama' => $kategori_rfc2350,
                'kategori_rfc2350_slug' => strtolower(url_title($kategori_rfc2350))
            );

            $this->M_data->update_data($where, $data,'kategori_rfc2350');

            redirect(base_url().'dashboard/kategori_rfc2350');
            
        }else{

            $id = $this->input->post('id');
            $where = array(
                'kategori_rfc2350_id' => $id
            );
            $data['kategori_rfc2350'] = $this->M_data->edit_data($where,'kategori_rfc2350')->result();
            $this->load->view('dashboard/v_header');
            $this->load->view('dashboard/v_edit_kategori_rfc2350',$data);
            $this->load->view('dashboard/v_footer');
        }
    }

    public function hapus_kategori_rfc2350($id)
    {
        $where = array(
            'kategori_rfc2350_id' => $id
        );

        $this->M_data->delete_data($where,'kategori_rfc2350 ');

        redirect(base_url().'dashboard/kategori_rfc2350');
    }


    // END CRUD KATEGORI RFC2350

    // CRUD KATEGORI PROFIL

    public function kategori_profil()
    {
        $data['kategori_profil'] = $this->M_data->get_data('kategori_profil')->result();
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_kategori_profil', $data);
        $this->load->view('dashboard/v_footer');
    }

    public function tambah_kategori_profil() 
    {
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_tambah_kategori_profil'); 
        $this->load->view('dashboard/v_footer');
    }

    public function kategori_profil_aksi()
    {
        // Set validation rules for category input
        $this->form_validation->set_rules('kategori_profil', 'Kategori_profil', 'required|trim|htmlspecialchars');

        // Check if validation is successful
        if ($this->form_validation->run() == TRUE) {
            // Get category data from input
            $kategori_profil = $this->input->post('kategori_profil', TRUE); 

            // Prepare data to be saved
            $data = array(
                'kategori_profil_nama' => $kategori_profil,
                'kategori_profil_slug' => strtolower(url_title($kategori_profil)) 
            );

            // Save category data to the kategori table
            $this->M_data->insert_data($data, 'kategori_profil'); 

            // Set flashdata for success notification
            $this->session->set_flashdata('alert', 'Kategori profil berhasil ditambahkan!');

            // Redirect to the category page
            redirect('dashboard/kategori_profil');
        } else {
            // If validation fails, return to the add category page
            $this->load->view('dashboard/v_header');
            $this->load->view('dashboard/v_tambah_kategori_profil'); 
            $this->load->view('dashboard/v_footer');
        }
    }

    // Function untuk Edit kategori_profil
    public function edit_kategori_profil($id) 
    { 
        $where = array( 
            'kategori_profil_id' => $id 
        );

        // Prepare the data to be passed to the view
        $data['kategori_profil'] = $this->M_data->edit_data($where,'kategori_profil')->result();

        $this->load->view('dashboard/v_header'); 
        $this->load->view('dashboard/v_edit_kategori_profil',$data); 
        $this->load->view('dashboard/v_footer'); 
    }

    // Function untuk Update kategori_profil
    public function update_kategori_profil()
    {
        $this->form_validation->set_rules('kategori_profil','kategori_profil','required');

        if($this->form_validation->run() != false){

            $id = $this->input->post('id');
            $kategori_profil = $this->input->post('kategori_profil');

            $where = array(
                'kategori_profil_id' => $id
            );

            $data = array(
                'kategori_profil_nama' => $kategori_profil,
                'kategori_profil_slug' => strtolower(url_title($kategori_profil))
            );

            $this->M_data->update_data($where, $data,'kategori_profil');

            redirect(base_url().'dashboard/kategori_profil');
            
        }else{

            $id = $this->input->post('id');
            $where = array(
                'kategori_profil_id' => $id
            );
            $data['kategori_profil'] = $this->M_data->edit_data($where,'kategori_profil')->result();
            $this->load->view('dashboard/v_header');
            $this->load->view('dashboard/v_edit_kategori_profil',$data);
            $this->load->view('dashboard/v_footer');
        }
    }

        // Function untuk Hapus Kategori
        public function hapus_kategori_profil($id)
        {
            $where = array(
                'kategori_profil_id' => $id
            );

            $this->M_data->delete_data($where,'kategori_profil');

            redirect(base_url().'dashboard/kategori_profil');
        }

        //END CRUD KATEGORI PROFIL

    // CRUD ARTIKEL
    public function artikel() {
        $level = $this->session->userdata('level');
        $satker_id = $this->session->userdata('satker_id');
    
        // Ambil artikel berdasarkan level dan satker_id
        $data['artikel'] = $this->Artikel_model->get_artikel_with_satker();
    
        // Load views
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_artikel', $data);
        $this->load->view('dashboard/v_footer');
    }
    



    public function tambah_artikel() {
        $data['kategori'] = $this->M_data->get_data('kategori')->result();

        if (empty($data['kategori'])) {
            $this->session->set_flashdata('error', 'Tidak ada kategori ditemukan.');
            redirect('dashboard/kategori'); // Ganti dengan URL yang sesuai
        }

        // Load tampilan tambah artikel
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_tambah_artikel', $data);
        $this->load->view('dashboard/v_footer');
    }

    public function artikel_aksi() {
        // Validasi jika ada error di upload gambar
        $config['upload_path'] = './gambar/artikel/';
        $config['allowed_types'] = 'jpg|jpeg|png|gif';
        $this->load->library('upload', $config);
    
        if ($this->upload->do_upload('sampul')) {
            $gambar = $this->upload->data();
            
            $data = array(
                'artikel_tanggal' => date('Y-m-d H:i:s'),
                'artikel_judul' => $this->input->post('judul'),
                'artikel_slug' => strtolower(url_title($this->input->post('judul'))),
                'artikel_konten' => $this->input->post('konten'),
                'artikel_sampul' => $gambar['file_name'],
                'artikel_kategori' => $this->input->post('kategori'),
                'artikel_author' => $this->session->userdata('id'),
                'satker_id' => $this->input->post('satker_id')  // Ambil dari form yang sudah diset dengan session
            );
    
            // Masukkan data artikel ke dalam database
            $this->M_data->insert_data($data, 'artikel');
            redirect(base_url() . 'dashboard/artikel');
        } else {
            $this->session->set_flashdata('error', $this->upload->display_errors());
            redirect(base_url() . 'dashboard/artikel_tambah');
        }
    }
    



    // Fungsi untuk memuat form dengan kategori dan pesan kesalahan
    private function load_form_with_errors()
    {
        $data['kategori'] = $this->M_data->get_data('kategori')->result();
        $data['errors'] = validation_errors() ?: $this->session->flashdata('gambar_error');

        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_tambah_artikel', $data);
        $this->load->view('dashboard/v_footer');
    }
    
    public function artikel_hapus($id) {
        $level = $this->session->userdata('level');
        $satker_id = $this->session->userdata('satker_id');

        $artikel = $this->db->get_where('artikel', ['artikel_id' => $id])->row();

        if (!$artikel) {
            redirect(base_url('dashboard/artikel'));
        }

        if ($level != 'admin' && $artikel->satker_id != $satker_id) {
            show_error('Anda tidak berhak menghapus artikel ini.');
        }

        // Hapus gambar
        @unlink('./gambar/artikel/' . $artikel->artikel_sampul);

        // Hapus data artikel
        $this->M_data->delete_data(['artikel_id' => $id], 'artikel');
        redirect(base_url('dashboard/artikel'));
    }


    public function edit_artikel($id) {
        $level = $this->session->userdata('level');
        $satker_id = $this->session->userdata('satker_id');
    
        // Ambil data artikel berdasarkan ID
        $data['artikel'] = $this->M_data->edit_data(['artikel_id' => $id], 'artikel')->row();
    
        // Jika artikel tidak ditemukan
        if (!$data['artikel']) {
            show_error('Artikel tidak ditemukan.');
        }
    
        // Cek hak akses berdasarkan level dan satker_id
        if ($level != 'admin' && $data['artikel']->satker_id != $satker_id) {
            show_error('Anda tidak berhak mengedit artikel ini.');
        }
    
        // Ambil data kategori
        $data['kategori'] = $this->M_data->get_data('kategori')->result();
    
        // Load tampilan edit artikel
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_edit_artikel', $data);
        $this->load->view('dashboard/v_footer');
    }
    
    public function artikel_update() {
        $this->form_validation->set_rules('judul', 'Judul', 'required');
        $this->form_validation->set_rules('konten', 'Konten', 'required');
        $this->form_validation->set_rules('kategori', 'Kategori', 'required');

        $id = $this->input->post('id');

        if ($this->form_validation->run() == TRUE) {
            $judul = $this->input->post('judul');
            $slug = strtolower(url_title($judul));
            $konten = $this->input->post('konten');
            $kategori = $this->input->post('kategori');
            $status = $this->input->post('status');

            $data = array(
                'artikel_judul' => $judul,
                'artikel_slug' => $slug,
                'artikel_konten' => $konten,
                'artikel_kategori' => $kategori,
                'artikel_status' => $status,
            );

            $where = array('artikel_id' => $id);
            $this->M_data->update_data($where, $data, 'artikel');

            // Cek jika ada gambar yang di-upload
            if (!empty($_FILES['sampul']['name'])) {
                $config['upload_path'] = './gambar/artikel/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = 4096; // Max size 4MB
                $this->load->library('upload', $config);

                if ($this->upload->do_upload('sampul')) {
                    $gambar = $this->upload->data();
                    $data_gambar = array('artikel_sampul' => $gambar['file_name']);
                    $this->M_data->update_data($where, $data_gambar, 'artikel');
                } else {
                    $data['gambar_error'] = $this->upload->display_errors();
                }
            }

            redirect(base_url('dashboard/artikel'));
        } else {
            // Tampilkan form kembali jika validasi gagal
            $where = array('artikel_id' => $id);
            $data['artikel'] = $this->M_data->edit_data($where, 'artikel')->result();
            $data['kategori'] = $this->M_data->get_data('kategori')->result();
            $data['gambar_error'] = isset($data['gambar_error']) ? $data['gambar_error'] : '';

            $this->load->view('dashboard/v_header');
            $this->load->view('dashboard/v_edit_artikel', $data);
            $this->load->view('dashboard/v_footer');
        }
    }
    // END CRUD ARTIKEL



    // CRUD PROFILE
    public function profile() {
        $user_id = $this->session->userdata('id');
    
        // Ambil data pengguna dan join nama satker
        $this->db->select('pengguna.*, satker.nama_satker');
        $this->db->from('pengguna');
        $this->db->join('satker', 'satker.satker_id = pengguna.satker_id', 'left');
        $this->db->where('pengguna.pengguna_id', $user_id);
        $user = $this->db->get()->row();
    
        $data['user'] = $user;
    
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_profile', $data);
        $this->load->view('dashboard/v_footer');
    }
    
    public function profile_update() {
        // Validasi form
        $this->form_validation->set_rules('nama', 'Nama Lengkap', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required');
    
        if ($this->form_validation->run() == FALSE) {
            // Jika validasi gagal
            $this->load->view('v_profile');
        } else {
            // Ambil data dari form
            $nama = $this->input->post('nama');
            $email = $this->input->post('email');
            $foto_lama = $this->input->post('foto_lama');
    
            // Proses upload foto
            if (!empty($_FILES['foto']['name'])) {
                $config['upload_path'] = './gambar/pengguna/';
                $config['allowed_types'] = 'jpg|jpeg|png|gif';
                $config['max_size'] = 2048;  // Maksimal ukuran file 2MB
                $config['file_name'] = uniqid(); // Untuk memberikan nama unik pada file
    
                $this->load->library('upload', $config);
    
                if ($this->upload->do_upload('foto')) {
                    // Jika upload berhasil, ambil nama file
                    $upload_data = $this->upload->data();
                    $foto_baru = $upload_data['file_name'];
    
                    // Hapus foto lama jika ada
                    if (!empty($foto_lama)) {
                        unlink('./gambar/pengguna/' . $foto_lama); // Hapus foto lama
                    }
                } else {
                    // Jika gagal upload
                    $foto_baru = $foto_lama; // Tetap pakai foto lama jika gagal upload
                }
            } else {
                // Jika tidak ada file yang di-upload, tetap pakai foto lama
                $foto_baru = $foto_lama;
            }
    
            // Update data pengguna
            $data = [
                'pengguna_nama' => $nama,
                'pengguna_email' => $email,
                'pengguna_sampul' => $foto_baru,
            ];
    
            // Update data ke database
            $this->db->where('pengguna_id', $this->session->userdata('id'));
            $this->db->update('pengguna', $data);
    
            // Set pesan sukses dan redirect
            $this->session->set_flashdata('alert', 'Profil berhasil diperbarui');
            redirect('dashboard/profile');
        }
    }
    
    
    

    // CRUD PENGGUNA
	public function pengguna()
    {
    // Ambil data pengguna beserta nama satker
    $data['pengguna'] = $this->M_data->get_all_pengguna_with_satker(); // Memastikan kita menggunakan fungsi yang benar

    // Muat view dengan data yang sudah tersedia
    $this->load->view('dashboard/v_header');
    $this->load->view('dashboard/v_pengguna', $data);  // Data sudah ada di sini
    $this->load->view('dashboard/v_footer');
    }



    // Tambah Pengguna
    public function pengguna_tambah()
    {
    // Mengambil data satker_id dan nama_satker dari tabel satker
    $this->db->select('satker_id, nama_satker');
    $this->db->from('satker');
    $this->db->where('nama_satker IS NOT NULL');
    $data['satkers'] = $this->db->get()->result();


    $this->load->view('dashboard/v_header');
    $this->load->view('dashboard/v_pengguna_tambah', $data); // kirim $satkers ke view
    $this->load->view('dashboard/v_footer');
    }


    public function pengguna_aksi()
    {
    $this->load->library('form_validation');

    // Aturan validasi
    $this->form_validation->set_rules('nama', 'Nama Pengguna', 'trim|required');
    $this->form_validation->set_rules('email', 'Email Pengguna', 'trim|required|valid_email|is_unique[pengguna.pengguna_email]');
    $this->form_validation->set_rules('username', 'Username Pengguna', 'trim|required|is_unique[pengguna.pengguna_username]');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]');
    $this->form_validation->set_rules('level', 'Level', 'required');
    $this->form_validation->set_rules('status', 'Status', 'required');
    
    // Validasi tambahan jika level = satker
    if ($this->input->post('level') == 'satker') {
        $this->form_validation->set_rules('satker_id', 'Nama Satker', 'required');
    }
    
    if ($this->form_validation->run() == FALSE) {
        // Ambil data satker untuk dropdown jika validasi gagal
        $data['satkers'] = $this->M_data->get_satkers();  // ambil data dari model
    
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_pengguna_tambah', $data);
        $this->load->view('dashboard/v_footer');
    } else {
        // Simpan data pengguna
        $data = array(
            'pengguna_nama' => $this->input->post('nama'),
            'pengguna_email' => $this->input->post('email'),
            'pengguna_username' => $this->input->post('username'),
            'pengguna_password' => md5($this->input->post('password')),
            'pengguna_level' => $this->input->post('level'),
            'pengguna_status' => $this->input->post('status'),
            'satker_id' => ($this->input->post('level') == 'satker') ? $this->input->post('satker_id') : null
        );

        // Upload gambar jika ada
        if (!empty($_FILES['sampul']['name'])) {
            $config['upload_path'] = './gambar/pengguna/';
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = 2048;
            $this->load->library('upload', $config);

            if ($this->upload->do_upload('sampul')) {
                $upload_data = $this->upload->data();
                $data['pengguna_sampul'] = $upload_data['file_name'];
            } else {
                // Jika gagal upload, bisa ditambahkan flash error jika ingin
                $data['gambar_error'] = $this->upload->display_errors();
            }
        }

        // Simpan ke database
        $this->M_data->insert_data($data, 'pengguna');

        $this->session->set_flashdata('success', 'Pengguna berhasil ditambahkan.');
        redirect('dashboard/pengguna');
    }
    }



    public function pengguna_edit($id)
	{
		$where = array(
			'pengguna_id' => $id
		);
		$data['pengguna'] = $this->M_data->edit_data($where,'pengguna')->result();
		$this->load->view('dashboard/v_header');
		$this->load->view('dashboard/v_pengguna_edit',$data);
		$this->load->view('dashboard/v_footer');
	}

    public function pengguna_update()
    {
        // Aturan validasi
        $this->form_validation->set_rules('nama', 'Nama Pengguna', 'required');
        $this->form_validation->set_rules('email', 'Email Pengguna', 'required|valid_email');
        $this->form_validation->set_rules('username', 'Username Pengguna', 'required');
        $this->form_validation->set_rules('level', 'Level Pengguna', 'required');
        $this->form_validation->set_rules('status', 'Status Pengguna', 'required');

        if ($this->form_validation->run() == TRUE) {
            // Ambil data dari input
            $id = $this->input->post('id');
            $nama = $this->input->post('nama');
            $email = $this->input->post('email');
            $username = $this->input->post('username');
            $level = $this->input->post('level');
            $status = $this->input->post('status');

            // Siapkan data untuk diupdate
            $data = array(
                'pengguna_nama' => $nama,
                'pengguna_email' => $email,
                'pengguna_username' => $username,
                'pengguna_level' => $level,
                'pengguna_status' => $status
            );

            // Cek apakah password diisi
            $password = $this->input->post('password');
            if (!empty($password)) {
                $data['pengguna_password'] = md5($password); // Hash password jika diisi
            }

            // Update data pengguna
            $where = array('pengguna_id' => $id);
            $this->M_data->update_data($where, $data, 'pengguna');

            // Set flashdata untuk pesan sukses
            $this->session->set_flashdata('success', 'Pengguna berhasil diperbarui.');

            // Redirect setelah berhasil
            redirect('dashboard/pengguna');
        } else {
            // Jika validasi gagal, ambil data pengguna untuk ditampilkan kembali
            $id = $this->input->post('id');
            $where = array('pengguna_id' => $id);
            $data['pengguna'] = $this->M_data->edit_data($where, 'pengguna')->result();

            // Load view
            $this->load->view('dashboard/v_header');
            $this->load->view('dashboard/v_pengguna_edit', $data);
            $this->load->view('dashboard/v_footer');
        }
    }

    public function pengguna_hapus($id)
    {
        // Pastikan ID pengguna valid
        if (!is_numeric($id)) {
            redirect(site_url('dashboard/pengguna'));
        }

        // Ambil data pengguna yang akan dihapus
        $where = array('pengguna_id' => $id);
        $data['pengguna_hapus'] = $this->M_data->edit_data($where, 'pengguna')->row();

        // Jika pengguna tidak ditemukan, kembalikan ke daftar pengguna
        if (!$data['pengguna_hapus']) {
            redirect(site_url('dashboard/pengguna'));
        }

        // Ambil daftar pengguna lain yang tidak termasuk pengguna yang akan dihapus
        $data['pengguna_lain'] = $this->db->where('pengguna_id !=', $id)->get('pengguna')->result();

        // Load tampilan konfirmasi hapus pengguna
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_pengguna_hapus', $data);
        $this->load->view('dashboard/v_footer');
    }

    public function pengguna_hapus_aksi()
    {
        // Ambil data dari form
        $pengguna_hapus = $this->input->post('pengguna_hapus');
        $pengguna_tujuan = $this->input->post('pengguna_tujuan');

        // Validasi input untuk mencegah ID kosong atau tidak valid
        if (!is_numeric($pengguna_hapus) || !is_numeric($pengguna_tujuan)) {
            redirect(site_url('dashboard/pengguna'));
        }

        // Ambil data pengguna yang akan dihapus untuk mendapatkan nama file gambar
        $where = array('pengguna_id' => $pengguna_hapus);
        $pengguna = $this->M_data->edit_data($where, 'pengguna')->row();

        // Pastikan pengguna ditemukan
        if ($pengguna) {
            // Hapus pengguna dari database
            $this->M_data->delete_data($where, 'pengguna');

            // Hapus gambar pengguna dari direktori
            $gambar_path = './gambar/pengguna/' . $pengguna->pengguna_sampul; // Sesuaikan dengan nama kolom gambar di database
            if (file_exists($gambar_path)) {
                if (!unlink($gambar_path)) {
                    // Log error atau set flashdata untuk kesalahan
                    log_message('error', 'Gagal menghapus gambar: ' . $gambar_path);
                }
            }

            // Pindahkan semua artikel pengguna yang dihapus ke pengguna yang dipilih
            $this->db->where('artikel_author', $pengguna_hapus);
            $this->db->update('artikel', array('artikel_author' => $pengguna_tujuan));

            // Set flashdata untuk pesan sukses
            $this->session->set_flashdata('success', 'Pengguna berhasil dihapus.');
        }

        // Redirect kembali ke daftar pengguna
        redirect(site_url('dashboard/pengguna'));
    }
    // END CRUD PENGGUNA
    
    // CRUD PANDUAN
    public function panduan() {
        $data['panduan'] = $this->db->query("SELECT * FROM panduan, kategori_panduan, pengguna WHERE panduan_kategori = kategori_panduan_id AND panduan_author = pengguna_id ORDER BY panduan_id DESC")->result();
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_panduan', $data);
        $this->load->view('dashboard/v_footer');
    }
    
    public function tambah_panduan() {
        $data['kategori_panduan'] = $this->M_data->get_data('kategori_panduan')->result();
    
        if (empty($data['kategori_panduan'])) {
            $this->session->set_flashdata('error', 'Tidak ada kategori ditemukan.');
            redirect('dashboard/kategori_panduan');
        }
    
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_tambah_panduan', $data);
        $this->load->view('dashboard/v_footer');
    }

    public function panduan_aksi() {
        $this->form_validation->set_rules('nama', 'Nama', 'trim|required|is_unique[panduan.panduan_nama]');
        $this->form_validation->set_rules('kategori', 'Kategori', 'required');
    
        // Memastikan Dokumen diisi
        if (empty($_FILES['dokumen_baru']['name'])) {
            $this->form_validation->set_rules('dokumen_baru', 'Dokumen Baru Panduan', 'required');
        }
    
        // Jika validasi berhasil
        if ($this->form_validation->run() == TRUE) {
            $config['upload_path']   = './dokumen/panduan/';
            $config['allowed_types'] = 'pdf|doc|pptx|docx|ppt|xls|xlsx|txt|rtf';
            $config['max_size'] = 51200; // Maksimal ukuran file dalam KB
            $this->load->library('upload', $config);
    
            // Proses upload dokumen
            if ($this->upload->do_upload('dokumen_baru')) {
                // Mengambil data tentang dokumen
                $dokumen_ok = $this->upload->data();
    
                // Mengambil data dari form
                $data = array(
                    'panduan_tanggal' => date('Y-m-d H:i:s'),
                    'panduan_nama' => $this->input->post('nama'),                  
                    'panduan_doc' => $dokumen_ok['file_name'],
                    'panduan_author' => $this->session->userdata('id'),
                    'panduan_kategori' => $this->input->post('kategori'),
                    'panduan_status' => $this->input->post('masuk_bro'), // Menangani masuk_bro dari tombol submit
                );
    
                // Menyimpan data panduan ke database
                if ($this->M_data->insert_data($data, 'panduan')) {
                    // Menambahkan flash data untuk pesan sukses
                    $this->session->set_flashdata('success', 'Panduan berhasil ditambahkan.');
                    // Redirect ke halaman panduan setelah sukses
                    redirect('dashboard/panduan');
                } else {
                    // Menyimpan pesan kesalahan jika insert gagal
                    $this->session->set_flashdata('error', 'Gagal menyimpan data panduan.');
                    $this->load_form_with_errors();
                }
            } else {
                // Menyimpan pesan kesalahan upload dokumen
                $this->session->set_flashdata('dokumen_ok_error', $this->upload->display_errors());
                $this->load_form_with_errors();
            }
        } else {
            // Jika validasi gagal, tampilkan form dengan pesan kesalahan
            $this->load_form_with_errors();
        }
    } 

    public function edit_panduan($id)
	{
        $where = array('panduan_id' => $id);
		$data['panduan'] = $this->M_data->edit_data($where,'panduan')->result();
		$data['kategori_panduan'] = $this->M_data->get_data('kategori_panduan')->result();

		$this->load->view('dashboard/v_header');
		$this->load->view('dashboard/v_edit_panduan',$data);
		$this->load->view('dashboard/v_footer');
	}

    public function panduan_update() {
        $id = $this->input->post('id');
    
        // Pastikan ID valid
        if (empty($id)) {
            show_error('ID tidak ditemukan', 404);
        }
    
        // Validasi form
        $this->form_validation->set_rules('nama', 'Nama', 'trim|required');
        $this->form_validation->set_rules('kategori', 'Kategori', 'required');
    
        if ($this->form_validation->run() == TRUE) {
            $where = array('panduan_id' => $id);

            $judul = $this->input->post('judul');
			$kategori = $this->input->post('kategori');
			$status = $this->input->post('status');


            // Data update
            $data = array(
                'panduan_nama' => $this->input->post('nama'),
                'panduan_kategori' => $this->input->post('kategori'),
                'panduan_status' => $this->input->post('masuk_bro'), // Draft atau Publish
                'panduan_tanggal' => date('Y-m-d H:i:s')
            );
    
            // Cek apakah ada dokumen baru yang diunggah
            if (!empty($_FILES['dokumen_baru']['name'])) {
                $config['upload_path']   = './dokumen/panduan/';
                $config['allowed_types'] = 'pdf|doc|pptx|docx|ppt|xls|xlsx|txt|rtf';
                $config['max_size']      = 51200; // Maksimal 50MB
    
                $this->load->library('upload', $config);
    
                if ($this->upload->do_upload('dokumen_baru')) {
                    // Hapus dokumen lama jika ada
                    if (!empty($panduan->panduan_doc) && file_exists('./dokumen/panduan/' . $panduan->panduan_doc)) {
                        unlink('./dokumen/panduan/' . $panduan->panduan_doc);
                    }
    
                    // Simpan dokumen baru
                    $uploadData = $this->upload->data();
                    $data['panduan_doc'] = $uploadData['file_name'];
                } else {
                    $this->session->set_flashdata('dokumen_error', $this->upload->display_errors());
                    redirect('dashboard/edit_panduan/' . $id);
                }
            }
    
            // Update data panduan di database
            $this->M_data->update_data($where, $data, 'panduan');
    
            // Set flash message & redirect
            $this->session->set_flashdata('success', 'Panduan berhasil diperbarui.');
            redirect('dashboard/panduan');
        } else {
            // Jika validasi gagal, tampilkan form edit lagi
            $this->edit_panduan($id);
        }
    }

    public function panduan_hapus($id)
    {
        // Validasi ID panduan
        if (empty($id) || !is_numeric($id)) {
            $this->session->set_flashdata('error', 'ID panduan tidak valid.');
            redirect('dashboard/panduan');
            return;
        }

        // Mencari panduan berdasarkan ID
        $where = array('panduan_id' => $id);
        $panduan = $this->M_data->get_data('panduan', $where)->row();

        // Cek apakah panduan ada
        if (!$panduan) {
            $this->session->set_flashdata('error', 'panduan tidak ditemukan.');
            redirect('dashboard/panduan');
            return;
        }

        // Hapus dokumen dari folder jika ada
        $dokumen_path = './dokumen/panduan/' . $panduan->panduan_doc;
        if (!empty($panduan->panduan_doc) && file_exists($dokumen_path)) {
            unlink($dokumen_path); // Menghapus file dokumen
        }

        // Hapus panduan dari database
        $this->M_data->delete_data($where, 'panduan');

        // Set pesan sukses
        $this->session->set_flashdata('success', 'panduan berhasil dihapus.');
        redirect('dashboard/panduan');
    }  
    // END CRUD PANDUAN
    

    // CRUD LAYANAN
    public function layanan()
	{
		$data['layanan'] = $this->db->query("SELECT * FROM layanan,kategori_layanan,pengguna WHERE layanan_kategori=kategori_layanan_id and layanan_author=pengguna_id order by layanan_id desc")->result();	
		$this->load->view('dashboard/v_header');
		$this->load->view('dashboard/v_layanan',$data);
		$this->load->view('dashboard/v_footer');
	}

    public function tambah_layanan() {
        $data['kategori_layanan'] = $this->M_data->get_data('kategori_layanan')->result();

        if (empty($data['kategori_layanan'])) {
            $this->session->set_flashdata('error', 'Tidak ada kategori ditemukan.');
            redirect('dashboard/kategori_layanan');
            return;
        }

        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_tambah_layanan', $data);
        $this->load->view('dashboard/v_footer');
    }

    public function layanan_aksi() {
        $this->form_validation->set_rules('judul', 'Judul', 'required|is_unique[layanan.layanan_judul]');
        $this->form_validation->set_rules('konten', 'Deskripsi', 'required');
        $this->form_validation->set_rules('kategori', 'Kategori', 'required');

        if (empty($_FILES['layanan_sampul']['name'])) {
            $this->form_validation->set_rules('sampul', 'Gambar Sampul', 'required');
        }

        if ($this->form_validation->run() == FALSE) {
            $this->tambah_layanan();
            return;
        }

        $upload_path = './gambar/layanan/';
        if (!is_dir($upload_path) || !is_writable($upload_path)) {
            $this->session->set_flashdata('error', 'Folder upload tidak bisa ditulis. Periksa izin folder.');
            redirect('dashboard/tambah_layanan');
            return;
        }

        $config['upload_path']   = $upload_path;
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size']      = 4096;
        $config['encrypt_name']  = FALSE;

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

        if (!$this->upload->do_upload('layanan_sampul')) {
            $this->session->set_flashdata('image_error', $this->upload->display_errors());
            $this->tambah_layanan();
            return;
        }

        $gambar = $this->upload->data();

        $data = array(
            'layanan_tanggal'  => date('Y-m-d H:i:s'),
            'layanan_judul'    => $this->input->post('judul', TRUE),
            'layanan_slug'     => url_title($this->input->post('judul'), 'dash', TRUE),
            'layanan_konten'   => $this->input->post('konten', TRUE),
            'layanan_sampul'   => $gambar['file_name'],
            'layanan_author'   => $this->session->userdata('id'),
            'layanan_kategori' => $this->input->post('kategori', TRUE),
            'layanan_status'   => $this->input->post('status', TRUE),
        );

        if ($this->M_data->insert_data($data, 'layanan')) {
            $this->session->set_flashdata('success', 'Layanan berhasil ditambahkan.');
            redirect('dashboard/layanan');
        } else {
            $this->session->set_flashdata('error', 'Gagal menyimpan layanan.');
            redirect('dashboard/tambah_layanan');
        }
    }

    public function edit_layanan($id)
    {
        $where = array('layanan_id' => $id);
        $data['layanan'] = $this->M_data->edit_data($where, 'layanan')->result();
        $data['kategori_layanan'] = $this->M_data->get_data('kategori_layanan')->result();
        
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_edit_layanan', $data);
        $this->load->view('dashboard/v_footer');
    }

    public function layanan_update()
    {
        // Validasi input
        $this->form_validation->set_rules('judul', 'Judul', 'required');
        $this->form_validation->set_rules('konten', 'Deskripsi', 'required');
        $this->form_validation->set_rules('kategori', 'Kategori', 'required');
        
        $id = $this->input->post('id');
        
        if ($this->form_validation->run() == TRUE) {
            // Ambil data dari form
            $judul = $this->input->post('judul', TRUE);
            $slug = url_title($judul, 'dash', TRUE);
            $konten = $this->input->post('konten', TRUE);
            $kategori = $this->input->post('kategori', TRUE);
            $status = $this->input->post('status', TRUE);

            // Data untuk update
            $data = array(
                'layanan_judul'    => $judul,
                'layanan_slug'     => $slug,
                'layanan_konten'   => $konten,
                'layanan_kategori' => $kategori,
                'layanan_status'   => $status,
            );

            // Update data layanan
            $where = array('layanan_id' => $id);
            $this->M_data->update_data($where, $data, 'layanan');

            // Cek apakah ada file gambar yang diupload
            if (!empty($_FILES['layanan_sampul']['name'])) {
                // Konfigurasi upload
                $config['upload_path']   = './gambar/layanan/';
                $config['allowed_types'] = 'gif|jpg|png|jpeg';
                $config['max_size']      = 4096;
                $config['encrypt_name']  = TRUE;
                
                $this->load->library('upload', $config);
                $this->upload->initialize($config);

                if ($this->upload->do_upload('layanan_sampul')) {
                    // Ambil data tentang gambar
                    $gambar = $this->upload->data();
                    $data_gambar = array('layanan_sampul' => $gambar['file_name']);
                    
                    // Update gambar sampul
                    $this->M_data->update_data($where, $data_gambar, 'layanan');
                } else {
                    // Set pesan kesalahan jika upload gagal
                    $this->session->set_flashdata('image_error', $this->upload->display_errors());
                }
            }

            // Redirect ke halaman layanan setelah berhasil
            $this->session->set_flashdata('success', 'Layanan berhasil diperbarui.');
            redirect('dashboard/layanan');
        } else {
            // Jika validasi gagal, ambil data layanan dan kategori untuk ditampilkan kembali
            $where = array('layanan_id' => $id);
            $data['layanan'] = $this->M_data->edit_data($where, 'layanan')->result();
            $data['kategori'] = $this->M_data->get_data('kategori')->result();
            
            // Tampilkan kembali form edit layanan
            $this->load->view('dashboard/v_header');
            $this->load->view('dashboard/v_edit_layanan', $data);
            $this->load->view('dashboard/v_footer');
        }
    }

    public function layanan_hapus($id)
    {
        // Validasi ID layanan
        if (empty($id) || !is_numeric($id)) {
            $this->session->set_flashdata('error', 'ID layanan tidak valid.');
            redirect('dashboard/layanan');
            return;
        }

        // Mencari layanan berdasarkan ID
        $where = array('layanan_id' => $id);
        $layanan = $this->M_data->get_data('layanan', $where)->row();

        // Cek apakah layanan ada
        if (!$layanan) {
            $this->session->set_flashdata('error', 'Layanan tidak ditemukan.');
            redirect('dashboard/layanan');
            return;
        }

        // Hapus gambar dari folder jika ada
        $gambar_path = './gambar/layanan/' . $layanan->layanan_sampul;
        if (!empty($layanan->layanan_sampul) && file_exists($gambar_path)) {
            unlink($gambar_path); // Menghapus file gambar
        }

        // Hapus layanan dari database
        $this->M_data->delete_data($where, 'layanan');

        // Set pesan sukses
        $this->session->set_flashdata('success', 'Layanan berhasil dihapus.');
        redirect('dashboard/layanan');
    }

    // END CRUD LAYANAN

    // CRUD RFC 2350
    public function rfc2350()
    {
        $data['rfc2350'] = $this->db->query("SELECT * FROM rfc2350, kategori_rfc2350, pengguna WHERE rfc2350_kategori=kategori_rfc2350_id and rfc2350_author=pengguna_id order by rfc2350_id desc")->result();
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_rfc2350', $data);
        $this->load->view('dashboard/v_footer');
    }

    public function tambah_rfc2350() {
        $data['kategori_rfc2350'] = $this->M_data->get_data('kategori_rfc2350')->result();

        if (empty($data['kategori_rfc2350'])) {
            $this->session->set_flashdata('error', 'Tidak ada kategori ditemukan.');
            redirect('dashboard/kategori_rfc2350');
            return;
        }

        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_tambah_rfc2350', $data);
        $this->load->view('dashboard/v_footer');
    }

    public function rfc2350_aksi() {
        $this->form_validation->set_rules('judul', 'Judul', 'trim|required|is_unique[rfc2350.rfc2350_nama]');
        $this->form_validation->set_rules('kategori', 'Kategori', 'required');
        
        if (empty($_FILES['dokumen_rfc']['name'])) {
            $this->form_validation->set_rules('dokumen_rfc', 'Dokumen', 'required');
        }
        
        if ($this->form_validation->run() == TRUE) {
            $config['upload_path']   = './dokumen/rfc2350/';
            $config['allowed_types'] = 'pdf|doc|docx|ppt|pptx|xls|xlsx|txt|rtf';
            $config['max_size']      = 51200; // 50MB
            $config['encrypt_name']  = FALSE; // Gunakan nama asli file
            
            $this->load->library('upload', $config);
            
            if ($this->upload->do_upload('dokumen_rfc')) {
                $rfc_dokumen = $this->upload->data();
                
                $data = array(
                    'rfc2350_tanggal'  => date('Y-m-d H:i:s'),
                    'rfc2350_nama'     => $this->input->post('judul', TRUE),
                    'rfc2350_doc'      => $rfc_dokumen['file_name'],
                    'rfc2350_author'   => $this->session->userdata('id'),
                    'rfc2350_kategori' => $this->input->post('kategori', TRUE),
                    'rfc2350_status'   => $this->input->post('status', TRUE),
                );
                
                if ($this->M_data->insert_data($data, 'rfc2350')) {
                    $this->session->set_flashdata('success', 'RFC2350 berhasil ditambahkan.');
                    redirect('dashboard/rfc2350');
                } else {
                    $this->session->set_flashdata('error', 'Gagal menyimpan data RFC2350.');
                    $this->load_form_with_errors();
                }
            } else {
                $this->session->set_flashdata('rfc_dokumen_error', $this->upload->display_errors());
                $this->load_form_with_errors();
            }
        } else {
            $this->load_form_with_errors();
        }
    }
    

    public function edit_rfc2350($id)
	{
        $where = array('rfc2350_id' => $id);
		$data['rfc2350'] = $this->M_data->edit_data($where,'rfc2350')->result();
		$data['kategori_rfc2350'] = $this->M_data->get_data('kategori_rfc2350')->result();

		$this->load->view('dashboard/v_header');
		$this->load->view('dashboard/v_edit_rfc2350',$data);
		$this->load->view('dashboard/v_footer');
	}

    public function rfc2350_update() {
        $id = $this->input->post('id');
    
        // Pastikan ID valid
        if (empty($id)) {
            show_error('ID tidak ditemukan', 404);
        }
    
        // Validasi form
        $this->form_validation->set_rules('nama', 'Nama', 'trim|required');
        $this->form_validation->set_rules('kategori', 'Kategori', 'required');
    
        if ($this->form_validation->run() == TRUE) {
            $where = array('rfc2350_id' => $id);

            $judul = $this->input->post('judul');
			$kategori = $this->input->post('kategori');
			$status = $this->input->post('status');


            // Data update
            $data = array(
                'rfc2350_nama' => $this->input->post('nama'),
                'rfc2350_kategori' => $this->input->post('kategori'),
                'rfc2350_status' => $this->input->post('masuk_bro'), // Draft atau Publish
                'rfc2350_tanggal' => date('Y-m-d H:i:s')
            );
    
            // Cek apakah ada dokumen baru yang diunggah
            if (!empty($_FILES['dokumen_rfc']['name'])) {
                $config['upload_path']   = './dokumen/rfc2350/';
                $config['allowed_types'] = 'pdf|doc|pptx|docx|ppt|xls|xlsx|txt|rtf';
                $config['max_size']      = 51200; // Maksimal 50MB
    
                $this->load->library('upload', $config);
    
                if ($this->upload->do_upload('dokumen_rfc')) {
                    // Hapus dokumen lama jika ada
                    if (!empty($rfc2350->rfc2350_doc) && file_exists('./dokumen/rfc2350/' . $rfc2350->rfc2350_doc)) {
                        unlink('./dokumen/rfc2350/' . $rfc2350->rfc2350_doc);
                    }
    
                    // Simpan dokumen baru
                    $uploadData = $this->upload->data();
                    $data['rfc2350_doc'] = $uploadData['file_name'];
                } else {
                    $this->session->set_flashdata('dokumen_error', $this->upload->display_errors());
                    redirect('dashboard/edit_rfc2350/' . $id);
                }
            }
    
            // Update data rfc2350 di database
            $this->M_data->update_data($where, $data, 'rfc2350');
    
            // Set flash message & redirect
            $this->session->set_flashdata('success', 'rfc2350 berhasil diperbarui.');
            redirect('dashboard/rfc2350');
        } else {
            // Jika validasi gagal, tampilkan form edit lagi
            $this->edit_rfc2350($id);
        }
    }
    
    public function rfc2350_hapus($id)
    {
        if (empty($id) || !is_numeric($id)) {
            $this->session->set_flashdata('error', 'ID RFC2350 tidak valid.');
            redirect('dashboard/rfc2350');
            return;
        }
    
        $where = array('rfc2350_id' => $id);
        $rfc2350 = $this->M_data->get_data('rfc2350', $where)->row();
    
        if (!$rfc2350) {
            $this->session->set_flashdata('error', 'RFC2350 tidak ditemukan.');
            redirect('dashboard/rfc2350');
            return;
        }
    
        // Pastikan file dokumen ada sebelum menghapus
        if (!empty($rfc2350->rfc2350_doc)) {
            $dokumen_path = './dokumen/rfc2350/' . $rfc2350->rfc2350_doc;
    
            if (file_exists($dokumen_path)) {
                unlink($dokumen_path); // Hapus file
            }
        }
    
        // Hapus data dari database setelah file dihapus
        $this->M_data->delete_data($where, 'rfc2350');
    
        $this->session->set_flashdata('success', 'RFC2350 berhasil dihapus.');
        redirect('dashboard/rfc2350');
    }
    
    
    // END CRUD RFC2350

    // CRUD PROFIL
    public function profil()
	{
		$data['profil'] = $this->db->query("SELECT * FROM profil,kategori_profil,pengguna WHERE profil_kategori=kategori_profil_id and profil_author=pengguna_id order by profil_id desc")->result();	
		$this->load->view('dashboard/v_header');
		$this->load->view('dashboard/v_profil',$data);
		$this->load->view('dashboard/v_footer');
	}

    public function tambah_profil() {
        $data['kategori_profil'] = $this->M_data->get_data('kategori_profil')->result();

        if (empty($data['kategori_profil'])) {
            $this->session->set_flashdata('error', 'Tidak ada kategori ditemukan.');
            redirect('dashboard/kategori_profil');
            return;
        }

        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_tambah_profil', $data);
        $this->load->view('dashboard/v_footer');
    }

    public function profil_aksi() {
        $this->form_validation->set_rules('judul', 'Judul', 'required|is_unique[profil.profil_judul]');
        $this->form_validation->set_rules('konten', 'Deskripsi', 'required');
        $this->form_validation->set_rules('kategori', 'Kategori', 'required');

        if (empty($_FILES['sampul']['name'])) {
            $this->form_validation->set_rules('sampul', 'Gambar Sampul', 'required');
        }

        if ($this->form_validation->run() == FALSE) {
            $this->tambah_profil();
            return;
        }

        $upload_path = './gambar/profil/';
        if (!is_dir($upload_path) || !is_writable($upload_path)) {
            $this->session->set_flashdata('error', 'Folder upload tidak bisa ditulis. Periksa izin folder.');
            redirect('dashboard/tambah_profil');
            return;
        }

        $config['upload_path']   = $upload_path;
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size']      = 4096;
        $config['encrypt_name']  = FALSE;

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

        if (!$this->upload->do_upload('sampul')) {
            $this->session->set_flashdata('gambar_error', $this->upload->display_errors());
            $this->tambah_profil();
            return;
        }

        $gambar = $this->upload->data();

        $data = array(
            'profil_tanggal'  => date('Y-m-d H:i:s'),
            'profil_judul'    => $this->input->post('judul', TRUE),
            'profil_slug'     => url_title($this->input->post('judul'), 'dash', TRUE),
            'profil_konten'   => $this->input->post('konten', TRUE),
            'profil_sampul'   => $gambar['file_name'],
            'profil_author'   => $this->session->userdata('id'),
            'profil_kategori' => $this->input->post('kategori', TRUE),
            'profil_status'   => $this->input->post('status', TRUE),
        );

        if ($this->M_data->insert_data($data, 'profil')) {
            $this->session->set_flashdata('success', 'profil berhasil ditambahkan.');
            redirect('dashboard/profil');
        } else {
            $this->session->set_flashdata('error', 'Gagal menyimpan profil.');
            redirect('dashboard/tambah_profil');
        }
    }

    public function edit_profil($id)
    {
        $where = array('profil_id' => $id);
        $data['profil'] = $this->M_data->edit_data($where, 'profil')->result();
        $data['kategori_profil'] = $this->M_data->get_data('kategori_profil')->result();
        
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_edit_profil', $data);
        $this->load->view('dashboard/v_footer');
    }

    public function profil_update()
    {
        // Validasi input
        $this->form_validation->set_rules('judul', 'Judul', 'required');
        $this->form_validation->set_rules('konten', 'Deskripsi', 'required');
        $this->form_validation->set_rules('kategori', 'Kategori', 'required');
        
        $id = $this->input->post('id');
        
        if ($this->form_validation->run() == TRUE) {
            // Ambil data dari form
            $judul = $this->input->post('judul', TRUE);
            $slug = url_title($judul, 'dash', TRUE);
            $konten = $this->input->post('konten', TRUE);
            $kategori = $this->input->post('kategori', TRUE);
            $status = $this->input->post('status', TRUE);

            // Data untuk update
            $data = array(
                'profil_judul'    => $judul,
                'profil_slug'     => $slug,
                'profil_konten'   => $konten,
                'profil_kategori' => $kategori,
                'profil_status'   => $status,
            );

            // Update data profil
            $where = array('profil_id' => $id);
            $this->M_data->update_data($where, $data, 'profil');

            // Cek apakah ada file gambar yang diupload
            if (!empty($_FILES['sampul']['name'])) {
                // Konfigurasi upload
                $config['upload_path']   = './gambar/profil/';
                $config['allowed_types'] = 'gif|jpg|png|jpeg';
                $config['max_size']      = 4096;
                $config['encrypt_name']  = TRUE;
                
                $this->load->library('upload', $config);
                $this->upload->initialize($config);

                if ($this->upload->do_upload('sampul')) {
                    // Ambil data tentang gambar
                    $gambar = $this->upload->data();
                    $data_gambar = array('profil_sampul' => $gambar['file_name']);
                    
                    // Update gambar sampul
                    $this->M_data->update_data($where, $data_gambar, 'profil');
                } else {
                    // Set pesan kesalahan jika upload gagal
                    $this->session->set_flashdata('gambar_error', $this->upload->display_errors());
                }
            }

            // Redirect ke halaman profil setelah berhasil
            $this->session->set_flashdata('success', 'profil berhasil diperbarui.');
            redirect('dashboard/profil');
        } else {
            // Jika validasi gagal, ambil data profil dan kategori untuk ditampilkan kembali
            $where = array('profil_id' => $id);
            $data['profil'] = $this->M_data->edit_data($where, 'profil')->result();
            $data['kategori'] = $this->M_data->get_data('kategori')->result();
            
            // Tampilkan kembali form edit profil
            $this->load->view('dashboard/v_header');
            $this->load->view('dashboard/v_edit_profil', $data);
            $this->load->view('dashboard/v_footer');
        }
    }

    public function profil_hapus($id)
    {
        // Validasi ID profil
        if (empty($id) || !is_numeric($id)) {
            $this->session->set_flashdata('error', 'ID profil tidak valid.');
            redirect('dashboard/profil');
            return;
        }

        // Mencari profil berdasarkan ID
        $where = array('profil_id' => $id);
        $profil = $this->M_data->get_data('profil', $where)->row();

        // Cek apakah profil ada
        if (!$profil) {
            $this->session->set_flashdata('error', 'profil tidak ditemukan.');
            redirect('dashboard/profil');
            return;
        }

        // Hapus gambar dari folder jika ada
        $gambar_path = './gambar/profil/' . $profil->profil_sampul;
        if (!empty($profil->profil_sampul) && file_exists($gambar_path)) {
            unlink($gambar_path); // Menghapus file gambar
        }

        // Hapus profil dari database
        $this->M_data->delete_data($where, 'profil');

        // Set pesan sukses
        $this->session->set_flashdata('success', 'Profil berhasil dihapus.');
        redirect('dashboard/profil');
    }
    // END CRUD PROFIL


    // CRUD HALAMAN
    public function halaman()
    {
        $data['halaman'] = $this->M_data->get_data('halaman')->result();
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_halaman', $data);
        $this->load->view('dashboard/v_footer');
    }

    // Function to display the add page form
    public function tambah_halaman()
    {
        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_tambah_halaman');
        $this->load->view('dashboard/v_footer');
    }

    // Function to handle the page submission
    public function halaman_aksi()
    {
        // Set validation rules for page input
        $this->form_validation->set_rules('judul', 'Judul', 'required|trim|htmlspecialchars|is_unique[halaman.halaman_judul]');
        $this->form_validation->set_rules('konten', 'Konten', 'required|trim|htmlspecialchars');
    
        // Check if validation is successful
        if ($this->form_validation->run() == TRUE) {
            // Get page data from input
            $judul = $this->input->post('judul', TRUE);
            $konten = $this->input->post('konten', TRUE);
            $slug = strtolower(url_title($judul));
    
            // Prepare data to be saved
            $data = array(
                'halaman_judul' => $judul,
                'halaman_slug' => $slug,
                'halaman_konten' => $konten
            );
    
            // Save page data to the halaman table
            $this->M_data->insert_data($data, 'halaman');
    
            // Set flashdata for success notification
            $this->session->set_flashdata('alert', 'Halaman berhasil ditambahkan!');
    
            // Redirect to the page list
            redirect('dashboard/halaman');
        } else {
            // If validation fails, return to the add page form
            $this->load->view('dashboard/v_header');
            $this->load->view('dashboard/v_tambah_halaman');
            $this->load->view('dashboard/v_footer');
        }
    }


    public function halaman_edit($id)
{
    $where = array('halaman_id' => $id);
    $data['halaman'] = $this->M_data->edit_data($where, 'halaman')->result();
    
    $this->load->view('dashboard/v_header');
    $this->load->view('dashboard/v_edit_halaman', $data);
    $this->load->view('dashboard/v_footer');
}

public function halaman_update()
{
    // Validasi input wajib diisi
    $this->form_validation->set_rules('judul', 'Judul', 'required|trim|htmlspecialchars');
    $this->form_validation->set_rules('konten', 'Konten', 'required|trim|htmlspecialchars');

    if ($this->form_validation->run() == TRUE) {
        $id = $this->input->post('id', TRUE);
        $judul = $this->input->post('judul', TRUE);
        $slug = strtolower(url_title($judul));
        $konten = $this->input->post('konten', TRUE);

        $where = array(
            'halaman_id' => $id
        );

        $data = array(
            'halaman_judul' => $judul,
            'halaman_slug' => $slug,
            'halaman_konten' => $konten
        );

        // Update data halaman
        $this->M_data->update_data($where, $data, 'halaman');

        // Set flashdata untuk notifikasi sukses
        $this->session->set_flashdata('alert', 'Halaman berhasil diperbarui!');

        // Redirect kembali ke halaman daftar halaman
        redirect('dashboard/halaman');
    } else {
        // Jika validasi gagal, kembali ke halaman edit
        $id = $this->input->post('id', TRUE);
        $where = array('halaman_id' => $id);
        $data['halaman'] = $this->M_data->edit_data($where, 'halaman')->result();

        $this->load->view('dashboard/v_header');
        $this->load->view('dashboard/v_edit_halaman', $data);
        $this->load->view('dashboard/v_footer');
    }
}

    public function halaman_hapus($id)
    {
    $where = array('halaman_id' => $id);
    $this->M_data->delete_data($where,'halaman');
    redirect(base_url().'dashboard/halaman');
    }

    // CRUD HALAMAN


    // CRUD PENGATURAN
    public function pengaturan()
    {
    $data['pengaturan'] = $this->M_data->get_data('pengaturan')->result();
    $this->load->view('dashboard/v_header');
    $this->load->view('dashboard/v_pengaturan',$data);
    $this->load->view('dashboard/v_footer');
    }

    public function pengaturan_update()
    {
        // Wajib isi nama dan deskripsi website
        $this->form_validation->set_rules('nama', 'Nama Website', 'required');
        $this->form_validation->set_rules('deskripsi', 'Deskripsi Website', 'required');
        
        if ($this->form_validation->run() != false) {
            $nama = $this->input->post('nama');
            $deskripsi = $this->input->post('deskripsi');
            $link_facebook = $this->input->post('link_facebook');
            $link_twitter = $this->input->post('link_twitter');
            $link_instagram = $this->input->post('link_instagram');
            $link_github = $this->input->post('link_github');
            
            $data = array(
                'nama' => $nama,
                'deskripsi' => $deskripsi,
                'link_facebook' => $link_facebook,
                'link_twitter' => $link_twitter,
                'link_instagram' => $link_instagram,
                'link_github' => $link_github
            );
            
            // Update pengaturan
            $this->M_data->update_data([], $data, 'pengaturan');
            
            // Pastikan folder upload tersedia dan writable
            $upload_folder = './gambar/pengaturan/';
            if (!is_dir($upload_folder)) {
                mkdir($upload_folder, 0777, true);
            }
            chmod($upload_folder, 0777);
            
            // Periksa apakah ada gambar logo yang diupload
            if (!empty($_FILES['logo']['name'])) {
                $config['upload_path'] = $upload_folder;
                $config['allowed_types'] = 'jpg|jpeg|png|gif';
                $config['max_size'] = 2048; // 2MB
                $config['file_name'] = time() . '_' . $_FILES['logo']['name']; // Buat nama unik
                $config['overwrite'] = TRUE;
                
                $this->load->library('upload', $config);
                
                if ($this->upload->do_upload('logo')) {
                    // Mengambil data tentang gambar logo yang diupload
                    $upload_data = $this->upload->data();
                    $logo = $upload_data['file_name'];
                    
                    // Simpan nama file ke database
                    $this->db->update('pengaturan', ['logo' => $logo]);
                } else {
                    // Jika upload gagal, tampilkan error
                    echo "Upload gagal: " . $this->upload->display_errors();
                    exit;
                }
            }
            
            redirect(base_url() . 'dashboard/pengaturan/?alert=sukses');
        } else {
            $data['pengaturan'] = $this->M_data->get_data('pengaturan')->result();
            
            $this->load->view('dashboard/v_header');
            $this->load->view('dashboard/v_pengaturan', $data);
            $this->load->view('dashboard/v_footer');
        }
    }
    // END CRUD PENGATURAN

    
    // CRUD KONTAK
    public function kontak()
    {
    $data['kontak'] = $this->M_data->get_data('kontak')->result();
    $this->load->view('dashboard/v_header');
    $this->load->view('dashboard/v_kontak',$data);
    $this->load->view('dashboard/v_footer');
    }

    public function kontak_update()
    {
        // Wajib isi deskripsi website
        $this->form_validation->set_rules('nama', 'Deskripsi Website', 'required');
    
        if ($this->form_validation->run() != false) {
            // Ambil data dari form
            $nama = $this->input->post('nama');
            $alamat = $this->input->post('alamat');
            $email = $this->input->post('email');
            $no_telp = $this->input->post('no_telp');
            $pesan = $this->input->post('pesan');
            $lokasi_alamat = $this->input->post('lokasi_alamat');
            $lokasi_latitude = $this->input->post('lokasi_latitude');
            $lokasi_longtitude = $this->input->post('lokasi_longtitude');
    
            // Array data yang akan diperbarui
            $data = array(
                'nama' => $nama,
                'alamat' => $alamat,
                'email' => $email,
                'no_telp' => $no_telp,
                'pesan' => $pesan,
                'lokasi_alamat' => $lokasi_alamat,
                'lokasi_latitude' => $lokasi_latitude,
                'lokasi_longtitude' => $lokasi_longtitude
            );
    
            // Kondisi update berdasarkan id
            $where = array('nama' => $nama);
    
            // Update data kontak
            $this->M_data->update_data($where, $data, 'kontak');
    
            // Redirect dengan alert sukses
            redirect(base_url() . 'dashboard/kontak/?alert=sukses');
        } else {
            $data['kontak'] = $this->M_data->get_data('kontak')->result();
    
            $this->load->view('dashboard/v_header');
            $this->load->view('dashboard/v_kontak', $data);
            $this->load->view('dashboard/v_footer');
        }
    }
    
    // END CRUD KONTAK

    
}