<?php
require_once __DIR__ . '/../../config/config.php';
require_once __DIR__ . '/../../includes/functions.php';
startSession(); requireLogin(); requireRole('superadmin', 'admin');
$db   = Database::getInstance();
$page = 'Company Branding';

// company_settings is a single shared row used to print the letterhead on
// every generated Letter and Payslip (see letter.php / payslip.php) —
// it is intentionally separate from the per-company legal details in
// "Company Management" (companies table).
$settings = $db->fetchOne("SELECT * FROM company_settings LIMIT 1");

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    verifyCsrf();

    $data = [
        'company_name' => post('company_name'),
        'address'      => post('address'),
        'city'         => post('city'),
        'state'        => post('state'),
        'pincode'      => post('pincode'),
        'phone'        => post('phone'),
        'email'        => post('email'),
        'website'      => post('website'),
        'pan'          => strtoupper(post('pan')),
        'tan'          => strtoupper(post('tan')),
        'pf_number'    => post('pf_number'),
        'esic_number'  => post('esic_number'),
    ];

    if (!empty($_FILES['logo']['name'])) {
        $path = uploadFile($_FILES['logo'], 'company', ALLOWED_IMAGE_TYPES);
        if ($path === false) {
            setFlash('error', 'Logo upload failed — please use a JPG, PNG or GIF under the size limit.');
            header('Location: ' . BASE_URL . '/modules/company/branding.php'); exit;
        }
        $data['logo_path'] = $path;
    }

    if ($settings) {
        $db->updateRow('company_settings', $data, $settings['id']);
    } else {
        $db->insertRow('company_settings', $data);
    }
    setFlash('success', 'Company branding updated. This logo and name now appear on every generated letter and payslip.');
    header('Location: ' . BASE_URL . '/modules/company/branding.php'); exit;
}

