<?php
/* Copyright (C) 2026 Eduard Wisch <data@data-it-solution.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 */

/**
 * \file    importzugferd/admin/setup.php
 * \ingroup importzugferd
 * \brief   ImportZugferd setup page.
 */

// Load Dolibarr environment
$res = 0;
if (!$res && !empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) {
    $res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php";
}
$tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME'];
$tmp2 = realpath(__FILE__);
$i = strlen($tmp) - 1;
$j = strlen($tmp2) - 1;
while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) {
    $i--;
    $j--;
}
if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1))."/main.inc.php")) {
    $res = @include substr($tmp, 0, ($i + 1))."/main.inc.php";
}
if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php")) {
    $res = @include dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php";
}
if (!$res && file_exists("../../main.inc.php")) {
    $res = @include "../../main.inc.php";
}
if (!$res && file_exists("../../../main.inc.php")) {
    $res = @include "../../../main.inc.php";
}
if (!$res) {
    die("Include of main fails");
}

// Libraries
require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php";
require_once '../lib/importzugferd.lib.php';

// Translations
$langs->loadLangs(array("admin", "importzugferd@importzugferd"));

// Parameters
$action = GETPOST('action', 'aZ09');
$backtopage = GETPOST('backtopage', 'alpha');

// Access control
if (!$user->admin) {
    accessforbidden();
}

// Form setup using FormSetup class
if (!class_exists('FormSetup')) {
    require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php';
}
$formSetup = new FormSetup($db);

/*
 * Setup configuration items
 */

// IMAP Settings Section
$formSetup->newItem('IMAPSettings')->setAsTitle();

$item = $formSetup->newItem('IMPORTZUGFERD_IMAP_HOST');
$item->defaultFieldValue = '';
$item->cssClass = 'minwidth300';
$item->fieldAttr['placeholder'] = 'imap.example.com';

$item = $formSetup->newItem('IMPORTZUGFERD_IMAP_PORT');
$item->defaultFieldValue = '993';
$item->cssClass = 'width100';

$item = $formSetup->newItem('IMPORTZUGFERD_IMAP_USER');
$item->defaultFieldValue = '';
$item->cssClass = 'minwidth300';
$item->fieldAttr['placeholder'] = 'invoices@example.com';

$item = $formSetup->newItem('IMPORTZUGFERD_IMAP_PASSWORD');
$item->cssClass = 'minwidth300';
$item->fieldAttr['type'] = 'password';

$item = $formSetup->newItem('IMPORTZUGFERD_IMAP_FOLDER');
$item->defaultFieldValue = 'INBOX';
$item->cssClass = 'minwidth200';

$formSetup->newItem('IMPORTZUGFERD_IMAP_SSL')->setAsYesNo();

// Import Settings Section
$formSetup->newItem('ImportSettings')->setAsTitle();

$formSetup->newItem('IMPORTZUGFERD_AUTO_CREATE_INVOICE')->setAsYesNo();

// Email Notification Settings Section
$formSetup->newItem('NotificationSettings')->setAsTitle();

$formSetup->newItem('IMPORTZUGFERD_NOTIFY_ENABLED')->setAsYesNo();

$item = $formSetup->newItem('IMPORTZUGFERD_NOTIFY_EMAIL');
$item->defaultFieldValue = '';
$item->cssClass = 'minwidth300';
$item->fieldAttr['placeholder'] = 'admin@example.com';

$formSetup->newItem('IMPORTZUGFERD_NOTIFY_MANUAL')->setAsYesNo();
$formSetup->newItem('IMPORTZUGFERD_NOTIFY_ERROR')->setAsYesNo();
$formSetup->newItem('IMPORTZUGFERD_NOTIFY_PRICE_DIFF')->setAsYesNo();

$item = $formSetup->newItem('IMPORTZUGFERD_PRICE_DIFF_THRESHOLD');
$item->defaultFieldValue = '10';
$item->cssClass = 'width75';
$item->fieldAttr['type'] = 'number';
$item->fieldAttr['min'] = '0';
$item->fieldAttr['max'] = '100';
$item->fieldAttr['step'] = '1';

// Scheduling Settings Section
$formSetup->newItem('SchedulingSettings')->setAsTitle();

$item = $formSetup->newItem('IMPORTZUGFERD_IMPORT_FREQUENCY');
$item->setAsSelect(array(
    'manual' => $langs->trans('FrequencyManual'),
    'hourly' => $langs->trans('FrequencyHourly'),
    'daily' => $langs->trans('FrequencyDaily'),
    'weekly' => $langs->trans('FrequencyWeekly')
));
$item->defaultFieldValue = 'manual';

