<?php $this->extend("layouts/main"); $this->section("content"); $title = "Global Settings";
$tabLinks = ["job-types"=>"Job Types","statuses"=>"Job Statuses","pricing"=>"Pricing Types","templates"=>"Checklist Templates","customer-fields"=>"Customer Fields","job-fields"=>"Job Fields","quote-fields"=>"Quote Fields","invoice-fields"=>"Invoice Fields"];
?>
<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/global/<?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==="job-types"): ?>
<div class="card mb-3"><div class="card-header"><h6 class="mb-0">Job Types</h6></div><div class="card-body p-0"><table class="table mb-0"><thead><tr><th>Name</th><th>Colour</th><th></th></tr></thead><tbody>
<?php foreach ($jobTypes as $t): ?>
<tr><td><?php echo esc($t["name"]) ?></td><td><span class="badge" style="background:<?php echo esc($t["color"]) ?>"><?php echo esc($t["color"]) ?></span></td>
<td><div class="table-actions">
<form method="POST" action="/fieldly/public/index.php/global/job-types/<?php echo $t["id"] ?>/delete" data-confirm="Delete?" class="d-inline"><?php echo csrf_field() ?><button class="btn btn-sm btn-outline-danger btn-icon"><i class="bi bi-trash3"></i></button></form>
</div></td></tr>
<?php endforeach; ?>
<?php if (empty($jobTypes)): ?><tr><td colspan="3" class="text-center text-muted py-3">No job types</td></tr><?php endif; ?>
</tbody></table></div></div>
<div class="card"><div class="card-header"><h6 class="mb-0">Add Job Type</h6></div>
<form method="POST" action="/fieldly/public/index.php/global/job-types/save"><?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" required/></div><div class="col-md-4"><label class="form-label">Colour</label><input type="color" name="color" class="form-control form-control-color" value="#4f46e5"/></div><div class="col-12"><button type="submit" class="btn btn-primary">Add</button></div></div></div>
</form></div>

<?php elseif ($tab==="statuses"): ?>
<div class="card mb-3"><div class="card-header"><h6 class="mb-0">Job Statuses</h6></div><div class="card-body p-0"><table class="table mb-0"><thead><tr><th>Name</th><th>Colour</th><th>Options</th><th></th></tr></thead><tbody>
<?php foreach ($jobStatuses as $s): ?>
<tr><td><span class="badge me-2" style="background:<?php echo esc($s["color"]) ?>"><?php echo esc($s["name"]) ?></span></td><td><?php echo esc($s["color"]) ?></td><td style="font-size:12px"><?php echo $s["is_default"]?"Default · ":"" ?><?php echo $s["is_complete"]?"Complete · ":"" ?><?php echo $s["is_cancelled"]?"Cancelled · ":"" ?></td>
<td><form method="POST" action="/fieldly/public/index.php/global/statuses/<?php echo $s["id"] ?>/delete" data-confirm="Delete?"><?php echo csrf_field() ?><button class="btn btn-sm btn-outline-danger btn-icon"><i class="bi bi-trash3"></i></button></form></td></tr>
<?php endforeach; ?>
</tbody></table></div></div>
<div class="card"><div class="card-header"><h6 class="mb-0">Add Status</h6></div>
<form method="POST" action="/fieldly/public/index.php/global/statuses/save"><?php echo csrf_field() ?>
<div class="card-body"><div class="row g-3"><div class="col-md-5"><label class="form-label">Name *</label><input type="text" name="name" class="form-control" required/></div><div class="col-md-3"><label class="form-label">Colour</label><input type="color" name="color" class="form-control form-control-color" value="#6b7280"/></div><div class="col-md-4 d-flex gap-3 align-items-end pb-1"><div class="form-check"><input type="checkbox" name="is_complete" class="form-check-input"/><label class="form-check-label small">Complete</label></div><div class="form-check"><input type="checkbox" name="is_cancelled" class="form-check-input"/><label class="form-check-label small">Cancelled</label></div></div><div class="col-12"><button type="submit" class="btn btn-primary">Add</button></div></div></div>
</form></div>

<?php elseif ($tab==="pricing"): ?>
<div class="card mb-3"><div class="card-header"><h6 class="mb-0">Pricing Types</h6></div><div class="card-body p-0"><table class="table mb-0"><thead><tr><th>Name</th><th>Basis</th><th>Chargeable</th><th></th></tr></thead><tbody>
<?php foreach ($pricingTypes as $p): ?><tr><td><?php echo esc($p["name"]) ?></td><td><?php echo ucfirst(str_replace("_"," ",$p["basis"])) ?></td><td><?php echo $p["chargeable"]?"<span class="badge bg-success">Yes</span>":"<span class="badge bg-secondary">No</span>" ?></td><td><form method="POST" action="/fieldly/public/index.php/global/pricing/<?php echo $p["id"] ?>/delete" data-confirm="Delete?"><?php echo csrf_field() ?><button class="btn btn-sm btn-outline-danger btn-icon"><i class="bi bi-trash3"></i></button></form></td></tr><?php endforeach; ?></tbody></table></div></div>
<div class="card"><div class="card-header"><h6 class="mb-0">Add Pricing Type</h6></div>
<form method="POST" action="/fieldly/public/index.php/global/pricing/save"><?php echo csrf_field() ?>
<div class="card-body"><div class="row g-3"><div class="col-md-5"><label class="form-label">Name *</label><input type="text" name="name" class="form-control" required/></div><div class="col-md-4"><label class="form-label">Basis</label><select name="basis" class="form-select"><option value="hourly">Hourly</option><option value="fixed_amount">Fixed Amount</option><option value="quoted">Quoted</option><option value="no_charge">No Charge</option></select></div><div class="col-md-3 d-flex align-items-end pb-1"><div class="form-check"><input type="checkbox" name="chargeable" class="form-check-input" checked/><label class="form-check-label">Chargeable</label></div></div><div class="col-12"><button type="submit" class="btn btn-primary">Add</button></div></div></div>
</form></div>

