<?php $this->extend("layouts/main"); $this->section("content");
$title = "Settings — ".ucfirst(str_replace("_"," ",$tab));
$tabLinks = ["branding"=>"Branding","regional"=>"Regional","numbering"=>"Numbering","tax"=>"Tax Rates","invoice_defaults"=>"Invoice Defaults","quote_defaults"=>"Quote Defaults","job_defaults"=>"Job Defaults","schedule"=>"Schedule","stripe"=>"Stripe","roles"=>"Roles","users"=>"Users","notifications"=>"Notifications","billing"=>"Billing"];
?>
<div class="row">
<div class="col-lg-2 mb-3">
    <div class="list-group">
    <?php foreach ($tabLinks as $t=>$label): ?>
    <a href="/fieldly/public/index.php/settings/<?php echo $t ?>" class="list-group-item list-group-item-action <?php echo $tab===$t?"active":"" ?>" style="font-size:13px"><?php echo $label ?></a>
    <?php endforeach; ?>
    </div>
</div>
<div class="col-lg-10">

<?php if ($tab==="branding"): ?>
<div class="card"><div class="card-header"><h6 class="mb-0">Branding</h6></div>
<form method="POST" action="/fieldly/public/index.php/settings/branding"><?php echo csrf_field() ?>
<div class="card-body"><div class="row g-3">
    <div class="col-md-8"><label class="form-label">Company Name</label><input type="text" name="company_name" class="form-control" value="<?php echo esc($settings["branding"]["company_name"]??"") ?>"/></div>
    <div class="col-md-4"><label class="form-label">Theme Colour</label><input type="color" name="primary_colour" class="form-control form-control-color" value="<?php echo $settings["branding"]["primary_colour"]??"#4f46e5" ?>"/></div>
    <div class="col-12"><button type="submit" class="btn btn-primary">Save</button></div>
</div></div>
</form></div>

<?php elseif ($tab==="regional"): ?>
<div class="card"><div class="card-header"><h6 class="mb-0">Regional Settings</h6></div>
<form method="POST" action="/fieldly/public/index.php/settings/regional"><?php echo csrf_field() ?>
<div class="card-body"><div class="row g-3">
    <div class="col-md-6"><label class="form-label">Country</label><select name="country" class="form-select"><option value="GB" <?php echo ($settings["regional"]["country"]??"GB")==="GB"?"selected":"" ?>>United Kingdom</option><option value="IE" <?php echo ($settings["regional"]["country"]??"")==="IE"?"selected":"" ?>>Ireland</option><option value="US" <?php echo ($settings["regional"]["country"]??"")==="US"?"selected":"" ?>>United States</option><option value="AU" <?php echo ($settings["regional"]["country"]??"")==="AU"?"selected":"" ?>>Australia</option></select></div>
    <div class="col-md-6"><label class="form-label">Currency</label><select name="currency" class="form-select"><option value="GBP" <?php echo ($settings["regional"]["currency"]??"GBP")==="GBP"?"selected":"" ?>>GBP — British Pound</option><option value="EUR" <?php echo ($settings["regional"]["currency"]??"")==="EUR"?"selected":"" ?>>EUR — Euro</option><option value="USD" <?php echo ($settings["regional"]["currency"]??"")==="USD"?"selected":"" ?>>USD — US Dollar</option><option value="AUD" <?php echo ($settings["regional"]["currency"]??"")==="AUD"?"selected":"" ?>>AUD — Australian Dollar</option></select></div>
    <div class="col-md-6"><label class="form-label">Timezone</label><select name="timezone" class="form-select"><option value="Europe/London" <?php echo ($settings["regional"]["timezone"]??"Europe/London")==="Europe/London"?"selected":"" ?>>Europe/London</option><option value="Europe/Dublin" <?php echo ($settings["regional"]["timezone"]??"")==="Europe/Dublin"?"selected":"" ?>>Europe/Dublin</option><option value="America/New_York" <?php echo ($settings["regional"]["timezone"]??"")==="America/New_York"?"selected":"" ?>>America/New_York</option></select></div>
    <div class="col-md-6"><label class="form-label">Date Format</label><select name="date_format" class="form-select"><option value="DD/MM/YYYY" <?php echo ($settings["regional"]["date_format"]??"DD/MM/YYYY")==="DD/MM/YYYY"?"selected":"" ?>>DD/MM/YYYY</option><option value="MM/DD/YYYY" <?php echo ($settings["regional"]["date_format"]??"")==="MM/DD/YYYY"?"selected":"" ?>>MM/DD/YYYY</option><option value="YYYY-MM-DD" <?php echo ($settings["regional"]["date_format"]??"")==="YYYY-MM-DD"?"selected":"" ?>>YYYY-MM-DD</option></select></div>
    <div class="col-12"><button type="submit" class="btn btn-primary">Save</button></div>
