<?php

namespace App\Helpers\Customer;

use App\Models\Customer\ServiceItemModel;
use CodeIgniter\HTTP\IncomingRequest;

class ServiceItemHelper extends CustomerHelper
{
    protected ?IncomingRequest $request = null;
    public function __construct(?IncomingRequest $request = null)
    {
        parent::__construct($request);
        $this->setTitleField(field: ServiceItemModel::TITLE);
    }

    //ItemType에 따른 조건부 추가 Index Page
    public function getFieldFormByItemType(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        $form = "";
        if (in_array($viewDatas['action'], ['create', 'modify', 'create_form', 'modify_form'])) {
            $extras = (strpos($viewDatas['field_rules'][$field], 'required') !== false) ? ["class" => "form-control", "required" => "", ...$extras] : ["class" => "form-control", ...$extras];
        }
        switch ($viewDatas['item_type']) {
            case 'DOMAIN':
                if (in_array($viewDatas['action'], ['create', 'modify', 'create_form', 'modify_form'])) {
                    $form = form_input($field, $value ?? "", ["placeholder" => "예)example.com", ...$extras]);
                } else {
                    $form = parent::getFieldForm($field, $value, $viewDatas, $extras);
                }
                break;
            default:
                $form = parent::getFieldForm($field, $value, $viewDatas, $extras);
                break;
        }
        return $form;
    }
    public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        if (in_array($viewDatas['action'], ['create', 'modify', 'create_form', 'modify_form'])) {
            $extras = (strpos($viewDatas['field_rules'][$field], 'required') !== false) ? ["class" => "form-control", "required" => "", ...$extras] : ["class" => "form-control", ...$extras];
        }
        switch ($field) {
            case 'item_uid':
                $form = $this->getFieldFormByItemType($field, $value, $viewDatas, $extras);
                break;
            default:
                $form = parent::getFieldForm($field, $value, $viewDatas, $extras);
                break;
        }
        return $form;
    }
}
