<?php

namespace App\Controllers\Panel\AttendanceSolution;

use App\Controllers\Panel\BC;
use CodeIgniter\API\ResponseTrait;

class Device extends BC
{
    use ResponseTrait;
    public function __construct()
    {
        parent::__construct();
        helper('AttendanceSolution');
    }
    public function index()
    {
        $data['page_title'] = "Attendance Solution";
        return view('Panel/AttendanceSolution/device', $data);
    }
    public function all()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $is_active = $this->request->getVar('is_active');
        $db = db_connect();
        $devices = $db->table('attendance_solution_device');
        if (in_array($is_active, ['0', '1'])) $devices->where('is_active', $is_active);
        $devices = $devices->get()->getResultArray();
        return $this->respond(['data' => $devices]);
    }
    public function detail($id)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data = db_connect()->table('attendance_solution_device')->getWhere(['id' => $id])->getRowArray();
        if (!$data) return $this->failNotFound("Perangkat tidak ditemukan");
        return $this->respond($data);
    }

    public function add()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data['ip'] = $this->request->getVar('ip');
        $data['nama'] = $this->request->getVar('nama');
        $data['port'] = $this->request->getVar('port');
        $data['commKey'] = $this->request->getVar("commKey");
        $data['is_active'] = $this->request->getVar("is_active");
        $response = getDataAttendance($data['ip'], $data['port']);
        $data['is_connected'] = $response['status'] == 1 ? '1' : '0';
        $db = db_connect();
        $db->table('attendance_solution_device')->insert($data);
        setLog("Menambah perangkat $data[nama]");
        return $this->respond(['message' => "Berhasil menambah perangkat"]);
    }
    public function edit($id)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $db = db_connect();
        $device = $db->table('attendance_solution_device')->getWhere(['id' => $id])->getRowArray();
        if (!$device) return $this->failNotFound('Data tidak ditemukan');
        $data['ip'] = $this->request->getVar('ip');
        $data['nama'] = $this->request->getVar('nama');
        $data['port'] = $this->request->getVar('port');
        $data['commKey'] = $this->request->getVar("commKey");
        $data['is_active'] = $this->request->getVar("is_active");
        $response = getDataAttendance($data['ip'], $data['port']);
        $data['is_connected'] = $response['status'] == 1 ? '1' : '0';
        $db->table('attendance_solution_device')->update($data, ['id' => $id]);
        setLog("Mengubah perangkat $data[nama]");
        return $this->respond(['message' => "Berhasil mengubah perangkat"]);
    }
    public function delete($id)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $db =  db_connect();
        $data = $db->table('attendance_solution_device')->where('id', $id)->get()->getRowArray();
        $db->table('attendance_solution_device')->delete(['id' => $id]);
        if ($db->affectedRows() == 0) return $this->fail("Gagal menghapus perangkat");
        setLog("Menghapus perangkat $data[nama]");
        return $this->respond(['message' => "Berhasil menghapus perangkat"]);
    }
    public function checkConnection($id)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $db = db_connect();
        $device = $db->table('attendance_solution_device')->getWhere(['id' => $id])->getRowArray();
        if (!$device) return $this->failNotFound('Data tidak ditemukan');
        $response = getDataAttendance($device['ip'], $device['port']);
        $data['is_connected'] = $response['status'] == 1 ? '1' : '0';
        $db->table('attendance_solution_device')->set($data)->where('id', $id)->update();
        if ($data['is_connected'] == '1') return $this->respond(['message' => "Terhubung"]);
        return $this->respond(['message' => "Tidak Terhubung"]);
    }
}