</div></div>
</form></div>

<?php elseif ($tab==="numbering"): ?>
<div class="card"><div class="card-header"><h6 class="mb-0">Numbering</h6></div>
<form method="POST" action="/fieldly/public/index.php/settings/numbering"><?php echo csrf_field() ?>
<div class="card-body"><div class="row g-3">
    <?php foreach (["cust"=>"Customer","job"=>"Job","quote"=>"Quote","inv"=>"Invoice"] as $k=>$label): ?>
    <div class="col-md-6"><label class="form-label"><?php echo $label ?> Number Prefix</label><div class="input-group"><input type="text" name="<?php echo $k ?>_prefix" class="form-control" value="<?php echo esc($settings["numbering"][$k."_prefix"]??strtoupper($k)."-") ?>"/><span class="input-group-text">0001</span></div></div>
    <div class="col-md-6"><label class="form-label">Next <?php echo $label ?> Number</label><input type="number" name="<?php echo $k ?>_next" class="form-control" value="<?php echo esc($settings["numbering"][$k."_next"]??1) ?>" min="1"/></div>
    <?php endforeach; ?>
    <div class="col-12"><button type="submit" class="btn btn-primary">Save</button></div>
</div></div>
</form></div>

<?php elseif ($tab==="invoice_defaults"): ?>
<div class="card"><div class="card-header"><h6 class="mb-0">Invoice Defaults</h6></div>
<form method="POST" action="/fieldly/public/index.php/settings/invoice_defaults"><?php echo csrf_field() ?>
<div class="card-body"><div class="row g-3">
    <div class="col-12"><label class="form-label">Default Payment Terms</label><textarea name="payment_terms" class="form-control" rows="2"><?php echo esc($settings["invoice_defaults"]["payment_terms"]??"Payment due within 30 days") ?></textarea></div>
    <div class="col-12"><label class="form-label">Bank Details (shown on invoices)</label><textarea name="bank_details" class="form-control" rows="3"><?php echo esc($settings["invoice_defaults"]["bank_details"]??"") ?></textarea></div>
    <div class="col-12"><label class="form-label">Footer Text</label><textarea name="footer_text" class="form-control" rows="2"><?php echo esc($settings["invoice_defaults"]["footer_text"]??"Thank you for your business") ?></textarea></div>
    <div class="col-12"><button type="submit" class="btn btn-primary">Save</button></div>
</div></div>
</form></div>

