<?php
$img_kses = apply_filters(
    'dokan_product_image_attributes',
    [
        'img' => [
            'alt'         => [],
            'class'       => [],
            'height'      => [],
            'src'         => [],
            'width'       => [],
            'srcset'      => [],
            'data-srcset' => [],
            'data-src'    => [],
        ],
    ]
);

$row_actions_kses = apply_filters(
    'dokan_row_actions_kses',
    [
        'span' => [
            'class' => [],
        ],
        'a' => [
            'href'    => [],
            'onclick' => [],
        ],
    ]
);

$price_kses = apply_filters(
    'dokan_price_kses',
    [
        'span' => [
            'class' => [],
        ],
    ]
);

if(isset($_GET['auto_id']) && $_GET['auto_id'] != ''){
    if(isset($_GET['hide']) && $_GET['hide'] == 'true'){

        $update_auto = array(
            'ID' => $_GET['auto_id'],
            'post_status' => 'draft'
        );
        wp_update_post($update_auto);

    } elseif (isset($_GET['hide']) && $_GET['hide'] == 'false'){
        $update_auto = array(
            'ID' => $_GET['auto_id'],
            'post_status' => 'publish'
        );
        wp_update_post($update_auto);
    }
}


if(class_exists('Pixad_Booking_AUTO') && class_exists('Pix_Autos')){
    $catalog_item_id = get_post_meta($post->ID, 'pixba_sim_product_id', true);
    $featured = get_post_meta($catalog_item_id, 'pixad_auto_featured', true);
    $status = get_post_status($catalog_item_id);
}
?>
<tr class="<?php echo esc_attr( $tr_class ); ?>">
    <td data-title="<?php esc_attr_e( 'Image', 'rvhero' ); ?>" class="column-thumb">
        <?php if(class_exists('Pixad_Booking_AUTO') && class_exists('Pix_Autos') && isset($catalog_item_id) && $catalog_item_id != ''){ ?>
            <?php $featured_image =  get_the_post_thumbnail_url($catalog_item_id);?>

            <a href="<?php echo esc_url( get_permalink($catalog_item_id) ); ?>">
                <img src="<?php echo esc_url($featured_image)?>"/>
            </a>

        <?php } else { ?>
            <?php if ( current_user_can( 'dokan_edit_product' ) ) { ?>
                <a href="<?php echo esc_url( dokan_edit_product_url( $post->ID ) ); ?>"><?php echo wp_kses( $product->get_image( 'thumbnail' ), $img_kses ); ?></a>
            <?php } else { ?>
                <?php echo wp_kses( $product->get_image( 'thumbnail' ), $img_kses ); ?>
            <?php } ?>
        <?php } ?>
    </td>
    <td data-title="<?php esc_attr_e( 'Name', 'rvhero' ); ?>" class="column-primary">

        <?php if(class_exists('Pixad_Booking_AUTO') && class_exists('Pix_Autos') && isset($catalog_item_id) && $catalog_item_id != ''){ ?>
            <strong><a href="<?php echo esc_url(get_permalink($catalog_item_id) ); ?>"><?php echo esc_html( $product->get_title() ); ?></a></strong>
        <?php } else { ?>
            <?php if ( current_user_can( 'dokan_edit_product' ) ) { ?>
                <strong><a href="<?php echo esc_url( dokan_edit_product_url( $post->ID ) ); ?>"><?php echo esc_html( $product->get_title() ); ?></a></strong>
            <?php } else { ?>
                <strong><a href=""><?php echo esc_html( $product->get_title() ); ?></a></strong>
            <?php } ?>
        <?php } ?>
        <?php if(class_exists('Pixad_Booking_AUTO') && class_exists('Pix_Autos') && isset($catalog_item_id) && $catalog_item_id != ''){ ?>

            <div class="row-actions">
                <?php if (class_exists('BuddyPress') && class_exists('Youzify')){
                    if(is_user_logged_in()) {
                        $author_ID = get_current_user_id();
                        $user = get_user_by('ID', $author_ID);
                        if (class_exists('WeDevs_Dokan')) {
                            $vendor = dokan()->vendor->get($author_ID);
                            if ($vendor->data->user_nicename != '') {
                                $header_btn_link = get_site_url() . '/members/' . $vendor->data->user_nicename . '/add_item';
                                $header_btn_link_cat = get_site_url() . '/members/' . $vendor->data->user_nicename . '/' . 'pixad-autos';
                            } else {
                                $header_btn_link = get_site_url() . '/members/' . $user->user_login . '/add_item';
                                $header_btn_link_cat = get_site_url() . '/members/' .$user->user_login  . '/' . 'pixad-autos';
                            }
                        } else {
                            $header_btn_link = get_site_url() . '/members/' . $user->user_login . '/add_item';
                            $header_btn_link_cat = get_site_url() . '/members/' . $user->user_login . '/' . 'pixad-autos';
                        }
                    } else {
                        if(isset($header_btn_link) && $header_btn_link == ''){
                            $perm_id = get_option('youzify_membership_pages');
                            if(isset($perm_id['login']) && $perm_id['login'] != ''){
                                $header_btn_link = get_permalink($perm_id['login']);
                                $header_btn_link_cat = get_permalink($perm_id['login']);
                            } else {
                                $header_btn_link = wp_login_url();
                                $header_btn_link_cat = wp_login_url();
                            }
                        }
                    }

                    $dokan_dash = get_option('dokan_pages')['dashboard'];
                    $header_btn_link = get_permalink($dokan_dash). '/add/' . '?auto_id='.$catalog_item_id;
                    $header_btn_link_del = get_permalink($dokan_dash) . '/products/?auto_id='.$catalog_item_id;
                }
                ?>
                <span class="edit">
                    <a href="<?php echo esc_url($header_btn_link)?>"><?php echo __('Edit', 'rvhero');?></a> |
                </span>
                <?php if(get_post_status($catalog_item_id) == 'draft'){ ?>
                    <span class="delete">
                    <a href="<?php echo esc_url($header_btn_link_del . '&hide=false')?>"><?php echo __('Publish', 'rvhero');?></a> |
                </span>
                <?php } else { ?>
                    <span class="delete">
                    <a href="<?php echo esc_url($header_btn_link_del . '&hide=true')?>"><?php echo __('Delete', 'rvhero');?></a> |
                </span>
                <?php } ?>
                <?php if( is_user_logged_in() ){
                    $author_ID =  get_current_user_id();
                    $user = get_user_by('ID', $author_ID);
                    $dokan_dash = get_option('dokan_pages')['dashboard'];
                    $header_btn_link = get_permalink($dokan_dash). '/add/' . '?id='.get_the_ID();
                    $link = '';
                    if(class_exists('MemberOrder')) {
                        $author_level = pmpro_getMembershipLevelForUser(get_current_user_id());
                    }
                    if($author_level) {
                        $args = array(
                            'fields' => 'ids',
                            'numberposts' => -1,
                            'post_type' => 'pixad-autos',
                            'author' => get_current_user_ID(),
                            'meta_query' => array(
                                array(
                                    'key' => 'pixad_auto_featured',
                                    'value' => 'enable',
                                )
                            )
                        );
                        $max_feat_items = get_pmpro_membership_level_meta($author_level->ID, 'max_feat_items', true);
                        $user_feat_posts_count = count(get_posts($args));

                        if (isset($featured) && $featured == 'disable') {
                            if (intval($user_feat_posts_count) < intval($max_feat_items)) {
                                $link = get_permalink($dokan_dash) . '/products/' . '?feat=true&auto_id=' . $catalog_item_id;
                            }
                        } elseif (isset($featured) && $featured == 'enable') {
                            $link = get_permalink($dokan_dash) . '/products/' . '?feat=false&auto_id=' . $catalog_item_id;
                        }
                    }
                } ?>
                <?php if(isset($link) && $link != ''){ ?>
                    <span class="featured">
                        <a href="<?php echo esc_url($link); ?>">
                            <?php if ($featured == 'enable') { ?>
                                <?php echo __('Remove from Featured', 'suprek');?>
                            <?php } else { ?>
                                <?php echo __('Do Featured', 'suprek');?>
                            <?php } ?>
                        </a>
                    </span>
                <?php } ?>
            </div>

        <?php } else { ?>
            <?php if ( ! empty( $row_actions ) ) { ?>
                <div class="row-actions">
                    <?php echo wp_kses( $row_actions, $row_actions_kses ); ?>
                </div>
            <?php } ?>
        <?php } ?>

        <button type="button" class="toggle-row"></button>
    </td>
    <?php do_action( 'dokan_product_list_table_after_status_table_data', $post, $product, $tr_class, $row_actions ); ?>
    <td data-title="<?php esc_attr_e( 'Price', 'rvhero' ); ?>">

        <?php if(class_exists('Pixad_Booking_AUTO') && class_exists('Pix_Autos') && isset($catalog_item_id) && $catalog_item_id != ''){ ?>
            <?php

            $Auto = new PIXAD_Autos();
            $Auto->Query_Args( array('auto_id' => $catalog_item_id) );

            $price = get_post_meta($catalog_item_id, '_auto_price', true);
            $price_car_page = $Auto->get_price($price);

            ?>
            <?php echo wp_kses_post(
                $price_car_page
            ); ?>

        <?php } else { ?>
            <?php
            if ( $product->get_price_html() ) {
                echo wp_kses_post( $product->get_price_html() );
            } else {
                echo '<span class="na">&ndash;</span>';
            }
            ?>

        <?php } ?>

    </td>
    <td class="post-date" data-title="<?php esc_attr_e( 'Date', 'rvhero' ); ?>">
        <?php



        if ( '0000-00-00 00:00:00' === $post->post_date ) {
            $post_published_date = __( 'Unpublished', 'rvhero' );
            $human_readable_time = __( 'Unpublished', 'rvhero' );
            $time_diff = 0;
        } else {
            // get current time
            $current_time = dokan_current_datetime();
            // set timezone to gmt
            $gmt_time = $current_time->setTimezone( new DateTimeZone( 'UTC' ) );
            // read post gmt time
            $post_time_gmt = $gmt_time->modify( $post->post_date_gmt );

            $format = apply_filters( 'dokan_date_time_format', wc_date_format() . ' ' . wc_time_format() );
            // currently dokan_format_date doesn't support time format, will update this soon,
            // right now we need to send $format from our end.
            // if you need date only remove $format from dokan_format_date function parameter
            $human_readable_time = dokan_format_date( $post_time_gmt->getTimestamp(), $format );
            $post_published_date = apply_filters( 'post_date_column_time', dokan_format_date( $post_time_gmt->getTimestamp() ), $post, 'date', 'all' );

            // get human readable time
            $time_diff = $current_time->getTimestamp() - $post_time_gmt->getTimestamp();
            if ( $time_diff > 0 && $time_diff < 24 * 60 * 60 ) {
                /* translators: %s: time difference */
                $human_readable_time = sprintf( __( '%s ago', 'rvhero' ), human_time_diff( $post_time_gmt->getTimestamp() ) );
            }
        }

        echo '<abbr title="' . esc_attr( $human_readable_time ) . '">' . esc_html( $post_published_date ) . '</abbr>';
        echo '<div class="status">';

        if ( 'publish' === $post->post_status ) {
            esc_html_e( 'Published', 'rvhero' );
        } elseif ( 'future' === $post->post_status ) {
            if ( $time_diff > 0 ) {
                echo '<strong class="attention">' . esc_html__( 'Missed schedule', 'rvhero' ) . '</strong>';
            } else {
                esc_html_e( 'Scheduled', 'rvhero' );
            }
        } else {
            esc_html_e( 'Last Modified', 'rvhero' );
        }
        ?>
        </div>
    </td>
</tr>
