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

use PSSG_Sync_Sheet\App\Service\Request_Log;

$limit = $this->Products->get_one_load_limit();
$per_page = $this->Products->posts_per_page;
$checkout_sheet_url = $saved_data['sheet_url'] ?? '';
$sheet_name = $saved_data['sheet_name'] ?? '';

$stats = $this->Products->get_stats();
$founded_posts = $stats['found_posts'] ?? false;

$spreadsheet_id = $saved_data['spreadsheet_id'] ?? '';
$sheet_names = [];

$current_sheet_name = $saved_data['sheet_name'] ?? '';

// Get Access Token
$AppsScriptAccessKey = Request_Log::get_access_key();
$has_access_key = !empty($AppsScriptAccessKey);

// Check for errors
$submit_errors = $this->get_form_submited_errors();
$has_errors = count( $submit_errors ) > 0;
?>

<div class="pssg-finish-step-modern">
    <!-- Header Section -->
    <div class="pssg-finish-header">
        <?php if ( !$has_errors ) : ?>
            <div class="pssg-finish-success-icon">
                <span class="dashicons dashicons-yes-alt"></span>
            </div>
            <h2><?php echo esc_html__( 'Setup Complete!', 'product-sync-master-sheet' ); ?></h2>
            <p class="pssg-finish-subtitle"><?php echo esc_html__( 'Your WooCommerce store is now connected to Google Sheet.', 'product-sync-master-sheet' ); ?></p>
        <?php else : ?>
            <div class="pssg-finish-warning-icon">
                <span class="dashicons dashicons-warning"></span>
            </div>
            <h2><?php echo esc_html__( 'Almost There!', 'product-sync-master-sheet' ); ?></h2>
            <p class="pssg-finish-subtitle"><?php echo esc_html__( 'Please complete the following steps to finish the setup.', 'product-sync-master-sheet' ); ?></p>
        <?php endif; ?>
    </div>

    <!-- Error Messages -->
    <?php if ( $has_errors ) : ?>
    <div class="pssg-finish-errors">
        <h4><span class="dashicons dashicons-info"></span> <?php echo esc_html__( 'Steps to Complete', 'product-sync-master-sheet' ); ?></h4>
        <ul>
            <?php foreach($submit_errors as $key => $subm_error) : ?>
                <li class="pssg-error-item">
                    <span class="dashicons dashicons-dismiss"></span>
                    <?php echo esc_html( $subm_error ); ?>
                </li>
            <?php endforeach; ?>
        </ul>
        <a href="javascript:void(0);" class="pssg-btn pssg-has-icon warning pssg_modify_setup_wizard">
            <span><i class="pssg_icon-cog"></i></span>
            <strong><?php echo esc_html__( 'Modify Setup', 'product-sync-master-sheet' ); ?></strong>
        </a>
    </div>
    <?php endif; ?>

    <!-- Main Actions Grid -->
    <div class="pssg-finish-actions-grid">

         <!-- Synchronize Section -->
        <div class="pssg-finish-card pssg-sync-card <?php echo $has_errors ? 'disabled' : ''; ?>">
            <div class="pssg-card-icon">
                <span class="dashicons dashicons-update"></span>
            </div>
            <h3><?php echo esc_html__( 'Synchronize Products', 'product-sync-master-sheet' ); ?></h3>
            <p><?php echo esc_html__( 'Sync your WooCommerce products to Google Sheet.', 'product-sync-master-sheet' ); ?></p>
            
            <div class="pssg-sync-actions">
                <a id="pssg-syncronize-button" class="pssg-btn pssg-has-icon primary" data-limit="<?php echo esc_attr( $limit ); ?>" data-per_page="<?php echo esc_attr( $per_page ); ?>" data-nonce="<?php echo esc_attr( wp_create_nonce( plugin_basename( PSSG_BASE_FILE ) ) ); ?>">
                    <span><i class="pssg_icon-spin3"></i></span>
                    <strong class="sync-btn-txt"><?php echo esc_html__( 'Synchronize Sheet', 'product-sync-master-sheet' ); ?></strong>
                    <i class="product_count"></i>
                </a>
                
                <a id="pssg-clear-sheet-button" class="pssg-btn pssg-has-icon secondary">
                    <span><i class="pssg_icon-trash-empty"></i></span>
                    <strong class="delete-btn-txt"><?php echo esc_html__( 'Clear Sheet Data', 'product-sync-master-sheet' ); ?></strong>
                </a>
            </div>
            
            <div class="message-showing-area"></div>
            <div class="errors-details-area">
                <p class="error-code"></p>
                <div class="errors-lists"></div>
            </div>
        </div>       

        <!-- AppsScript Section -->
        <div class="pssg-finish-card pssg-appscript-card">
            <div class="pssg-card-icon">
                <span class="dashicons dashicons-editor-code"></span>
            </div>
            <h3><?php echo esc_html__( 'AppsScript Code', 'product-sync-master-sheet' ); ?></h3>
            <p><?php echo esc_html__( 'Copy this code and paste it in your Google Sheet.', 'product-sync-master-sheet' ); ?></p>
            
            <div class="pssg-appscript-actions">
                <a id="pssg-appscript-copy-button" class="pssg-btn pssg-has-icon primary" data-has-access-key="<?php echo $has_access_key ? 'yes' : 'no'; ?>">
                    <span><i class="pssg_icon-note"></i></span>
                    <strong><?php echo esc_html__( 'Copy AppsScript', 'product-sync-master-sheet' ); ?> <i>V<?php echo esc_html( PSSG_APPS_SCRIPT_VERSION ); ?></i></strong>
                </a>
                <p class="script-copy-msg"></p>
            </div>
            
            <?php if ( ! $has_access_key ) : ?>
            <p class="pssg-access-key-warning">
                <span class="dashicons dashicons-warning"></span>
                <?php echo esc_html__( 'Warning: Access Token is not generated.', 'product-sync-master-sheet' ); ?>
                <a href="<?php echo esc_url( admin_url( 'admin.php?page=pssg-setup-wizard#pssg-security-settings' ) ); ?>"><?php echo esc_html__( 'Generate Token', 'product-sync-master-sheet' ); ?></a>
            </p>
            <?php endif; ?>

            <div class="pssg-appscript-steps">
                <p><strong><?php echo esc_html__( 'Steps:', 'product-sync-master-sheet' ); ?></strong></p>
                <ol>
                    <li><?php echo esc_html__( 'Click "Copy AppsScript" above', 'product-sync-master-sheet' ); ?></li>
                    <li><?php echo esc_html__( 'Open your Google Sheet', 'product-sync-master-sheet' ); ?></li>
                    <li><?php echo esc_html__( 'Go to Extensions > Apps Script', 'product-sync-master-sheet' ); ?></li>
                    <li><?php echo esc_html__( 'Paste the code and Save', 'product-sync-master-sheet' ); ?></li>
                </ol>
            </div>
            <p class="pssg-finish-errors">
                <span class="dashicons dashicons-warning"></span>
                <?php echo esc_html__( 'Make sure to update the Google Sheet AppsScript code.', 'product-sync-master-sheet' ); ?>
            </p>
        </div>

        

        <!-- Quick Links Section -->
        <div class="pssg-finish-card pssg-links-card">
            <div class="pssg-card-icon">
                <span class="dashicons dashicons-admin-links"></span>
            </div>
            <h3><?php echo esc_html__( 'Quick Links', 'product-sync-master-sheet' ); ?></h3>
            
            <div class="pssg-quick-links">
                <a href="<?php echo esc_url( $checkout_sheet_url ); ?>" class="pssg-quick-link" target="_blank">
                    <span class="dashicons dashicons-media-spreadsheet"></span>
                    <?php echo esc_html__( 'Open Google Sheet', 'product-sync-master-sheet' ); ?>
                </a>
                
                <a href="javascript:void(0);" class="pssg-quick-link pssg_modify_setup_wizard">
                    <span class="dashicons dashicons-admin-generic"></span>
                    <?php echo esc_html__( 'Modify Setup', 'product-sync-master-sheet' ); ?>
                </a>
                
                <a href="<?php echo esc_url( admin_url( 'admin.php?page=pssg-setup-wizard#pssg-security-settings' ) ); ?>" class="pssg-quick-link">
                    <span class="dashicons dashicons-lock"></span>
                    <?php echo esc_html__( 'Security Settings', 'product-sync-master-sheet' ); ?>
                </a>
                
                <a href="<?php echo esc_url( admin_url( 'admin.php?page=pssg-api-logs' ) ); ?>" class="pssg-quick-link">
                    <span class="dashicons dashicons-list-view"></span>
                    <?php echo esc_html__( 'API Logs', 'product-sync-master-sheet' ); ?>
                </a>
            </div>
        </div>
    </div>

    <!-- Stats Section -->
    <div class="pssg-finish-stats">
        <h3><span class="dashicons dashicons-chart-bar"></span> <?php echo esc_html__( 'Basic Information', 'product-sync-master-sheet' ); ?></h3>
        <div class="pssg-stats-grid">
            <?php 
            unset($stats['rows_count']);
            if(is_array( $stats )){
                foreach($stats as $key => $stat){
                    echo '<div class="pssg-stat-item">';
                    echo '<span class="pssg-stat-label">'.esc_html( ucfirst(str_replace('_', ' ', $key)) ).'</span>';
                    echo '<span class="pssg-stat-value">'.esc_html( $stat ).'</span>';
                    echo '</div>';
                }
            }
            ?>
        </div>
        
        <?php if( ! $this->is_premium ) : ?>
        <div class="pssg-premium-notice">
            <span class="dashicons dashicons-heart"></span>
            <span><?php echo esc_html__( 'Free Version: Sync up to 100 Products.', 'product-sync-master-sheet' ); ?></span>
            <strong><?php echo sprintf( esc_html__( 'Current Products: %s', 'product-sync-master-sheet' ), esc_html( $founded_posts ) ); ?></strong>
            <a class="pssg-btn primary small" href="https://codeastrology.com/sync-master-sheet/pricing/" target="_blank">
                <?php echo esc_html__( 'Get Premium', 'product-sync-master-sheet' ); ?>
            </a>
        </div>
        <?php endif; ?>
    </div>