<?php elseif ($tab==="tax"): ?>
<div class="card mb-3"><div class="card-header"><h6 class="mb-0">Tax Rates</h6></div><div class="card-body p-0">
<table class="table mb-0"><thead><tr><th>Name</th><th>Rate</th><th>Default</th><th></th></tr></thead><tbody>
<?php foreach ($taxRates as $t): ?>
<tr><td><?php echo esc($t["name"]) ?></td><td><?php echo $t["rate"] ?>%</td><td><?php echo $t["is_default"]?"<span class="badge bg-success">Yes</span>":"" ?></td><td><?php if (!$t["is_default"]): ?><form method="POST" action="/fieldly/public/index.php/settings/tax/<?php echo $t["id"] ?>/delete" data-confirm="Delete this tax rate?"><?php echo csrf_field() ?><button class="btn btn-sm btn-outline-danger btn-icon"><i class="bi bi-trash3"></i></button></form><?php endif; ?></td></tr>
<?php endforeach; ?>
</tbody></table>
</div></div>
<div class="card"><div class="card-header"><h6 class="mb-0">Add Tax Rate</h6></div>
<form method="POST" action="/fieldly/public/index.php/settings/tax/create"><?php echo csrf_field() ?>
<div class="card-body"><div class="row g-3">
    <div class="col-md-6"><label class="form-label">Name</label><input type="text" name="name" class="form-control" placeholder="e.g. Standard Rate (20%)"/></div>
    <div class="col-md-6"><label class="form-label">Rate (%)</label><input type="number" name="rate" class="form-control" step="0.1" min="0" max="100"/></div>
    <div class="col-12"><button type="submit" class="btn btn-primary">Add Rate</button></div>
</div></div>
</form></div>

<?php elseif ($tab==="users"): ?>
<div class="card mb-3"><div class="card-header d-flex justify-content-between"><h6 class="mb-0">Users</h6><button class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#addUserModal"><i class="bi bi-plus me-1"></i> Add User</button></div>
<div class="card-body p-0"><table class="table mb-0"><thead><tr><th>Name</th><th>Email</th><th>Role</th><th>Status</th><th></th></tr></thead><tbody>
<?php foreach ($users as $u): ?>
<tr><td><span class="badge me-1" style="background:<?php echo esc($u["color"]??"#4f46e5") ?>"><?php echo esc($u["initials"]) ?></span><?php echo esc($u["first_name"]." ".$u["last_name"]) ?></td><td style="font-size:13px"><?php echo esc($u["email"]) ?></td><td><span class="badge" style="background:<?php echo esc($u["role_color"]??"#6b7280") ?>"><?php echo esc($u["role_name"]) ?></span></td><td><span class="badge bg-<?php echo $u["status"]==="active"?"success":"secondary" ?>"><?php echo ucfirst($u["status"]) ?></span></td>
<td><div class="table-actions"><?php if ($u["id"]!==$currentUser["id"] && in_array("manage_users", $permissions)): ?><form method="POST" action="/fieldly/public/index.php/settings/users/<?php echo $u["id"] ?>/delete" data-confirm="Deactivate this user?"><?php echo csrf_field() ?><button class="btn btn-sm btn-outline-danger"><i class="bi bi-person-x"></i></button></form><?php endif; ?></div></td></tr>
<?php endforeach; ?>
</tbody></table></div></div>
<!-- Add User Modal -->
<div class="modal fade" id="addUserModal" tabindex="-1"><div class="modal-dialog modal-dialog-centered"><div class="modal-content">
<form method="POST" action="/fieldly/public/index.php/settings/users/create"><?php echo csrf_field() ?>
<div class="modal-header"><h5 class="modal-title">Add User</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
<div class="modal-body"><div class="row g-3">
    <div class="col-md-6"><label class="form-label">First Name *</label><input type="text" name="first_name" class="form-control" required/></div>
    <div class="col-md-6"><label class="form-label">Last Name *</label><input type="text" name="last_name" class="form-control" required/></div>
    <div class="col-12"><label class="form-label">Email *</label><input type="email" name="email" class="form-control" required style="font-size:16px"/></div>
    <div class="col-md-6"><label class="form-label">Role *</label><select name="role_id" class="form-select" required><option value="">Select role...</option><?php foreach ($roles as $r): ?><option value="<?php echo $r["id"] ?>"><?php echo esc($r["name"]) ?></option><?php endforeach; ?></select></div>
    <div class="col-md-6"><label class="form-label">Hourly Rate</label><div class="input-group"><span class="input-group-text">£</span><input type="number" name="hourly_rate" class="form-control" step="0.01" min="0" value="0"/></div></div>
    <div class="col-md-6"><label class="form-label">Password *</label><input type="password" name="password" class="form-control" minlength="8" required/></div>
    <div class="col-md-6"><label class="form-label">Colour</label><input type="color" name="color" class="form-control form-control-color" value="#4f46e5"/></div>
