<?php

namespace SRVX\Traits;

use SRVX\Msg;
use Phalcon\Logger;
use Phalcon\Logger\Adapter\Stream;
use SRVX\Utils\Common;

trait BasisFun
{
    /**
     * 输出信息并退出程序
     * @param mixed $data
     * @param int   $count
     * @param bool  $urlDecodeFlag
     */
    public function api($data = Msg::Suc, $count = 0, $urlDecodeFlag = false, $isUnderLine = true) {
        $data === true && $data = Msg::Suc;
        // !$data && $data = Msg::ErrFailure;

        $res = isset($data['statusCode'], $data['code'], $data['msg']) ? [
            'code' => $data['code'],
            'msg'  => $data['msg'],
            'data' => $data['data'] ?? []
        ] : ['code' => 0, 'msg' => 'success', 'data' => $data, 'count' => !empty($count) ? $count : count($data)];

        header('Content-type:application/json;charset=utf-8');
        isset($data['statusCode']) && header('HTTP/2.0 ' . $data['statusCode']);

        if (!empty($res['data'])) {
            if ($isUnderLine) {
                $res['data'] = (new Common)->camel_2_underline($res['data']);
            }
        } else {
            $res['data'] = [];
        }
        die($urlDecodeFlag ? urldecode(json_encode($res, 320)) : json_encode($res, 320));
    }
    /**
     * 输出信息并退出程序
     * @param mixed $data
     * @param int   $count
     * @param bool  $urlDecodeFlag
     */
    public function wxApi($data = Msg::Suc, $count = 0, $urlDecodeFlag = false, $isUnderLine = true) {
        $data === true && $data = Msg::Suc;

        $res = isset($data['statusCode'], $data['code'], $data['msg']) ? [
            'code' => $data['code'],
            'msg'  => $data['msg'],
        ] : ['code' => 0, 'msg' => 'success', 'data' => $data, 'count' => !empty($count) ? $count : count($data)];

        header('Content-type:application/json;charset=utf-8');
        isset($data['statusCode']) && header('HTTP/2.0 ' . $data['statusCode']);

        if (!empty($res['data'])) {
            if ($isUnderLine) {
                $res['data'] = (new Common)->camel_2_underline($res['data']);
            }
        }
        die($urlDecodeFlag ? urldecode(json_encode($res, 320)) : json_encode($res, 320));
    }

    /**
    * 记录日志
    * @param        $msg
    * @param string $name
    * @param bool   $mode
    */
    public function logger($msg, $name = 'debug', $mode = false) 
    {
        //开启了debug模式才记录日志
        $time = time();
        if (file_exists(DIR_LOG)) {
            $path   = DIR_LOG . date('Y_m_d', $time) . '_' . $name . '.log';
            $logger = new Logger('messages', ['main' => new Stream($path),]);
            $msg    = is_array($msg) ? json_encode($msg, 320) : $msg;
            $ip     = Common::getRealIP() ? Common::getRealIP() : ' IP NULL ';
            $msg    = $ip . ' ' . $msg;
            $logger->debug($msg);
        } else {
            if ($mode) {
                return;
            }
            @mkdir(DIR_LOG, 0755, true);
            $this->logger($msg, $name, true);
        }
    }


    public function getRawData($useArray = false) 
    {
        $res = !$this->request->isPost() && !$this->request->isPut() && $this->request->isPatch() && !$this->request->isDelete() ? $this->api(Msg::ErrNotFound) : json_decode(file_get_contents('php://input'), $useArray);

        // return $res ?? $this->api(Msg::ErrInvalidArgument);
        return $res ?? [];
    }
}