// Folder Import Settings Section
$formSetup->newItem('FolderImportSettings')->setAsTitle();

$item = $formSetup->newItem('IMPORTZUGFERD_WATCH_FOLDER');
$item->defaultFieldValue = '';
$item->cssClass = 'minwidth400';
$item->fieldAttr['placeholder'] = '/path/to/invoices';

$item = $formSetup->newItem('IMPORTZUGFERD_ARCHIVE_FOLDER');
$item->defaultFieldValue = '';
$item->cssClass = 'minwidth400';
$item->fieldAttr['placeholder'] = '/path/to/archive';

$item = $formSetup->newItem('IMPORTZUGFERD_ERROR_FOLDER');
$item->defaultFieldValue = '';
$item->cssClass = 'minwidth400';
$item->fieldAttr['placeholder'] = '/path/to/errors';

$item = $formSetup->newItem('IMPORTZUGFERD_IMAP_ARCHIVE_FOLDER');
$item->defaultFieldValue = 'Archive';
$item->cssClass = 'minwidth200';

// Datanorm Settings Section
$formSetup->newItem('DatanormSettings')->setAsTitle();

$item = $formSetup->newItem('IMPORTZUGFERD_DATANORM_MARKUP');
$item->defaultFieldValue = '30';
$item->cssClass = 'width100';
$item->fieldAttr['placeholder'] = '30';

$formSetup->newItem('IMPORTZUGFERD_DATANORM_SEARCH_ALL')->setAsYesNo();

// Accounting Codes Section (Standard-Konten für neue Produkte)
$formSetup->newItem('AccountingSettings')->setAsTitle();

$item = $formSetup->newItem('IMPORTZUGFERD_ACCOUNTING_CODE_SELL');
$item->defaultFieldValue = '';
$item->cssClass = 'minwidth200';
$item->fieldAttr['placeholder'] = '700000';

$item = $formSetup->newItem('IMPORTZUGFERD_ACCOUNTING_CODE_SELL_INTRA');
$item->defaultFieldValue = '';
$item->cssClass = 'minwidth200';
$item->fieldAttr['placeholder'] = '700100';

$item = $formSetup->newItem('IMPORTZUGFERD_ACCOUNTING_CODE_SELL_EXPORT');
$item->defaultFieldValue = '';
$item->cssClass = 'minwidth200';
$item->fieldAttr['placeholder'] = '700200';

$item = $formSetup->newItem('IMPORTZUGFERD_ACCOUNTING_CODE_BUY');
$item->defaultFieldValue = '';
$item->cssClass = 'minwidth200';
$item->fieldAttr['placeholder'] = '400000';

$item = $formSetup->newItem('IMPORTZUGFERD_ACCOUNTING_CODE_BUY_INTRA');
$item->defaultFieldValue = '';
$item->cssClass = 'minwidth200';
$item->fieldAttr['placeholder'] = '400100';

$item = $formSetup->newItem('IMPORTZUGFERD_ACCOUNTING_CODE_BUY_EXPORT');
$item->defaultFieldValue = '';
$item->cssClass = 'minwidth200';
$item->fieldAttr['placeholder'] = '400200';

/*
 * Actions
 */

if (versioncompare(explode('.', DOL_VERSION), array(15)) < 0 && $action == 'update' && !empty($user->admin)) {
    $formSetup->saveConfFromPost();
}

include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';

// AJAX action for folder browsing
if ($action == 'browse_folders') {
    $path = GETPOST('path', 'alpha');
    $target = GETPOST('target', 'alpha');

    // Sanitize path - default to /home for easier navigation
    if (empty($path)) {
        $path = '/home';
    }
    $path = realpath($path);
    if ($path === false) {
        $path = '/home';
        if (!is_dir($path)) {
            $path = '/';
        }
    }

    // Get directories
    $dirs = array();
    if (is_dir($path) && is_readable($path)) {
        $entries = @scandir($path);
        if ($entries) {
            foreach ($entries as $entry) {
                if ($entry == '.') continue;
                $fullPath = $path . '/' . $entry;
                if (is_dir($fullPath) && is_readable($fullPath)) {
                    $dirs[] = array(
                        'name' => $entry,
                        'path' => $fullPath
                    );
                }
            }
        }
    }

    // Return JSON
    header('Content-Type: application/json');
    echo json_encode(array(
        'current' => $path,
        'parent' => dirname($path),
        'dirs' => $dirs,
        'target' => $target
    ));
    exit;
}

