<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * @property CI_DB $db
 * @property CI_Input $input
 * @property CI_Output $output
 */
class Sewa extends CI_Controller {

    private $tableMaster = 'sewa';

    public function __construct() {
        parent::__construct();
        $this->load->database();
         $this->load->library('session'); 
        $this->output->set_content_type('application/json');
    }

    // Tangkap semua request
    public function _remap($method, $params = []) {
        if(method_exists($this, $method)){
            return call_user_func_array([$this, $method], $params);
        }
        echo json_encode(['status'=>'error','message'=>'Method tidak ditemukan']);
    }

    private function getJsonInput() {
        $data = json_decode(file_get_contents('php://input'), true);
        if (!$data) {
            echo json_encode([
                'status' => false,
                'message' => 'JSON kosong atau tidak valid'
            ]);
            exit;
        }
        return $data;
    }

    public function insert() {
        $data = $this->getJsonInput();


        if (!$this->db->insert($this->tableMaster, $data)) {
            echo json_encode([
                'status' => false,
                'message' => 'Gagal insert data sewa',
                'db_error' => $this->db->error()
            ]);
            return;
        }

        echo json_encode([
            'status' => true,
            'message' => 'Data sewa berhasil disimpan'
        ]);
    }



public function get() {

    // WAJIB login
    if (!$this->session->userdata('logged_in')) {
        echo json_encode([
            'status' => false,
            'message' => 'Belum login'
        ]);
        return;
    }

    $id_user = $this->session->userdata('id_user');
    $role    = $this->session->userdata('role');

    // ADMIN → semua data
    if ($role === 'admin') {
        $data = $this->db
            ->get($this->tableMaster)
            ->result_array();
    } 
    // USER → hanya data miliknya
    else {
        $data = $this->db
            ->where('id_user', $id_user)
            ->get($this->tableMaster)
            ->result_array();
    }

    echo json_encode([
        'status' => true,
        'data'   => $data
    ]);
}


    public function update($id_sewa = null) {
        if (!$id_sewa) {
            echo json_encode(['status'=>false,'message'=>'id_sewa wajib disertakan di URL']);
            return;
        }

        $data = $this->getJsonInput();

        $this->db->where('id_sewa', $id_sewa);
        if (!$this->db->update($this->tableMaster, $data)) {
            echo json_encode(['status'=>false,'message'=>'Gagal update data sewa','db_error'=>$this->db->error()]);
            return;
        }

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

    public function delete($id_sewa = null) {
        if (!$id_sewa) {
            echo json_encode(['status'=>false,'message'=>'id_sewa wajib disertakan di URL']);
            return;
        }

        $this->db->where('id_sewa', $id_sewa);
        if (!$this->db->delete($this->tableMaster)) {
            echo json_encode(['status'=>false,'message'=>'Gagal menghapus data sewa','db_error'=>$this->db->error()]);
            return;
        }

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