<?php

namespace Application\Service\Extension\Formatter;


use Application\Service\Extension\Helper\CheckLogicMessageFormatterHelper;
use Application\Service\Extension\Helper\UserHelper;
use Application\Service\Extension\Laminas;

class LogicErrorFormatter extends BaseFormatter
{
    use CheckLogicMessageFormatterHelper;

    private static array $cache = [];
    public function __construct()
    {
        $this->registerHandler([
            'patient_id' => 'patientIdHandler',
            'status' => 'statusHandler',
            'reply' => 'contentHandler',
            'create_user' => 'createUserHandler',
            'expression_note' => 'expressionNoteHandler',
            'expression_id' => 'expressionIdHandler'
        ]);
    }

    public function expressionIdHandler($value, &$item)
    {
        if ($value === '0') {
            return $item['id'];
        }

        return $value;
    }

    public function patientIdHandler($value, &$item): string
    {
        $item['pid'] = $value;
        $q = Laminas::$serviceManager->patient->fetchOne([
            'where' => ['id' => $value]
        ]);

        $i = Laminas::$serviceManager->itemSignatory->fetchOne([
            'where' => ['id' => $q['itemsig_id']]
        ]);

        $sign = Laminas::$serviceManager->signatoryInfo->fetchOne([
            'where' => [
                'id' => $i['signatory_id']
            ]
        ]);

//        return $q['patient_number'] ? "{$sign['info_name']}[{$q['patient_number']}]" : "{$sign['info_name']}[未填写受试者编号]";
        return $q['patient_number'] ? $q['patient_number'] : "{$sign['info_name']}[未填写受试者编号]";
    }

    public function contentHandler($value, &$item)
    {
        $this->formatterErrorType = $item['error_type'];
        $q = Laminas::$serviceManager->itemPatientFormLogicErrorQuery->fetchAll([
            'columns' => ['content', 'platform','is_reply', 'create_time', 'create_user_id', 'error_id', 'is_auditor','close_role'],
            'where' => ['error_id' => $item['id']]
        ]);

        foreach ($q as &$i) {
//            $this->formatterCreateUserId = $item['create_user_id'];
//            $this->formatterLogicErrorId = $item['error_id'];
//            $this->formatterPlatform = $item['platform'];
//            $this->formatterCreateTime = $item['create_time'];
//            $this->formatterItemId = $_POST['item_id'];
            $this->formatterIsAuditor = $i['is_auditor'];
            $i['create_time'] = $this->setMessage('',$i['platform'] == 2)
                ->setItemId($_POST['item_id'])
                ->setErrorIdAlias($i['error_id'])
                ->setCreateTime($i['create_time'])
                ->setCreateUserId($i['create_user_id'] ? $i['create_user_id'] : 0)
                ->setcloseRole($i['close_role'])
                ->format();
        }
        return $q;
    }

    public function statusHandler($value, &$item): string
    {
        if ($item['is_queried'] == 1) {
            $item['reply'] = [];
        }

        if ($value == 1) {
            return '已关闭';
        }

        if ($item['is_queried'] == 1) {
            if ($item['is_reply'] == 1) {
                return '已回复';
            }

            return '质疑中';
        }

        unset($item['is_queried']);
        return '未质疑';
    }

    public function createUserHandler($val)
    {
        $q = Laminas::$serviceManager->adminUser->fetchOne([
            'where' => ['id' => $val]
        ]);

        return $q['realname'] ?? '';
    }

    public function expressionNoteHandler($val, $item)
    {
        $checkType = $this->getFormatterCheckTypeString($item['error_type']);

        return "[{$checkType}] {$val}";
    }
}
