<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
/**
* Provide a admin area view for the plugin
*
* This file is used to markup the admin-facing aspects of the plugin.
*
* @link       https://profiles.wordpress.org/gohelkunjan
* @since      1.0.0
*
* @package    KG_Digital_Catalog_For_WooCommerce
* @subpackage KG_Digital_Catalog_For_WooCommerce/admin/partials
*/
?>
<!-- This file should primarily consist of HTML with a little bit of PHP. -->
<?php if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ){ ?>
<div class="kgdc-settings-wrap wp-core-ui">
    <div class="kgdc-card">
        <div class="kgdc-card-header">
            <h3><span class="dashicons dashicons-email-alt" style="margin-top: 2px;"></span> Email PDF Catalog</h3>
        </div>
        <div class="kgdc-card-body">
            <p>Send your generated product catalogs directly to your customers.</p>
            <a title="Email PDF Catalog" href="#TB_inline?width=400&height=500&inlineId=my-content-id" class="button thickbox kgdc-button-primary">Email to customer</a>
            
            <div id="my-content-id" style="display:none;">
                <div class="kgdc-modal-inner" style="padding: 30px; border-radius: 12px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;">
                    <form method="post" action="" name="email-form" enctype="multipart/form-data">
                        
                        <div class="kgdc-modal-header" style="margin-bottom: 25px; border-bottom: 2px solid #f0f2f5; padding-bottom: 15px;">
                            <h2 style="margin:0; color: #0a5fa5; font-size: 20px; display: flex; align-items: center; gap: 10px;">
                                <span class="dashicons dashicons-email-alt" style="font-size: 24px; width: 24px; height: 24px;"></span>
                                <?php esc_html_e('Send PDF to Customers', 'kg-digital-catalog-for-woocommerce'); ?>
                            </h2>
                        </div>

                        <?php
                        $kgdc_user_query = new WP_User_Query( array( 'role' => 'customer' ) );
                        $kgdc_customers = $kgdc_user_query->get_results();
                        
                        if ( ! empty( $kgdc_customers ) ) {
                            ?>
                            <div class="kgdc-form-group" style="display: block; margin-bottom: 20px;">
                                <label style="font-weight: 600; color: #34495e; display: block; margin-bottom: 8px;"><?php esc_html_e('Select Customer(s)', 'kg-digital-catalog-for-woocommerce'); ?>:</label>
                                <select class="kgdc-select customer-list" name="customername[]" id="multiple-checkboxes" multiple="multiple" style="width: 100%; height: 120px; background: #fff;">
                                    <?php 
                                    foreach ( $kgdc_customers as $kgdc_customer ) {
                                        $kgdc_customer_info = get_userdata( $kgdc_customer->ID );
                                        echo '<option value="' . esc_attr( $kgdc_customer->ID ) . '" style="padding: 8px;">' . esc_html( $kgdc_customer_info->display_name ) . ' (' . esc_html($kgdc_customer_info->user_email) . ')</option>';
                                    }
                                    ?>
                                </select>
                                <p class="kgdc-note"><?php esc_html_e('Hold Ctrl/Cmd to select multiple customers.', 'kg-digital-catalog-for-woocommerce'); ?></p>
                            </div>
                            <?php
                        } else {
                            echo '<div class="kgdc-note" style="background: #fff5f5; color: #c53030; padding: 15px; border-radius: 8px; margin-bottom: 20px;">' . esc_html__( 'No customers found in your WooCommerce store.', 'kg-digital-catalog-for-woocommerce' ) . '</div>';
                        }

                        $kgdc_args = array('post_type' => 'kgdc_pdf_history', 'posts_per_page' => -1);
                        $kgdc_query = new WP_Query( $kgdc_args );
                        
                        if ( $kgdc_query->have_posts() ) {
                            ?>
                            <div class="kgdc-form-group" style="display: block; margin-bottom: 30px;">
                                <label style="font-weight: 600; color: #34495e; display: block; margin-bottom: 8px;"><?php esc_html_e('Select Catalog PDF', 'kg-digital-catalog-for-woocommerce'); ?>:</label>
                                <select class="kgdc-select pdf-history" name="pdfname" style="width: 100%; border: 2px solid #edf2f7; background: #fff;">
                                    <?php 
                                    while ( $kgdc_query->have_posts() ) { 
                                        $kgdc_query->the_post();
                                        $kgdc_permalink = get_post_meta( get_the_ID(), 'kgdc_pdf_history', true );
                                        $title = get_the_title();
                                        echo '<option value="' . esc_attr( $kgdc_permalink ) . '">' . esc_html( strtoupper( preg_replace('/\\.[^\\.\\s]{3,4}$/', '', $title ) ) ) . '</option>';
                                    } 
                                    ?>
                                </select>
                            </div>
                            <?php
                        }
                        wp_reset_postdata();
                        ?>

                        <div style="text-align: right; padding-top: 20px; border-top: 2px solid #f0f2f5;">
                            <input type="button" value="<?php echo esc_attr__( 'Send Catalog Now', 'kg-digital-catalog-for-woocommerce' ); ?>" id="submit_button" class="kgdc-button-primary" style="width: 100%;" />
                        </div>
                    </form>
                </div>
            </div>


        </div>
    </div>
</div>
<?php } ?>
