<?php

namespace App\Controllers;

use App\Models\Tingkat_model;


class Tingkat extends BaseController
{
    public function __construct()
    {
        $this->Tingkat_model = new Tingkat_model();
    }
    private $Tingkat_model;
    public function index()
    {
        // $ceklogin = cekLogin();
        // if ($ceklogin) return $ceklogin;
        // $user = getUserInfo();
        $setting = $this->appSetting();
        $data = [
            // 'nama_user' => $user->nama,
            'setting' => $setting,
            'title' => 'Daftar Tingkat',
        ];
        return view('Admin/Referensi/tingkat', $data);
    }
    public function getData()
    {
        $exec = $this->Tingkat_model->get_all_tingkat();
        $data['data'] = $exec;
        return $this->response->setJSON($data);
    }


    public function add()
    {
        $validationRules = [
            'tingkat' => 'required',
        ];

        if ($this->request->isAJAX()) {
            $response = ['success' => false, 'errors' => []];
            if ($this->validate($validationRules)) {
                $response['success'] = true;
                $data = array(

                    'tingkat' => $this->request->getPost('tingkat'),
                    'status' => $this->request->getPost('status'),
                );
                $cek = $this->Tingkat_model->get_where(['tingkat' => $this->request->getPost('tingkat')]);
                if (!$cek) {
                    $insert = $this->Tingkat_model->tingkat_add($data);
                    if ($insert) {
                        return $this->response->setJSON(['success' => true, 'message' => 'Tingkat Add successfully']);
                    } else {
                        return $this->response->setStatusCode(404)->setJSON(['success' => false, 'message' => 'Tingkat not found']);
                    }
                } else {
                    return $this->response->setJSON(['success' => true, 'message' => 'Tingkat sudah ada']);
                }
            } else {
                $response['errors'] = $this->validator->getErrors();
            }
            return $this->response->setJSON($response);
        }
    }

    public function edit($id)
    {
        $data = $this->Tingkat_model->get_by_id($id);
        echo json_encode($data);
    }

    public function update()
    {
        $data = array(

            'tingkat' => $this->request->getPost('tingkat'),
            'status' => $this->request->getPost('status'),
        );


        $exec = $this->Tingkat_model->tingkat_update(['id' => $this->request->getPost('id')], $data);
        // dd($error);
        if ($exec) {
            return $this->response->setJSON(['success' => true, 'message' => 'User update successfully']);
        } else {
            //return $this->response->setJSON($error);
            return $this->response->setJSON(['success' => false, 'message' => 'Tidak ada data yang mengalami perubahan']);
        }
    }

    public function delete($id)
    {
        $user = $this->Tingkat_model->find($id);

        if ($user) {
            $this->Tingkat_model->delete($id);
            return $this->response->setJSON(['success' => true, 'message' => 'User deleted successfully']);
        } else {
            return $this->response->setStatusCode(404)->setJSON(['success' => false, 'message' => 'User not found']);
        }
    }
}