</div></div>
<div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button><button type="submit" class="btn btn-primary">Create User</button></div>
</form>
</div></div></div>

<?php elseif ($tab==="roles"): ?>
<div class="row g-3">
<?php foreach ($roles as $r): ?>
<div class="col-12"><div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h6 class="mb-0"><span class="badge me-2" style="background:<?php echo esc($r["color"]) ?>">&nbsp;</span><?php echo esc($r["name"]) ?></h6>
        <?php if (!$r["is_system"] && in_array("manage_roles", $permissions)): ?>
        <form method="POST" action="/fieldly/public/index.php/settings/roles/<?php echo $r["id"] ?>/delete" data-confirm="Delete this role?"><?php echo csrf_field() ?><button class="btn btn-sm btn-outline-danger btn-icon"><i class="bi bi-trash3"></i></button></form>
        <?php endif; ?>
    </div>
    <form method="POST" action="/fieldly/public/index.php/settings/roles/<?php echo $r["id"] ?>/edit"><?php echo csrf_field() ?>
    <div class="card-body">
        <div class="row g-2 mb-3">
            <div class="col-md-6"><label class="form-label">Name</label><input type="text" name="name" class="form-control form-control-sm" value="<?php echo esc($r["name"]) ?>"/></div>
            <div class="col-md-2"><label class="form-label">Colour</label><input type="color" name="color" class="form-control form-control-sm form-control-color" value="<?php echo esc($r["color"]) ?>"/></div>
        </div>
        <div class="row g-2">
        <?php foreach ($permissionGroups as $group=>$perms): ?>
        <div class="col-md-3">
            <div class="fw-bold small mb-1"><?php echo $group ?></div>
            <?php foreach ($perms as $p): ?>
            <div class="form-check"><input type="checkbox" name="permissions[]" value="<?php echo $p ?>" class="form-check-input" id="<?php echo $r["id"]."_".$p ?>" <?php echo in_array($p,$r["permissions"])?"checked":"" ?>/><label class="form-check-label small" for="<?php echo $r["id"]."_".$p ?>"><?php echo ucfirst(str_replace(["_","view ","edit ","manage "],["  ","","",""],$p)) ?></label></div>
            <?php endforeach; ?>
        </div>
        <?php endforeach; ?>
        </div>
    </div>
    <div class="card-footer"><button type="submit" class="btn btn-sm btn-primary">Save Role</button></div>
    </form>
</div></div>
<?php endforeach; ?>
</div>

<?php else: ?>
<div class="card"><div class="card-header"><h6 class="mb-0"><?php echo ucfirst(str_replace("_"," ",$tab)) ?></h6></div>
<form method="POST" action="/fieldly/public/index.php/settings/<?php echo $tab ?>"><?php echo csrf_field() ?>
<div class="card-body"><div class="row g-3">
    <?php $group=$settings[$tab]??[]; foreach ($group as $key=>$value): ?>
    <div class="col-md-6"><label class="form-label"><?php echo ucfirst(str_replace("_"," ",$key)) ?></label><input type="text" name="<?php echo esc($key) ?>" class="form-control" value="<?php echo esc($value) ?>"/></div>
    <?php endforeach; ?>
    <div class="col-12"><button type="submit" class="btn btn-primary">Save</button></div>
</div></div>
</form></div>
<?php endif; ?>

</div></div>
<?php $this->endSection(); ?>