<?php

namespace Application\Service\Extension\CheckLogic;

use Application\Service\DB\AbstractDb;
use Application\Service\Extension\Helper\DataHelper;
use Application\Service\Extension\Helper\StringHelper;
use Application\Service\Extension\Laminas;

/**
 * @property
 */
abstract class BaseContentProvider
{
    public bool $isSkipEmptyException = false;

    public static array $cache = [];

    protected string $formVarName = '';

    protected string $fieldVarName = '';

    protected string $checkTimeId = '';

    protected ?ExpressionGenerator $expressionGenerator = null;

    protected int $itemId = 0;

    protected int $formId = 0;

    public function __construct(array $config)
    {
        list($this->checkTimeId, $this->formVarName, $this->fieldVarName) = $config;
    }

    abstract function get();

    protected function getFormData()
    {
        if (!isset(self::$cache['form'][$this->formVarName])) {
            self::$cache['form'][$this->formVarName] = Laminas::$serviceManager->itemForm->fetchOne([
                'where' => [
                    'var_name' => $this->formVarName,
                    'is_del' => 0,
                    'item_id' => $this->getItemId()
                ]
            ]);
        }

        return self::$cache['form'][$this->formVarName];
    }

    /**
     * @return ExpressionGenerator
     */
    private function getGenerator(): ExpressionGenerator
    {
        return $GLOBALS['generator'];
    }

    protected function getFieldData()
    {
        if (!isset(self::$cache['field'][$this->fieldVarName])) {
            self::$cache['field'][$this->fieldVarName] = Laminas::$serviceManager->itemFormField->fetchOne([
                    'where' => [
                        'item_id' => $this->getItemId(),
                        'var_name' => $this->fieldVarName,
                        'form_id' => $this->getFormId()
                    ]
                ]) ?? false;
        }

        return self::$cache['field'][$this->fieldVarName];
    }

    protected function getFieldType()
    {
        return $this->getFieldData()['type'];
    }

    protected function getFieldId()
    {
        return $this->getFieldData()['id'];
    }

    protected function getFormId()
    {
        return $this->getFormData()['id'];
    }

    protected function getFormGroupId()
    {
        return $this->getFormData()['group_id'];
    }

    /**
     * @return int|null
     */
    protected function getItemId(): ?int
    {
        return $this->getGenerator()->itemId;
    }

    protected function getPatientId()
    {
        return $GLOBALS['patientId'];
    }

    protected function getMultiId()
    {
        return $GLOBALS['multiId'];
    }


    protected function getContent(string $tableName, array $condition)
    {
//        var_dump($condition);
        /** @var AbstractDb $db */
        $db = Laminas::$serviceManager->{$tableName};
        $query = $db->fetchOne([
            'columns' => ['data'],
            'where' => $condition
        ]);

        if (!$query) {
            throw new NotWriteCheckTimeException('表单暂未填写');
        }

        return StringHelper::jsonDecode($query['data']);
    }

    protected function getVal(array $content, $fieldId)
    {
        foreach ($content as $field => $value) {
            if (stripos($field, '-') !== false) {
                $field = DataHelper::getFieldId($field);
            }

            if ($field == $fieldId) {
                return $value;
            }
        }

        return '';
    }
}