<?php

namespace App\Controllers\Configuracion;

use App\Controllers\BaseController;
use App\Models\Configuracion\MaquinaTareaModel;
use CodeIgniter\HTTP\Response;
use Hermawan\DataTables\DataTable;
use CodeIgniter\I18n\Time;

class MaquinaTarea extends BaseController
{

    protected MaquinaTareaModel $maquinaTareaModel;
    protected $format = 'json';
    protected array $viewData = [];


    protected static $viewPath = 'themes/vuexy/form/configuracion/maquina_tareas/';
    protected static $controllerSlug = "maquina-tareas";
    protected $indexRoute = 'viewMaquinaTarea';
    protected $editRoute = 'editMaquinaTarea';



    public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->maquinaTareaModel = model(MaquinaTareaModel::class);
    }


    public function index()
    {
        $this->viewData['breadcrumb'] = [
            ['title' => lang("App.menu_configuration"), 'route' => "javascript:void(0);", 'active' => false],
            ['title' => lang("App.menu_maquina_tareas"), 'route' => site_url('configuracion/maquina-tareas'), 'active' => true]
        ];
        return view(static::$viewPath . $this->indexRoute, $this->viewData);
    }
    public function viewForm(int $maquina_tarea_id)
    {
        $maquinaTarea = $this->maquinaTareaModel->find($maquina_tarea_id);
        $this->viewData['breadcrumb'] = [
            ['title' => lang("App.menu_configuration"), 'route' => "javascript:void(0);", 'active' => false],
            ['title' => lang("App.menu_maquina_tareas"), 'route' => site_url('configuracion/maquina-tareas'), 'active' => false],
            ['title' => $maquinaTarea->name, 'route' => site_url('configuracion/maquina-tareas/edit/' . $maquina_tarea_id), 'active' => true]
        ];
        $this->viewData["model"] = $maquinaTarea;

        return view(static::$viewPath . $this->editRoute, $this->viewData);
    }
    public function show(int $id)
    {
        $data = $this->maquinaTareaModel->find($id);
        return $this->response->setJSON($data);
    }
    public function update_servicio_cliente(int $id)
    {
        $data = $this->request->getPost();
        $status = $this->maquinaTareaModel->update($id, [
            "name" => $data["name"],
            "description" => $data["description"]
        ]);

        return $this->response->setJSON(["message" => lang("App.global_alert_save_success"), "status" => $status]);
    }
    public function store()
    {
        $bodyData = $this->request->getPost();
        $r = $this->maquinaTareaModel->insert($bodyData);
        return $this->response->setJSON(["message" => lang("App.global_alert_save_success"), "status" => $r,"data" => $bodyData]);
    }
    public function delete(int $maquina_tarea_id){
        $r = $this->maquinaTareaModel->delete($maquina_tarea_id);
        return $this->response->setJSON(["message" => lang("App.user_alert_delete"), "status" => $r]);
    }
    public function datatable()
    {
        $query = $this->maquinaTareaModel->getQueryDatatable()->orderBy("created_at", "DESC");
        return DataTable::of($query)
            ->edit('created_at', fn($q) => $q->created_at ? Time::createFromFormat('Y-m-d H:i:s', $q->created_at)->format("d/m/Y H:i") : "")
            ->add("action", fn($q) => $q->id)
            ->toJson(true);
    }
}