</div>

<!-- Hidden AppsScript Code Area -->
<textarea id="appscript-code-area" style="display: none;visibility:hidden;height:0;width:0;">
<?php 
$website_routespace = $this->Api->route_namespace . '/' . $this->Api->product_route;
$website_routespace_multiple = $this->Api->route_namespace . '/' . $this->Api->multiple_product_route;
$replacements = [
    'pssg_namespace_route' => $website_routespace,
    'multiple_product_route' => $website_routespace_multiple,
    'pssg_json_website' => trailingslashit( get_rest_url() )
];
$script_file = PSSG_BASE_DIR . 'assets/js/script.js';
if( is_file( $script_file ) ){
    $code = pssg_file_get_content( $script_file );
    $code = preg_replace('/\/\/(.+?)\n/', '', $code);
    $code = preg_replace('/\/\*.*?\*\//s', '', $code);
    $code = preg_replace('/^\s*[\r\n]/m', '', $code);
    $code = str_replace( array_keys($replacements), array_values($replacements), $code );
    $appsV = 'var AppsScriptVersion = "' . PSSG_APPS_SCRIPT_VERSION . '";';
    $appsAccessKey = 'var AppsScriptAccessKey = "' . esc_js( $AppsScriptAccessKey ) . '";';
    $extraCode = $appsV . $appsAccessKey;
    $code = $extraCode . $code;
    $code = 'var YourSheetTabName = "' . sanitize_text_field( $sheet_name ) . '";' . $code;
    echo esc_js( $code );
}
?>
</textarea>

