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

    private $tableMaster = 'metode_bayar';

    public function __construct() {
        parent::__construct();
        $this->load->database();
        $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;
    }

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

        // Auto-generate id_metode jika kosong
        if (empty($data['id_metode'])) {
            $prefix = 'MB-';
            do {
                $data['id_metode'] = $prefix . substr(md5(mt_rand()), 0, 8);
                $cek = $this->db->get_where($this->tableMaster, ['id_metode' => $data['id_metode']])->row_array();
            } while($cek);
        }

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

        echo json_encode([
            'status'=> true,
            'message'=> 'Data metode bayar berhasil disimpan',
            'id_metode'=> $data['id_metode']
        ]);
    }

    // Get all data
    public function get() {
        $data = $this->db->get($this->tableMaster)->result_array();
        echo json_encode([
            'status'=>true,
            'message'=>'Data metode bayar berhasil diambil',
            'data'=> $data
        ]);
    }

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

        $data = $this->getJsonInput();

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

        echo json_encode([
            'status'=>true,
            'message'=>'Data metode bayar berhasil diupdate',
            'id_metode'=>$id_metode
        ]);
    }

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

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

        echo json_encode([
            'status'=>true,
            'message'=>'Data metode bayar berhasil dihapus',
            'id_metode'=>$id_metode
        ]);
    }
}
