<?php
require_once __DIR__.'/includes/functions.php';
$user = require_user();
mj_require_user_feature('company_page_builder', (int)$user['id']);
$pageTitle = 'Company Page Builder';

mj_v288_ensure_schema();
$companies = table_exists('mj_companies') ? all_rows('SELECT * FROM mj_companies WHERE user_id=? ORDER BY id DESC',[(int)$user['id']]) : [];
$companyId = (int)($_GET['company_id'] ?? ($companies[0]['id'] ?? 0));
$company = null; foreach($companies as $c){ if((int)$c['id']===$companyId){ $company=$c; break; }}
if ($_SERVER['REQUEST_METHOD']==='POST' && $company) {
    verify_csrf();
    try {
        q('INSERT INTO mj_company_page_settings(company_id,layout_style,theme_primary,theme_secondary,theme_accent,video_url,show_reviews,show_booking,seo_title,seo_description,updated_at) VALUES(?,?,?,?,?,?,?,?,?,?,NOW()) ON DUPLICATE KEY UPDATE layout_style=VALUES(layout_style),theme_primary=VALUES(theme_primary),theme_secondary=VALUES(theme_secondary),theme_accent=VALUES(theme_accent),video_url=VALUES(video_url),show_reviews=VALUES(show_reviews),show_booking=VALUES(show_booking),seo_title=VALUES(seo_title),seo_description=VALUES(seo_description),updated_at=NOW()', [$companyId, $_POST['layout_style'] ?? 'modern_card', $_POST['theme_primary'] ?? '', $_POST['theme_secondary'] ?? '', $_POST['theme_accent'] ?? '', $_POST['video_url'] ?? '', !empty($_POST['show_reviews'])?1:0, !empty($_POST['show_booking'])?1:0, $_POST['seo_title'] ?? '', $_POST['seo_description'] ?? '']);
        if (!empty($_POST['section_title'])) {
            q('INSERT INTO mj_company_page_sections(company_id,section_key,section_title,section_content,is_enabled,sort_order,updated_at) VALUES(?,?,?,?,1,?,NOW())', [$companyId, slugify($_POST['section_title']), $_POST['section_title'], $_POST['section_content'] ?? '', (int)($_POST['sort_order'] ?? 50)]);
        }
        flash('success','Company page customization saved.'); redirect('company-page-builder.php?company_id='.$companyId);
    } catch(Throwable $e) { flash('error','Unable to save: '.$e->getMessage()); }
}
$settings = $company ? mj_v288_company_page_settings($companyId) : [];
$sections = $company ? mj_v288_company_sections($companyId) : [];

include __DIR__.'/includes/header.php';
?>
<section class="mj-section">
  <div class="container">
    <div class="mj-center-shell">
      <?php include __DIR__.'/includes/account-sidebar.php'; ?>
      <div class="mj-center-main">
        <div class="mj-section-title mb-4">
          <div><span class="mj-badge mb-2"><i class="bi bi-palette"></i> Mini website</span><h1 class="h2 fw-bold mb-1">Company Page Builder</h1><p class="text-muted mb-0">Customize company detail page colors, sections, SEO, booking button and preview.</p></div>
        </div>
        
<?php if(!$companies): ?><div class="alert alert-info rounded-4">Add a company first before using page builder.</div><a class="btn btn-primary" href="<?=base_url('add-company.php')?>">Add Company</a><?php elseif(!$company): ?><div class="alert alert-warning rounded-4">Company not found.</div><?php else: ?>
<div class="mb-3"><form method="get"><label class="form-label">Select company</label><select class="form-select" name="company_id" onchange="this.form.submit()"><?php foreach($companies as $c): ?><option value="<?=e($c['id'])?>" <?=$companyId==(int)$c['id']?'selected':''?>><?=e($c['name'])?></option><?php endforeach; ?></select></form></div>
<div class="row g-4">
  <div class="col-lg-7"><div class="mj-center-card"><div class="card-body"><h5 class="fw-bold">Customize Company Page</h5><form method="post" class="row g-3"><?=csrf_input()?><div class="col-md-6"><label class="form-label">Layout Style</label><select class="form-select" name="layout_style"><option value="modern_card">Modern Card</option><option value="premium_banner">Premium Banner</option><option value="service_provider">Service Provider</option><option value="shop_retail">Shop / Retail</option><option value="clinic_wellness">Clinic / Wellness</option></select></div><div class="col-md-6"><label class="form-label">Video URL</label><input class="form-control" name="video_url" value="<?=e($settings['video_url'] ?? '')?>"></div><div class="col-md-4"><label class="form-label">Primary Color</label><input class="form-control" name="theme_primary" value="<?=e($settings['theme_primary'] ?? '')?>" placeholder="#2563eb"></div><div class="col-md-4"><label class="form-label">Secondary Color</label><input class="form-control" name="theme_secondary" value="<?=e($settings['theme_secondary'] ?? '')?>" placeholder="#f8fafc"></div><div class="col-md-4"><label class="form-label">Accent Color</label><input class="form-control" name="theme_accent" value="<?=e($settings['theme_accent'] ?? '')?>" placeholder="#7c3aed"></div><div class="col-md-6 form-check ms-2"><input class="form-check-input" type="checkbox" name="show_reviews" value="1" <?=!empty($settings['show_reviews'])?'checked':''?>> <label class="form-check-label">Show reviews</label></div><div class="col-md-6 form-check"><input class="form-check-input" type="checkbox" name="show_booking" value="1" <?=!empty($settings['show_booking'])?'checked':''?>> <label class="form-check-label">Show booking button</label></div><div class="col-md-12"><label class="form-label">SEO Title</label><input class="form-control" name="seo_title" value="<?=e($settings['seo_title'] ?? '')?>"></div><div class="col-md-12"><label class="form-label">SEO Description</label><textarea class="form-control" name="seo_description" rows="3"><?=e($settings['seo_description'] ?? '')?></textarea></div><hr><h6>Add custom section</h6><div class="col-md-5"><input class="form-control" name="section_title" placeholder="Section title"></div><div class="col-md-2"><input class="form-control" name="sort_order" placeholder="Sort" value="50"></div><div class="col-md-12"><textarea class="form-control" name="section_content" rows="4" placeholder="Section content"></textarea></div><div class="col-12"><button class="btn btn-primary">Save Customization</button> <a class="btn btn-outline-primary" target="_blank" href="<?=base_url('company.php?id='.$companyId)?>">Preview Public Page</a></div></form></div></div></div>
  <div class="col-lg-5"><div class="company-builder-preview"><div class="company-builder-cover"><div><h3><?=e($company['name'] ?? 'Company')?></h3><p class="mb-0">Preview mini website mode</p></div></div><div class="p-3"><b>Sections</b><?php if(!$sections): ?><p class="text-muted">No custom sections yet.</p><?php else: foreach($sections as $s): ?><div class="company-section-block mt-2"><b><?=e($s['section_title'])?></b><p class="text-muted small mb-0"><?=e(mb_strimwidth($s['section_content'] ?? '',0,90,'...'))?></p></div><?php endforeach; endif; ?></div></div></div>
</div><?php endif; ?>

      </div>
    </div>
  </div>
</section>
<?php include __DIR__.'/includes/footer.php'; ?>
