<?php

namespace Application\Form\item\patient;

use Application\Common\EventEnum;
use Application\Service\Extension\ErrorHandler;
use Application\Service\Extension\Helper\ArrayHelper;
use Application\Service\Extension\Helper\SDMHelper;
use Application\Service\Extension\Helper\StringHelper;
use Application\Service\Extension\Laminas;

class NormalOCRFormModel extends PatientFormModel
{
    /**
     *
     *
     * @param  int  $annexId
     * @param  int  $params 写1就会一直向一对多的表单内追加数据。跟田老师商量过的
     *
     * @throws \Exception
     */
    public function importMultiRow(int $annexId, int $params = 0)
    {
//        $trace = [];
//        $trace['annexId'] = $annexId;
        $query = Laminas::$serviceManager->itemPatientformimgcontent->fetchOne([
            'where' => ['annex_id' => $annexId, 'is_del' => 0]
        ]);
//        $trace['query'] = $query;
        if (!$query) {
            return;
        }

        $data = StringHelper::jsonDecode($query['table']);
        //每笔数据中字段值都无值的话则清除此笔信息
        if(!empty($data)){
            foreach($data as $dataForm=>$dataFormContents){
                $is_have_value = 0;
                foreach($dataFormContents as $dataFormContentKey=>$dataFormContentDatas){
                    $child_is_have_value = 0;
                    foreach($dataFormContentDatas as $dataFormContentData){
                        if($dataFormContentData['value'] != ''){
                            $child_is_have_value = 1;
                        }
                    }
                    //判断此笔数据中是否都有值
                    if($child_is_have_value == 1){
                        $is_have_value = 1;
                    }else{
                        unset($dataFormContents[$dataFormContentKey]);
                        unset($data[$dataForm][$dataFormContentKey]);
                    }
                    unset($child_is_have_value);
                }
                if($is_have_value == 0){
                    unset($data[$dataForm]);
                }
                unset($is_have_value);
            }
        }
        $query['data'] = $data;
        if(!empty($data)){
            foreach ($data as $formId => $ocrData) {
                $postVal = [
                    'patient_id' => $query['patient_id'],
                    'form_id' => $formId,
                    'checktime_id' => $query['checktime_id'],
                    'item_id' => $query['item_id'],
                    'itemsig_id' => $query['itemsig_id'],
                    'id' => ''
                ];

                if ($params != 1 && SDMHelper::app()->formContent->getMultiTempStorage($query['patient_id'], $query['checktime_id'], $formId)) {
                    continue;
                }

                foreach ($ocrData as $formRowData) {
                    $formData = ArrayHelper::getColumn($formRowData, 'value');
                    // 选项值转换。如果是单选的多选的 要把文本转换成对应的值
                    foreach ($formData as $key => &$item) {
                        if (!is_numeric($key)) {
                            continue;
                        }
                        $item = SDMHelper::app()->form->field->getLabelValue($key, $item);
                    }
//                $trace['postVal'] = ArrayHelper::merge($postVal, $formData);
                    $this->setPostValues(ArrayHelper::merge($postVal, $formData));
                    $this->event = EventEnum::EVENT_FORM_CONTENT_TEMP_STORAGE;
                    $this->tempStorageMulti(false);
                }

            }
        }


//        ErrorHandler::log2txt($trace);
    }
}