<?php
require_once __DIR__.'/../includes/functions.php';
$admin = require_admin();
verify_csrf();
if (function_exists('mj_v290_ensure_schema')) { mj_v290_ensure_schema(); }
$adminTitle = 'Admin Layout Settings';
$adminSubtitle = 'Control admin dashboard layout, theme mode, sidebar behavior and widget visibility.';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $adminId = (int)($admin['id'] ?? 0);
    q('INSERT INTO mj_admin_layout_settings(admin_id,layout_mode,theme_mode,sidebar_default,fixed_header,card_shadow,dashboard_widgets,updated_at) VALUES(?,?,?,?,?,?,?,NOW()) ON DUPLICATE KEY UPDATE layout_mode=VALUES(layout_mode),theme_mode=VALUES(theme_mode),sidebar_default=VALUES(sidebar_default),fixed_header=VALUES(fixed_header),card_shadow=VALUES(card_shadow),dashboard_widgets=VALUES(dashboard_widgets),updated_at=NOW()', [
        $adminId,
        in_array($_POST['layout_mode'] ?? 'comfortable', ['compact','comfortable'], true) ? $_POST['layout_mode'] : 'comfortable',
        in_array($_POST['theme_mode'] ?? 'light', ['light','dark','auto'], true) ? $_POST['theme_mode'] : 'light',
        in_array($_POST['sidebar_default'] ?? 'expanded', ['expanded','collapsed'], true) ? $_POST['sidebar_default'] : 'expanded',
        isset($_POST['fixed_header']) ? 1 : 0,
        isset($_POST['card_shadow']) ? 1 : 0,
        json_encode(array_values((array)($_POST['widgets'] ?? [])))
    ]);
    flash('success','Admin layout settings saved.');
    redirect('admin-layout-settings.php');
}
$row = [];
try { $row = table_exists('mj_admin_layout_settings') ? (one('SELECT * FROM mj_admin_layout_settings WHERE admin_id=? LIMIT 1', [(int)($admin['id'] ?? 0)]) ?: []) : []; } catch(Throwable $e) { $row=[]; }
$widgets = json_decode((string)($row['dashboard_widgets'] ?? '[]'), true) ?: ['users','listings','companies','payments','support','bookings','revenue','health','activity'];
include __DIR__.'/_header.php';
?>
<div class="card card-primary card-outline"><form method="post"><?=csrf_input()?><div class="card-header"><h3 class="card-title"><i class="fas fa-columns mr-2"></i>Advanced Admin Layout</h3></div><div class="card-body">
  <div class="row">
    <div class="col-md-4"><label>Layout Density</label><select class="form-control" name="layout_mode"><option value="comfortable" <?=($row['layout_mode']??'comfortable')==='comfortable'?'selected':''?>>Comfortable</option><option value="compact" <?=($row['layout_mode']??'')==='compact'?'selected':''?>>Compact</option></select></div>
    <div class="col-md-4"><label>Theme Mode</label><select class="form-control" name="theme_mode"><option value="light" <?=($row['theme_mode']??'light')==='light'?'selected':''?>>Light</option><option value="dark" <?=($row['theme_mode']??'')==='dark'?'selected':''?>>Dark</option><option value="auto" <?=($row['theme_mode']??'')==='auto'?'selected':''?>>Auto</option></select></div>
    <div class="col-md-4"><label>Sidebar Default</label><select class="form-control" name="sidebar_default"><option value="expanded" <?=($row['sidebar_default']??'expanded')==='expanded'?'selected':''?>>Expanded</option><option value="collapsed" <?=($row['sidebar_default']??'')==='collapsed'?'selected':''?>>Collapsed</option></select></div>
  </div>
  <div class="row mt-4"><div class="col-md-6"><div class="custom-control custom-switch"><input type="checkbox" class="custom-control-input" id="fixed" name="fixed_header" <?=((int)($row['fixed_header']??1))?'checked':''?>><label class="custom-control-label" for="fixed">Fixed header</label></div></div><div class="col-md-6"><div class="custom-control custom-switch"><input type="checkbox" class="custom-control-input" id="shadow" name="card_shadow" <?=((int)($row['card_shadow']??1))?'checked':''?>><label class="custom-control-label" for="shadow">Card shadow</label></div></div></div>
  <hr><h5 class="font-weight-bold">Dashboard Widgets</h5><div class="row">
  <?php foreach(['users'=>'Users','listings'=>'Listings','companies'=>'Companies','payments'=>'Payments','support'=>'Support','bookings'=>'Bookings','revenue'=>'Revenue','health'=>'System Health','activity'=>'Recent Activity','countries'=>'Country Performance'] as $key=>$label): ?>
    <div class="col-md-3"><div class="custom-control custom-checkbox mb-2"><input type="checkbox" class="custom-control-input" id="w_<?=e($key)?>" name="widgets[]" value="<?=e($key)?>" <?=in_array($key,$widgets,true)?'checked':''?>><label class="custom-control-label" for="w_<?=e($key)?>"><?=e($label)?></label></div></div>
  <?php endforeach; ?></div>
</div><div class="card-footer"><button class="btn btn-primary"><i class="fas fa-save mr-1"></i>Save Layout</button></div></form></div>
<?php include __DIR__.'/_footer.php'; ?>
