<?php

namespace Application\Service\Extension\Middleware\components;

use Application\Form\item\patient\PatientFormModel;
use Application\Service\DB\Dictionary\FormField;
use Application\Service\Extension\Helper\DataHelper;
use Application\Service\Extension\Helper\SDMHelper;
use Application\Service\Extension\Laminas;
use Application\Service\Extension\Middleware\BaseMiddlewareComponent;

class HandleAutoReplyQuestion extends BaseMiddlewareComponent
{

    /** @var PatientFormModel */
    public $owner;

    // 【SDM】图片质疑，在原表单直接传图而非通过回复质疑时来传图的情况，系统会自动补充一条该表单对应质疑的回复且将该条质疑转至“待确认”入口。 －－胡
    //https://e.gitee.com/yikeen_project/issues/table?issue=IBB2HJ
    function execute()
    {
        $version = SDMHelper::app()->form->getVersionData($this->owner->formId);
        foreach ($this->owner->postData as $fieldId => $v) {
            if (DataHelper::isField($fieldId) && $field = DataHelper::getFieldId($fieldId)) {
                if (isset($version[$field]['type']) && in_array($version[$field]['type'], [FormField::FORM_FIELD_TYPE_UPLOAD_IMAGE, FormField::FORM_FIELD_TYPE_UPLOAD_SHARE_IMAGE])) {

                    // 图片变更了再自动回复质疑
                    if (!isset($this->owner->logger->changeData[$fieldId])) {
                        continue;
                    }
                    // 自动回质疑
                    $question = Laminas::$serviceManager->itemQuestion->fetchAll([
                        'where' => [
                            'form_id' => $this->owner->formId,
                            'patient_id' => $this->owner->patientId,
                            'checktime_id' => $this->owner->checktimeId,
                            'question_type' => 1, // 表单质疑
                            'is_del' => 0,
                            'question_status' => 0 // 待确认的
                        ]
                    ]);

                    foreach ($question as $item) {
                        // 更改质疑的状态
                        Laminas::$serviceManager->itemQuestion->update([
                            'question_status' => 1 // 待确认
                        ], ['id' => $item['id']]);

                        // 构建回复数据
                        Laminas::$serviceManager->itemReply->save([
                          'question_id' => $item['id'],
                          'reply_status' => 0,
                          'remarks' => json_encode([
                              'form' => $this->_generateRemarks(DataHelper::getFieldId($field)),
                              'formData' => [
                                  $field => $v,
                              ],
                              'remarks' => '原始数据已更新'
                          ]),
                          'annex_aligned' => 3,
                          'completed_id' => Laminas::$serviceManager->identity->getId(),
                          'completed_time' => time(),
                          'completed_type' => 1,
                          'is_del' => 0,
                          'create_user_id' => Laminas::$serviceManager->identity->getId(),
                          'create_time' => time(),
                          'update_user_id' => 0,
                          'update_time' => 0,
                        ]);
                    }
                }
            }
        }
    }

    private function _generateRemarks($fieldId)
    {
        $res = [];

//        $formData = Laminas::$serviceManager->itemFormVersion->getField(['form_id' => $this->owner->formId], true);
//        if (isset($formData[$fieldId])) {
//            $config = Laminas::$serviceManager->itemForm->buildConfig($formData[$fieldId]);
//            $res[] = $config;
//        }

        return $res ?? [];
    }
}