<style>
.pssg-finish-step-modern {
    max-width: 1200px;
    margin: 0 auto;
    padding: 30px 20px;
}

.pssg-finish-header {
    text-align: center;
    margin-bottom: 40px;
}

.pssg-finish-success-icon,
.pssg-finish-warning-icon {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 20px;
}

.pssg-finish-success-icon {
    background: linear-gradient(135deg, #673AB7, #673AB7);
}

.pssg-finish-warning-icon {
    background: linear-gradient(135deg, #fdcb6e, #f39c12);
}

.pssg-finish-success-icon .dashicons,
.pssg-finish-warning-icon .dashicons {
    font-size: 40px;
    width: 40px;
    height: 40px;
    color: #fff;
}

.pssg-finish-header h2 {
    font-size: 28px;
    margin: 0 0 10px 0;
    color: #2d3436;
}

.pssg-finish-subtitle {
    font-size: 16px;
    color: #636e72;
    margin: 0;
}

.pssg-finish-errors {
    background: #fff3cd;
    border: 1px solid #ffc107;
    border-radius: 10px;
    padding: 20px;
    margin-bottom: 30px;
}

.pssg-finish-errors h4 {
    margin: 0 0 15px 0;
    color: #856404;
    display: flex;
    align-items: center;
    gap: 8px;
}

.pssg-finish-errors ul {
    margin: 0 0 15px 0;
    padding: 0;
    list-style: none;
}

.pssg-error-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 0;
    color: #856404;
}

.pssg-error-item .dashicons {
    color: #dc3545;
}

.pssg-finish-actions-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 25px;
    margin-bottom: 30px;
}

