<?php
$titles = __('error_titles', true);
$bodies = __('error_bodies', true);

if (isset($_GET['err'])) {
    pjUtil::printNotice(@$titles[$_GET['err']], @$bodies[$_GET['err']]);
}

$lp_arr = isset($tpl['lp_arr']) && is_array($tpl['lp_arr']) ? $tpl['lp_arr'] : array();
$is_flag_ready = !empty($tpl['is_flag_ready']);
$locale_str = isset($tpl['locale_str']) ? $tpl['locale_str'] : '{}';
$arr = isset($tpl['arr']) ? $tpl['arr'] : array();
$defaultLocaleId = 1;
foreach ($lp_arr as $locale) {
    if ((int) $locale['is_default'] === 1) {
        $defaultLocaleId = (int) $locale['id'];
        break;
    }
}
$scProfileGetLabel = function ($key, $fallback) {
    $value = __($key, true);
    return !empty($value) && $value !== $key ? $value : $fallback;
};
$scProfileLabel = function ($key, $fallback) use ($scProfileGetLabel) {
    echo htmlspecialchars($scProfileGetLabel($key, $fallback), ENT_QUOTES, 'UTF-8');
};
$companyTokenRows = array(
    array('plugin_invoice_i_logo', 'Logo', '{CompanyLogo}'),
    array('plugin_invoice_i_company', 'Company name', '{CompanyName}'),
    array('plugin_invoice_i_street_address', 'Street address', '{CompanyAddress}'),
    array('plugin_invoice_i_zip', 'ZIP / postal code', '{CompanyZip}'),
    array('plugin_invoice_i_city', 'City', '{CompanyCity}'),
    array('plugin_invoice_i_state', 'State', '{CompanyState}'),
    array('plugin_invoice_i_country', 'Country', '{CompanyCountry}'),
    array('plugin_invoice_i_phone', 'Phone', '{CompanyPhone}'),
    array('plugin_invoice_i_email', 'Email', '{CompanyEmail}'),
    array('plugin_invoice_i_url', 'Website', '{CompanyUrl}'),
);
?>