// Save folder from browser
if ($action == 'set_folder') {
    $target = GETPOST('target', 'alpha');
    $folder_path = GETPOST('folder_path', 'alpha');

    if (in_array($target, array('IMPORTZUGFERD_WATCH_FOLDER', 'IMPORTZUGFERD_ARCHIVE_FOLDER'))) {
        if (is_dir($folder_path)) {
            dolibarr_set_const($db, $target, $folder_path, 'chaine', 0, '', $conf->entity);
            setEventMessages($langs->trans('FolderSelected').': '.$folder_path, null, 'mesgs');
        } else {
            setEventMessages($langs->trans('ErrorFolderNotFound'), null, 'errors');
        }
    }
    header('Location: '.$_SERVER['PHP_SELF']);
    exit;
}

/*
 * View
 */

$form = new Form($db);

$title = "ImportZugferdSetup";
llxHeader('', $langs->trans($title), '', '', 0, 0, '', '', '', 'mod-importzugferd page-admin');

// Subheader
$linkback = '<a href="'.($backtopage ? $backtopage : DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1').'">'.$langs->trans("BackToModuleList").'</a>';

print load_fiche_titre($langs->trans($title), $linkback, 'title_setup');

// Configuration header
$head = importzugferdAdminPrepareHead();
print dol_get_fiche_head($head, 'settings', $langs->trans($title), -1, "importzugferd@importzugferd");

// Setup page description
print '<span class="opacitymedium">'.$langs->trans("ImportZugferdSetupPage").'</span><br><br>';

// Display the form
print $formSetup->generateOutput(true);

// Build folder validation data for JavaScript
$folderValidation = array();

// Watch folder - only needs to be readable
$watchFolder = getDolGlobalString('IMPORTZUGFERD_WATCH_FOLDER');
if (!empty($watchFolder)) {
    $watchExists = is_dir($watchFolder);
    $watchReadable = $watchExists && is_readable($watchFolder);
    if (!$watchExists) {
        $folderValidation['IMPORTZUGFERD_WATCH_FOLDER'] = array('ok' => false, 'msg' => $langs->trans('FolderNotFound'));
    } elseif (!$watchReadable) {
        $folderValidation['IMPORTZUGFERD_WATCH_FOLDER'] = array('ok' => false, 'msg' => $langs->trans('FolderNotReadable'));
    } else {
        $files = glob($watchFolder.'/*.pdf');
        $files = array_merge($files ?: [], glob($watchFolder.'/*.PDF') ?: []);
        $folderValidation['IMPORTZUGFERD_WATCH_FOLDER'] = array('ok' => true, 'msg' => $langs->trans('FolderOK').' ('.count($files).' PDF)');
    }
}

// Archive folder - needs to be writable
$archiveFolder = getDolGlobalString('IMPORTZUGFERD_ARCHIVE_FOLDER');
if (!empty($archiveFolder)) {
    $archiveExists = is_dir($archiveFolder);
    $archiveWritable = $archiveExists && is_writable($archiveFolder);
    if (!$archiveExists) {
        $folderValidation['IMPORTZUGFERD_ARCHIVE_FOLDER'] = array('ok' => false, 'msg' => $langs->trans('FolderNotFound'));
    } elseif (!$archiveWritable) {
        $folderValidation['IMPORTZUGFERD_ARCHIVE_FOLDER'] = array('ok' => false, 'msg' => $langs->trans('FolderNotWritable'));
    } else {
        $folderValidation['IMPORTZUGFERD_ARCHIVE_FOLDER'] = array('ok' => true, 'msg' => $langs->trans('FolderOK'));
    }
}

// Error folder - needs to be writable
$errorFolder = getDolGlobalString('IMPORTZUGFERD_ERROR_FOLDER');
if (!empty($errorFolder)) {
    $errorExists = is_dir($errorFolder);
    $errorWritable = $errorExists && is_writable($errorFolder);
    if (!$errorExists) {
        $folderValidation['IMPORTZUGFERD_ERROR_FOLDER'] = array('ok' => false, 'msg' => $langs->trans('FolderNotFound'));
    } elseif (!$errorWritable) {
        $folderValidation['IMPORTZUGFERD_ERROR_FOLDER'] = array('ok' => false, 'msg' => $langs->trans('FolderNotWritable'));
    } else {
        $folderValidation['IMPORTZUGFERD_ERROR_FOLDER'] = array('ok' => true, 'msg' => $langs->trans('FolderOK'));
    }
}

// Folder Browser Modal
print '
<div id="folderBrowserModal" style="display:none; position:fixed; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,0.5); z-index:9999;">
    <div style="position:absolute; top:50%; left:50%; transform:translate(-50%,-50%); background:white; padding:20px; border-radius:8px; min-width:500px; max-width:80%; max-height:80%; overflow:auto; box-shadow:0 4px 20px rgba(0,0,0,0.3);">
        <h3 style="margin-top:0;"><i class="fas fa-folder-open paddingright"></i>'.$langs->trans('SelectFolder').'</h3>
        <div style="margin-bottom:10px;">
            <input type="text" id="pathInput" style="width:80%; font-family:monospace;" placeholder="/path/to/folder">
            <a class="button buttongen smallpaddingimp" href="#" onclick="goToPath(); return false;" title="'.$langs->trans('Go').'"><i class="fas fa-arrow-right"></i></a>
        </div>
        <div style="margin-bottom:5px;">
            <span class="opacitymedium">'.$langs->trans('QuickLinks').':</span>
            <a href="#" onclick="loadFolderContents(\'/home\'); return false;" class="paddingleft">/home</a>
            <a href="#" onclick="loadFolderContents(\'/srv\'); return false;" class="paddingleft">/srv</a>
            <a href="#" onclick="loadFolderContents(\'/var\'); return false;" class="paddingleft">/var</a>
            <a href="#" onclick="loadFolderContents(\'/tmp\'); return false;" class="paddingleft">/tmp</a>
        </div>
        <div style="border:1px solid #ccc; padding:10px; max-height:300px; overflow-y:auto; background:#f9f9f9;" id="folderList">
        </div>
        <div style="margin-top:15px; text-align:right;">
            <input type="hidden" id="folderTarget" value="">
            <a class="button" href="#" onclick="selectCurrentFolder(); return false;"><i class="fas fa-check paddingright"></i>'.$langs->trans('SelectThisFolder').'</a>
            <a class="button" href="#" onclick="closeFolderBrowser(); return false;">'.$langs->trans('Cancel').'</a>
        </div>
    </div>
</div>

<script>
// Folder validation data from PHP
var folderValidation = '.json_encode($folderValidation).';

// Add browse buttons and validation icons next to folder input fields
document.addEventListener("DOMContentLoaded", function() {
    var folderFields = ["IMPORTZUGFERD_WATCH_FOLDER", "IMPORTZUGFERD_ARCHIVE_FOLDER", "IMPORTZUGFERD_ERROR_FOLDER"];
    folderFields.forEach(function(fieldName) {
        var input = document.querySelector("input[name=\"" + fieldName + "\"]");
        if (input) {
            // Add browse button
            var btn = document.createElement("a");
            btn.href = "#";
            btn.className = "button buttongen smallpaddingimp";
            btn.style.marginLeft = "5px";
            btn.innerHTML = "<i class=\"fas fa-folder-open\"></i>";
            btn.title = "'.$langs->trans('Browse').'";
            btn.onclick = function(e) {
                e.preventDefault();
                var startPath = input.value || "/home";
                openFolderBrowser(fieldName, startPath);
            };
            input.parentNode.insertBefore(btn, input.nextSibling);

            // Add validation icon if folder is configured
            if (folderValidation[fieldName]) {
                var validIcon = document.createElement("span");
                validIcon.style.marginLeft = "10px";
                validIcon.id = "validation_" + fieldName;
                if (folderValidation[fieldName].ok) {
                    validIcon.innerHTML = "<i class=\"fas fa-check-circle\" style=\"color:green;\"></i> <span class=\"opacitymedium\">" + folderValidation[fieldName].msg + "</span>";
                } else {
                    validIcon.innerHTML = "<i class=\"fas fa-times-circle\" style=\"color:red;\"></i> <span style=\"color:red;\">" + folderValidation[fieldName].msg + "</span>";
                }
                btn.parentNode.insertBefore(validIcon, btn.nextSibling);
            }
        }
    });
});

function openFolderBrowser(target, startPath) {
    document.getElementById("folderTarget").value = target;
    document.getElementById("folderBrowserModal").style.display = "block";
    document.getElementById("pathInput").value = startPath || "/home";
    loadFolderContents(startPath || "/home");
}

function goToPath() {
    var path = document.getElementById("pathInput").value;
    if (path) {
        loadFolderContents(path);
    }
}

function closeFolderBrowser() {
    document.getElementById("folderBrowserModal").style.display = "none";
}

function loadFolderContents(path) {
    var target = document.getElementById("folderTarget").value;
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "'.$_SERVER['PHP_SELF'].'?action=browse_folders&path=" + encodeURIComponent(path) + "&target=" + target + "&token='.newToken().'", true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4 && xhr.status === 200) {
            var data = JSON.parse(xhr.responseText);
            document.getElementById("pathInput").value = data.current;

            var html = "";
            if (data.current !== "/" && data.parent) {
                html += "<div style=\"padding:5px; cursor:pointer; border-bottom:1px solid #eee;\" onclick=\"loadFolderContents(\'" + data.parent.replace(/\'/g, "\\\'") + "\')\">";
                html += "<i class=\"fas fa-level-up-alt paddingright\"></i><strong>..</strong> ('.$langs->trans('ParentFolder').')";
                html += "</div>";
            }

            for (var i = 0; i < data.dirs.length; i++) {
                var dir = data.dirs[i];
                html += "<div style=\"padding:5px; cursor:pointer; border-bottom:1px solid #eee;\" onclick=\"loadFolderContents(\'" + dir.path.replace(/\'/g, "\\\'") + "\')\" onmouseover=\"this.style.background=\'#e8f4fc\'\" onmouseout=\"this.style.background=\'transparent\'\">";
                html += "<i class=\"fas fa-folder paddingright\" style=\"color:#f0ad4e;\"></i>" + dir.name;
                html += "</div>";
            }

            if (data.dirs.length === 0 && data.current !== "/") {
                html += "<div style=\"padding:10px; color:#666; text-align:center;\">'.$langs->trans('NoSubfolders').'</div>";
            }

            document.getElementById("folderList").innerHTML = html;
        }
    };
    xhr.send();
}

function selectCurrentFolder() {
    var path = document.getElementById("pathInput").value;
    var target = document.getElementById("folderTarget").value;
    // Update the input field directly
    var input = document.querySelector("input[name=\"" + target + "\"]");
    if (input) {
        input.value = path;
    }
    closeFolderBrowser();
}

// Close modal on escape key
document.addEventListener("keydown", function(e) {
    if (e.key === "Escape") closeFolderBrowser();
});

// Close modal on background click
document.getElementById("folderBrowserModal").addEventListener("click", function(e) {
    if (e.target === this) closeFolderBrowser();
});
</script>
';

// Email Notification Test Section
if (getDolGlobalString('IMPORTZUGFERD_NOTIFY_ENABLED') && getDolGlobalString('IMPORTZUGFERD_NOTIFY_EMAIL')) {
    print '<br>';
    print '<div class="div-table-responsive-no-min">';
    print '<table class="noborder centpercent">';
    print '<tr class="liste_titre">';
    print '<td colspan="2">'.$langs->trans('TestEmailNotification').'</td>';
    print '</tr>';

    // Handle test email action
    if ($action == 'test_email') {
        dol_include_once('/importzugferd/class/importnotification.class.php');
        $notification = new ImportNotification($db);
        $result = $notification->sendTestNotification();

        if ($result > 0) {
            setEventMessages($langs->trans('TestEmailSent', getDolGlobalString('IMPORTZUGFERD_NOTIFY_EMAIL')), null, 'mesgs');
        } else {
            setEventMessages($langs->trans('TestEmailFailed').': '.$notification->error, null, 'errors');
        }
    }

    print '<tr class="oddeven">';
    print '<td class="titlefield">'.$langs->trans('SendTestEmail').'</td>';
    print '<td>';
    print '<a class="button" href="'.$_SERVER['PHP_SELF'].'?action=test_email&token='.newToken().'">';
    print '<i class="fas fa-paper-plane paddingright"></i>'.$langs->trans('SendTestEmail');
    print '</a>';
    print ' <span class="opacitymedium">'.$langs->trans('SendTo').': '.getDolGlobalString('IMPORTZUGFERD_NOTIFY_EMAIL').'</span>';
    print '</td>';
    print '</tr>';

    print '</table>';
    print '</div>';
}

// Test IMAP connection button and folder selection
if (getDolGlobalString('IMPORTZUGFERD_IMAP_HOST')) {
    print '<br>';
    print '<div class="div-table-responsive-no-min">';
    print '<table class="noborder centpercent">';
    print '<tr class="liste_titre">';
    print '<td colspan="3">'.$langs->trans('TestConnection').'</td>';
    print '</tr>';

    // Check if IMAP extension is available
    $imap_available = function_exists('imap_open');

    // Test connection action
    $imap_folders = array();
    $connection_ok = false;

    if (!$imap_available) {
        print '<tr class="oddeven">';
        print '<td colspan="3">';
        print '<span class="error"><i class="fas fa-exclamation-triangle paddingright"></i>';
        print $langs->trans('IMAPExtensionNotInstalled');
        print '</span><br>';
        print '<span class="opacitymedium">'.$langs->trans('IMAPExtensionHelp').'</span>';
        print '</td>';
        print '</tr>';
    } elseif ($action == 'test_imap' || $action == 'select_folder') {
        $host = getDolGlobalString('IMPORTZUGFERD_IMAP_HOST');
        $port = getDolGlobalString('IMPORTZUGFERD_IMAP_PORT', '993');
        $imap_user = getDolGlobalString('IMPORTZUGFERD_IMAP_USER');
        $password = getDolGlobalString('IMPORTZUGFERD_IMAP_PASSWORD');
        $ssl = getDolGlobalString('IMPORTZUGFERD_IMAP_SSL');

        $mailbox_base = '{' . $host . ':' . $port . '/imap' . ($ssl ? '/ssl' : '') . '/novalidate-cert}';
        $mailbox = $mailbox_base . 'INBOX';

        $connection = @imap_open($mailbox, $imap_user, $password);

        if ($connection) {
            $connection_ok = true;
            setEventMessages($langs->trans('ConnectionSuccessful'), null, 'mesgs');

            // Get list of folders
            $folders_raw = imap_list($connection, $mailbox_base, '*');
            if ($folders_raw) {
                foreach ($folders_raw as $folder) {
                    // Remove the mailbox base from folder name
                    $folder_name = str_replace($mailbox_base, '', $folder);
                    // Decode folder name (IMAP uses modified UTF-7)
                    $folder_name_decoded = imap_utf7_decode($folder_name);
                    $imap_folders[$folder_name] = $folder_name_decoded;
                }
            }

            imap_close($connection);
        } else {
            setEventMessages($langs->trans('ConnectionFailed') . ': ' . imap_last_error(), null, 'errors');
        }
    }

    // Save selected folder
    if ($action == 'select_folder' && GETPOST('imap_folder', 'alpha')) {
        $selected_folder = GETPOST('imap_folder', 'alpha');
        dolibarr_set_const($db, 'IMPORTZUGFERD_IMAP_FOLDER', $selected_folder, 'chaine', 0, '', $conf->entity);
        setEventMessages($langs->trans('FolderSelected').': '.$selected_folder, null, 'mesgs');
    }

    // Only show status and folder selection if IMAP is available
    if ($imap_available) {
        print '<tr class="oddeven">';
        print '<td class="titlefield">'.$langs->trans('Status').'</td>';
        print '<td colspan="2">';
        if ($action == 'test_imap' || $action == 'select_folder') {
            if ($connection_ok) {
                print '<span class="ok"><i class="fas fa-check paddingright"></i>'.$langs->trans('ConnectionSuccessful').'</span>';
            } else {
                print '<span class="error"><i class="fas fa-times paddingright"></i>'.$langs->trans('ConnectionFailed').'</span>';
            }
        } else {
            print '<span class="opacitymedium">'.$langs->trans('ClickTestToCheck').'</span>';
        }
        print '</td>';
        print '</tr>';
    }

    // Show folder selection if connection was successful
    if ($imap_available && $connection_ok && !empty($imap_folders)) {
        print '<tr class="oddeven">';
        print '<td>'.$langs->trans('SelectFolder').'</td>';
        print '<td>';
        print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" class="inline-block">';
        print '<input type="hidden" name="token" value="'.newToken().'">';
        print '<input type="hidden" name="action" value="select_folder">';

        $current_folder = getDolGlobalString('IMPORTZUGFERD_IMAP_FOLDER', 'INBOX');
        print '<select name="imap_folder" class="flat minwidth200">';
        foreach ($imap_folders as $folder_raw => $folder_decoded) {
            $selected = ($folder_raw == $current_folder) ? ' selected' : '';
            print '<option value="'.dol_escape_htmltag($folder_raw).'"'.$selected.'>';
            print dol_escape_htmltag($folder_decoded);
            print '</option>';
        }
        print '</select>';
        print ' <input type="submit" class="button" value="'.$langs->trans('Save').'">';
        print '</form>';
        print '</td>';
        print '<td>';
        print '<span class="opacitymedium">'.$langs->trans('FoundFolders').': '.count($imap_folders).'</span>';
        print '</td>';
        print '</tr>';
    }

    // Only show test button if IMAP extension is available
    if ($imap_available) {
        print '<tr class="oddeven">';
        print '<td colspan="3" class="center">';
        print '<a class="button" href="'.$_SERVER['PHP_SELF'].'?action=test_imap&token='.newToken().'">';
        print '<i class="fas fa-plug paddingright"></i>'.$langs->trans('TestConnection');
        print '</a>';
        print '</td>';
        print '</tr>';
    }

    print '</table>';
    print '</div>';
}

// Manual Import Trigger Section
$hasFolder = getDolGlobalString('IMPORTZUGFERD_WATCH_FOLDER') && is_dir(getDolGlobalString('IMPORTZUGFERD_WATCH_FOLDER'));
$hasImap = getDolGlobalString('IMPORTZUGFERD_IMAP_HOST') && getDolGlobalString('IMPORTZUGFERD_IMAP_USER');

if ($hasFolder || $hasImap) {
    print '<br>';
    print '<div class="div-table-responsive-no-min">';
    print '<table class="noborder centpercent">';
    print '<tr class="liste_titre">';
    print '<td colspan="2">'.$langs->trans('ManualImportTrigger').'</td>';
    print '</tr>';

    // Handle manual import action
    if ($action == 'run_import') {
        $source = GETPOST('import_source', 'alpha');
        dol_include_once('/importzugferd/class/zugferdimport.class.php');
        dol_include_once('/importzugferd/class/zugferdparser.class.php');

        $successCount = 0;
        $errorCount = 0;
        $skippedCount = 0;

        if ($source == 'folder' && $hasFolder) {
            $watchFolder = getDolGlobalString('IMPORTZUGFERD_WATCH_FOLDER');
            $archiveFolder = getDolGlobalString('IMPORTZUGFERD_ARCHIVE_FOLDER');
            $errorFolder = getDolGlobalString('IMPORTZUGFERD_ERROR_FOLDER');
            $autoCreate = getDolGlobalInt('IMPORTZUGFERD_AUTO_CREATE_INVOICE');

            $files = glob($watchFolder.'/*.pdf');
            $files = array_merge($files, glob($watchFolder.'/*.PDF'));

            // Create archive folder if configured but doesn't exist
            if (!empty($archiveFolder) && !is_dir($archiveFolder)) {
                dol_mkdir($archiveFolder);
            }
            // Create error folder if configured but doesn't exist
            if (!empty($errorFolder) && !is_dir($errorFolder)) {
                dol_mkdir($errorFolder);
            }

            // Helper function for moving files with fallback
            $moveFile = function($file, $targetFolder, $prefix) {
                if (empty($targetFolder)) {
                    return false;
                }
                if (!is_dir($targetFolder)) {
                    dol_mkdir($targetFolder);
                }
                if (!is_dir($targetFolder) || !is_writable($targetFolder)) {
                    dol_syslog("ImportZugferd: Target folder not accessible: ".$targetFolder, LOG_WARNING);
                    return false;
                }
                $destFile = $targetFolder.'/'.$prefix.date('Y-m-d_His').'_'.basename($file);
                if (@rename($file, $destFile)) {
                    dol_syslog("ImportZugferd: Moved file to: ".$destFile, LOG_INFO);
                    return true;
                }
                // Fallback: copy + delete (for cross-filesystem moves)
                if (@copy($file, $destFile)) {
                    @unlink($file);
                    dol_syslog("ImportZugferd: Copied file to: ".$destFile, LOG_INFO);
                    return true;
                }
                dol_syslog("ImportZugferd: Failed to move file: ".$file." to ".$destFile, LOG_ERR);
                return false;
            };

            foreach ($files as $file) {
                $import = new ZugferdImport($db);
                $result = $import->importFromFile($user, $file, $autoCreate);

                if ($result > 0) {
                    $successCount++;
                    $moveFile($file, $archiveFolder, 'imported_');
                } elseif ($result == -2) {
                    // Duplicate - move to archive
                    $skippedCount++;
                    if (!$moveFile($file, $archiveFolder, 'duplicate_')) {
                        @unlink($file);
                    }
                } else {
                    // Error - move to error folder, fallback to archive
                    $errorCount++;
                    if (!$moveFile($file, $errorFolder, 'error_')) {
                        if (!$moveFile($file, $archiveFolder, 'error_')) {
                            dol_syslog("ImportZugferd: File stays in watch folder: ".$file, LOG_WARNING);
                        }
                    }
                }
            }
        } elseif ($source == 'imap' && $hasImap && function_exists('imap_open')) {
            $host = getDolGlobalString('IMPORTZUGFERD_IMAP_HOST');
            $port = getDolGlobalString('IMPORTZUGFERD_IMAP_PORT', '993');
            $imap_user = getDolGlobalString('IMPORTZUGFERD_IMAP_USER');
            $password = getDolGlobalString('IMPORTZUGFERD_IMAP_PASSWORD');
            $ssl = getDolGlobalString('IMPORTZUGFERD_IMAP_SSL');
            $folder = getDolGlobalString('IMPORTZUGFERD_IMAP_FOLDER', 'INBOX');
            $archiveFolder = getDolGlobalString('IMPORTZUGFERD_IMAP_ARCHIVE_FOLDER');
            $autoCreate = getDolGlobalInt('IMPORTZUGFERD_AUTO_CREATE_INVOICE');

            $mailbox = '{' . $host . ':' . $port . '/imap' . ($ssl ? '/ssl' : '') . '/novalidate-cert}' . $folder;
            $connection = @imap_open($mailbox, $imap_user, $password);

            if ($connection) {
                $emails = imap_search($connection, 'UNSEEN');
                if ($emails) {
                    foreach ($emails as $email_number) {
                        $structure = imap_fetchstructure($connection, $email_number);

                        // Find PDF attachments
                        if (isset($structure->parts)) {
                            foreach ($structure->parts as $partIndex => $part) {
                                $filename = '';
                                if ($part->ifdparameters) {
                                    foreach ($part->dparameters as $param) {
                                        if (strtolower($param->attribute) == 'filename') {
                                            $filename = $param->value;
                                        }
                                    }
                                }
                                if (empty($filename) && $part->ifparameters) {
                                    foreach ($part->parameters as $param) {
                                        if (strtolower($param->attribute) == 'name') {
                                            $filename = $param->value;
                                        }
                                    }
                                }

                                if (!empty($filename) && preg_match('/\.pdf$/i', $filename)) {
                                    $attachment = imap_fetchbody($connection, $email_number, $partIndex + 1);
                                    if ($part->encoding == 3) { // BASE64
                                        $attachment = base64_decode($attachment);
                                    } elseif ($part->encoding == 4) { // QUOTED-PRINTABLE
                                        $attachment = quoted_printable_decode($attachment);
                                    }

                                    // Save to temp file
                                    $tempFile = $conf->importzugferd->dir_temp.'/'.uniqid().'_'.$filename;
                                    if (!is_dir($conf->importzugferd->dir_temp)) {
                                        dol_mkdir($conf->importzugferd->dir_temp);
                                    }
                                    file_put_contents($tempFile, $attachment);

                                    // Import
                                    $import = new ZugferdImport($db);
                                    $result = $import->importFromFile($user, $tempFile, $autoCreate);

                                    if ($result > 0) {
                                        $successCount++;
                                    } elseif ($result == -2) {
                                        $skippedCount++;
                                    } else {
                                        $errorCount++;
                                    }

                                    unlink($tempFile);
                                }
                            }
                        }
                    }

                    // Archive processed emails
                    if (!empty($archiveFolder) && $successCount > 0) {
                        foreach ($emails as $email_number) {
                            imap_mail_move($connection, $email_number, $archiveFolder);
                        }
                        imap_expunge($connection);
                    }
                }
                imap_close($connection);
            } else {
                setEventMessages($langs->trans('ConnectionFailed').': '.imap_last_error(), null, 'errors');
            }
        }

        if ($successCount > 0 || $errorCount > 0 || $skippedCount > 0) {
            setEventMessages($langs->trans('BatchImportComplete', $successCount, $errorCount, $skippedCount), null, 'mesgs');
        } else {
            setEventMessages($langs->trans('NoFilesFound'), null, 'warnings');
        }
    }

    print '<tr class="oddeven">';
    print '<td class="titlefield">'.$langs->trans('ImportFromFolder').'</td>';
    print '<td>';
    if ($hasFolder) {
        print '<a class="button" href="'.$_SERVER['PHP_SELF'].'?action=run_import&import_source=folder&token='.newToken().'">';
        print '<i class="fas fa-folder-open paddingright"></i>'.$langs->trans('StartImport');
        print '</a>';
        print ' <span class="opacitymedium">'.getDolGlobalString('IMPORTZUGFERD_WATCH_FOLDER').'</span>';
    } else {
        print '<span class="opacitymedium">'.$langs->trans('ErrorWatchFolderNotConfigured').'</span>';
    }
    print '</td>';
    print '</tr>';

    print '<tr class="oddeven">';
    print '<td>'.$langs->trans('ImportFromIMAP').'</td>';
    print '<td>';
    if ($hasImap && function_exists('imap_open')) {
        print '<a class="button" href="'.$_SERVER['PHP_SELF'].'?action=run_import&import_source=imap&token='.newToken().'">';
        print '<i class="fas fa-envelope paddingright"></i>'.$langs->trans('StartImport');
        print '</a>';
        print ' <span class="opacitymedium">'.getDolGlobalString('IMPORTZUGFERD_IMAP_USER').'</span>';
    } elseif (!function_exists('imap_open')) {
        print '<span class="opacitymedium">'.$langs->trans('IMAPExtensionNotInstalled').'</span>';
    } else {
        print '<span class="opacitymedium">'.$langs->trans('ErrorIMAPNotConfigured').'</span>';
    }
    print '</td>';
    print '</tr>';

    print '</table>';
    print '</div>';
}

print '<br>';

// Page end
print dol_get_fiche_end();

llxFooter();
$db->close();
