<?php

namespace Application\Form\item;

use Application\Form\FieldForm;
use Application\Service\DB\Dictionary\FormField;
use Application\Service\Exceptions\InvalidArgumentException;
use Application\Service\Extension\Laminas;

class ItemFieldForm extends FieldForm
{
    protected string $prefix = 'item';

    protected function getForm()
    {
        return Laminas::$serviceManager->itemForm;
    }

    protected function getFormField()
    {
        return Laminas::$serviceManager->itemFormField;
    }

    protected function getFormVersion()
    {
        return Laminas::$serviceManager->itemFormVersion;
    }

    protected function getFormFieldText()
    {
        return Laminas::$serviceManager->itemFormFieldText;
    }

    protected function getFormFieldRadio()
    {
        return Laminas::$serviceManager->itemFormFieldRadio;
    }

    public function getFieldConfig(string $validatorName = 'ItemFormField')
    {
        return parent::getFieldConfig($validatorName); // TODO: Change the autogenerated stub
    }

    protected function getFormRelation()
    {
        return Laminas::$serviceManager->itemFormRelation;
    }

    /**
     * 验证是否更改了表单属性类型
     * @param $value
     */
    public function invalidEditType($value)
    {

    }

    public function validMax(&$value)
    {
        $min = $_POST['min'] ?? '';

        if ($value < $min) {
            throw new InvalidArgumentException('最大值不能小于最小值');
        }

        return null;
    }

    /**
     * 创建项目表单字段
     * @param array $field
     * @return int
     */
    public function createField(array $field = [],$type = 0): int
    {
        if(!$field){
            $field = [
                'form_id' => $this->validator->attributes['form_id'],
                'name' => $this->validator->attributes['name'],
                'parent' => $this->validator->attributes['parent'],
                'type' => $this->validator->attributes['form_type'] == FormField::FORM_CONFIG_RADIO ? 0 : $this->validator->attributes['type'], // 0 是选项
                'is_list' => $this->validator->attributes['is_list'] ?? 0,
                'dicform_id' => $this->validator->attributes['dicform_id'] ?? 0,
                'order' => $this->validator->attributes['order'],
                'rules' => $this->getRules(),
                'version' => 'v1.0',
                'is_anchor' => $this->validator->attributes['is_anchor'] ?? 0,
                'is_booked_time' => $this->validator->attributes['is_booked_time'] ?? 0,
                'item_id' => $this->validator->attributes['item_id'],
                'var_name' => $this->validator->attributes['var_name'],
                'default' => $this->getDefault() ?: '',
                'unit' => $this->validator->attributes['unit'] ?? '',
                'is_hyperwindow' => call_user_func(function () {
                    $val = $this->validator->attributes['is_hyperwindow'] ?? 0;
                    if ($val && !$this->validator->attributes['is_anchor']) {
                        throw new InvalidArgumentException('必须是基点日期才能设置超窗判断');
                    }
                    return $val;
                }),
                'english' => $this->validator->attributes['english'] ?? '',
                'cut_pre' => $this->validator->attributes['cut_pre'] ?? '',
                'cut_after' => $this->validator->attributes['cut_after'] ?? '',
                'cut_start' => $this->validator->attributes['cut_start'] ?? '',
                'cut_length' => $this->validator->attributes['cut_length'] ?? '',
                'is_csae' => $this->validator->attributes['is_csae'] ?? 0,
                'medical_type' => $this->validator->attributes['medical_type'] ?? '',
                'checktime_id' => $this->validator->attributes['checktime_id'] ?? '',
                'checkname_id' => $this->validator->attributes['checkname_id'] ?? '',
                'form_field_id' => $this->validator->attributes['form_field_id'] ?? '',
                'default_field_id' => $this->validator->attributes['default_field_id'] ?? '',
                'default_field_type' => $this->validator->attributes['default_field_type'] ?? '',
                'is_item_form_source' => $this->validator->attributes['is_item_form_source'] ?? '',
                'item_form_source_set' => $this->validator->attributes['item_form_source_set'] ?? '',
                'formchecktime_id' => $this->validator->attributes['formchecktime_id'] ?? '',
                'is_hide' => $this->validator->attributes['is_hide']?? 0,   //是否隐私字段
                'is_export' => $this->validator->attributes['is_export']?? 1,    //是否导出
                'remind_type' => $this->validator->attributes['remind_type']?? 0   //是否导出
            ];
        }else{
            $field['parent']         = $field['parent'] ?: 0;
            $field['type']           = $field['type'] ?: 0;
            $field['is_list']        = $field['is_list'] ?: 0;
            $field['is_export']      = $field['is_export'] ?: 1;
            $field['is_hide']        = $field['is_hide'] ?: 0;
            $field['dicform_id']     = $field['dicform_id'] ?: 0;
            $field['order']          = $field['order'] ?: 0;
            $field['is_anchor']      = $field['is_anchor'] ?: 0;
            $field['is_booked_time'] = $field['is_booked_time'] ?: 0;
            $field['version']        = $field['version'] ?: 'v1.0';
            $field['form_type']      = $field['form_type'];

        }

        if($type == 1){
            return parent::createField($field,1);
        }else{
            return parent::createField($field);
        }


    }

    public function getRuleConfig(){
        return $this->getRules();
    }
}