<style>
.sc-profile-form { max-width: 980px; }
.sc-profile-form .adm-card { margin-bottom: 16px; padding: 18px; border: 1px solid var(--admin-card-border, #e7eaec); border-radius: 6px; background: var(--admin-card-bg-solid, #fff); }
.sc-profile-form .adm-card__heading { margin: 0 0 15px; font-size: 15px; font-weight: 700; color: var(--admin-heading-text, #333); }
.sc-profile-form .form-group { margin-bottom: 12px; }
.sc-profile-form label { display: block; margin-bottom: 5px; font-weight: 600; color: var(--admin-main-text, #333); }
.sc-profile-form .logo-box { display: flex; align-items: center; gap: 10px; min-height: 38px; }
.sc-profile-form .logo-box img { max-height: 48px; max-width: 220px; border: 1px solid var(--admin-card-border, #e7eaec); border-radius: 4px; padding: 3px; background: var(--admin-card-bg-solid, #fff); }
.sc-profile-form .logo-hint { margin-top: 4px; font-size: 12px; color: var(--admin-muted-text, #888); }
.sc-profile-tokens { margin: 0 0 16px; padding: 14px; border: 1px solid var(--admin-card-border, #e7eaec); border-radius: 6px; background: var(--admin-table-row-alt-bg, #f9fafb); }
.sc-profile-tokens__title { margin: 0 0 10px; font-weight: 700; color: var(--admin-heading-text, #333); }
.sc-profile-token-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 8px; }
.sc-profile-token { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 10px; border: 1px solid var(--admin-card-border, #e5e7eb); border-radius: 4px; background: var(--admin-card-bg-solid, #fff); }
.sc-profile-token__name { font-weight: 600; color: var(--admin-main-text, #444); }
.sc-profile-token code { flex: 0 0 auto; padding: 2px 6px; border: 1px solid var(--admin-input-border, #dfe3e8); border-radius: 3px; background: var(--admin-table-row-alt-bg, #f7f8fa); color: var(--admin-muted-text, #555); font-size: 12px; }
.sc-profile-langbar { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; padding: 10px 12px; border: 1px solid var(--admin-card-border, #e7eaec); border-radius: 6px; background: var(--admin-table-row-alt-bg, #f9fafb); }
.sc-profile-copy-lang { margin-left: auto; cursor: pointer; }
</style>

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-sm-12">
        <h2><i class="fa fa-building"></i> <?php $lbl = __('admin_menu_company_profile', true); echo !empty($lbl) && $lbl !== 'admin_menu_company_profile' ? htmlspecialchars($lbl) : 'Company profile'; ?></h2>
    </div>
</div>

<div class="row wrapper wrapper-content animated fadeInRight">
    <div class="col-lg-12">
        <div class="ibox float-e-margins">
            <div class="ibox-content">
                <form action="<?php echo PJ_INSTALL_URL; ?>index.php?controller=pjAdminProfile&amp;action=pjActionIndex" id="frmProfile" method="post" class="form sc-profile-form" enctype="multipart/form-data">
			<input type="hidden" name="csrf_token" value="<?php echo pjAppController::getCsrfToken(); ?>">
                    <input type="hidden" name="profile_post" value="1" />

                    <?php if ($is_flag_ready): ?>
                        <div class="sc-profile-langbar">
                            <div class="multilang"></div>
                            <a class="sc-profile-copy-lang" id="scProfileCopyLang" data-default-id="<?php echo (int) $defaultLocaleId; ?>">
                                <i class="fa fa-copy"></i> <?php $scProfileLabel('admin_profile_copy_default_language', 'Copy from default language'); ?>
                            </a>
                        </div>
                    <?php endif; ?>

                    <?php foreach ($lp_arr as $v): $lid = (int) $v['id']; $isDef = (int) $v['is_default']; ?>
                        <div class="pj-multilang-wrap" data-index="<?php echo $lid; ?>" style="display: <?php echo $isDef === 1 ? 'block' : 'none'; ?>">
                            <div class="adm-card">
                                <h4 class="adm-card__heading"><i class="fa fa-building"></i> <?php $scProfileLabel('admin_profile_company_contact', 'Company and contact'); ?></h4>
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label><?php $scProfileLabel('plugin_invoice_i_company', 'Company'); ?></label>
                                            <input type="text" name="i18n[<?php echo $lid; ?>][y_company]" class="form-control<?php echo $isDef ? ' required' : ''; ?>" value="<?php echo pjSanitize::html(@$arr['i18n'][$lid]['y_company']); ?>" />
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label><?php $scProfileLabel('plugin_invoice_i_name', 'Contact name'); ?></label>
                                            <input type="text" name="i18n[<?php echo $lid; ?>][y_name]" class="form-control" value="<?php echo pjSanitize::html(@$arr['i18n'][$lid]['y_name']); ?>" />
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label><?php $scProfileLabel('plugin_invoice_i_phone', 'Phone'); ?></label>
                                            <input type="text" name="i18n[<?php echo $lid; ?>][y_phone]" class="form-control" value="<?php echo pjSanitize::html(@$arr['i18n'][$lid]['y_phone'] ?: @$arr['y_phone']); ?>" />
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label><?php $scProfileLabel('plugin_invoice_i_email', 'Email'); ?></label>
                                            <input type="email" name="i18n[<?php echo $lid; ?>][y_email]" class="form-control" value="<?php echo pjSanitize::html(@$arr['i18n'][$lid]['y_email'] ?: @$arr['y_email']); ?>" />
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label><?php $scProfileLabel('plugin_invoice_i_url', 'Website'); ?></label>
                                            <input type="url" name="i18n[<?php echo $lid; ?>][y_url]" class="form-control" placeholder="https://" value="<?php echo pjSanitize::html(@$arr['i18n'][$lid]['y_url'] ?: @$arr['y_url']); ?>" />
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="adm-card">
                                <h4 class="adm-card__heading"><i class="fa fa-map-marker"></i> <?php $scProfileLabel('admin_profile_address', 'Address'); ?></h4>
                                <div class="row">
                                    <div class="col-md-8">
                                        <div class="form-group">
                                            <label><?php $scProfileLabel('plugin_invoice_i_street_address', 'Street address'); ?></label>
                                            <input type="text" name="i18n[<?php echo $lid; ?>][y_street_address]" class="form-control" value="<?php echo pjSanitize::html(@$arr['i18n'][$lid]['y_street_address']); ?>" />
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label><?php $scProfileLabel('plugin_invoice_i_zip', 'ZIP / postal code'); ?></label>
                                            <input type="text" name="i18n[<?php echo $lid; ?>][y_zip]" class="form-control" value="<?php echo pjSanitize::html(@$arr['i18n'][$lid]['y_zip'] ?: @$arr['y_zip']); ?>" />
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label><?php $scProfileLabel('plugin_invoice_i_city', 'City'); ?></label>
                                            <input type="text" name="i18n[<?php echo $lid; ?>][y_city]" class="form-control" value="<?php echo pjSanitize::html(@$arr['i18n'][$lid]['y_city']); ?>" />
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label><?php $scProfileLabel('plugin_invoice_i_state', 'State'); ?></label>
                                            <input type="text" name="i18n[<?php echo $lid; ?>][y_state]" class="form-control" value="<?php echo pjSanitize::html(@$arr['i18n'][$lid]['y_state']); ?>" />
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label><?php $scProfileLabel('plugin_invoice_i_country', 'Country'); ?></label>
                                            <input type="text" name="i18n[<?php echo $lid; ?>][y_country]" class="form-control" value="<?php echo pjSanitize::html(@$arr['i18n'][$lid]['y_country']); ?>" />
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="adm-card">
                                <h4 class="adm-card__heading"><i class="fa fa-credit-card"></i> <?php $scProfileLabel('admin_profile_registration_payment', 'Registration and payment details'); ?></h4>
                                <div class="row">
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label><?php $scProfileLabel('admin_profile_tax_number', 'Tax number'); ?></label>
                                            <input type="text" name="i18n[<?php echo $lid; ?>][y_vat_number]" class="form-control" value="<?php echo pjSanitize::html(@$arr['i18n'][$lid]['y_vat_number'] ?: @$arr['y_vat_number']); ?>" />
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label><?php $scProfileLabel('admin_profile_company_registration_number', 'Company registration number'); ?></label>
                                            <input type="text" name="i18n[<?php echo $lid; ?>][y_coc_number]" class="form-control" value="<?php echo pjSanitize::html(@$arr['i18n'][$lid]['y_coc_number'] ?: @$arr['y_coc_number']); ?>" />
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label><?php $scProfileLabel('admin_profile_bank_account_name', 'Bank account name'); ?></label>
                                            <input type="text" name="i18n[<?php echo $lid; ?>][y_bank_name]" class="form-control" value="<?php echo pjSanitize::html(@$arr['i18n'][$lid]['y_bank_name'] ?: @$arr['y_bank_name']); ?>" />
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label><?php $scProfileLabel('admin_profile_iban_bank_account', 'IBAN / bank account'); ?></label>
                                    <input type="text" name="i18n[<?php echo $lid; ?>][y_bank_iban]" class="form-control" value="<?php echo pjSanitize::html(@$arr['i18n'][$lid]['y_bank_iban'] ?: @$arr['y_bank_iban']); ?>" />
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>

                    <div class="adm-card">
                        <h4 class="adm-card__heading"><i class="fa fa-image"></i> <?php $scProfileLabel('admin_profile_logos', 'Logos'); ?></h4>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label><?php $scProfileLabel('plugin_invoice_i_logo', 'Logo'); ?></label>
                                    <div class="logo-box" id="profile_box_logo">
                                        <?php if (!empty($arr['y_logo']) && is_file(PJ_INSTALL_PATH . $arr['y_logo'])): ?>
                                            <img src="<?php echo PJ_INSTALL_URL . $arr['y_logo']; ?>" alt="Logo" />
                                            <a class="btn btn-danger btn-sm profile_delete_logo"><i class="fa fa-trash"></i></a>
                                        <?php else: ?>
                                            <input type="file" name="y_logo" class="form-control" accept=".png,.jpg,.jpeg,.gif,.svg,.webp" />
                                        <?php endif; ?>
                                    </div>
                                    <div class="logo-hint"><?php $scProfileLabel('admin_profile_logo_hint', 'Recommended: transparent PNG or SVG.'); ?></div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label><?php $scProfileLabel('admin_profile_email_logo', 'Email logo'); ?></label>
                                    <div class="logo-box" id="profile_box_email_logo">
                                        <?php if (!empty($arr['y_email_logo']) && is_file(PJ_INSTALL_PATH . $arr['y_email_logo'])): ?>
                                            <img src="<?php echo PJ_INSTALL_URL . $arr['y_email_logo']; ?>" alt="Email logo" />
                                            <a class="btn btn-danger btn-sm profile_delete_email_logo"><i class="fa fa-trash"></i></a>
                                        <?php else: ?>
                                            <input type="file" name="y_email_logo" class="form-control" accept=".png,.jpg,.jpeg" />
                                        <?php endif; ?>
                                    </div>
                                    <div class="logo-hint"><?php $scProfileLabel('admin_profile_email_logo_hint', 'Used in invoice and email templates.'); ?></div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="sc-profile-tokens">
                        <div class="sc-profile-tokens__title"><?php $scProfileLabel('plugin_invoice_available_company_tokens', 'Available company tokens'); ?></div>
                        <div class="sc-profile-token-grid">
                            <?php foreach ($companyTokenRows as $tokenRow): ?>
                                <div class="sc-profile-token">
                                    <span class="sc-profile-token__name"><?php echo htmlspecialchars($scProfileGetLabel($tokenRow[0], $tokenRow[1]), ENT_QUOTES, 'UTF-8'); ?></span>
                                    <code><?php echo htmlspecialchars($tokenRow[2], ENT_QUOTES, 'UTF-8'); ?></code>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    </div>

                    <?php
                    $overviewUrl = null;
                    include PJ_VIEWS_PATH . 'pjLayouts/elements/form-buttons.php';
                    ?>
                </form>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
var myLabel = myLabel || {};
myLabel.confirm_delete_msg = <?php echo json_encode(__('plugin_base_grid_confirmation_title', true) ?: 'Are you sure?'); ?>;
myLabel.btn_delete = <?php echo json_encode(__('btnDelete', true) ?: 'Delete'); ?>;
myLabel.btn_cancel = <?php echo json_encode(__('btnCancel', true) ?: 'Cancel'); ?>;
	myLabel.csrf_token = "<?php echo pjAppController::getCsrfToken(); ?>";

var pjLocale = pjLocale || {};
pjLocale.langs = <?php echo $locale_str; ?>;
pjLocale.flagPath = "<?php echo PJ_FRAMEWORK_LIBS_PATH; ?>pj/img/flags/";
pjLocale.defaultId = "<?php echo (int) $defaultLocaleId; ?>";
</script>
