var jQueryWeb357LoginAsUserPhocaCart = jQuery.noConflict();
jQueryWeb357LoginAsUserPhocaCart(function ($) {
    /**
    Phocacart - Users' page
    */
    // Login as user button
    $(
        "form[action='/administrator/index.php?option=com_phocacart&view=phocacartusers'] table tbody"
    )
        .find('tr')
        .each(function () {
            var name = $(this).find('td').eq(2).text();
            var phocaEmailCol = $(this).find('td').eq(9).text();

            const getOnlyEmail = phocaEmailCol
                .trim()
                .match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi);
            const email = getOnlyEmail[0].trim();

            var build_login_as_user_url = login_as_user_url.replace(
                '{{email}}',
                email.trim()
            );

            $(this)
                .find('td')
                .eq(9)
                .after(
                    '<td><a target="_blank" class="btn button btn-primary" href="' +
                        build_login_as_user_url +
                        '">Login as <span class="loginasuser-email" style="font-weight:bold">' +
                        name +
                        '</span></td>'
                );
        });

    // Column
    $(
        "form[action='/administrator/index.php?option=com_phocacart&view=phocacartusers'] table thead"
    )
        .find('th')
        .eq(9)
        .after('<th>Login as User</th>');
    /**
    Phocacart - Orders' page
    */
    // Login as user button
    $(
        "form[action='/administrator/index.php?option=com_phocacart&view=phocacartorders'] table tbody"
    )
        .find('tr')
        .each(function () {
            var last_name_col = $(this).find('td').eq(3).text();
            var last_name_col_clean = last_name_col.trim();
            var username = last_name_col_clean.substring(
                last_name_col_clean.indexOf('(') + 1,
                last_name_col_clean.indexOf(')')
            );

            var build_login_as_user_url =
                login_as_user_with_username_url.replace(
                    '{{phoca_username}}',
                    username.trim()
                );

            console.log(build_login_as_user_url);
            if (username.length === 0) {
                $(this).find('td').eq(3).after('<td>---</td>');
            } else {
                $(this)
                    .find('td')
                    .eq(3)
                    .after(
                        '<td><a target="_blank" class="btn button btn-primary" href="' +
                            build_login_as_user_url +
                            '">Login as <span class="loginasuser-username" style="font-weight:bold">' +
                            username +
                            '</span></td>'
                    );
            }
        });

    // Column
    $(
        "form[action='/administrator/index.php?option=com_phocacart&view=phocacartusers'] table thead"
    )
        .find('th')
        .eq(9)
        .after('<th>Login as User</th>');
});
