<?php
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;

class Siswa extends CI_Controller
{
    
    function __construct()
    {
        parent::__construct();
        chek_role();
        $this->load->model('Model_barang');
        $this->load->model('Model_kategori');
        // $this->load->library('PhpSpreadsheet');
    }
    function index()
    {
        $data['record'] = $this->db->get("siswa")->result();
        $this->template->load('Template/template', 'Siswa/lihat_data', $data);
        $this->load->view('Template/datatables');

    }
    function post()
    {
        if (isset($_POST["submit"])) {
           
                // proses barang
                $id = $this->input->post('id');
                $nama = $this->input->post('nama_siswa');
                $kelas = $this->input->post('kelas');
                $nis = $this->input->post('nis');
                $status = $this->input->post('status');
              
                $data = array(
                    'nama' => $nama,
                    'kelas' => $kelas,
                    'nis' => $nis,
                    'status' => $status,
                    
                );
                $this->db->insert("siswa",$data);
                $this->session->set_flashdata('message', 'Data Siswa berhasil ditambahkan!');
                redirect('siswa');
            
        } else {
            $id = $this->uri->segment(3);
            $data['error'] = $this->upload->display_errors();
            $this->load->model("Model_kategori");
           
            $this->template->load("Template/template", "Siswa/form_input", $data);
        }
    }


    function edit()
    {
        if (isset($_POST['submit'])) {
            $id         =   $this->input->post('id');
            $nama = $this->input->post('nama_siswa');
            $kelas = $this->input->post('kelas');
            $nis = $this->input->post('nis');
            $status = $this->input->post('status');
                $data       = array(
                    'nama' => $nama,
                    'kelas' => $kelas,
                    'nis' => $nis,
                    'status' => $status,
                );
                $this->Model_barang->editsiswa($data, $id);
                $this->session->set_flashdata('message', 'Data Barang berhasil dirubah!');
                redirect('siswa');
            
        } else {
            $id =  $this->uri->segment(3);
            $this->load->model('Model_kategori');
            
            $data['record']     =  $this->Model_barang->get_ones($id)->row_array();
            
            $this->template->load('Template/template', 'Siswa/form_edit', $data);
        }
    }
    function hapus()
    {
        $id = $this->uri->segment(3);
        $this->Model_barang->hapussiswa($id);
        $this->session->set_flashdata('message', 'Data Siswa berhasil dihapus!');
        redirect('barang');
    }

    function detail_modal($id)
    {
        $id = $this->input->get('id');
        $data['detail'] = $this->Model_barang->get_detail_modal($id);
        $this->load->view('Siswa/modal_detail', $data);
    }


    public function import_excel() {
        $config['upload_path']          = './uploads/';
        $config['allowed_types']        = 'xlsx|xls'; // Jenis file yang diizinkan
        $config['max_size']             = 2048; // Ukuran maksimal file (dalam kilobita)
    
        $this->upload->initialize($config);
    
        if (!$this->upload->do_upload('excel_file')) {
            // Jika upload gagal, tampilkan pesan error
            $error = $this->upload->display_errors();
            $this->session->set_flashdata('message', $error);
            redirect('siswa'); // Redirect kembali ke halaman siswa
        } else {
            // Jika upload berhasil, lanjutkan dengan membaca file Excel
            $file_data = $this->upload->data();
            $file_path = './uploads/' . $file_data['file_name'];
             
    
            // Load file Excel
            $objPHPExcel = IOFactory::load($file_path);
    
            // Mendapatkan objek worksheet
            $sheet = $objPHPExcel->getActiveSheet();
    
            // Mendapatkan jumlah baris di file Excel
            $highest_row = $sheet->getHighestDataRow();
    
            // Mulai dari baris kedua (karena baris pertama biasanya adalah header)
            for ($row = 2; $row <= $highest_row; $row++) {
                // Ambil nilai dari setiap kolom pada baris saat ini
                $nama = $sheet->getCellByColumnAndRow(2, $row)->getValue();
                $kelas = $sheet->getCellByColumnAndRow(3, $row)->getValue();
                $nis = $sheet->getCellByColumnAndRow(1, $row)->getValue();
                $status = $sheet->getCellByColumnAndRow(4, $row)->getValue();
    
                // Simpan data siswa ke dalam database
                $data = array(
                    'nama' => $nama,
                    'kelas' => $kelas,
                    'nis' => $nis,
                    'status' => $status
                );
                $this->db->insert('siswa', $data);
            }
    
            // Hapus file Excel yang diunggah
            unlink($file_path);
    
            // Set pesan sukses
            $this->session->set_flashdata('message', 'Data berhasil diimpor dari file Excel.');
    
            // Redirect kembali ke halaman siswa
            redirect('siswa');
        }
    }
    
    
}
