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

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

class Suplier 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("suplier")->result();
        $this->template->load('Template/template', 'Suplier/lihat_data', $data);
        $this->load->view('Template/datatables');

    }
    function post()
    {
        if (isset($_POST["submit"])) {
           
                // proses suplier
                $id = $this->input->post('id');
                $nama = $this->input->post('nama_perusahaan');
                $alamat = $this->input->post('alamat');
                $tlp = $this->input->post('no_tlp');
                $email = $this->input->post('email');
              
                $data = array(
                    'nama_perusahaan' => $nama,
                    'alamat' => $alamat,
                    'no_tlp' => $tlp,
                    'email' => $email,
                    
                );
                $this->db->insert("suplier",$data);
                $this->session->set_flashdata('message', 'Data Suplier berhasil ditambahkan!');
                redirect('suplier');
            
        } else {
            $id = $this->uri->segment(3);
            $data['error'] = $this->upload->display_errors();
            $this->load->model("Model_kategori");
           
            $this->template->load("Template/template", "Suplier/form_input", $data);
        }
    }


    function edit()
    {
        if (isset($_POST['submit'])) {
            $id         =   $this->input->post('id');
            $nama = $this->input->post('nama_perusahaan');
            $alamat = $this->input->post('alamat');
            $tlp = $this->input->post('no_tlp');
            $email = $this->input->post('email');
                $data       = array(
                    'nama_perusahaan' => $nama,
                    'alamat' => $alamat,
                    'no_tlp' => $tlp,
                    'email' => $email,
                );
                $this->Model_barang->editsuplier($data, $id);
                $this->session->set_flashdata('message', 'Data Suplier berhasil dirubah!');
                redirect('suplier');
            
        } else {
            $id =  $this->uri->segment(3);
            $this->load->model('Model_kategori');            
            $data['record']     =  $this->Model_barang->get_ones2($id)->row_array();            
            $this->template->load('Template/template', 'Suplier/form_edit', $data);
        }
    }
    function hapus()
    {
        $id = $this->uri->segment(3);
        $this->Model_barang->hapussuplier($id);
        $this->session->set_flashdata('message', 'Data Suplier berhasil dihapus!');
        redirect('suplier');
    }

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

}
