<?php

namespace App\Helpers\Customer;

use App\Entities\Equipment\DomainEntity;
use App\Entities\Equipment\Part\IpEntity;
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
    private function getFieldFormByItemType(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        $form = "";
        if (in_array($viewDatas['control']['action'], ['create', 'modify', 'create_form', 'modify_form'])) {
            $extras = (strpos($viewDatas['control']['field_rules'][$field], 'required') !== false) ? ["class" => "form-control", "required" => "", ...$extras] : ["class" => "form-control", ...$extras];
        }
        $item_type = $viewDatas['item_type'];
        //Field는 item_uid이지만 , item_tpe에 따라 filter_options가 달라진다.
        switch ($item_type) {
            case 'DOMAIN':
                if (in_array($viewDatas['control']['action'], ['create', 'modify', 'create_form', 'modify_form'])) {
                    $form = form_input($field, $value ?? "", ["placeholder" => "예)example.com", ...$extras]);
                } else {
                    if (!is_array($viewDatas['control']['filter_optons'][$item_type])) {
                        throw new \Exception(__METHOD__ . "에서 {$item_type}의 field_options가 array형태가 아닙니다.");
                    }
                    //CodeIgniter 4의 form_dropdown()은 options 배열로만 값을 받기 때문에, disabled 속성 같은 개별 옵션에 대한 HTML 속성 추가는 기본적으로 지원하지 않습니다.
                    $form_temps = ["<select name=\"{$field}\" class=\"select-field\"" . (isset($extras['onChange']) ? " onChange=\"{$extras['onChange']}\"" : "") . ">"];
                    $form_temps[] = "<option value=\"\">" . lang($viewDatas['class_path'] . '.label.' . $item_type) . " 선택</option>";
                    foreach ($viewDatas['control']['filter_optons'][$item_type]  as $key => $filterEntity) {
                        $disabled = "";
                        if (!array_key_exists('index_content_top_filter', $extras)) {
                            $disabled = in_array($filterEntity->getStatus(), [DomainEntity::STATUS_OCCUPIED, DomainEntity::STATUS_FORBIDDEN]) ? 'disabled="disabled"' : '';
                        }
                        $selected = ($value === $key) ? 'selected="selected"' : '';
                        $form_temps[] = "<option value=\"{$key}\"{$selected} {$disabled}>{$filterEntity->getTitle()}</option>";
                    }
                    $form_temps[] = '</select>';
                    $form = implode("", $form_temps);
                }
                break;
            case "LINE":
            case "SERVER":
            case "CPU":
            case "RAM":
            case "STORAGE":
            case "SOFTWARE":
            case "DEFENCE":
                // dd($viewDatas['control']['filter_optons']);
                if (!is_array($viewDatas['control']['filter_optons'][$item_type])) {
                    throw new \Exception(__METHOD__ . "에서 {$item_type}의 field_options가 array형태가 아닙니다.");
                }
                //CodeIgniter 4의 form_dropdown()은 options 배열로만 값을 받기 때문에, disabled 속성 같은 개별 옵션에 대한 HTML 속성 추가는 기본적으로 지원하지 않습니다.
                $form_temps = ["<select name=\"{$field}\" class=\"select-field\"" . (isset($extras['onChange']) ? " onChange=\"{$extras['onChange']}\"" : "") . ">"];
                $form_temps[] = "<option value=\"\">" . lang($viewDatas['class_path'] . '.label.' . $item_type) . " 선택</option>";
                foreach ($viewDatas['control']['filter_optons'][$item_type]  as $key => $filterEntity) {
                    $disabled = '';
                    $selected = ($value === $key) ? 'selected="selected"' : '';
                    $form_temps[] = "<option value=\"{$key}\"{$selected} {$disabled}>{$filterEntity->getTitle()}</option>";
                }
                $form_temps[] = '</select>';
                $form = implode("", $form_temps);
                break;
            case "IP":
                if (!is_array($viewDatas['control']['filter_optons'][$item_type])) {
                    throw new \Exception(__METHOD__ . "에서 {$item_type}의 field_options가 array형태가 아닙니다.");
                }
                //CodeIgniter 4의 form_dropdown()은 options 배열로만 값을 받기 때문에, disabled 속성 같은 개별 옵션에 대한 HTML 속성 추가는 기본적으로 지원하지 않습니다.
                $form_temps = ["<select name=\"{$field}\" class=\"select-field\"" . (isset($extras['onChange']) ? " onChange=\"{$extras['onChange']}\"" : "") . ">"];
                $form_temps[] = "<option value=\"\">" . lang($viewDatas['class_path'] . '.label.' . $item_type) . " 선택</option>";
                foreach ($viewDatas['control']['filter_optons'][$item_type]  as $key => $filterEntity) {
                    $disabled = "";
                    if (!array_key_exists('index_content_top_filter', $extras)) {
                        $disabled = in_array($filterEntity->getStatus(), [IpEntity::STATUS_OCCUPIED, IpEntity::STATUS_FORBIDDEN]) ? 'disabled="disabled"' : '';
                    }
                    $selected = ($value === $key) ? 'selected="selected"' : '';
                    $form_temps[] = "<option value=\"{$key}\"{$selected} {$disabled}>{$filterEntity->getTitle()}</option>";
                }
                $form_temps[] = '</select>';
                $form = implode("", $form_temps);
                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['control']['action'], ['create', 'modify', 'create_form', 'modify_form'])) {
            $extras = (strpos($viewDatas['control']['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;
    }
}
