<?php $this->start('body');
    $p = $this->data['profile'] ?? [];
    $w = $this->data['writable_fields'] ?? [];
    $group = (string)($this->data['user_group'] ?? '');
    $canWrite = !empty($w);
    // Small helper for readonly rendering
    $ro = function ($field) use ($w) { return in_array($field, $w, true) ? '' : 'readonly disabled'; };
    $editableIcon = function ($field) use ($w) {
        return in_array($field, $w, true)
            ? '<i class="fas fa-pencil-alt ms-1" style="color:#7A9E7E;font-size:.6rem;" title="You can edit this"></i>'
            : '<i class="fas fa-lock ms-1" style="color:#94A3B8;font-size:.6rem;" title="Read-only for your role"></i>';
    };
?>
<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="mb-0" style="font-weight:600;color:#334155;"><i class="fas fa-building me-1" style="color:#7A8BA1;"></i>Company Profile</h5>
        <div class="text-muted" style="font-size:.72rem;">
            Signed in as <strong style="color:#334155;"><?= htmlspecialchars($group ?: '—'); ?></strong>.
            Your editable fields are marked
            <i class="fas fa-pencil-alt" style="color:#7A9E7E;font-size:.66rem;"></i>;
            fields locked to other roles are marked
            <i class="fas fa-lock" style="color:#94A3B8;font-size:.66rem;"></i>.
        </div>
    </div>
</div>

<div class="card border-0 shadow-sm mb-3" style="background:#FDF7EC;border-left:3px solid #D4A574 !important;">
    <div class="card-body p-3" style="font-size:.82rem;color:#334155;">
        <div><strong style="color:#8B6914;"><i class="fas fa-lightbulb me-1"></i>Who edits what</strong></div>
        <div class="mt-1" style="line-height:1.5;">
            <strong style="color:#334155;">CRM</strong> owns the day-to-day <em>contact + trading identity</em> (name, logo, addresses, phones, emails). &nbsp;
            <strong style="color:#334155;">Executive</strong> owns the <em>legal, tax, and bank</em> details. &nbsp;
            <strong style="color:#334155;">Administrator</strong> can edit everything. &nbsp;
            <strong style="color:#334155;">Finance Manager</strong> can view (needed on invoices &amp; PVs) but cannot edit.
        </div>
        <div class="mt-2" style="font-size:.76rem;color:#64748B;">
            <i class="fas fa-info-circle me-1" style="color:#7A8BA1;"></i>The value here is the one auto-filled as "Winning bidder" on the Awarded tab when a tender is marked <strong>Won</strong>.
        </div>
    </div>
</div>

