<?php

namespace Application\Service\Extension\Form;

use Application\Service\DB\Dictionary\ItemForm;
use Application\Service\DB\Dictionary\ItemFormField;
use Application\Service\DB\Item\Info;
use Application\Service\DB\Item\PatientForm;
use Application\Service\DB\Item\PatientFormContentImg;
use Application\Service\Extension\Event;
use Application\Service\Extension\Helper\ArrayHelper;
use Application\Service\Extension\Laminas;
use Application\Service\Extension\Validator\ValidatorApplication;
use Laminas\Stdlib\ArrayUtils;

/**
 * @property ItemForm|null $itemForm table [yikeen_item_form]
 * @property ItemFormField|null $itemFormField table [yikeen_item_form_field]
 * @property Info|null $itemInfo table [yikeen_item_info]
 * @property PatientFormContentImg|null $itemPatientFormContentImg table [yikeen_item_patient_form_content_img]
 * @property int $formId // post['form_id']
 * @property int $id // post['id']
 * @property int $itemId // post['item_id']
 * @property int $patientId // post['patient_id']
 * @property int $checktimeId // post['checktime_id']
 * @property int $itemsigId // post['itemsig_id']
 * @property string $data // post['data']
 * @property string $editReason // post['edit_reason']
 */
class BaseForm
{
    public ?string $event = null;

    private array $_attr = [];

    /**
     * 表单数据
     * @var array|null
     */
    private $_dataCollection = [];

    /**
     * 验证器对象
     * @var ValidatorApplication|null
     */
    protected ?ValidatorApplication $validator = null;

    public function __construct(ValidatorApplication $validator = null)
    {
        // 兼容性处理
//        $this->validator = $validator;
        if ($validator) {
            $this->setAttributes($validator->attributes);
        } else {
            $this->validator = new ValidatorApplication();
        }
    }

    public function setAttributes($attributes)
    {
        $this->validator = new ValidatorApplication($attributes);
        foreach ($attributes as $attribute => $val) {
            $exStr = explode('_', $attribute);
            $firstWord = array_shift($exStr);
            if ($exStr) {
                foreach ($exStr as &$str) {
                    $str = ucfirst($str);
                }
            }

            $key = $firstWord . implode('', $exStr);
            $this->_attr[$key] = $val;
        }
    }

    /**
     * 获取当前操作的表单数据
     * @param int $formId
     * @return array
     */
    public function getItemFormData(int $formId = 0)
    {
        if (!isset($this->_dataCollection[__FUNCTION__])) {
            $this->_dataCollection[__FUNCTION__] = $this->toObject(Laminas::$serviceManager->itemForm->memoryCache()->fetchOne([
                'where' => ['id' => $formId ?: $this->formId]
            ]) ?: []);
        }

        return $this->_dataCollection[__FUNCTION__] ?: [];
    }


    public function getTest(string $methodName)
    {
        return $this->{"_{$methodName}"}();
    }

    public function __get($name)
    {
        $methodName = "get" . ucfirst($name);
        if (method_exists($this, $methodName) === true) {
            return $this->{$methodName}();
        }

        if (!isset($this->_attr[$name])) {
            return null;
        }

        return $this->_attr[$name];
    }

    public function toObject($arr)
    {
        return new \ArrayObject($arr, \ArrayObject::ARRAY_AS_PROPS);
    }
}