<?php elseif ($tab==="templates"): ?>
<div class="row g-3">
<?php foreach ($templates as $tpl): ?>
<div class="col-12"><div class="card"><div class="card-header d-flex justify-content-between"><span class="fw-bold"><?php echo esc($tpl["name"]) ?></span><form method="POST" action="/fieldly/public/index.php/global/templates/<?php echo $tpl["id"] ?>/delete" data-confirm="Delete template?"><?php echo csrf_field() ?><button class="btn btn-sm btn-outline-danger btn-icon"><i class="bi bi-trash3"></i></button></form></div><div class="card-body"><ul class="mb-0" style="font-size:14px"><?php foreach ($tpl["items"] as $item): ?><li><?php echo esc($item["text"]) ?></li><?php endforeach; ?></ul></div></div></div>
<?php endforeach; ?>
</div>
<div class="card mt-3"><div class="card-header"><h6 class="mb-0">New Template</h6></div>
<form method="POST" action="/fieldly/public/index.php/global/templates/save"><?php echo csrf_field() ?>
<div class="card-body"><div class="row g-3">
<div class="col-md-8"><label class="form-label">Template Name *</label><input type="text" name="name" class="form-control" required/></div>
<div class="col-md-4"><label class="form-label">Job Type</label><select name="job_type_id" class="form-select"><option value="">Any</option><?php foreach ($jobTypes as $jt): ?><option value="<?php echo $jt["id"] ?>"><?php echo esc($jt["name"]) ?></option><?php endforeach; ?></select></div>
<div class="col-12"><label class="form-label">Checklist Items (one per line)</label><textarea name="items[]" class="form-control" rows="6" placeholder="Enter each checklist item on a new line..."></textarea><div class="form-text">Each line will become a separate checklist item.</div></div>
<div class="col-12"><button type="submit" class="btn btn-primary">Create Template</button></div>
</div></div>
</form></div>

<?php elseif (in_array($tab,["customer-fields","job-fields","quote-fields","invoice-fields"])): ?>
<div class="card mb-3"><div class="card-header"><h6 class="mb-0">Custom Fields for <?php echo ucfirst($entityType) ?>s</h6></div><div class="card-body p-0">
<table class="table mb-0"><thead><tr><th>Name</th><th>Type</th><th>Required</th><th></th></tr></thead><tbody>
<?php foreach ($fields as $f): ?><tr><td><?php echo esc($f["name"]) ?></td><td><?php echo ucfirst($f["field_type"]) ?></td><td><?php echo $f["required"]?"<span class="badge bg-danger">Yes</span>":"No" ?></td><td><form method="POST" action="/fieldly/public/index.php/global/custom-fields/<?php echo $f["id"] ?>/delete" data-confirm="Delete this field?"><?php echo csrf_field() ?><button class="btn btn-sm btn-outline-danger btn-icon"><i class="bi bi-trash3"></i></button></form></td></tr><?php endforeach; ?>
<?php if (empty($fields)): ?><tr><td colspan="4" class="text-center text-muted py-3">No custom fields</td></tr><?php endif; ?>
</tbody></table></div></div>
<div class="card"><div class="card-header"><h6 class="mb-0">Add Custom Field</h6></div>
<form method="POST" action="/fieldly/public/index.php/global/custom-fields/save"><?php echo csrf_field() ?>
<input type="hidden" name="entity_type" value="<?php echo esc($entityType) ?>"/>
<div class="card-body"><div class="row g-3">
<div class="col-md-6"><label class="form-label">Field Name *</label><input type="text" name="name" class="form-control" required/></div>
<div class="col-md-4"><label class="form-label">Field Type</label><select name="field_type" class="form-select"><option value="text">Text</option><option value="number">Number</option><option value="date">Date</option><option value="dropdown">Dropdown</option><option value="textarea">Textarea</option><option value="checkbox">Checkbox</option></select></div>
<div class="col-md-2 d-flex align-items-end pb-1"><div class="form-check"><input type="checkbox" name="required" class="form-check-input"/><label class="form-check-label">Required</label></div></div>
<div class="col-12"><button type="submit" class="btn btn-primary">Add Field</button></div>
</div></div>
</form></div>
<?php endif; ?>

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