<form id="cp_form" class="row g-3">

    <!-- Trading identity -->
    <div class="col-md-6">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-header bg-white" style="border-bottom:1px solid #E2E8F0;">
                <strong style="color:#334155;font-size:.86rem;"><i class="fas fa-id-badge me-1" style="color:#7A9E7E;"></i>Trading identity <span class="badge ms-1" style="background:#EAF2E8;color:#3F5C43;border:1px solid #7A9E7E;font-size:.62rem;text-transform:capitalize;font-weight:500;">CRM</span></strong>
            </div>
            <div class="card-body p-3">
                <div class="row g-2">
                    <div class="col-md-8"><label class="form-label" style="color:#334155;font-size:.74rem;">Trading name <?= $editableIcon('trading_name'); ?></label>
                        <input type="text" name="trading_name" value="<?= htmlspecialchars((string)($p['trading_name'] ?? '')); ?>" class="form-control form-control-sm" <?= $ro('trading_name'); ?>></div>
                    <div class="col-md-4"><label class="form-label" style="color:#334155;font-size:.74rem;">Short name <?= $editableIcon('short_name'); ?></label>
                        <input type="text" name="short_name" value="<?= htmlspecialchars((string)($p['short_name'] ?? '')); ?>" class="form-control form-control-sm" <?= $ro('short_name'); ?>></div>
                    <div class="col-12"><label class="form-label" style="color:#334155;font-size:.74rem;">Tagline <?= $editableIcon('tagline'); ?></label>
                        <input type="text" name="tagline" value="<?= htmlspecialchars((string)($p['tagline'] ?? '')); ?>" placeholder="One-line pitch" class="form-control form-control-sm" <?= $ro('tagline'); ?>></div>
                    <div class="col-12"><label class="form-label" style="color:#334155;font-size:.74rem;">Logo (path or URL) <?= $editableIcon('logo_path'); ?></label>
                        <input type="text" name="logo_path" value="<?= htmlspecialchars((string)($p['logo_path'] ?? '')); ?>" placeholder="images/logo.png" class="form-control form-control-sm" <?= $ro('logo_path'); ?>></div>
                </div>
            </div>
        </div>
    </div>

    <!-- Contact -->
    <div class="col-md-6">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-header bg-white" style="border-bottom:1px solid #E2E8F0;">
                <strong style="color:#334155;font-size:.86rem;"><i class="fas fa-map-marker-alt me-1" style="color:#7A9E7E;"></i>Contact <span class="badge ms-1" style="background:#EAF2E8;color:#3F5C43;border:1px solid #7A9E7E;font-size:.62rem;text-transform:capitalize;font-weight:500;">CRM</span></strong>
            </div>
            <div class="card-body p-3">
                <div class="row g-2">
                    <div class="col-md-6"><label class="form-label" style="color:#334155;font-size:.74rem;">Physical address <?= $editableIcon('physical_address'); ?></label>
                        <input type="text" name="physical_address" value="<?= htmlspecialchars((string)($p['physical_address'] ?? '')); ?>" class="form-control form-control-sm" <?= $ro('physical_address'); ?>></div>
                    <div class="col-md-6"><label class="form-label" style="color:#334155;font-size:.74rem;">Postal address <?= $editableIcon('postal_address'); ?></label>
                        <input type="text" name="postal_address" value="<?= htmlspecialchars((string)($p['postal_address'] ?? '')); ?>" placeholder="P.O. Box …" class="form-control form-control-sm" <?= $ro('postal_address'); ?>></div>
                    <div class="col-md-6"><label class="form-label" style="color:#334155;font-size:.74rem;">Region <?= $editableIcon('region'); ?></label>
                        <input type="text" name="region" value="<?= htmlspecialchars((string)($p['region'] ?? '')); ?>" class="form-control form-control-sm" <?= $ro('region'); ?>></div>
                    <div class="col-md-6"><label class="form-label" style="color:#334155;font-size:.74rem;">District <?= $editableIcon('district'); ?></label>
                        <input type="text" name="district" value="<?= htmlspecialchars((string)($p['district'] ?? '')); ?>" class="form-control form-control-sm" <?= $ro('district'); ?>></div>
                    <div class="col-md-6"><label class="form-label" style="color:#334155;font-size:.74rem;">Phone (primary) <?= $editableIcon('phone_primary'); ?></label>
                        <input type="text" name="phone_primary" value="<?= htmlspecialchars((string)($p['phone_primary'] ?? '')); ?>" class="form-control form-control-sm" <?= $ro('phone_primary'); ?>></div>
                    <div class="col-md-6"><label class="form-label" style="color:#334155;font-size:.74rem;">Phone (secondary) <?= $editableIcon('phone_secondary'); ?></label>
                        <input type="text" name="phone_secondary" value="<?= htmlspecialchars((string)($p['phone_secondary'] ?? '')); ?>" class="form-control form-control-sm" <?= $ro('phone_secondary'); ?>></div>
                    <div class="col-md-6"><label class="form-label" style="color:#334155;font-size:.74rem;">Email (primary) <?= $editableIcon('email_primary'); ?></label>
                        <input type="email" name="email_primary" value="<?= htmlspecialchars((string)($p['email_primary'] ?? '')); ?>" class="form-control form-control-sm" <?= $ro('email_primary'); ?>></div>
                    <div class="col-md-6"><label class="form-label" style="color:#334155;font-size:.74rem;">Email (secondary) <?= $editableIcon('email_secondary'); ?></label>
                        <input type="email" name="email_secondary" value="<?= htmlspecialchars((string)($p['email_secondary'] ?? '')); ?>" class="form-control form-control-sm" <?= $ro('email_secondary'); ?>></div>
                    <div class="col-12"><label class="form-label" style="color:#334155;font-size:.74rem;">Website <?= $editableIcon('website'); ?></label>
                        <input type="text" name="website" value="<?= htmlspecialchars((string)($p['website'] ?? '')); ?>" placeholder="https://dspera.co.tz" class="form-control form-control-sm" <?= $ro('website'); ?>></div>
                </div>
            </div>
        </div>
    </div>

    <!-- Legal / Tax -->
    <div class="col-md-6">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-header bg-white" style="border-bottom:1px solid #E2E8F0;">
                <strong style="color:#334155;font-size:.86rem;"><i class="fas fa-gavel me-1" style="color:#7A8BA1;"></i>Legal &amp; Tax <span class="badge ms-1" style="background:#F1F5F9;color:#334155;border:1px solid #7A8BA1;font-size:.62rem;text-transform:capitalize;font-weight:500;">Executive</span></strong>
            </div>
            <div class="card-body p-3">
                <div class="row g-2">
                    <div class="col-md-8"><label class="form-label" style="color:#334155;font-size:.74rem;">Legal name (winning-bidder default) <?= $editableIcon('legal_name'); ?></label>
                        <input type="text" name="legal_name" value="<?= htmlspecialchars((string)($p['legal_name'] ?? '')); ?>" class="form-control form-control-sm" <?= $ro('legal_name'); ?>></div>
                    <div class="col-md-4"><label class="form-label" style="color:#334155;font-size:.74rem;">Reg. number <?= $editableIcon('reg_number'); ?></label>
                        <input type="text" name="reg_number" value="<?= htmlspecialchars((string)($p['reg_number'] ?? '')); ?>" class="form-control form-control-sm" <?= $ro('reg_number'); ?>></div>
                    <div class="col-md-4"><label class="form-label" style="color:#334155;font-size:.74rem;">TIN <?= $editableIcon('tin'); ?></label>
                        <input type="text" name="tin" value="<?= htmlspecialchars((string)($p['tin'] ?? '')); ?>" class="form-control form-control-sm" <?= $ro('tin'); ?>></div>
                    <div class="col-md-4"><label class="form-label" style="color:#334155;font-size:.74rem;">VAT number <?= $editableIcon('vat_number'); ?></label>
                        <input type="text" name="vat_number" value="<?= htmlspecialchars((string)($p['vat_number'] ?? '')); ?>" class="form-control form-control-sm" <?= $ro('vat_number'); ?>></div>
                    <div class="col-md-4"><label class="form-label" style="color:#334155;font-size:.74rem;">Tax regime <?= $editableIcon('tax_regime'); ?></label>
                        <input type="text" name="tax_regime" value="<?= htmlspecialchars((string)($p['tax_regime'] ?? '')); ?>" placeholder="e.g. Standard VAT" class="form-control form-control-sm" <?= $ro('tax_regime'); ?>></div>
                </div>
            </div>
        </div>
    </div>

    <!-- Bank -->
    <div class="col-md-6">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-header bg-white" style="border-bottom:1px solid #E2E8F0;">
                <strong style="color:#334155;font-size:.86rem;"><i class="fas fa-university me-1" style="color:#7A8BA1;"></i>Bank <span class="badge ms-1" style="background:#F1F5F9;color:#334155;border:1px solid #7A8BA1;font-size:.62rem;text-transform:capitalize;font-weight:500;">Executive</span></strong>
            </div>
            <div class="card-body p-3">
                <div class="row g-2">
                    <div class="col-md-6"><label class="form-label" style="color:#334155;font-size:.74rem;">Bank name <?= $editableIcon('bank_name'); ?></label>
                        <input type="text" name="bank_name" value="<?= htmlspecialchars((string)($p['bank_name'] ?? '')); ?>" class="form-control form-control-sm" <?= $ro('bank_name'); ?>></div>
                    <div class="col-md-6"><label class="form-label" style="color:#334155;font-size:.74rem;">Branch <?= $editableIcon('bank_branch'); ?></label>
                        <input type="text" name="bank_branch" value="<?= htmlspecialchars((string)($p['bank_branch'] ?? '')); ?>" class="form-control form-control-sm" <?= $ro('bank_branch'); ?>></div>
                    <div class="col-md-8"><label class="form-label" style="color:#334155;font-size:.74rem;">Account name <?= $editableIcon('bank_account_name'); ?></label>
                        <input type="text" name="bank_account_name" value="<?= htmlspecialchars((string)($p['bank_account_name'] ?? '')); ?>" class="form-control form-control-sm" <?= $ro('bank_account_name'); ?>></div>
                    <div class="col-md-4"><label class="form-label" style="color:#334155;font-size:.74rem;">Currency <?= $editableIcon('bank_currency'); ?></label>
                        <input type="text" name="bank_currency" maxlength="6" value="<?= htmlspecialchars((string)($p['bank_currency'] ?? 'TZS')); ?>" class="form-control form-control-sm" style="text-transform:uppercase;" <?= $ro('bank_currency'); ?>></div>
                    <div class="col-md-8"><label class="form-label" style="color:#334155;font-size:.74rem;">Account number <?= $editableIcon('bank_account_no'); ?></label>
                        <input type="text" name="bank_account_no" value="<?= htmlspecialchars((string)($p['bank_account_no'] ?? '')); ?>" class="form-control form-control-sm" <?= $ro('bank_account_no'); ?>></div>
                    <div class="col-md-4"><label class="form-label" style="color:#334155;font-size:.74rem;">SWIFT <?= $editableIcon('bank_swift'); ?></label>
                        <input type="text" name="bank_swift" value="<?= htmlspecialchars((string)($p['bank_swift'] ?? '')); ?>" class="form-control form-control-sm" <?= $ro('bank_swift'); ?>></div>
                </div>
            </div>
        </div>
    </div>

    <?php if (!empty($p['edited_on'])): ?>
        <div class="col-12 small" style="color:#64748B;font-size:.72rem;">
            <i class="fas fa-history me-1"></i>Last edited <?= htmlspecialchars((string)$p['edited_on']); ?><?= !empty($p['edited_by_name']) ? ' by ' . htmlspecialchars((string)$p['edited_by_name']) : ''; ?>
        </div>
    <?php endif; ?>

    <?php if ($canWrite): ?>
        <div class="col-12 text-end">
            <button type="button" id="cp_save" class="btn btn-sm" style="background:#7A8BA1;border-color:#7A8BA1;color:#fff;font-weight:600;padding:.4rem 1.2rem;">
                <i class="fas fa-save me-1"></i>Save my editable fields
            </button>
        </div>
    <?php endif; ?>
</form>

<script>
$(document).ready(function () {
    $('#cp_save').on('click', function () {
        var $btn = $(this).prop('disabled', true).html('<i class="fas fa-spinner fa-spin me-1"></i>Saving…');
        var data = $('#cp_form').serialize();
        $.post(PROOT + 'CompanyProfile/saveAj', data, null, 'json')
            .done(function (res) {
                if (res && res.ok) {
                    var msg = 'Saved ' + (res.saved || []).length + ' field(s).';
                    if ((res.ignored || []).length) msg += ' Ignored ' + res.ignored.length + ' (locked for your role).';
                    rsToast(msg, 'success');
                    setTimeout(function () { location.reload(); }, 500);
                } else rsToast('Save failed: ' + (res && res.msg || 'unknown'), 'error', 5000);
            })
            .fail(function () { rsToast('Network error saving profile.', 'error'); })
            .always(function () { $btn.prop('disabled', false).html('<i class="fas fa-save me-1"></i>Save my editable fields'); });
    });
});
</script>
<?php $this->end(); ?>
