<?php /* driver/profile.php */
require_once __DIR__.'/../../config/app.php';
require_once __DIR__.'/../../app/Models/User.php';
$user = requireAuth(['driver']);
$errors = [];
if($_SERVER['REQUEST_METHOD']==='POST') {
    verifyCsrf();
    $name  = trim($_POST['name']??'');
    $phone = trim($_POST['phone']??'');
    $pwd   = $_POST['password']??'';
    $conf  = $_POST['confirm']??'';
    if(!$name) $errors[]=t('fill_all_fields');
    if($pwd && $pwd!==$conf) $errors[]=t('password_mismatch');
    if(!$errors) {
        $upd = ['name'=>$name,'phone'=>$phone];
        if($pwd) $upd['password']=$pwd;
        User::update($user['id'],$upd);
        $user = array_merge($user,$upd);
        $_SESSION['user'] = $user;
        flash('dash',t('profile_updated'));
        redirect(BASE_URL.'/driver/profile.php');
    }
}
$pageTitle=''; $activePage=BASE_URL.'/driver/profile.php';
require __DIR__.'/../../app/Views/shared/layout.php';
?>
<div style="max-width:520px;margin:0 auto">
<div class="card">
  <div class="card-header"><span class="card-title">👤 <?= t('my_profile') ?></span></div>
  <div class="card-body">
    <div style="text-align:center;margin-bottom:20px">
      <div class="sidebar-avatar" style="width:64px;height:64px;font-size:24px;margin:0 auto 8px"><?= avatarInitials($user['name']) ?></div>
      <div style="font-weight:700"><?= e($user['name']) ?></div>
      <span class="badge badge-info"><?= t('role_driver') ?></span>
    </div>
    <?php foreach($errors as $e): ?><div class="alert alert-danger"><?= e($e) ?></div><?php endforeach; ?>
    <form method="POST">
      <?= csrfField() ?>
      <div class="form-group">
        <label class="form-label"><?= t('full_name') ?> *</label>
        <input type="text" name="name" class="form-control" value="<?= e($user['name']) ?>" required>
      </div>
      <div class="form-row">
        <div class="form-group">
          <label class="form-label"><?= t('email') ?></label>
          <input type="email" class="form-control" value="<?= e($user['email']) ?>" disabled style="opacity:.6">
        </div>
        <div class="form-group">
          <label class="form-label"><?= t('phone') ?></label>
          <input type="tel" name="phone" class="form-control" value="<?= e($user['phone']??'') ?>">
        </div>
      </div>
      <hr>
      <div class="form-row">
        <div class="form-group">
          <label class="form-label"><?= t('password') ?></label>
          <input type="password" name="password" class="form-control" placeholder="••••••••">
        </div>
        <div class="form-group">
          <label class="form-label"><?= t('confirm_password') ?></label>
          <input type="password" name="confirm" class="form-control" placeholder="••••••••">
        </div>
      </div>
      <button type="submit" class="btn btn-primary" style="width:100%">💾 <?= t('save') ?></button>
    </form>
  </div>
</div>
</div>
<?php require __DIR__.'/../../app/Views/shared/layout_end.php'; ?>
