// Account Summary View — shared JS for the "Edit Account Details" modal
// rendered by src/AccountSummaryView.php's renderAccountAdditionalDetails(),
// used by both Trading Center's Account Home and Journal's "View Account
// Summary" modal. Extracted 2026-08-18 from trading-center.php's own inline
// script (byte-identical behavior), the same account-picker.css/.js
// precedent this app already established for the shared account picker.
//
// tcRevealSecret(btn, accountId) — the one deliberate change from
// trading-center.php's original version, which read a page-level
// `<?= json_encode($accountId ?? '') ?>` PHP-baked variable instead of a
// parameter. That only worked because Trading Center's Account Home is a
// single-account-per-page-load context; a shared script loaded on two
// different pages can't rely on a page-global like that, so the account id
// is now passed explicitly from the onclick handler
// (renderAccountAdditionalDetails()'s own `tcRevealSecret(this, <id>)` call).

function tcOpenMetaModal() {
    document.getElementById('tc-meta-backdrop').style.display = 'flex';
    document.body.style.overflow = 'hidden';
}
function tcCloseMetaModal() {
    document.getElementById('tc-meta-backdrop').style.display = 'none';
    document.body.style.overflow = '';
}
function tcSaveMeta(accountId) {
    var errEl = document.getElementById('tc-meta-error');
    errEl.style.display = 'none';
    var payload = {
        account_id: accountId,
        platform: document.getElementById('tc-meta-platform').value,
        broker_server: document.getElementById('tc-meta-server').value,
        broker_login: document.getElementById('tc-meta-login').value,
        account_number: document.getElementById('tc-meta-account-number').value,
        tags: document.getElementById('tc-meta-tags').value,
        account_notes: document.getElementById('tc-meta-notes').value
    };
    var execEl = document.getElementById('tc-meta-execution-model');
    if (execEl) payload.execution_model = execEl.value;
    var clearBox = document.getElementById('tc-meta-password-clear');
    var pwField = document.getElementById('tc-meta-password');
    if ((clearBox && clearBox.checked) || (pwField && pwField.value !== '')) {
        payload.broker_password = (clearBox && clearBox.checked) ? '' : pwField.value;
    }
    var btn = document.getElementById('tc-meta-save-btn');
    btn.disabled = true;
    btn.textContent = 'Saving…';
    fetch('/traderalign/api/journal/update-account-meta', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(payload)
    }).then(function (r) { return r.json(); }).then(function (data) {
        btn.disabled = false;
        btn.textContent = 'Save';
        if (data && data.success) {
            window.location.reload();
        } else {
            errEl.textContent = (data && data.error) ? data.error : 'Could not save. Try again.';
            errEl.style.display = 'block';
        }
    }).catch(function () {
        btn.disabled = false;
        btn.textContent = 'Save';
        errEl.textContent = 'Could not save. Try again.';
        errEl.style.display = 'block';
    });
}
function tcRevealSecret(btn, accountId) {
    var display = document.getElementById('tc-pw-display');
    if (btn.getAttribute('data-revealed') === '1') {
        display.textContent = '••••••••';
        btn.setAttribute('data-revealed', '0');
        return;
    }
    btn.disabled = true;
    fetch('/traderalign/api/journal/reveal-account-secret', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ account_id: accountId })
    }).then(function (r) { return r.json(); }).then(function (data) {
        btn.disabled = false;
        if (data && data.success && data.password) {
            display.textContent = data.password;
            btn.setAttribute('data-revealed', '1');
        }
    }).catch(function () { btn.disabled = false; });
}
