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

class Siswa extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        logged_in();
        $this->load->model('m_user');
    }

    public function index()
    {

        $data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
        $data['title'] = 'Sekolah X';
        $this->load->view('templates/menuheader', $data);
        $this->load->view('templates/menusidebar', $data);
        $this->load->view('siswa/index', $data);
        $this->load->view('templates/menufooter');
    }

    public function input_data()
    {
        //method untuk input data
        $name = $this->input->post('name');
        $date = $this->input->post('date');
        $from_school = $this->input->post('from_school');
        $id_user = $this->session->userdata('id_user'); //membuat variabel id user untuk menangkap sesion id_user 
//untuk menampung name data from dari view

// perintah untuk upload foto dsini yang diambil adalah nama dari fotonya dan filenya diupload ke folder "uploads" pada $path, kemduian untuk ekstensinya di allowed types
        $photo = $_FILES['photo'];
        $path = 'uploads';
        $config['upload_path'] = $path;
        $config['allowed_types'] = 'png|jpg|bmp|pdf|zip|rar';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if (!$this->upload->do_upload('photo')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());
        }
        if (!$this->upload->data('file_name')) {
            $photo = 'Tidak ada file';
        } else {
            $photo = $this->upload->data('file_name');
        }

        $file = $_FILES['file'];
        $path = 'uploads';
        $config['upload_path'] = $path;
        $config['allowed_types'] = 'pdf';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if (!$this->upload->do_upload('file')) {
            $error = array('error' => $this->upload->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());
        }
        if (!$this->upload->data('file_name')) {
            $file = 'Tidak ada file';
        } else {
            $file = $this->upload->data('file_name');
        }

        //membuat array sesuai dengan urutan yang ada di urutan database, (harus urut dan namanya sesuai) penamaan $name dkk juga harus sama dengan variabel yang dibuat diatas
        $data = array(
            'name' => $name,
            'photo' => $photo,
            'file' => $file,
            'date' => $date,
            'from_school' => $from_school,
            'id_user' => $id_user
        );
        $this->db->insert('siswa', $data); //query untuk insert tanpa model
        redirect('siswa');
    }
}
