<?php
$selectedPermissions=json_decode((string)($userRecord['permissions']??''),true)?:[];
$permissionOptions=[
'loads.manage'=>'Manage loads','carriers.manage'=>'Manage carriers','documents.manage'=>'Manage documents','border.manage'=>'Manage border crossings',
'tracking.view'=>'View live tracking','fuel.view'=>'View fuel allocations','fuel.manage'=>'Manage fuel allocations','hos.view'=>'View driver logbooks','hos.manage'=>'Manage HOS profiles and edits','accounting.view'=>'View accounting','accounting.manage'=>'Manage accounting','maintenance.manage'=>'Manage maintenance',
'tasks.manage'=>'Manage team tasks','reports.financial'=>'View financial reports','ai.use'=>'Use FleetLume AI'
];
?>
<form method="post" action="<?= e($action) ?>" class="panel form-stack">
    <?= csrf_field() ?>
    <?php if(!empty($userRecord['id'])): ?><input type="hidden" name="id" value="<?= e((string)$userRecord['id']) ?>"><?php endif; ?>
    <div class="form-grid three">
      <label><span>Name *</span><input name="name" value="<?= e(old('name',$userRecord['name']??'')) ?>" required></label>
      <label><span>Email *</span><input type="email" name="email" value="<?= e(old('email',$userRecord['email']??'')) ?>" required></label>
      <label><span>Phone</span><input name="phone" value="<?= e(old('phone',$userRecord['phone']??'')) ?>"></label>
      <label><span>Password <?= empty($userRecord['id'])?'*':'' ?></span><input type="password" name="password" <?= empty($userRecord['id'])?'required':'' ?>><small class="muted"><?= empty($userRecord['id'])?'Minimum 8 characters.':'Leave blank to keep current password.' ?></small></label>
      <label><span>Role / Portal</span><select name="role"><?php foreach($roles as $role): ?><option value="<?= e($role) ?>" <?= old('role',$userRecord['role']??'dispatcher')===$role?'selected':'' ?>><?= e(readable_status($role)) ?></option><?php endforeach; ?></select></label>
      <label><span>Department</span><input name="department" value="<?= e(old('department',$userRecord['department']??'')) ?>" placeholder="Dispatch, Finance, Safety"></label>
      <label><span>Status</span><select name="status"><option value="active" <?= old('status',$userRecord['status']??'active')==='active'?'selected':'' ?>>Active</option><option value="inactive" <?= old('status',$userRecord['status']??'')==='inactive'?'selected':'' ?>>Inactive</option></select></label>
      <label><span>Linked Driver</span><select name="driver_id"><option value="">Not a driver login</option><?php foreach($drivers as $driver): ?><option value="<?= e((string)$driver['id']) ?>" <?= (string)old('driver_id',$userRecord['driver_id']??'')===(string)$driver['id']?'selected':'' ?>><?= e($driver['name']) ?><?= !empty($driver['carrier_name'])?' · '.e($driver['carrier_name']):'' ?></option><?php endforeach; ?></select><small class="muted">Required only for the Driver portal.</small></label>
      <label><span>Linked Carrier Company</span><select name="carrier_id"><option value="">Not a carrier login</option><?php foreach($carriers as $carrier): ?><option value="<?= e((string)$carrier['id']) ?>" <?= (string)old('carrier_id',$userRecord['carrier_id']??'')===(string)$carrier['id']?'selected':'' ?>><?= e($carrier['company_name']) ?></option><?php endforeach; ?></select><small class="muted">Required only for the Carrier portal.</small></label>
    </div>
    <div><span class="field-title">Additional Permissions</span><div class="permission-grid"><?php foreach($permissionOptions as $key=>$label): ?><label class="permission-option"><input type="checkbox" name="permissions[]" value="<?= e($key) ?>" <?= in_array($key,$selectedPermissions,true)?'checked':'' ?>><span><?= e($label) ?></span></label><?php endforeach; ?></div><small class="muted">Role defaults are applied automatically. These options add extra access.</small></div>
    <div class="form-actions"><button class="btn btn-primary" type="submit">Save User</button><a class="btn btn-light" href="<?= url('/users') ?>">Cancel</a></div>
</form>
