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

class Kodebarang extends CI_Controller {

    public function __construct()
    {
        parent::__construct();

        if (!$this->session->userdata('logged_in')) {
            redirect('auth');
        }
    }
    
    public function index()
    {
        $data = ["active_menu" => "kode_barang"];
        $this->load->view('partials/header', $data);
        $this->load->view('kode_barang/layout');
        $this->load->view('partials/footer');
    }

    // =========================
    // GET DATA (DATATABLE)
    // =========================
    public function get_data()
    {
        $data = $this->db->get('kode_barang')->result();

        $result = [];
        $no = 1;

        foreach ($data as $row) {
            $result[] = [
                $no++,
                $row->kode_barang,
                $row->nama,
                $row->limit_stock,
                '
                <button class="btn btn-sm btn-secondary btn-editkode" data-id="'.$row->id.'">Edit</button>
                <button class="btn btn-sm btn-danger btn-delete" data-id="'.$row->id.'">Delete</button>
                '
            ];
        }

        echo json_encode(["data"=>$result]);
    }

    // =========================
    // DETAIL
    // =========================
    public function detail($id)
    {
        echo json_encode(
            $this->db->get_where('kode_barang',['id'=>$id])->row()
        );
    }

    // =========================
    // SAVE
    // =========================
    public function save()
    {
        $nama = $this->input->post('nama');
        $kode = $this->input->post('kode');
        $limit_stock = $this->input->post('limit_stock');

        if(!$nama){
            echo json_encode(['status'=>false,'message'=>'Nama wajib diisi']);
            return;
        }

        $this->db->insert('kode_barang', [
            'kode_barang' => $kode,
            'nama' => $nama,
            'limit_stock' => $limit_stock
        ]);

        log_activity(
            'kode_barang',
            'create',
            'Menambahkan kode barang: ' . $nama,
            'success'
        );

        echo json_encode(['status'=>true,'message'=>'Gudang berhasil ditambahkan']);
    }

    // =========================
    // UPDATE
    // =========================
    public function update()
    {
        $id   = $this->input->post('id');
        $kode = $this->input->post('kode');
        $nama = $this->input->post('nama');
        $limit_stock = $this->input->post('limit_stock');

        if(!$nama){
            echo json_encode(['status'=>false,'message'=>'Nama wajib diisi']);
            return;
        }

        $this->db->where('id',$id)->update('kode_barang', [
            'kode_barang' => $kode,
            'nama' => $nama,
            'limit_stock' => $limit_stock
        ]);

        log_activity(
            'kode_barang',
            'update',
            'Update kode barang ID: ' . $id . ' nama: ' . $nama,
            'success'
        );

        echo json_encode(['status'=>true,'message'=>'Gudang berhasil diupdate']);
    }

    // =========================
    // DELETE
    // =========================
    public function delete($id)
    {
        // optional: cek relasi ke items
        $cek = $this->db->get_where('items',['warehouse_id'=>$id])->num_rows();

        if($cek > 0){
            echo json_encode([
                'status'=>false,
                'message'=>'Gudang tidak bisa dihapus karena masih dipakai item'
            ]);
            return;
        }

        $this->db->delete('kode_barang',['id'=>$id]);

        log_activity(
            'kode_barang',
            'delete',
            'Hapus gudang ID: ' . $id,
            'success'
        );

        echo json_encode(['status'=>true,'message'=>'Gudang berhasil dihapus']);
    }

    
    public function list()
    {
        echo json_encode(
            $this->db->get('kode_barang')->result()
        );
    }
}