<?php
declare( strict_types=1 );
defined( 'ABSPATH' ) || exit;

$submenus = $attributes['submenus'] ?? [];

$user_id = get_current_user_id();
$avatar = MINECLOUDVOD_URL.'/static/img/user.png';

$viewDependencies = include( MINECLOUDVOD_PATH.'/build/lms/user/view.asset.php' );
foreach($viewDependencies['dependencies'] as $dpc){
    wp_enqueue_style( $dpc );
    wp_enqueue_script( $dpc );
}

if( $user_id ){
    $avatar = get_user_meta( $user_id, 'mcv_avatar', true )?:$avatar;
    
    $ucStr_escaped = '<div class="mcv-usercenter">
        <img class="mcv-avatar" src="'.esc_url($avatar).'" />
        <div class="submenu">';
        foreach( $submenus as $sm ){
            $ucStr_escaped .= '<a href="'.esc_url($sm['url']).'" rel="nofollow">'.esc_html($sm['title']).'</a>';
        }
        $wechat_open = MINECLOUDVOD_SETTINGS['uc_login3']['wechat_open']??false;
        // 优先使用服务号登录，则显示绑定链接
        if( $wechat_open && isset($wechat_open['MpAppID']) && isset($wechat_open['MpAppSecret']) && isset($wechat_open['mp_first']) && $wechat_open['mp_first'] ){
            $ucStr_escaped .= '<a href="javascript:;" class="mcv-bind-account">'.esc_html__('Bind Account', 'mine-cloudvod').'</a>';
        }
        $ucStr_escaped .= '<a href="'.esc_url(wp_logout_url(sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI']??'')))).'" rel="nofollow" style="border-top: 1px solid #ccc;margin-top: 5px;padding-top: 5px;">'.esc_html__('Log out', 'mine-cloudvod').'</a>
        </div>
    </div>';
    echo $ucStr_escaped;
}
else{
    echo '<div class="mcv-usercenter mcv-login"><img class="mcv-avatar" src="'.esc_url($avatar).'" title="'.esc_html__('Click to login', 'mine-cloudvod').'" /></div>';
}
?>