<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
$mt_primelink_settings           = get_option( 'mt_primelink_settings', array() );
$mt_primelink_dropbox_prefill    = isset( $mt_primelink_settings['dropbox_default_path'] ) ? $mt_primelink_settings['dropbox_default_path'] : '';
$mt_primelink_ftp_host           = isset( $mt_primelink_settings['ftp_host'] ) ? $mt_primelink_settings['ftp_host'] : '';
$mt_primelink_ftp_port           = isset( $mt_primelink_settings['ftp_port'] ) ? $mt_primelink_settings['ftp_port'] : '';
$mt_primelink_ftp_user           = isset( $mt_primelink_settings['ftp_user'] ) ? $mt_primelink_settings['ftp_user'] : '';
$mt_primelink_ftp_pass           = isset( $mt_primelink_settings['ftp_pass'] ) ? $mt_primelink_settings['ftp_pass'] : '';
$mt_primelink_ftp_root           = isset( $mt_primelink_settings['ftp_root'] ) ? $mt_primelink_settings['ftp_root'] : '';
$mt_primelink_ftp_default_file   = isset( $mt_primelink_settings['ftp_default_file'] ) ? $mt_primelink_settings['ftp_default_file'] : '';
$mt_primelink_ftp_protocol       = isset( $mt_primelink_settings['ftp_protocol'] ) ? $mt_primelink_settings['ftp_protocol'] : 'ftp';
$mt_primelink_ftp_passive        = ! empty( $mt_primelink_settings['ftp_passive'] );
$mt_primelink_gdrive_default     = isset( $mt_primelink_settings['gdrive_default_file'] ) ? $mt_primelink_settings['gdrive_default_file'] : '';
$mt_primelink_onedrive_default   = isset( $mt_primelink_settings['onedrive_default_file'] ) ? $mt_primelink_settings['onedrive_default_file'] : '';
?>

<div class="mt_primelink-summary-eyebrow"><?php esc_html_e( 'Step 2 · Source', 'primelink' ); ?></div>
<h2><?php esc_html_e( 'Select Source', 'primelink' ); ?></h2>
<input type="hidden" id="mt-primelink-step" value="2">

<div class="mt-primelink-grid">
    <label class="mt-primelink-choice glass">
        <input type="radio" name="source" value="desktop" checked>
        <span><?php esc_html_e( 'Upload from Desktop', 'primelink' ); ?></span>
    </label>

    <label class="mt-primelink-choice glass">
        <input type="radio" name="source" value="existing">
        <span><?php esc_html_e( 'Use existing file', 'primelink' ); ?></span>
    </label>

    <label class="mt-primelink-choice glass">
        <input type="radio" name="source" value="dropbox">
        <span><?php esc_html_e( 'Upload From Dropbox', 'primelink' ); ?></span>
    </label>

    <label class="mt-primelink-choice glass">
        <input type="radio" name="source" value="gdrive">
        <span><?php esc_html_e( 'Upload From Google Drive', 'primelink' ); ?></span>
    </label>

    <label class="mt-primelink-choice glass">
        <input type="radio" name="source" value="onedrive">
        <span><?php esc_html_e( 'Upload From OneDrive', 'primelink' ); ?></span>
    </label>

    <label class="mt-primelink-choice glass">
        <input type="radio" name="source" value="ftp">
        <span><?php esc_html_e( 'Upload From FTP/SFTP', 'primelink' ); ?></span>
    </label>

    <label class="mt-primelink-choice glass">
        <input type="radio" name="source" value="url">
        <span><?php esc_html_e( 'URL', 'primelink' ); ?></span>
    </label>

    <label class="mt-primelink-choice glass">
        <input type="radio" name="source" value="platform">
        <span>
            <strong><?php esc_html_e( 'Import from Platform (Real-time)', 'primelink' ); ?></strong>
            <span class="mt_primelink-summary-pill" style="margin-left:8px;"><?php esc_html_e( 'HOT', 'primelink' ); ?></span>
        </span>
    </label>
</div>

