<?php
defined('BASEPATH') or exit('No direct script access allowed');
// require APPPATH . "libraries/format.php";
// require APPPATH . "libraries/RestController.php";

include(APPPATH . 'libraries/Format.php');
include(APPPATH . 'libraries/RestController.php');

use chriskacerguis\RestServer\RestController;

class ControllerReviews extends RestController
{

    public function __construct()
    {
        parent::__construct();
        // Membatasi Jumlah akses sesuai kebutuhan
        $this->methods['index_get']['limit'] = 200;
        $this->methods['index_post']['limit'] = 200;
        $this->load->library('encrypt');
        $this->load->library('mycustom');
        $this->load->model('api/insert_model');
        $this->load->model('api/select_model');
        $this->load->model('api/update_model');
        $this->methods['index_get']['limit'] = 100;
        $this->methods['index_post']['limit'] = 100;
    }
    // Menampilkan data 
    public function index_get()
    {
        $id = $this->get('id');
        if ($id === null) {
            $this->response([
                'status' => 0,
                'message' => 'Kontrak ID Marketing Tidak Ada!'
            ], RestController::HTTP_NOT_FOUND);
        } else {
            $data_anggota = $this->select_model->vd_Anggota($id);
            if ($data_anggota != null) {
                $this->response([
                    'status' => 1,
                    'data'   => $data_anggota,
                ], RestController::HTTP_OK);
            } else {
                $this->response([
                    'status' => 0,
                    'message' => 'Kontrak Tidak Ada atau Sudah Lunas!'
                ], RestController::HTTP_NOT_FOUND);
            }
        }
    }

    // Menghapus data 
    // public function index_delete()
    // {
    //     $id = $this->delete('id');

    //     if ($id == null) {
    //         $this->response([
    //             'status' => false,
    //             'message' => 'profide an id!'
    //         ], RestController::HTTP_BAD_REQUEST);
    //     } else {
    //         if ($this->mahasiswa->deleteMahasiswa($id) > 0) {
    //             // ok
    //             $this->response([
    //                 'status' => true,
    //                 'data' => $id,
    //                 'message' => 'deleted.'
    //             ], RestController::HTTP_OK);
    //         } else {
    //             // id not found
    //             $this->response([
    //                 'status' => false,
    //                 'message' => 'id not found!'
    //             ], RestController::HTTP_BAD_REQUEST);
    //         }
    //     }
    // }
    // Menambah data
    function index_post()
    {
        // VWFWMwY2Cj9QMAQyCTtTYA46AGdTZghsXz5ZOQRgUWBQNA==
        $id = $this->post('kontrak_id');
        if ($id === null) {
            $this->response([
                'status' => false,
                'data'   => 'ID Kontrak Tidak Ada!',
                'id' => $id
            ], RestController::HTTP_BAD_REQUEST);
        } else {
            $kd_kontrak   = $this->encrypt->decode($id);
            // Cek apakah ada kontrak
            $data_kontrak = $this->select_model->vd_KontrakPinjaman($kd_kontrak);
            if ($data_kontrak) {
                // Data Review
                $hp_konsumen   = htmlspecialchars($this->post('hp_konsumen'));
                $cek_total_review_bulan = $this->select_model->vd_TotalReview($hp_konsumen);
                $ttl_review = count($cek_total_review_bulan);
                // Apakah Bulan ini sudah review atau belum
                if ($ttl_review >= 6) :
                    $this->response([
                        'status' => false,
                        'message' => 'Sudah Terlalu Banyak Reviews!'
                    ], RestController::HTTP_BAD_REQUEST);
                else:
                    $data_review = array(
                        'login_id'      => $data_kontrak['login_id'],
                        'tgl_review'    => date('Y-m-d'),
                        'nm_konsumen'   => htmlspecialchars($this->post('nm_konsumen')),
                        'hp_konsumen'   => htmlspecialchars($this->post('hp_konsumen')),
                        'rating'        => htmlspecialchars($this->post('rating')),
                        'kritik_saran'  => htmlspecialchars($this->post('kritik_saran'))
                    );
                    $this->db->insert('k_rkonsumen', $data_review);
                    $this->response([
                        'status'   => true,
                        'message' => 'Sukses Masukkan Reviews!'
                    ], RestController::HTTP_OK);
                endif;
            } else {
                $this->response([
                    'status' => false,
                    'message' => 'Kode ID Marketing Tidak Ada!'
                ], RestController::HTTP_NOT_FOUND);
            }
        }
    }


    // Update data
    // public function index_put()
    // {
    //     $id = $this->put('id');
    //     $data = [
    //         'nrp' => $this->put('nrp'),
    //         'nama' => $this->put('nama'),
    //         'email' => $this->put('email'),
    //         'jurusan' => $this->put('jurusan'),
    //     ];
    //     if ($this->mahasiswa->updateMahasiswa($data, $id) > 0) {
    //         $this->response([
    //             'status' => true,
    //             'message' => 'data mahasiswa has been updated'
    //         ], RestController::HTTP_OK);
    //     } else {
    //         $this->response([
    //             'status' => false,
    //             'message' => 'failed to update data!.'
    //         ], RestController::HTTP_BAD_REQUEST);
    //     }
    // }
}

/* End of file ControllerReviews.php and path \application\controllers\apikey\ControllerReviews.php */