<?php

namespace App\Controllers\Panel\AttendanceSolution;

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

class Assign extends BC
{
    use ResponseTrait;
    public function __construct()
    {
        parent::__construct();
        helper('AttendanceSolution');
    }
    public function index()
    {
        $data['page_title'] = "Attendance Solution";
        return view('Panel/AttendanceSolution/assign', $data);
    }
    public function sinkronData()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        helper("AttendanceSolution");
        $id = $this->request->getVar('id');
        $name = $this->request->getVar('name');
        $devices = $this->request->getVar('devices');
        foreach ($devices as $device) {
            $response = addUserSolution($device->ip, $device->port, $device->commKey, $id, $name);
            if ($response['status'] != '1') return $this->fail(['message' => "Gagal : \"$response[message]\""]);
        }
        setLog("Sinkron data ke mesin presensi $name");
        return $this->respond(['message' => "Berhasil mengirim data"]);
    }
    public function users()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $db = db_connect();
        $ms = $db->table('m_siswa ms');
        $ms->select("ms.nama fullname,mk.nama nama_kelas,ms.attendance_solution_id,'2' utype");
        $ms->join('m_kelas mk', 'mk.id=ms.kelas_id', 'left');
        $ms = $ms->getWhere('ms.attendance_solution_id !=', null)->getResultArray();
        $u = $db->table('users u');
        $u->select("u.fullname,mk.nama nama_kelas,u.attendance_solution_id,'1' utype");
        $u->join('m_kelas mk', 'mk.id=u.kelas_id', 'left');
        $u = $u->getWhere('u.attendance_solution_id !=', null)->getResultArray();
        $data = [...$u, ...$ms];
        return $this->respond(['data' => $data]);
    }
    // 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);
    //     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]);
    //     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();
    //     $db->table('attendance_solution_device')->delete(['id' => $id]);
    //     if ($db->affectedRows() > 0) return $this->respond(['message' => "Berhasil menghapus perangkat"]);
    //     return $this->fail("Gagal 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"]);
    // }
}