<div class="mt-primelink-source-panels">
    <div class="mt-panel glass" data-panel="desktop">
        <h3><?php esc_html_e( 'Upload file', 'primelink' ); ?></h3>
        <div class="mt-primelink-dropzone" id="mt-primelink-dropzone">
            <div class="mt-primelink-dropzone-content">
                <div class="mt-primelink-dropzone-icon">
                    <span class="dashicons dashicons-upload"></span>
                </div>
                <p class="mt-primelink-dropzone-text">
                    <strong><?php esc_html_e( 'Drag & drop your file here', 'primelink' ); ?></strong>
                    <?php esc_html_e( 'or', 'primelink' ); ?>
                    <label for="mt-primelink-upload-file" class="mt-primelink-dropzone-link">
                        <?php esc_html_e( 'browse', 'primelink' ); ?>
                    </label>
                </p>
                <p class="mt-primelink-dropzone-hint">
                    <?php esc_html_e( 'Supported formats: CSV, XML, XLSX, XLS', 'primelink' ); ?>
                </p>
            </div>
            <input type="file" id="mt-primelink-upload-file" accept=".csv,.xml,.xlsx,.xls" style="display:none;">
            <div class="mt-primelink-dropzone-progress" id="mt-primelink-dropzone-progress" style="display:none;">
                <div class="mt-primelink-dropzone-progress-bar">
                    <div class="mt-primelink-dropzone-progress-fill" id="mt-primelink-dropzone-progress-fill"></div>
                </div>
                <span class="mt-primelink-dropzone-progress-text" id="mt-primelink-dropzone-progress-text">0%</span>
            </div>
        </div>
    </div>

    <div class="mt-panel glass" data-panel="existing" style="display:none;">
        <h3><?php esc_html_e( 'Existing upload (path inside /wp-content/uploads/)', 'primelink' ); ?></h3>
        <div class="mt_primelink-input-row">
            <input type="text" id="mt-primelink-existing-path" name="path" placeholder="/wp-content/uploads/primelink/yourfile.csv" aria-label="<?php esc_attr_e( 'Existing file path', 'primelink' ); ?>">
            <div class="mt_primelink-input-actions">
                <select id="mt_primelink-existing-recent" aria-label="<?php esc_attr_e( 'Recent paths', 'primelink' ); ?>">
                    <option value=""><?php esc_html_e( 'Recent...', 'primelink' ); ?></option>
                </select>
                <button type="button" class="button" id="mt_primelink-existing-refresh"><?php esc_html_e( 'Refresh', 'primelink' ); ?></button>
                <button type="button" class="button" id="mt_primelink-existing-browse"><?php esc_html_e( 'Browse', 'primelink' ); ?></button>
            </div>
        </div>
        <p class="muted"><?php esc_html_e( 'Pick a recent uploads path or paste one (e.g. uploads/primelink/feed.csv).', 'primelink' ); ?></p>
    </div>

    <div class="mt-panel glass" data-panel="dropbox" style="display:none;">
        <h3><?php esc_html_e( 'Dropbox', 'primelink' ); ?></h3>
        <div class="mt_primelink-input-row">
            <input type="text" id="mt_primelink-dropbox-path" name="dropbox_path" value="<?php echo esc_attr( $mt_primelink_dropbox_prefill ); ?>" placeholder="/Products/feed.xml or shared link" aria-label="<?php esc_attr_e( 'Dropbox file path or shared link', 'primelink' ); ?>">
            <div class="mt_primelink-input-actions">
                <button type="button" class="button" id="mt_primelink-dropbox-fetch"><?php esc_html_e( 'Fetch', 'primelink' ); ?></button>
                <button type="button" class="button" id="mt_primelink-dropbox-list"><?php esc_html_e( 'Browse', 'primelink' ); ?></button>
            </div>
        </div>
        <p class="muted"><?php esc_html_e( 'Add a Dropbox access token in Settings → Integrations, then paste a file path (/folder/file.xml) or a shared link.', 'primelink' ); ?></p>
        <div id="mt_primelink-dropbox-files" class="mt_primelink-dropbox-list" style="margin-top:8px;"></div>
    </div>

    <div class="mt-panel glass" data-panel="gdrive" style="display:none;">
        <h3><?php esc_html_e( 'Google Drive', 'primelink' ); ?></h3>
        <div class="mt_primelink-input-row">
            <input type="text" id="mt_primelink-gdrive-file" name="gdrive_file" value="<?php echo esc_attr( $mt_primelink_gdrive_default ); ?>" placeholder="<?php esc_attr_e( 'File ID or share link', 'primelink' ); ?>" aria-label="<?php esc_attr_e( 'Google Drive file ID or link', 'primelink' ); ?>">
            <div class="mt_primelink-input-actions">
                <button type="button" class="button" id="mt_primelink-gdrive-fetch"><?php esc_html_e( 'Fetch', 'primelink' ); ?></button>
                <button type="button" class="button" id="mt_primelink-gdrive-list"><?php esc_html_e( 'Browse', 'primelink' ); ?></button>
            </div>
        </div>
        <div id="mt_primelink-gdrive-search" style="margin-top:8px; display:none;">
            <input type="text" id="mt_primelink-gdrive-search-input" placeholder="<?php esc_attr_e( 'Search files...', 'primelink' ); ?>" style="width:100%; padding:8px;">
        </div>
        <div id="mt_primelink-gdrive-files" class="mt_primelink-gdrive-list" style="margin-top:8px;"></div>
        <p class="muted">
            <?php esc_html_e( 'Add a Google Drive API Key or Client ID in Settings → Integrations.', 'primelink' ); ?><br>
            <strong><?php esc_html_e( 'How it works:', 'primelink' ); ?></strong><br>
            • <strong><?php esc_html_e( 'Fetch:', 'primelink' ); ?></strong> <?php esc_html_e( 'Works with API Key - paste a file ID or share link. File must be public (shared with "Anyone with the link").', 'primelink' ); ?><br>
            • <strong><?php esc_html_e( 'Browse:', 'primelink' ); ?></strong> <?php esc_html_e( 'Opens Google Picker to select files directly from your Google Drive. Requires Client ID and Google Picker API enabled.', 'primelink' ); ?>
        </p>
        <details style="margin-top:10px;">
            <summary style="cursor:pointer;color:#0073aa;text-decoration:underline;"><?php esc_html_e( 'How to configure Google Picker API', 'primelink' ); ?></summary>
            <ol style="margin:10px 0;padding-left:20px;">
                <li><?php esc_html_e( 'Go to', 'primelink' ); ?> <a href="https://console.cloud.google.com/" target="_blank"><?php esc_html_e( 'Google Cloud Console', 'primelink' ); ?></a></li>
                <li><?php esc_html_e( 'Select your project', 'primelink' ); ?></li>
                <li><strong><?php esc_html_e( 'Enable APIs:', 'primelink' ); ?></strong>
                    <ul style="margin:5px 0; padding-left:20px;">
                        <li><?php esc_html_e( 'APIs & Services → Library → Search "Google Picker API" → Enable', 'primelink' ); ?></li>
                        <li><?php esc_html_e( 'APIs & Services → Library → Search "Google Drive API" → Enable (if not already)', 'primelink' ); ?></li>
                    </ul>
                </li>
                <li><strong><?php esc_html_e( 'Configure OAuth Consent Screen:', 'primelink' ); ?></strong>
                    <ul style="margin:5px 0; padding-left:20px;">
                        <li><?php esc_html_e( 'APIs & Services → OAuth consent screen', 'primelink' ); ?></li>
                        <li><?php esc_html_e( 'Click "ADD OR REMOVE SCOPES"', 'primelink' ); ?></li>
                        <li><?php esc_html_e( 'Search and add:', 'primelink' ); ?> <code>https://www.googleapis.com/auth/drive.readonly</code></li>
                        <li><?php esc_html_e( 'Click "UPDATE" and "SAVE AND CONTINUE"', 'primelink' ); ?></li>
                    </ul>
                </li>
                <li><strong><?php esc_html_e( 'Add Test User (if app is in Testing mode):', 'primelink' ); ?></strong>
                    <ul style="margin:5px 0; padding-left:20px;">
                        <li><?php esc_html_e( 'In OAuth consent screen → Test users', 'primelink' ); ?></li>
                        <li><?php esc_html_e( 'Click "ADD USERS" and add your Google account email', 'primelink' ); ?></li>
                    </ul>
                </li>
                <li><?php esc_html_e( 'Save and try "Browse" again', 'primelink' ); ?></li>
            </ol>
        </details>
        <details style="margin-top:10px;">
            <summary style="cursor:pointer;color:#0073aa;text-decoration:underline;"><?php esc_html_e( 'How to make a file public for "Fetch"', 'primelink' ); ?></summary>
            <ol style="margin:10px 0;padding-left:20px;">
                <li><?php esc_html_e( 'Open the file in Google Drive', 'primelink' ); ?></li>
                <li><?php esc_html_e( 'Click the "Share" button (top right)', 'primelink' ); ?></li>
                <li><?php esc_html_e( 'Click "Change" next to "Restricted"', 'primelink' ); ?></li>
                <li><?php esc_html_e( 'Select "Anyone with the link"', 'primelink' ); ?></li>
                <li><?php esc_html_e( 'Set permission to "Viewer"', 'primelink' ); ?></li>
                <li><?php esc_html_e( 'Copy the share link or file ID and paste it in the "Fetch" field above', 'primelink' ); ?></li>
            </ol>
            <p style="margin-top:10px;"><strong><?php esc_html_e( 'Note:', 'primelink' ); ?></strong> <?php esc_html_e( 'If you use "Browse" with OAuth, you don\'t need to make files public - you can access all your files directly.', 'primelink' ); ?></p>
        </details>
    </div>

    <div class="mt-panel glass" data-panel="onedrive" style="display:none;">
        <h3><?php esc_html_e( 'OneDrive', 'primelink' ); ?></h3>
        <div class="mt_primelink-input-row">
            <input type="text" id="mt_primelink-onedrive-file" name="onedrive_file" value="<?php echo esc_attr( $mt_primelink_onedrive_default ); ?>" placeholder="<?php esc_attr_e( 'Item ID or share link', 'primelink' ); ?>" aria-label="<?php esc_attr_e( 'OneDrive file ID or link', 'primelink' ); ?>">
            <div class="mt_primelink-input-actions">
                <button type="button" class="button" id="mt_primelink-onedrive-list"><?php esc_html_e( 'Browse', 'primelink' ); ?></button>
                <button type="button" class="button" id="mt_primelink-onedrive-fetch"><?php esc_html_e( 'Fetch', 'primelink' ); ?></button>
            </div>
        </div>
        <div id="mt_primelink-onedrive-search" style="margin-top:8px; display:none;">
            <input type="text" id="mt_primelink-onedrive-search-input" placeholder="<?php esc_attr_e( 'Search files...', 'primelink' ); ?>" style="width:100%; padding:8px;">
        </div>
        <div id="mt_primelink-onedrive-files" class="mt_primelink-onedrive-list" style="margin-top:8px;"></div>
        <p class="muted">
            <?php esc_html_e( 'Configure OneDrive OAuth in Settings → Integrations.', 'primelink' ); ?><br>
            <strong><?php esc_html_e( 'How it works:', 'primelink' ); ?></strong><br>
            • <strong><?php esc_html_e( 'Fetch:', 'primelink' ); ?></strong> <?php esc_html_e( 'Paste a file ID or share link directly.', 'primelink' ); ?><br>
            • <strong><?php esc_html_e( 'Browse:', 'primelink' ); ?></strong> <?php esc_html_e( 'Lists files from your OneDrive. Requires OAuth authentication (Client ID, Client Secret, Tenant ID).', 'primelink' ); ?>
        </p>
    </div>

    <div class="mt-panel glass" data-panel="ftp" style="display:none;">
        <h3><?php esc_html_e( 'FTP / SFTP', 'primelink' ); ?></h3>
        <div class="mt_primelink-settings-row">
            <label class="mt_primelink-form-row">
                <span class="mt_primelink-label"><?php esc_html_e( 'Protocol', 'primelink' ); ?></span>
                <select id="mt_primelink-ftp-protocol">
                    <option value="ftp" <?php selected( 'ftp', $mt_primelink_ftp_protocol ); ?>>FTP</option>
                    <option value="ftps" <?php selected( 'ftps', $mt_primelink_ftp_protocol ); ?>>FTPS</option>
                    <option value="sftp" <?php selected( 'sftp', $mt_primelink_ftp_protocol ); ?>>SFTP</option>
                </select>
            </label>
            <label class="mt_primelink-form-row">
                <span class="mt_primelink-label"><?php esc_html_e( 'Host', 'primelink' ); ?></span>
                <input type="text" id="mt_primelink-ftp-host" value="<?php echo esc_attr( $mt_primelink_ftp_host ); ?>" placeholder="ftp.example.com">
            </label>
            <label class="mt_primelink-form-row">
                <span class="mt_primelink-label"><?php esc_html_e( 'Port', 'primelink' ); ?></span>
                <input type="number" id="mt_primelink-ftp-port" value="<?php echo esc_attr( $mt_primelink_ftp_port ); ?>" placeholder="21">
            </label>
        </div>
        <div class="mt_primelink-settings-row">
            <label class="mt_primelink-form-row">
                <span class="mt_primelink-label"><?php esc_html_e( 'Username', 'primelink' ); ?></span>
                <input type="text" id="mt_primelink-ftp-user" value="<?php echo esc_attr( $mt_primelink_ftp_user ); ?>" placeholder="ftpuser">
            </label>
            <label class="mt_primelink-form-row">
                <span class="mt_primelink-label"><?php esc_html_e( 'Password', 'primelink' ); ?></span>
                <input type="password" id="mt_primelink-ftp-pass" value="<?php echo esc_attr( $mt_primelink_ftp_pass ); ?>" placeholder="ftp password">
            </label>
            <label class="mt_primelink-check" style="align-self:flex-end;">
                <input type="checkbox" id="mt_primelink-ftp-passive" <?php checked( $mt_primelink_ftp_passive ); ?>>
                <?php esc_html_e( 'Passive mode', 'primelink' ); ?>
            </label>
        </div>
        <div class="mt_primelink-settings-row">
            <label class="mt_primelink-form-row" style="flex:1;">
                <span class="mt_primelink-label"><?php esc_html_e( 'Root folder (optional)', 'primelink' ); ?></span>
                <input type="text" id="mt_primelink-ftp-root" value="<?php echo esc_attr( $mt_primelink_ftp_root ); ?>" placeholder="/feeds">
            </label>
            <label class="mt_primelink-form-row" style="flex:1;">
                <span class="mt_primelink-label"><?php esc_html_e( 'File path (optional)', 'primelink' ); ?></span>
                <input type="text" id="mt_primelink-ftp-file" value="<?php echo esc_attr( $mt_primelink_ftp_default_file ); ?>" placeholder="/feeds/products.xml">
                <small class="description" style="margin-top:4px;display:block;"><?php esc_html_e( 'Leave empty to test connection only', 'primelink' ); ?></small>
            </label>
            <div class="mt_primelink-input-actions" style="align-self:flex-end;">
                <button type="button" class="button" id="mt_primelink-ftp-test"><?php esc_html_e( 'Test Connection', 'primelink' ); ?></button>
                <button type="button" class="button button-primary" id="mt_primelink-ftp-fetch"><?php esc_html_e( 'Fetch', 'primelink' ); ?></button>
            </div>
        </div>
        <p class="muted"><?php esc_html_e( 'We will download this file to your uploads/primelink/ folder and continue the import.', 'primelink' ); ?></p>
    </div>

    <div class="mt-panel glass" data-panel="url" style="display:none;">
        <h3><?php esc_html_e( 'Remote URL', 'primelink' ); ?></h3>
        <input type="url" id="mt-primelink-url" name="url" placeholder="https://example.com/products.csv" class="widefat" style="width:100%;">
    </div>

    <div class="mt-panel glass" data-panel="platform" style="display:none;">
        <h3><?php esc_html_e( 'Import from E-commerce Platform', 'primelink' ); ?></h3>
        <p class="description" style="margin-bottom:16px;">
            <?php esc_html_e( 'Connect to external platforms and import products, orders, or customers in real-time.', 'primelink' ); ?>
        </p>

        <div class="mt_primelink-form-row">
            <label for="mt_primelink-platform-connector"><?php esc_html_e( 'Select Platform', 'primelink' ); ?></label>
            <select id="mt_primelink-platform-connector" class="widefat">
                <option value=""><?php esc_html_e( '-- Select Platform --', 'primelink' ); ?></option>
                <option value="shopify"><?php esc_html_e( 'Shopify', 'primelink' ); ?></option>
                <option value="magento"><?php esc_html_e( 'Magento', 'primelink' ); ?></option>
                <option value="bigcommerce"><?php esc_html_e( 'BigCommerce', 'primelink' ); ?></option>
            </select>
        </div>

        <div id="mt_primelink-platform-credentials" style="display:none;margin-top:16px;padding-top:16px;border-top:1px solid rgba(255,255,255,0.1);">
            <h4><?php esc_html_e( 'Connection Credentials', 'primelink' ); ?></h4>
            <div id="mt_primelink-platform-credentials-fields"></div>
            <div style="margin-top:12px;">
                <button type="button" class="button" id="mt_primelink-platform-test-connection">
                    <?php esc_html_e( 'Test Connection', 'primelink' ); ?>
                </button>
                <span id="mt_primelink-platform-connection-status" style="margin-left:12px;"></span>
            </div>
        </div>

        <div id="mt_primelink-platform-options" style="display:none;margin-top:16px;padding-top:16px;border-top:1px solid rgba(255,255,255,0.1);">
            <h4><?php esc_html_e( 'Import Options', 'primelink' ); ?></h4>
            
            <div id="mt_primelink-platform-import-info" style="margin-top:12px;padding:12px;background:rgba(0,150,255,0.1);border-left:3px solid #0096ff;border-radius:4px;">
                <p style="margin:0;"><strong><?php esc_html_e( 'Platform Import', 'primelink' ); ?></strong></p>
                <p style="margin:4px 0 0;font-size:13px;"><?php esc_html_e( 'Data will be automatically fetched and previewed after testing the connection.', 'primelink' ); ?></p>
            </div>

            <div class="mt_primelink-form-row">
                <label class="mt_primelink-check">
                    <input type="checkbox" id="mt_primelink-platform-realtime" />
                    <strong><?php esc_html_e( 'Enable Real-time Sync', 'primelink' ); ?></strong>
                </label>
                <p class="description" style="margin-top:4px;">
                    <?php esc_html_e( 'Automatically sync changes from the platform using webhooks.', 'primelink' ); ?>
                </p>
            </div>

            <div id="mt_primelink-platform-realtime-options" style="display:none;margin-top:12px;padding:12px;background:rgba(0,0,0,0.2);border-radius:4px;">
                <div class="mt_primelink-form-row">
                    <label><?php esc_html_e( 'Sync Events', 'primelink' ); ?></label>
                    <label class="mt_primelink-check"><input type="checkbox" class="mt_primelink-platform-event" value="created" checked /> <?php esc_html_e( 'Created', 'primelink' ); ?></label>
                    <label class="mt_primelink-check"><input type="checkbox" class="mt_primelink-platform-event" value="updated" checked /> <?php esc_html_e( 'Updated', 'primelink' ); ?></label>
                    <label class="mt_primelink-check"><input type="checkbox" class="mt_primelink-platform-event" value="deleted" /> <?php esc_html_e( 'Deleted', 'primelink' ); ?></label>
                </div>
            </div>


            <div id="mt_primelink-platform-preview" style="display:none;margin-top:16px;padding:12px;background:rgba(0,0,0,0.2);border-radius:4px;">
                <h5><?php esc_html_e( 'Preview', 'primelink' ); ?></h5>
                <div id="mt_primelink-platform-preview-content"></div>
            </div>
        </div>
    </div>

    <div class="mt-panel glass" id="mt-primelink-source-result" style="display:none;"></div>
</div>
