<?php

namespace App\Controllers;

use App\Models\Perusahaan_model;
use App\Models\Produk_model;

class Perusahaan extends BaseController
{
    public function __construct()
	{
		$this->p_model = new Perusahaan_model();
        $this->pk_model = new Produk_model();
	}
    
	public function profil()
	{
        $data['page_title'] = 'Perusahaan';
        //$data['sub_title'] = 'Profil';
        $data['data'] = $this->p_model->get_perusahaan();
		return view('perusahaan/profil', $data);
	}

    public function produk()
    {
        $data['data'] = $this->pk_model->get_produk();
        $data['page_title'] = 'Produk';
        //$data['sub_title'] = 'Produk';
		return view('perusahaan/produk', $data);
    }

    public function tambah_data($jenis)
    {
        if($jenis == 'profil'){
            $name_page = 'tambah_perusahaan';
            $data = $this->proses_tambah_profil($jenis);
        }else if($jenis == 'produk'){
            $name_page = 'tambah_produk';
            $data = $this->proses_tambah_produk($jenis);
        }

        return view('perusahaan/'.$name_page, $data);
    }

    public function proses_tambah_profil($jenis)
    {
        $data = [];
        $data['page_title'] = 'Perusahaan';
        $data['sub_title'] = 'Produk';

        return $data;
    }

    public function proses_tambah_produk($jenis)
    {
        $data = [];
        $data['page_title'] = 'Perusahaan';
        $data['sub_title'] = 'Produk';
        $data['perusahaan'] = $this->p_model->get_perusahaan();

        return $data;
    }

    public function save($jenis)
    {
        if($jenis == 'profil'){
            $data = $this->request->getVar();
            $status = $this->save_tambah_profil($data);
        }else if($jenis == 'produk'){
            $data = $this->request->getVar();
            $status = $this->save_tambah_produk($data);
        }

        return redirect()->to("/perusahaan/$jenis");
    }

    public function save_tambah_profil($data)
    {
        $this->p_model->save([
			'nama' => $data['nama'],
			'keterangan' => $data['keterangan']
		]);
        return 1;
    }

    public function save_tambah_produk($data)
    {
        $this->pk_model->save([
            'id_perusahaan' => $data['id_perusahaan'],
		    'nama' => $data['nama'],
		    'keterangan' => $data['keterangan']
		]);
        return 1;
    }

    public function edit($jenis, $id)
    {
        if($jenis == 'profil'){
            $page_name = 'edit_perusahaan';
            $data = $this->proses_edit_profil($jenis, $id);
        }
        if($jenis == 'produk'){
            $page_name = 'edit_produk';
            $data = $this->proses_edit_produk($jenis, $id);
        }
		return view("perusahaan/$page_name", $data);
    }

    public function proses_edit_profil($jenis, $id)
    {
        $data['page_title'] = 'Edit Data '. ucwords($jenis);
        $data['sub_title'] = 'Profil';
        $data['id'] = $id;
        $data['data'] = $this->p_model->get_perusahaan($id);
        return $data;
    }

    public function proses_edit_produk($jenis, $id)
    {
        $data['page_title'] = 'Edit Data '. ucwords($jenis);
        $data['sub_title'] = 'Produk';
        $data['id'] = $id;
        $data['data'] = $this->pk_model->get_produk($id);
        $data['perusahaan'] = $this->p_model->get_perusahaan();
        return $data;
    }

    public function update($jenis, $id)
    {
        if($jenis == 'profil'){
            $data = $this->request->getVar();
            $status = $this->update_profil($data, $id);
        }
        if($jenis == 'produk'){
            $data = $this->request->getVar();
            $status = $this->update_produk($data, $id);
        }
        session()->setFlashdata('pesan', 'Data '.$jenis.' berhasil diupdate.');
		return redirect()->to("/perusahaan/$jenis");
    }

    public function update_profil($data, $id)
    {
        $this->p_model->save([
            'id' =>$id,
			'nama' => $data['nama'],
			'keterangan' => $data['keterangan']
		]);
        return 1;
    }

    public function update_produk($data, $id)
    {
        $this->pk_model->save([
            'id' =>$id,
            'id_perusahaan' => $data['id_perusahaan'],
			'nama' => $data['nama'],
	        'keterangan' => $data['keterangan']
		]);
        return 1;
    }

    public function delete($jenis, $id)
    {
        if($jenis == 'profil'){
            $this->p_model->delete($id);
        }
        if($jenis == 'produk'){
            
        }
        session()->setFlashdata('pesan', 'Data '.$jenis.' berhasil diHapus.');
		return redirect()->to("/perusahaan/$jenis");
    }

    public function detail($jenis, $id)
    {
        if($jenis == 'profil'){
            $name_page = 'detail_perusahaan';
            $data['page_title'] = 'Detail Perusahaan';
            $data['data'] = $this->p_model->get_perusahaan($id);
            $data['daftar_produk'] = $this->pk_model->get_produk_by_id_perusahaan($id);
        }else if($jenis == 'produk'){
            $name_page = 'detail_produk';
            $data['page_title'] = 'Detail Produk';
            $data['data'] = $this->pk_model->get_produk($id);
        }

        return view('perusahaan/'.$name_page, $data);
    }
}