<?php

namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;
use App\Models\PenjaminanAgen_Model;
use CodeIgniter\RESTful\ResourceController;

class Penjaminan_Agen extends ResourceController
{

    use ResponseTrait;

    public function index()
    {
        $model = new PenjaminanAgen_Model();
        $data = $model->findAll();
        return $this->respond($data, 200);
    }

    // get single product
    public function show($id = null)
    {
        $model = new PenjaminanAgen_Model();
        $data = $model->getWhere(['no_transaksi' => $id])->getResult();
        if ($data) {
            return $this->respond($data);
        } else {
            return $this->failNotFound('No Data Found with id ' . $id);
        }
    }

    // create a product
    public function create()
    {
        $model = new PenjaminanAgen_Model();
        $data = [
            'no_transaksi' => $this->request->getPost('no_transaksi'),

            'kd_agen' => $this->request->getPost('kd_agen'),
            'keterangan' => $this->request->getPost('keterangan'),
            'nilai' => $this->request->getPost('nilai'),
            'kd_user' => $this->request->getPost('kd_user'),

        ];
        $data = json_decode(file_get_contents("php://input"));
        //$data = $this->request->getPost();
        $model->insert($data);
        $response = [
            'status'   => 201,
            'error'    => null,
            'messages' => [
                'success' => 'Data Saved'
            ]
        ];

        return $this->respondCreated($data, 201);
    }

    // update product
    public function update($id = null)
    {
        $model = new PenjaminanAgen_Model();

        $json = $this->request->getJSON();
        if ($json) {
            $data = [


                'kd_agen' => $json->kd_agen,
                'keterangan' => $json->keterangan,
                'nilai' => $json->nilai,
                'kd_user' => $json->kd_user

            ];
        } else {
            $input = $this->request->getRawInput();
            $data = [



                'kd_agen' => $input['kd_agen'],
                'keterangan' => $input['keterangan'],
                'nilai' => $input['nilai'],
                'kd_user' => $input['kd_user'],
            ];
        }
        // Insert to Database
        $model->set($data);
        $model->where('no_transaksi', $id);
        $model->update();
        $response = [
            'status'   => 200,
            'error'    => null,
            'messages' => [
                'success' => 'Data Updated'
            ]
        ];
        return $this->respond($response);
    }

    // // delete product
    public function delete($id = null)
    {
        $model = new PenjaminanAgen_Model();

        $data = $model->find($id);
        $model->set($data);
        $model->where('no_transaksi', $id);
        $model->delete();
        if ($data) {
            $model->delete($id);
            $response = [
                'status'   => 200,
                'error'    => null,
                'messages' => [
                    'success' => 'Data Deleted'
                ]
            ];

            return $this->respondDeleted($response);
        } else {
            return $this->failNotFound('No Data Found with id ' . $id);
        }
    }
}
