<?php

namespace Application\Service\Extension\Middleware\components;

use Application\Form\item\patient\PatientFormModel;
use Application\Service\DB\Dictionary\FormGroup;
use Application\Service\Exceptions\InvalidArgumentException;
use Application\Service\Extension\Helper\SDMHelper;
use Application\Service\Extension\Laminas;
use Application\Service\Extension\Middleware\BaseMiddlewareComponent;

/**
 * @property PatientFormModel $owner
 */
class ValidIsAnnexLock extends BaseMiddlewareComponent
{

    function execute()
    {
        // 导入不判断
        if ($this->owner->scenario === PatientFormModel::SCENARIO_EXCEL_IMPORT) {
            return ;
        }

        if (!in_array(SDMHelper::app()->setAttributes(['form_id' => $this->owner->formId])->form->getGroupId(), [FormGroup::CHECK_OCR, FormGroup::NORMAL_OCR])) {
            return;
        }

        if ($this->owner->getAnnexLock()) {

            // 1不能 2可以
            $canRepair = Laminas::$serviceManager->itemIdentificationresultchange->checKRepairImg([
                'item_id' => $this->owner->validator->attributes['item_id'],
                'checktime_id' => $this->owner->validator->attributes['checktime_id'],
                'patient_form_id' => $this->h()->setAttributes(['patient_id' => $this->owner->patientId])->patient->getPatientFormId($this->owner->formId, $this->owner->checktimeId),
            ]);

            if ($canRepair == 1) {
                throw new InvalidArgumentException("表单数据完整，如需修改请申请解锁。");
            }

            $this->owner->isReplacementOrder = true;
        }
    }
}