<?php


use support\Response;
use Webman\Event\Event;

if (!function_exists("getDateFull"))
{
    /**
     * @param string $format
     * @param string $time
     * @return string
     */
    function getDateFull(string $format = "Y-m-d H:i:s",string $time = ""): string
    {
        if (empty($time)) $time = time();
        return date($format??'Y-m-d H:i:s',$time);
    }
}

if (!function_exists("events"))
{
    /**
     * 事件发布
     * @param $name
     * @param $data
     * @return array|mixed|null
     */
    function events($name,$data): mixed
    {
        return Event::dispatch($name,$data);
    }
}

if (!function_exists("_json")) {
    /**
     * @param string $msg
     * @param array $data
     * @param int $code
     * @return Response
     */
    function _json(int $code = 1, string $msg = "", array $data = []): Response
    {
        return json(compact('code', 'data', 'msg'));
    }
}

if (!function_exists("pageFormat")) {
    /**
     * @param $data
     * @param int $size
     * @return array {page:"",pageSize:"",rows:[],total:""}
     */
    function pageFormat($data, int $size = 10): array
    {
        if (empty($data)) return [];
        return [
            'total'     => $data->total(),
            'page'      => $data->currentPage(),
            'pageSize'  => $size,
            'rows'      => $data->items()
        ];
    }
}

if(!function_exists('format_money')){
    function format_money($str,$len = '2',$append = ""): string
    {
        if (empty($str)) {
            return "0.00";
        }
        return number_format($str, $len, ".", $append);
    }
}
