<?php
/**
 * Here is your custom functions.
 */

use plugin\admin\app\model\Admin;
use plugin\admin\app\model\AdminAccess;

error_reporting(E_ALL & ~E_WARNING);
function url($path='',$args=[]){
    $pos = strpos($path,'/');
    if($pos === 0){
        $path = substr($path,1);
    }elseif($pos===false){
        $class = get_controller_name();
        $path = $class.'/'.$path;
    }else{
        //return $path;
    }
    if($args){
        if(is_array($args)){
            $args = http_build_query($args);
        }
        $path = $path.'?'.$args;
    }
    return admin_path().'/'.$path;
}
/**
 * 当前管理员id
 * @return integer|null
 */
function admin_id(): ?int
{
    return session('admin.id');
}

/**
 * 当前管理员
 * @param null|array|string $fields
 * @return array|mixed|null
 * @throws Exception
 */
function admin($fields = null)
{
    refresh_admin_session();
    if (!$admin = session('admin')) {
        return null;
    }
    if ($fields === null) {
        return $admin;
    }
    if (is_array($fields)) {
        $results = [];
        foreach ($fields as $field) {
            $results[$field] = $admin[$field] ?? null;
        }
        return $results;
    }
    return $admin[$fields] ?? null;
}


/**
 * 刷新当前管理员session
 * @param bool $force
 * @return void
 * @throws Exception
 */
function refresh_admin_session(bool $force = false)
{
    $admin_session = session('admin');
    if (!$admin_session) {
        return null;
    }
    $admin_id = $admin_session['id'];
    $time_now = time();
    // session在2秒内不刷新
    $session_ttl = 2;
    $session_last_update_time = session('admin.session_last_update_time', 0);
    if (!$force && $time_now - $session_last_update_time < $session_ttl) {
        return null;
    }
    $session = request()->session();
    $admin = Admin::find($admin_id);
    if (!$admin) {
        $session->forget('admin');
        return null;
    }
    $admin = $admin->toArray();
    $admin['password'] = md5($admin['password']);
    $admin_session['password'] = $admin_session['password'] ?? '';
    if ($admin['password'] != $admin_session['password']) {
        $session->forget('admin');
        return null;
    }
    // 账户被禁用
    if ($admin['status'] != 1) {
        $session->forget('admin');
        return;
    }
    $admin['roles'] = AdminAccess::where('admin_id', $admin_id)->column('role_id');
    $admin['session_last_update_time'] = $time_now;
    $session->set('admin', $admin);
}

function getConfig($name=''){
    if($name){
        if(strpos($name,'.')>-1){
            $name = explode('.',$name); 
            if(Config('site.'.$name[0].'.type') == 'array'){
                return Config('site.'.$name[0].'.value.'.$name[1]);
            }else{
                return Config('site.'.$name[0].'.value');
            }
        }else{
            return Config('site.'.$name);
        }
    }else{
        return Config('site');
    }
}
function null($var='',$defaut=''){
    if(isset($var) && !$var){
        return $defaut;
    }else{
        return $var;
    }
}
function buildFileInput($name='',$value='',$type='image',$limit=0,$allowChoose=true){
    $tpl = '
                    <input id="c-'.$name.'" class="form-control" size="50" name="'.$name.'" type="hidden" value="'.null($value).'" data-tip="头像">
                    <ul class="list-inline clearfix lyear-uploads-pic" data-template="preview" id="p-'.$name.'">
                        <li nodelete class="col-xs-4 col-sm-3 col-md-2">
                            <a class="pic-add faupload" style="height: auto;border: 0;" id="add-pic-btn" href="#!" title="点击上传" data-input-id="c-'.$name.'" data-mimetype="'.$type.'" data-multiple="'.($limit>0 ?'true' : 'false').'" data-preview-id="p-'.$name.'"></a>';
    if($allowChoose){
        $tpl.='                    <a class="pic-add fachoose" style="height: auto;border: 0;" id="choose-pic-btn" href="#!" title="选择文件" data-input-id="c-'.$name.'" data-mimetype="image/*" data-multiple="'.($limit>0 ?'true' : 'false').'" data-preview-id="p-'.$name.'"></a>';
    }
    $tpl.='
                        </li>
                    </ul>';
    return $tpl;
}