<?php

namespace Application\Service\Extension\Helper;

use Application\Service\DB\Dictionary\ItemForm;
use Application\Service\Exceptions\InvalidArgumentException;
use Application\Service\Extension\Laminas;
use Application\Common\Enum;

class Edcloghelper
{

    //事件
    public $Event = '';

    public $Params = [];

    public $contentId = '';

    //设置事件
    public function setEvent($event)
    {
        $this->Event = $event;
        return $this;
    }

    //设置传输参数
    public function setPostData(array $data)
    {
        $this->Params = $data;
        return $this;
    }

    public function setContentId(int $id)
    {
        $this->contentId = $id;
        return $this;
    }

    //操作日志
    public function flush(array $content = [])
    {
        try {
            Laminas::$serviceManager->patientFormContentUpdatedLog->save($this->getFLushContent($content));
        } catch (\Exception $exception) {
            throw new \Exception('保存日志出错啦!!!!!' . $exception->getMessage());
        }
    }

    /**
     * 获取存储日志的内容
     * @param array $content
     * @return array
     * @throws \Exception
     */
    public function getFLushContent(array $content = []): array
    {
        return [
            'item_id' => $content['item_id'],
            'patient_id' => $content['patient_id'],
            'form_id' => $content['form_id'],
            'sign_id' => $content['itemsig_id'],
            'checktime_id' => $content['checktime_id'],
            'content_id' => $this->contentId,
            'data' =>   json_encode($this->Params), // 已经处理后的post数据
            'source' => Laminas::$serviceManager->identity->getPlatform() ?? 'SCRIPT',
            'event' => $this->Event,
            'change_data' => '',
            'change_data_str' => Enum::LOG_EVENT[$this->Event],
            'create_user_id'=>0,
            'role'=>'System',
            'json' => json_encode([
                'behavior' => '',
                'post' => $this->Params,
                'headers' => Laminas::$app->getRequest()->getHeaders()->toArray() ?? [],
                'identity' => Laminas::$serviceManager->identity->getIdentityData() ?? [],
                'attach' => [],
                'message' => $this->getMessage()
            ]),
        ];
    }

    private function getMessage()
    {
        $message = $this->message === null ? $this->getDefaultMessage() : $this->message;

        $params = [
            'message' => $message,
            'createdBy' => Laminas::$serviceManager->identity->getRealName() ?? '系统脚本',
        ];
        $placeholders = [];
        foreach ($params as $name => $value) {
            $placeholders['{' . $name . '}'] = $value;
        }

        return strtr($this->messageTemplate, $placeholders);

    }
//SDV 、 Un-SDV
    //Review 、 Un-Review
    //Lock 、 Un-Lock
    //Sign 、 Un-Sign
    private function getDefaultMessage()
    {

        if(isset(Enum::LOG_EVENT[$this->Event])){
            return Enum::LOG_EVENT[$this->Event];
        }else{
            return '未知的操作';
        }
    }
}