<?php
namespace App\Controllers;

use App\Models\StaffModel;
use CodeIgniter\RESTful\ResourceController;

class StaffController extends ResourceController
{
    protected $staffModel;

    public function __construct()
    {
        $this->staffModel = new StaffModel();
    }

    public function getStaffMembers()
    {
        try {
            $staff = $this->staffModel
                ->select('id, name, current_jobs')
                ->orderBy('id', 'ASC')
                ->findAll();

            if (empty($staff)) {
                return $this->response->setJSON([
                    'status' => 'error',
                    'message' => 'ไม่มีข้อมูลพนักงาน'
                ]);
            }

            return $this->response->setJSON([
                'status' => 'success',
                'data' => $staff
            ]);

        } catch (\Exception $e) {
            return $this->response->setJSON([
                'status' => 'error',
                'message' => 'เกิดข้อผิดพลาด: ' . $e->getMessage()
            ]);
        }
    }

    public function resetJobs()
{
    try {
        // อัปเดต current_jobs ให้เป็น 0 สำหรับพนักงานทุกคน
        $this->staffModel->update(null, ['current_jobs' => 0]);

        return $this->response->setJSON([
            'status' => 'success',
            'message' => 'รีเซ็ต current_jobs สำเร็จ'
        ]);
    } catch (\Exception $e) {
        return $this->response->setJSON([
            'status' => 'error',
            'message' => 'เกิดข้อผิดพลาด: ' . $e->getMessage()
        ]);
    }
}

}
