<?php
require_once __DIR__.'/includes/functions.php';
$user = require_user();
mj_require_user_feature('appointment_booking', (int)$user['id']);
$pageTitle = 'Booking Settings';

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_booking_settings(company_id,is_enabled,booking_type,slot_duration,buffer_minutes,max_bookings_per_day,advance_days,same_day_booking,manual_approval,allow_reschedule,allow_cancel,business_hours_json,updated_at) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,NOW()) ON DUPLICATE KEY UPDATE is_enabled=VALUES(is_enabled),booking_type=VALUES(booking_type),slot_duration=VALUES(slot_duration),buffer_minutes=VALUES(buffer_minutes),max_bookings_per_day=VALUES(max_bookings_per_day),advance_days=VALUES(advance_days),same_day_booking=VALUES(same_day_booking),manual_approval=VALUES(manual_approval),allow_reschedule=VALUES(allow_reschedule),allow_cancel=VALUES(allow_cancel),business_hours_json=VALUES(business_hours_json),updated_at=NOW()', [$companyId,!empty($_POST['is_enabled'])?1:0,$_POST['booking_type']??'appointment',(int)($_POST['slot_duration']??30),(int)($_POST['buffer_minutes']??0),(int)($_POST['max_bookings_per_day']??20),(int)($_POST['advance_days']??30),!empty($_POST['same_day_booking'])?1:0,!empty($_POST['manual_approval'])?1:0,!empty($_POST['allow_reschedule'])?1:0,!empty($_POST['allow_cancel'])?1:0,$_POST['business_hours_json']??'']); flash('success','Booking settings saved.'); redirect('booking-settings.php?company_id='.$companyId); } catch(Throwable $e) { flash('error','Unable to save: '.$e->getMessage()); } }
$settings = $company ? mj_v288_booking_settings($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-calendar2-check"></i> Appointments</span><h1 class="h2 fw-bold mb-1">Booking Settings</h1><p class="text-muted mb-0">Enable booking on company page, set slots, approval rules, business hours and cancellation policy.</p></div>
        </div>
        
<?php if(!$companies): ?><div class="alert alert-info rounded-4">Add a company first before enabling booking.</div><?php elseif($company): ?><div class="mj-center-card"><div class="card-body"><form method="get" class="mb-3"><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><form method="post" class="row g-3"><?=csrf_input()?><div class="col-12 form-check ms-2"><input class="form-check-input" type="checkbox" name="is_enabled" value="1" <?=!empty($settings['is_enabled'])?'checked':''?>> <label class="form-check-label fw-bold">Enable appointment booking on company page</label></div><div class="col-md-4"><label class="form-label">Booking type</label><select class="form-select" name="booking_type"><option>appointment</option><option>consultation</option><option>site_visit</option><option>service_booking</option></select></div><div class="col-md-4"><label class="form-label">Slot duration</label><select class="form-select" name="slot_duration"><option <?=$settings['slot_duration']==15?'selected':''?>>15</option><option <?=$settings['slot_duration']==30?'selected':''?>>30</option><option <?=$settings['slot_duration']==45?'selected':''?>>45</option><option <?=$settings['slot_duration']==60?'selected':''?>>60</option></select></div><div class="col-md-4"><label class="form-label">Buffer minutes</label><input class="form-control" name="buffer_minutes" value="<?=e($settings['buffer_minutes'] ?? 0)?>"></div><div class="col-md-4"><label class="form-label">Max bookings/day</label><input class="form-control" name="max_bookings_per_day" value="<?=e($settings['max_bookings_per_day'] ?? 20)?>"></div><div class="col-md-4"><label class="form-label">Advance days</label><input class="form-control" name="advance_days" value="<?=e($settings['advance_days'] ?? 30)?>"></div><div class="col-md-4"><label class="form-label">Approval</label><div class="form-check"><input type="checkbox" class="form-check-input" name="manual_approval" value="1" <?=!empty($settings['manual_approval'])?'checked':''?>> <label class="form-check-label">Manual approve booking</label></div></div><div class="col-md-4 form-check ms-2"><input class="form-check-input" type="checkbox" name="same_day_booking" value="1" <?=!empty($settings['same_day_booking'])?'checked':''?>> <label class="form-check-label">Allow same-day booking</label></div><div class="col-md-4 form-check"><input class="form-check-input" type="checkbox" name="allow_reschedule" value="1" <?=!empty($settings['allow_reschedule'])?'checked':''?>> <label class="form-check-label">Allow reschedule</label></div><div class="col-md-4 form-check"><input class="form-check-input" type="checkbox" name="allow_cancel" value="1" <?=!empty($settings['allow_cancel'])?'checked':''?>> <label class="form-check-label">Allow cancellation</label></div><div class="col-12"><label class="form-label">Business hours JSON / note</label><textarea class="form-control" name="business_hours_json" rows="5" placeholder='{"mon":{"open":"09:00","close":"18:00"}}'><?=e($settings['business_hours_json'] ?? '')?></textarea></div><div class="col-12"><button class="btn btn-primary">Save Booking Settings</button></div></form></div></div><?php endif; ?>

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