.pssg-finish-card {
    background: #fff;
    border-radius: 12px;
    padding: 25px;
    box-shadow: 0 2px 15px rgba(0,0,0,0.08);
    transition: transform 0.3s, box-shadow 0.3s;
}

.pssg-finish-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 5px 25px rgba(0,0,0,0.12);
}

.pssg-finish-card.disabled {
    opacity: 0.6;
    pointer-events: none;
}

.pssg-card-icon {
    width: 50px;
    height: 50px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 15px;
}

.pssg-appscript-card .pssg-card-icon {
    background: linear-gradient(135deg, #673AB7, #673AB7);
}

.pssg-sync-card .pssg-card-icon {
    background: linear-gradient(135deg, #00b894, #00b894);
}

.pssg-links-card .pssg-card-icon {
    background: linear-gradient(135deg, #0984e3, #0984e3);
}

.pssg-card-icon .dashicons {
    font-size: 24px;
    width: 24px;
    height: 24px;
    color: #fff;
}

.pssg-finish-card h3 {
    font-size: 18px;
    margin: 0 0 10px 0;
    color: #2d3436;
}

.pssg-finish-card p {
    color: #636e72;
    margin: 0 0 15px 0;
    font-size: 14px;
}

.pssg-appscript-actions,
.pssg-sync-actions {
    margin-bottom: 15px;
}

.pssg-sync-actions .pssg-btn {
    display: flex;
    margin-bottom: 10px;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
}

.pssg-access-key-warning {
    background: #fff3cd;
    padding: 10px 15px;
    border-radius: 6px;
    color: #856404;
    font-size: 13px;
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.pssg-access-key-warning a {
    color: #0984e3;
}

.pssg-appscript-steps {
    background: #f8f9fa;
    padding: 15px;
    border-radius: 8px;
    margin-top: 15px;
}

.pssg-appscript-steps p {
    margin: 0 0 10px 0;
}

.pssg-appscript-steps ol {
    margin: 0;
    padding-left: 20px;
}

.pssg-appscript-steps li {
    font-size: 13px;
    color: #636e72;
    margin-bottom: 5px;
}

.pssg-quick-links {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.pssg-quick-link {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 15px;
    background: #f8f9fa;
    border-radius: 8px;
    text-decoration: none;
    color: #2d3436;
    transition: background 0.3s;
}

.pssg-quick-link:hover {
    background: #e9ecef;
    color: #0984e3;
}

.pssg-quick-link .dashicons {
    color: #0984e3;
}

.pssg-finish-stats {
    background: #fff;
    border-radius: 12px;
    padding: 25px;
    box-shadow: 0 2px 15px rgba(0,0,0,0.08);
}

.pssg-finish-stats h3 {
    margin: 0 0 20px 0;
    display: flex;
    align-items: center;
    gap: 10px;
    color: #2d3436;
}

.pssg-stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 15px;
    margin-bottom: 20px;
}

.pssg-stat-item {
    background: #f8f9fa;
    padding: 15px;
    border-radius: 8px;
    text-align: center;
}

.pssg-stat-label {
    display: block;
    font-size: 12px;
    color: #636e72;
    text-transform: uppercase;
    margin-bottom: 5px;
}

.pssg-stat-value {
    display: block;
    font-size: 20px;
    font-weight: 600;
    color: #2d3436;
}

.pssg-premium-notice {
    background: linear-gradient(135deg, #c1c1c1, #95959552);
    padding: 15px 20px;
    border-radius: 10px;
    color: #ff0000a1;
    display: flex;
    align-items: center;
    gap: 15px;
    flex-wrap: wrap;
}

.pssg-premium-notice .dashicons {
    font-size: 24px;
}

.pssg-premium-notice strong {
    margin-left: auto;
}

.pssg-btn.secondary {
    background: #6c757d;
    border-color: #5a6268;
    color: #fff;
}

.pssg-btn.secondary:hover {
    background: #545b62;
}

.pssg-btn.small {
    padding: 8px 16px;
    font-size: 13px;
}
</style>

