<?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

if ( ! current_user_can( 'edit_products' ) ) {
    return;
} 

// Nonce verification is done right before require_once this file
$page_name = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; ?>
<div id="mdspbe-container" class="overflow-y-auto">

    <div class="row product-filter-container sticky-col my-2 gy-2">
        <div class="col-auto filter-container">
            <form method="get">
                <div class="d-flex align-items-end gap-2 flex-wrap">
                    <?php wp_nonce_field( 'mdspbe_filter_action', 'mdspbe_filter_action_nonce' ); ?>
                    <div class="form-floating">
                        <input type="search" value="<?php echo esc_attr( $args['s'] ) ?>" class="form-control" name="s" id="query-search" autocomplete="off" title="<?php esc_html_e( 'Search', 'mds-product-bulk-editor-for-woocommerce' ) ?>">
                        <label for="query-search"><?php esc_html_e( 'Search', 'mds-product-bulk-editor-for-woocommerce' ) ?></label>
                    </div>

                    <div class="form-floating">
                        <select class="form-select" name="product_category_id" id="categories" title="<?php esc_html_e( 'Category', 'mds-product-bulk-editor-for-woocommerce' ) ?>">
                            <option value="all"><?php esc_html_e( 'All', 'mds-product-bulk-editor-for-woocommerce' ) ?></option>
                            <?php 
                                $categories = mdspbe_get_categories();
                                foreach ( $categories as $category_id => $cat_name ) {
                                    ?><option value="<?php echo esc_attr( $category_id ) ?>" <?php echo selected( $category_id, $args['product_category_id'], false ) ?>><?php echo esc_html( $cat_name ) ?></option><?php
                                }
                            ?>
                        </select>
                        <label for="categories"><?php esc_html_e( 'Category', 'mds-product-bulk-editor-for-woocommerce' ) ?></label>
                    </div>

                    <div class="form-floating">
                        <select class="form-select" name="product_tag_id" id="tags" title="<?php esc_html_e( 'Tag', 'mds-product-bulk-editor-for-woocommerce' ) ?>">
                            <option value="all"><?php esc_html_e( 'All', 'mds-product-bulk-editor-for-woocommerce' ) ?></option>
                            <?php 
                                $tags = mdspbe_get_tags();
                                foreach ( $tags as $t_id => $tag_name ) {
                                    ?><option value="<?php echo esc_attr( $t_id ) ?>" <?php echo selected( $t_id, $args['product_tag_id'], false ) ?>><?php echo esc_html( $tag_name ) ?></option><?php
                                }
                            ?>
                        </select>
                        <label for="tags"><?php esc_html_e( 'Tag', 'mds-product-bulk-editor-for-woocommerce' ) ?></label>
                    </div>

                    <div class="form-floating">
                        <select class="form-select" name="stock_status" id="stock-status" title="<?php esc_html_e( 'Stock status', 'mds-product-bulk-editor-for-woocommerce' ) ?>">
                            <option value=""><?php esc_html_e( 'All', 'mds-product-bulk-editor-for-woocommerce' ) ?></option>
                            <?php 
                                $statuses = wc_get_product_stock_status_options();
                                foreach ( $statuses as $status => $status_name ) {
                                    ?><option value="<?php echo esc_attr( $status ) ?>" <?php echo selected( $status, $args['stock_status'], false ) ?>><?php echo esc_html( $status_name ) ?></option><?php
                                }
                            ?>
                        </select>
                        <label for="stock-status"><?php esc_html_e( 'Stock status', 'mds-product-bulk-editor-for-woocommerce' ) ?></label>
                    </div>

                    <div class="form-floating">
                        <select class="form-select" name="visibility" id="visibility" title="<?php esc_html_e( 'Visibility', 'mds-product-bulk-editor-for-woocommerce' ) ?>">
                            <option value=""><?php esc_html_e( 'All', 'mds-product-bulk-editor-for-woocommerce' ) ?></option>
                            <?php 
                                $visibility_opts = mdspbe_get_wc_visibility_options();
                                foreach ( $visibility_opts as $slug => $name ) {
                                    ?><option value="<?php echo esc_attr( $slug ) ?>" <?php echo selected( $slug, $args['visibility'], false ) ?>><?php echo esc_html( $name ) ?></option><?php
                                }
                            ?>
                        </select>
                        <label for="visibility"><?php esc_html_e( 'Visibility', 'mds-product-bulk-editor-for-woocommerce' ) ?></label>
                    </div>

                    <div class="form-floating">
                        <select class="form-select" name="status" id="post-status" title="<?php esc_html_e( 'Status', 'mds-product-bulk-editor-for-woocommerce' ) ?>">
                            <option value=""><?php esc_html_e( 'All', 'mds-product-bulk-editor-for-woocommerce' ) ?></option>
                            <?php 
                                $statuses = mdspbe_get_product_statuses();
                                foreach ( $statuses as $status => $status_name ) {
                                    ?><option value="<?php echo esc_attr( $status ) ?>" <?php echo selected( $status, $args['status'], false ) ?>><?php echo esc_html( $status_name ) ?></option><?php
                                }
                            ?>
                        </select>
                        <label for="post-status"><?php esc_html_e( 'Status', 'mds-product-bulk-editor-for-woocommerce' ) ?></label>
                    </div>

                    <?php do_action( 'mdspbe_add_product_filter', $args ); ?>

                    <div class="form-floating">
                        <select class="form-select" name="orderby" id="orderby" title="<?php esc_html_e( 'Order by', 'mds-product-bulk-editor-for-woocommerce' ) ?>">
                            <option value="name" <?php echo selected( $args['orderby'], 'name', false ) ?>><?php esc_html_e( 'Name', 'mds-product-bulk-editor-for-woocommerce' ) ?></option>
                            <option value="price" <?php echo selected( $args['orderby'], 'price', false ) ?>><?php esc_html_e( 'Price', 'mds-product-bulk-editor-for-woocommerce' ) ?></option>
                            <option value="ID" <?php echo selected( $args['orderby'], 'ID', false ) ?>><?php esc_html_e( 'ID', 'mds-product-bulk-editor-for-woocommerce' ) ?></option>
                            <option value="type" <?php echo selected( $args['orderby'], 'type', false ) ?>><?php esc_html_e( 'Type', 'mds-product-bulk-editor-for-woocommerce' ) ?></option>
                            <option value="date" <?php echo selected( $args['orderby'], 'date', false ) ?>><?php esc_html_e( 'Date created', 'mds-product-bulk-editor-for-woocommerce' ) ?></option>
                            <option value="modified" <?php echo selected( $args['orderby'], 'modified', false ) ?>><?php esc_html_e( 'Date modified', 'mds-product-bulk-editor-for-woocommerce' ) ?></option>
                        </select>
                        <label for="orderby"><?php esc_html_e( 'Order by', 'mds-product-bulk-editor-for-woocommerce' ) ?></label>
                    </div>

                    <div class="form-floating">
                        <select class="form-select" name="order" id="order" title="<?php esc_html_e( 'Desc', 'mds-product-bulk-editor-for-woocommerce' ) ?> / <?php esc_html_e( 'Asc', 'mds-product-bulk-editor-for-woocommerce' ) ?>">
                            <option value="DESC" <?php echo selected( $args['order'], 'DESC', false ) ?>><?php esc_html_e( 'Desc', 'mds-product-bulk-editor-for-woocommerce' ) ?></option>
                            <option value="ASC" <?php echo selected( $args['order'], 'ASC', false ) ?>><?php esc_html_e( 'Asc', 'mds-product-bulk-editor-for-woocommerce' ) ?></option>
                        </select>
                        <label for="order"><?php esc_html_e( 'Desc', 'mds-product-bulk-editor-for-woocommerce' ) ?> / <?php esc_html_e( 'Asc', 'mds-product-bulk-editor-for-woocommerce' ) ?></label>
                    </div>

                    <div class="form-floating">
                        <input type="number" value="<?php echo esc_attr( $args['limit'] ) ?>" class="form-control" name="limit" id="query-limit" min="5" max="300" autocomplete="off" title="<?php esc_html_e( 'Per page',  'mds-product-bulk-editor-for-woocommerce' ) ?>">
                        <label for="query-limit"><?php esc_html_e( 'Per page',  'mds-product-bulk-editor-for-woocommerce' ) ?></label>
                    </div>

                    <?php if ( isset( $pages ) && $pages > 1 ) { ?>
                    <div class="form-floating">
                        <select class="form-select" name="page_number" id="page-number" title="<?php esc_html_e( 'Page', 'mds-product-bulk-editor-for-woocommerce' ) ?>">
                            <?php 
                                $i = 1;
                                while ( $i <= $pages ) {
                                    ?><option value="<?php echo esc_attr( $i ); ?>" <?php echo selected( $i, $args['page_number'], false ) ?>><?php echo esc_attr( $i ) ?></option><?php
                                    $i++;
                                }
                            ?>
                        </select>
                        <label for="page-number"><?php esc_html_e( 'Page', 'mds-product-bulk-editor-for-woocommerce' ) ?></label>
                    </div>
                    <?php } ?>

                    <input type="hidden" name="page" value="<?php echo esc_attr( $page_name ) ?>">

                    <button type="submit" class="button button-primary action">
                        <?php esc_html_e( 'Filter', 'mds-product-bulk-editor-for-woocommerce' ) ?>
                    </button>
                    <a href="<?php echo esc_url( admin_url( 'admin.php?page=mds-product-bulk-editor-for-woocommerce' ) ); ?>" class="button action">
                        <?php esc_html_e( 'Clear', 'mds-product-bulk-editor-for-woocommerce' ) ?>
                    </a>

                    <?php $attr_btn_style = empty( $args['product_attrs'] ) ? '' : 'background: orange;'; ?>
                    <a href="#" class="button action js-toggle-extra-filters" style="<?php echo esc_attr( $attr_btn_style ) ?>">
                        <?php esc_html_e( 'More filters', 'mds-product-bulk-editor-for-woocommerce' ) ?> 
                    </a>
                </div>

                <!-- Hidden filters -->
                <div id="mdspbe-hidden-filters" class="d-flex align-items-end gap-2 flex-wrap mt-2 d-none">

                    <?php
                    $wc_attribute_taxs = wc_get_attribute_taxonomies();
                    if ( ! empty( $wc_attribute_taxs ) ) {
                        foreach ( $wc_attribute_taxs as $wc_attribute_tax ) {
                            $attr_name = $wc_attribute_tax->attribute_name;
                            $attr_label = $wc_attribute_tax->attribute_label;
                            $attr_options = mdspbe_get_attr_options( $attr_name );
                        ?>

                        <div class="form-floating">
                            <select class="form-select" name="product_attrs[<?php echo esc_attr( $attr_name ) ?>]" id="attrs-<?php echo esc_attr( $attr_name ) ?>" title="<?php echo sprintf( 'Attribute: %s', esc_html( $attr_label ) ) ?>">
                                <option value=""><?php esc_html_e( 'All', 'mds-product-bulk-editor-for-woocommerce' ) ?></option>
                                <?php 
                                    $tags = mdspbe_get_tags();
                                    $selected_attr_opt = isset( $args['product_attrs'] ) && isset( $args['product_attrs']['pa_' . $attr_name] ) ? $args['product_attrs']['pa_' . $attr_name] : '';
                                    foreach ( $attr_options as $opt_id => $opt_name ) {
                                        ?><option value="<?php echo esc_attr( $opt_id ) ?>" <?php echo selected( $opt_id, $selected_attr_opt, false ) ?>><?php echo esc_html( $opt_name ) ?></option><?php
                                    }
                                ?>
                            </select>
                            <label for="attrs-<?php echo esc_attr( $attr_name ) ?>"><?php echo esc_html( $attr_label ) ?></label>
                        </div>

                        <?php }
                    } else { ?>
                        <span class="fst-italic"><?php esc_html_e( 'Create global product attributes to view more filters.', 'mds-product-bulk-editor-for-woocommerce' ); ?>
                            <a href="<?php echo esc_attr( admin_url( 'edit.php?post_type=product&page=product_attributes' ) ) ?>" target="_blank">
                                <?php esc_html_e( 'Go to attributes.', 'mds-product-bulk-editor-for-woocommerce' ); ?>
                            </a>
                        </span>
                    <?php } ?>

                </div>
            </form>
            
        </div>
    </div>

    <div class="row align-items-end sticky-col my-2 gy-2">
        <div class="col-auto bulk-action-container">
            <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" id="bulk-actions-form" class="d-flex align-items-end gap-2 flex-wrap">
                <input type="hidden" name="action" value="mdspbe_bulk_action">
                <input type="hidden" id="bulk-ids" name="bulk_ids" value="">
                <input type="hidden" name="page" value="<?php echo esc_attr( $page_name ) ?>">
                <?php do_action( 'mdspbe_before_bulk_actions' ); ?>

                <div class="form-floating">
                    <select class="form-select" name="bulk_action" id="bulk-action" title="<?php esc_html_e( 'Bulk actions',  'mds-product-bulk-editor-for-woocommerce' ) ?>">
                        <option value=""></option>
                        <?php if ( $args['status'] === 'trash' ) {
                            if ( current_user_can( 'delete_products' ) ) { ?>
                                <option value="untrash"><?php esc_html_e( 'Restore', 'mds-product-bulk-editor-for-woocommerce' ) ?></option>
                                <option value="delete"><?php esc_html_e( 'Delete permanently', 'mds-product-bulk-editor-for-woocommerce' ) ?></option><?php 
                            } 
                        } else {
                            if ( current_user_can( 'delete_products' ) ) { ?>
                                <option value="trash"><?php echo esc_html_x( 'Trash', 'verb', 'mds-product-bulk-editor-for-woocommerce' ) ?></option> <?php 
                            } 
                            
                            if ( current_user_can( 'publish_products' ) ) { ?>
                                <option value="duplicate"><?php esc_html_e( 'Duplicate', 'mds-product-bulk-editor-for-woocommerce' ) ?></option><?php 
                            } 
                            
                            do_action( 'mdspbe_bulk_actions' ); ?>
                        <?php } ?>
                    </select>
                    <label for="bulk-action"><?php esc_html_e( 'Bulk actions',  'mds-product-bulk-editor-for-woocommerce' ) ?></label>
                </div>

                <?php wp_nonce_field( 'mdspbe_bulk_action', 'mdspbe_bulk_action_nonce' ); ?>
                
                <button type="submit" class="button action js-bulk-action-apply" disabled>
                    <?php esc_html_e( 'Apply', 'mds-product-bulk-editor-for-woocommerce' ) ?>
                </button>
            </form>
        </div>
    
        <div class="col text-end me-2">
            <?php if ( $args['status'] === 'trash' ) { ?>
                <button class="button action js-back-from-trash mt-1"><?php esc_html_e( 'Back from bin',  'mds-product-bulk-editor-for-woocommerce' ) ?></button>
            <?php } else { ?>
                <button class="button action js-go-to-trash mt-1"><?php esc_html_e( 'Recycle bin',  'mds-product-bulk-editor-for-woocommerce' ) ?></button>
                <a class="button action mt-1" href="<?php echo esc_url( admin_url( 'edit.php?post_type=product&page=product_importer' ) ) ?>"><?php esc_html_e( 'Import',  'mds-product-bulk-editor-for-woocommerce' ) ?></a>
                <a class="button action mt-1" href="<?php echo esc_url( admin_url( 'edit.php?post_type=product&page=product_exporter' ) ) ?>"><?php esc_html_e( 'Export',  'mds-product-bulk-editor-for-woocommerce' ) ?></a>
                <button class="button action js-create-tags-dialog mt-1"><?php esc_html_e( 'Create print tags',  'mds-product-bulk-editor-for-woocommerce' ) ?></button>
            <?php } ?>
            
            <button class="button action js-toggle-fields mt-1"><?php esc_html_e( 'Activate fields',  'mds-product-bulk-editor-for-woocommerce' ) ?></button>
        </div>
    </div>

    <form id="mdspbe-main-form">
        <div class="bg-light border rounded-3" style="height: 75vh;">
            <table id="mdspbe-main-table" class="table table-light table-striped table-hover table-bordered">
                <thead class="table-primary sticky-top">
                    <tr>
                    <?php 
                        $i = 0;
                        foreach ( $attributes as $attribute ) {
                            $purchase_order_keep = ! empty( $attribute['purchaseOrderKeep'] ) ? 'js-po-keep' : ''; 
                            $sticky = $i === 0 ? 'sticky-col' : '';
                            ?>
                            <th class="<?php echo esc_attr( $sticky . ' ' . $purchase_order_keep ); ?>" data-id="<?php echo esc_attr( $attribute['id'] ) ?>">
                                <div class="th-content-wrapper">
                                    <?php 
                                    $required_mark = ! empty( $attribute['required'] ) ? ' <span class="required-mark">*</span>' : '';
                                    $bulk_edit_btn = ! empty( $attribute['allow_edit'] ) ? '<button type="button" class="btn btn-sm btn-light js-bulk-attribute-edit" data-id="' . esc_attr( $attribute['id'] ) . '" disabled><span class="dashicons dashicons-edit"></span></button>' : '<button type="button" class="btn btn-sm btn-light js-no-bulk-action" disabled><span class="dashicons dashicons-lock"></span></button>';
                                    $bulk_edit_btn = $args['status'] === 'trash' ? '' : $bulk_edit_btn;
                                    $edit_opts = ! empty( $attribute['edit_options'] ) && isset( $attribute['type'] ) && in_array( $attribute['type'], ['select', 'tom_select'], true ) ? 
                                        ' <div class="btn-group" role="group">
                                            <button type="button" class="btn btn-sm btn-light px-1 js-add-option" data-id="' . esc_attr( $attribute['id'] ) . '" data-name="' . esc_attr( $attribute['name'] ) . '"><span class="dashicons dashicons-plus-alt"></span></button>
                                            <button type="button" class="btn btn-sm btn-light px-1 js-remove-options" data-id="' . esc_attr( $attribute['id'] ) . '" data-name="' . esc_attr( $attribute['name'] ) . '"><span class="dashicons dashicons-trash"></span></button>
                                        </div>' : '';

                                    $description = $attribute['description'] ?? ''; 

                                    $header = '<div class="text-center text-nowrap"> ' . $bulk_edit_btn . ' <span  title="' . $description . '">' . esc_html( $attribute['name'] ) . '</span>' . $required_mark . $edit_opts . '</div>';
                                    
                                    $arr = [
                                        'div' => [
                                            'class' => true
                                        ],
                                        'span' => [
                                            'class' => true,
                                            'title' => true
                                        ],
                                        'button' => [
                                            'type' => true,
                                            'class' => true,
                                            'data-id' => true,
                                            'data-name' => true,
                                            'disabled' => true
                                        ]
                                    ];

                                    if ( $i === 0 ) {
                                        echo '<div class="d-inline-flex align-items-center gap-2 ms-2">';
                                        mdspbe_render_product_selector( 'all' );
                                        echo wp_kses( $header, $arr ); 
                                        echo '</div>';
                                    } else {
                                        echo wp_kses( $header, $arr );
                                    }
                                    ?>
                                    <div class="resizer" data-resize="col"></div>
                                </div>
                            </th>
                            <?php 
                            $i++;
                        } 
                    ?>
                    </tr>
                </thead>

                <tbody>
                    <?php foreach ( $products as $product ) {
                        $product_type = $product->get_type(); 
                        if ( $product_type === 'variable' ) {
                            mdspbe_render_product( $product, $attributes, 'variable' );
                            //mdspbe_render_variation_td( $attributes );
                            foreach ( $product->get_children() as $variation_id ) {
                                $get_product_func = mdspbe_get_product_function_name();
                                $variation = $get_product_func( $variation_id );
                                mdspbe_render_product( $variation, $attributes, 'variation' );
                            }
                        } else {
                            mdspbe_render_product( $product, $attributes, $product_type );
                        }
                    } ?>
                </tbody>
                <tfoot style="display: none;">
                    <?php 
                    foreach ( mdspbe_get_supported_product_types() as $product_type ) {
                        mdspbe_render_product( false, $attributes, $product_type );
                    }
                    ?>
                </tfoot>
            </table>
        </div>
    </form>


    <!-- Secondary elements -->
    <?php if ( $args['status'] !== 'trash' ) { ?>
    <div class="float-div float-div-left">
        <button class="btn btn-secondary btn-lg me-2 float-btn js-undo-changes" disabled>
            <span class="dashicons dashicons-undo pt-1"></span>
        </button>
        <button class="btn btn-secondary btn-lg float-btn js-redo-changes" disabled>
            <span class="dashicons dashicons-redo pt-1"></span>
        </button>
    </div>
    <div class="float-div float-div-right">
        <button class="btn btn-success btn-lg me-2 float-btn js-save-btn" style="display: none;">
            <span class="d-none d-sm-block"><?php esc_html_e( 'Save changes', 'mds-product-bulk-editor-for-woocommerce' ); ?></span>
            <span class="d-block d-sm-none dashicons dashicons-cloud-upload"></span>
        </button>
        <?php if ( current_user_can( 'publish_products' ) ) { ?>
        <button class="btn btn-primary btn-lg me-4 float-btn js-new-btn">
            <span class="d-none d-sm-block"><?php esc_html_e( 'Add new', 'mds-product-bulk-editor-for-woocommerce' ); ?></span>
            <span class="d-block d-sm-none dashicons dashicons-plus"></span>
        </button>
        <?php } ?>
    </div>
    <?php } ?>

    <div id="da-alert-container" class="position-fixed bottom-0 start-0 p-3" style="z-index: 999999;"></div>

    <div class="modal fade" id="daPromptModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-top mt-5">
            <div class="modal-content shadow-lg rounded-3">
                <div class="modal-header bg-primary text-white">
                    <h5 class="modal-title" id="customPromptLabel"></h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body bg-light">
                    <input type="text" class="form-control rounded-2 shadow-sm js-custom-prompt-input mt-2" style="display: none">
                </div>
                <div class="modal-footer bg-light border-0">
                    <button type="button" class="btn btn-primary w-100" id="customPromptSubmit"><?php esc_html_e( 'Save', 'mds-product-bulk-editor-for-woocommerce' ); ?></button>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="daFormPromptModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-top mt-5">
            <div class="modal-content shadow-lg rounded-3">
                <div class="modal-header bg-primary text-white">
                    <h5 class="modal-title" id="customFormPromptLabel"></h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body bg-light">
                    <form id="form-prompt">
                    </form>
                </div>
                <div class="modal-footer bg-light border-0 pt-0">
                    <button type="button" class="btn btn-primary w-100" id="formPromptSubmit"></button>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="daConfirmModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-top mt-5">
            <div class="modal-content shadow-lg rounded-3">
                <div class="modal-header bg-primary text-white">
                    <h5 class="modal-title"><?php esc_html_e( 'Confirm', 'mds-product-bulk-editor-for-woocommerce' ); ?></h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body bg-light">
                    <span id="daConfirmModalMessage"></span>
                </div>
                <div class="modal-footer bg-light border-0">
                    <button type="button" class="btn btn-primary w-100" id="daConfirmModalYes"><?php esc_html_e( 'Confirm', 'mds-product-bulk-editor-for-woocommerce' ); ?></button>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="floating-loader" style="display: none;">
    <div class="spinner-border text-primary" role="status">
        <span class="visually-hidden">Loading...</span>
    </div>
</div>