include __DIR__ . '/../../includes/header.php';
?>
<div class="row g-3">
  <div class="col-md-7">
    <div class="pc-card">
      <div class="pc-card-title"><i class="ti ti-photo me-2"></i>Letterhead — Logo &amp; Company Name</div>
      <p class="text-muted small mb-3">This logo and name are printed automatically on every Offer Letter, Experience Letter, Salary Certificate, and any custom letter template — no need to add them manually in each template.</p>
      <form method="POST" enctype="multipart/form-data">
        <?= csrfField() ?>

        <div class="pc-section-divider">Logo</div>
        <div class="d-flex align-items-center gap-3 mb-3">
          <div style="width:88px;height:88px;border:1px solid #dee2e6;border-radius:10px;display:flex;align-items:center;justify-content:center;overflow:hidden;background:#fff">
            <?php if ($settings['logo_path'] ?? ''): ?>
              <img src="<?= BASE_URL ?>/uploads/<?= clean($settings['logo_path']) ?>" alt="Company Logo" style="max-width:100%;max-height:100%;object-fit:contain">
            <?php else: ?>
              <i class="ti ti-photo-off text-muted" style="font-size:28px"></i>
            <?php endif; ?>
          </div>
          <div class="flex-fill">
            <input type="file" name="logo" accept="image/png,image/jpeg,image/gif" class="form-control form-control-sm">
            <div class="text-muted mt-1" style="font-size:11.5px">PNG with a transparent background looks best. Square logos display most consistently.</div>
          </div>
        </div>

        <div class="pc-section-divider">Company Name &amp; Address</div>
        <div class="row g-2">
          <div class="col-12">
            <label class="pc-label">Company Name <span class="text-danger">*</span></label>
            <input type="text" name="company_name" class="form-control form-control-sm" value="<?= clean($settings['company_name'] ?? '') ?>" required>
          </div>
          <div class="col-12">
            <label class="pc-label">Address</label>
            <textarea name="address" class="form-control form-control-sm" rows="2"><?= clean($settings['address'] ?? '') ?></textarea>
          </div>
          <div class="col-md-4"><label class="pc-label">City</label><input type="text" name="city" class="form-control form-control-sm" value="<?= clean($settings['city'] ?? '') ?>"></div>
          <div class="col-md-4"><label class="pc-label">State</label><input type="text" name="state" class="form-control form-control-sm" value="<?= clean($settings['state'] ?? '') ?>"></div>
          <div class="col-md-4"><label class="pc-label">Pincode</label><input type="text" name="pincode" class="form-control form-control-sm" value="<?= clean($settings['pincode'] ?? '') ?>"></div>
        </div>

        <div class="pc-section-divider">Contact</div>
        <div class="row g-2">
          <div class="col-md-6"><label class="pc-label">Phone</label><input type="text" name="phone" class="form-control form-control-sm" value="<?= clean($settings['phone'] ?? '') ?>"></div>
          <div class="col-md-6"><label class="pc-label">Email</label><input type="email" name="email" class="form-control form-control-sm" value="<?= clean($settings['email'] ?? '') ?>"></div>
          <div class="col-12"><label class="pc-label">Website</label><input type="text" name="website" class="form-control form-control-sm" value="<?= clean($settings['website'] ?? '') ?>" placeholder="https://"></div>
        </div>

        <div class="pc-section-divider">Statutory Numbers <span class="text-muted" style="font-weight:400">(used on Salary Certificates &amp; NOC letters)</span></div>
        <div class="row g-2">
          <div class="col-md-6"><label class="pc-label">PAN</label><input type="text" name="pan" class="form-control form-control-sm" style="text-transform:uppercase" value="<?= clean($settings['pan'] ?? '') ?>"></div>
          <div class="col-md-6"><label class="pc-label">TAN</label><input type="text" name="tan" class="form-control form-control-sm" style="text-transform:uppercase" value="<?= clean($settings['tan'] ?? '') ?>"></div>
          <div class="col-md-6"><label class="pc-label">PF Number</label><input type="text" name="pf_number" class="form-control form-control-sm" value="<?= clean($settings['pf_number'] ?? '') ?>"></div>
          <div class="col-md-6"><label class="pc-label">ESIC Number</label><input type="text" name="esic_number" class="form-control form-control-sm" value="<?= clean($settings['esic_number'] ?? '') ?>"></div>
        </div>

        <div class="mt-3">
          <button class="btn btn-pc"><i class="ti ti-device-floppy me-1"></i>Save Branding</button>
        </div>
      </form>
    </div>
  </div>

  <div class="col-md-5">
    <div class="pc-card">
      <div class="pc-card-title"><i class="ti ti-eye me-2"></i>Letter Header Preview</div>
      <div class="p-3 border rounded" style="font-family:Cambria,Georgia,'Times New Roman',serif">
        <div class="d-flex justify-content-between align-items-center pb-2" style="border-bottom:2px solid var(--pc-brand)">
          <div>
            <?php if ($settings['logo_path'] ?? ''): ?>
              <img src="<?= BASE_URL ?>/uploads/<?= clean($settings['logo_path']) ?>" alt="Logo" style="max-height:56px;max-width:150px;object-fit:contain">
            <?php endif; ?>
          </div>
          <div class="text-end">
            <div style="font-size:18px;font-weight:700;color:var(--pc-brand)"><?= clean($settings['company_name'] ?? 'Your Company Name') ?></div>
            <?php if ($settings['address'] ?? ''): ?><div class="text-muted" style="font-size:11.5px"><?= clean($settings['address']) ?></div><?php endif; ?>
          </div>
        </div>
        <div class="text-center fw-bold text-uppercase mt-3" style="letter-spacing:2px;font-size:13px">Offer Letter</div>
      </div>
      <p class="text-muted small mt-2 mb-0" style="font-size:11.5px">(Example only — the actual letter title changes to match the letter you generate, e.g. "Experience Letter", "Salary Certificate".)</p>
      <p class="text-muted small mt-3 mb-0">This exact header appears at the top of every letter — built-in and custom templates alike. Go to <a href="<?= BASE_URL ?>/modules/reports/letter_templates.php">Letter Templates</a> to edit the wording of individual letters.</p>
    </div>
  </div>
</div>
<?php include __DIR__ . '/../../includes/footer.php'; ?>
