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

class Lokasiasset extends CI_Controller {

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

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

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

        $result = [];
        $no = 1;

        foreach ($data as $row) {
            $result[] = [
                $no++,
                $row->nama,
                '
                <button class="btn btn-sm btn-secondary btn-editgudang" 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('lokasi_asset',['id'=>$id])->row()
        );
    }

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

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

        $this->db->insert('lokasi_asset', [
            'nama' => $nama
        ]);

        log_activity(
            'kode_barang',
            'create',
            'Tambah gudang '.$nama,
            'success'
        );

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

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

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

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

        log_activity(
            'kode_barang',
            'update',
            'Update gudang '.$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('assets',['lokasi_asset_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('lokasi_asset',['id'=>$id]);

        log_activity(
            'kode_barang',
            'delete',
            'Delete gudang '.$nama,
            'success'
        );

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

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