<?php

namespace Application\Service\Extension\Middleware\components;

use Application\Common\EventEnumV2;
use Application\Service\DB\Dictionary\Form;
use Application\Service\DB\Dictionary\FormField;
use Application\Service\DB\Dictionary\FormGroup;
use Application\Service\Exceptions\InvalidArgumentException;
use Application\Service\Extension\Helper\ArrayHelper;
use Application\Service\Extension\Helper\SDMHelper;
use Application\Service\Extension\Helper\StringHelper;
use Application\Service\Extension\Middleware\BaseMiddlewareComponent;

/**
 * 对表单能否提交做处理
 */
class ValidPermission extends BaseMiddlewareComponent
{
    public $isError = 0;

    public $errors = [];

    function execute($isError = 0)
    {
        $this->isError = $isError;
        // 通过逻辑核查回复修改数据不做判断。
        if ($this->owner->event == EventEnumV2::getVal(EventEnumV2::EVENT_FORM_CONTENT_EDIT_BY_LOGIC_REPLY)) {
            if($this->isError == 1) {
                return [];
            }else{
                return;
            }
        }

        // 常规识别一对多的不让提交
        if ($this->owner->isEditFormByQuestion === false && $this->h()->form->getType($this->owner->formId) == Form::FORM_TYPE_MULTI && $this->h()->form->getGroupId($this->owner->formId) == FormGroup::NORMAL_OCR) {
            if ($this->h()->formContent->getIsLock($this->owner->contentPrimaryKey)) {
                if (SDMHelper::app()->form->getIdentificationEdit($this->owner->formId) == 0) {
                    if($this->isError == 1){
                        $this->errors[] = '常规识别类表单无法提交。';
                    }else{
                        throw new InvalidArgumentException('常规识别类表单无法提交。');
                    }
                }
            }
        } else {
            // 常规识别的, 锁定了不让提交 [已废弃]
            // 如果表单配置了锁定后可以提交, 就允许提交。
            if (SDMHelper::app()->setAttributes(['form_id' => $this->owner->formId])->form->getGroupId() == FormGroup::NORMAL_OCR) {
                if ($this->h()->formContent->getIsLock($this->owner->contentPrimaryKey)) {
                    if (SDMHelper::app()->form->getIdentificationEdit($this->owner->formId) == 0) {
                        if($this->isError == 1){
                            $this->errors[] = "已锁定。";
                        }else{
                            throw new InvalidArgumentException("已锁定。");
                        }
                    } else {
                        // 如果可以编辑, 先看看图片是否有修改。有修改就不让提
                        $versionData = SDMHelper::app()->form->getVersionData($this->owner->formId);
                        $shareFormFieldId = ArrayHelper::index($versionData, 'type')[FormField::FORM_FIELD_TYPE_UPLOAD_SHARE_IMAGE]['id'] ?? null;
                        if ($shareFormFieldId === null) {
                            if($this->isError == 1){
                                $this->errors[] = '获取共享图片字段失败。';
                            }else{
                                throw new InvalidArgumentException('获取共享图片字段失败。');
                            }
                        }

                        $images = SDMHelper::app()->form->getShareFormImages($this->owner->formId, $this->owner->patientId, $this->owner->checktimeId);
                        $postImages = StringHelper::jsonDecode($this->owner->postData[$shareFormFieldId], true);
                        if (count($images) !== count($postImages) || ArrayHelper::diff($images, $postImages)) {
                            if($this->isError == 1){
                                $this->errors[] = '修改图片请解锁。';
                            }else{
                                throw new InvalidArgumentException('修改图片请解锁。');
                            }
                        }
                    }
                }
            }
        }
        if($isError == 1) return $this->errors;
    }
}