/**
 * Creates a dropdown list of users.
 *
 * @since  3.1.2
 * @param  array  $args Arguments
 * @return string       Users dropdown
 */
function wpas_users_dropdown($args = array())
{
    global $current_user, $post;
    $defaults = array('name' => 'wpas_user', 'id' => '', 'class' => '', 'exclude' => array(), 'selected' => '', 'cap' => '', 'cap_exclude' => '', 'agent_fallback' => false, 'please_select' => false, 'select2' => false, 'disabled' => false);
    $args = wp_parse_args($args, $defaults);
    /* List all users */
    $all_users = wpas_get_users(array('cap' => $args['cap'], 'cap_exclude' => $args['cap_exclude'], 'exclude' => $args['exclude']));
    /**
     * We use a marker to keep track of when a user was selected.
     * This allows for adding a fallback if nobody was selected.
     * 
     * @var boolean
     */
    $marker = false;
    $options = '';
    /* The ticket is being created, use the current user by default */
    if (!empty($args['selected'])) {
        $user = get_user_by('id', intval($args['selected']));
        if (false !== $user && !is_wp_error($user)) {
            $marker = true;
            $options .= "<option value='{$user->ID}' selected='selected'>{$user->data->display_name}</option>";
        }
    }
    foreach ($all_users as $user) {
        /* This user was already added, skip it */
        if (!empty($args['selected']) && $user->ID === intval($args['selected'])) {
            continue;
        }
        $user_id = $user->ID;
        $user_name = $user->data->display_name;
        $selected_attr = '';
        if (false === $marker) {
            if (false !== $args['selected']) {
                if (!empty($args['selected'])) {
                    if ($args['selected'] === $user_id) {
                        $selected_attr = 'selected="selected"';
                    }
                } else {
                    if (isset($post) && $user_id == $post->post_author) {
                        $selected_attr = 'selected="selected"';
                    }
                }
            }
        }
        /* Set the marker as true to avoid selecting more than one user */
        if (!empty($selected_attr)) {
            $marker = true;
        }
        /* Output the option */
        $options .= "<option value='{$user_id}' {$selected_attr}>{$user_name}</option>";
    }
    /* In case there is no selected user yet we add the post author, or the currently logged user (most likely an admin) */
    if (true === $args['agent_fallback'] && false === $marker) {
        $fallback = $current_user;
        $fb_selected = false === $marker ? 'selected="selected"' : '';
        $options .= "<option value='{$fallback->ID}' {$fb_selected}>{$fallback->data->display_name}</option>";
    }
    $contents = wpas_dropdown(wp_parse_args($args, $defaults), $options);
    return $contents;
}
/**
 * Get a dropdown of the tickets.
 *
 * @since  3.1.3
 * @param  array  $args   Dropdown arguments
 * @param  string $status Specific ticket status to look for
 * @return void
 */
function wpas_tickets_dropdown($args = array(), $status = '')
{
    $defaults = array('name' => 'wpas_tickets', 'id' => '', 'class' => '', 'exclude' => array(), 'selected' => '', 'select2' => true, 'please_select' => false);
    /* List all tickets */
    $tickets = wpas_get_tickets($status);
    $options = '';
    foreach ($tickets as $ticket) {
        $options .= "<option value='{$ticket->ID}'>{$ticket->post_title}</option>";
    }
    echo wpas_dropdown(wp_parse_args($args, $defaults), $options);
}
    if (isset($post)) {
        $users_atts['selected'] = $post->post_author;
    }
    echo wpas_dropdown($users_atts, $client_option);
} else {
    ?>
			<a id="wpas-issuer" href="<?php 
    echo $client_link;
    ?>
"><?php 
    echo $client_name;
    ?>
</a>
		<?php 
}
?>
	</p>
	<label for="wpas-assignee"><strong data-hint="<?php 
esc_html_e('The agent currently responsible for this ticket', 'awesome-support');
?>
" class="hint-left hint-anim"><?php 
_e('Support Staff', 'awesome-support');
?>
</strong></label>
	<p>
		<?php 
$staff_atts = array('name' => 'wpas_assignee', 'id' => 'wpas-assignee', 'disabled' => !current_user_can('assign_ticket') ? true : false, 'select2' => true, 'data_attr' => array('capability' => 'edit_ticket'));
echo wpas_dropdown($staff_atts, "<option value='{$staff_id}' selected='selected'>{$staff_name}</option>");
?>
	</p>
</div>