<?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>
</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>
            </div>
        </div>
        <p class="muted"><?php esc_html_e( 'Add a Google Drive access token in Settings → Integrations, then paste a file ID or share link (ensure the file is accessible).', 'primelink' ); ?></p>
    </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-fetch"><?php esc_html_e( 'Fetch', 'primelink' ); ?></button>
            </div>
        </div>
        <p class="muted"><?php esc_html_e( 'Add a OneDrive access token in Settings → Integrations, then paste an item ID or share link.', '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/attributes.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/attributes.csv" class="widefat" style="width:100%;">
    </div>

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


