<?php

namespace Application\Service\Extension\Middleware\components;

use Application\Common\EventEnum;
use Application\Form\item\patient\PatientFormModel;
use Application\Service\DB\Dictionary\FormGroup;
use Application\Service\Extension\Helper\DataHelper;
use Application\Service\Extension\Helper\EdcHelper;
use Application\Service\Extension\Helper\SDMHelper;
use Application\Service\Extension\Laminas;
use Application\Service\Extension\Middleware\BaseMiddlewareComponent;
use Application\Service\Extension\ValidatorV2\RulesContainer;

/**
 * 自动发送质疑 (#IALS0M)
 */
class HandleLogic extends BaseMiddlewareComponent
{
    /** @var PatientFormModel $owner */
    public $owner;

    function execute()
    {
        if (!EdcHelper::isEdc(SDMHelper::app()->item->getFid($this->owner->itemId))) {
            return;
        }

        if (SDMHelper::app()->form->getGroupId($this->owner->formId) == FormGroup::NORMAL_OCR) {
            $editMode = SDMHelper::app()->form->getIdentificationEdit($this->owner->formId);
            // 不允许编辑, 只有在锁定的时候才会发质疑
            if ($editMode == 0) {
                if ($this->owner->event != EventEnum::EVENT_LOCK_SHARE_FORM_CONTENT) {
                    return;
                }
            }
        }

        $rules = SDMHelper::app()->form->getVersionData($this->owner->formId);
        $requiredField = [];
        foreach ($rules as $rule) {
            /** @var RulesContainer $rulesContainer */
            $rulesContainer = unserialize($rule['rules']);
            foreach (is_array($rulesContainer->originRules) ? $rulesContainer->originRules : [] as $originRuleName => $val) {
                if ($originRuleName == 'v-required' && $val == 1) {
                    // 必填的不写要发质疑
                    $requiredField[] = $rule['id'];
                }
            }
        }

        foreach ($this->owner->postData as $originFieldId => $postVal) {
            $fieldId = DataHelper::getFieldId($originFieldId);
            if (in_array($fieldId, $requiredField)) {
                $fieldName = SDMHelper::app()->form->field->getName($fieldId);
                $message = "`{$fieldName}` 为空, 请确认";
                $query = Laminas::$serviceManager->itemPatientFormLogicError->fetchOne([
                    'where' => ['expression_note' => $message, 'is_del' => 0, 'patient_id' => $this->owner->patientId, 'checktime_id' => $this->owner->checktimeId, 'form_id' => $this->owner->formId, 'multi_id'=>$this->owner->contentPrimaryKey]
                ]);

                // 如果是空值, 并且没有发起过质疑就自动质疑
                if ($postVal === '') {
                    if (!$query) {
                        SDMHelper::app()->logic->setError($originFieldId, $this->owner->contentPrimaryKey, $message);
                    }
                } else {
                    // 如果有质疑, 并且写值了 就关闭掉
                    if ($query && $this->owner->contentPrimaryKey) {
                        SDMHelper::app()->logic->closeErrorByErrorId($query['id'], '数据更新');
                    }
                }
            }
        }
    }
}