<?php
require_once __DIR__.'/../../config/app.php';
require_once __DIR__.'/../../app/Models/Document.php';
$user    = requireAuth(['driver']);
$license = Document::byUser($user['id'],'license')[0] ?? null;
$errors  = [];

if($_SERVER['REQUEST_METHOD']==='POST') {
    verifyCsrf();
    $expiry = $_POST['expiry_date']??'';
    if($_POST['action']==='delete' && $license) {
        Document::delete($license['id'],$user['id']);
        flash('dash',t('delete')); redirect(BASE_URL.'/driver/license.php');
    }
    if(!$expiry) { $errors[]=t('fill_all_fields'); }
    else {
        $fp = 'uploads/placeholder.pdf';
        if(!empty($_FILES['doc_file']['name'])) {
            $f = $_FILES['doc_file'];
            if($f['size']>MAX_UPLOAD) { $errors[]=t('file_too_large'); }
            elseif(!in_array($f['type'],ALLOWED_MIME,true)) { $errors[]=t('file_invalid'); }
            else {
                @mkdir(UPLOAD_DIR,0755,true);
                $fname = uniqid('lic_',true).'.'.pathinfo($f['name'],PATHINFO_EXTENSION);
                move_uploaded_file($f['tmp_name'],UPLOAD_DIR.$fname);
                $fp='uploads/'.$fname;
            }
        }
        if(!$errors) {
            Document::upsert(['user_id'=>$user['id'],'vehicle_id'=>null,'type'=>'license','file_path'=>$fp,'expiry_date'=>$expiry]);
            flash('dash',t('doc_uploaded')); redirect(BASE_URL.'/driver/license.php');
        }
    }
}
$license = Document::byUser($user['id'],'license')[0] ?? null;
$licSt   = $license ? docStatus($license['expiry_date']) : null;
$pageTitle=t('my_license'); $activePage=BASE_URL.'/driver/license.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_license') ?></span></div>
  <div class="card-body">
    <?php if($license): ?>
    <div style="background:linear-gradient(135deg,#1a56db,#0f3d8c);border-radius:var(--radius-md);padding:24px;color:#fff;margin-bottom:20px">
      <div style="display:flex;justify-content:space-between;align-items:flex-start;margin-bottom:16px">
        <div>
          <div style="opacity:.7;font-size:11px;text-transform:uppercase;letter-spacing:.1em"><?= t('role_driver') ?></div>
          <div style="font-size:20px;font-weight:800;margin-top:4px"><?= e($user['name']) ?></div>
        </div>
        <div style="font-size:40px">🪪</div>
      </div>
      <div style="display:flex;justify-content:space-between;align-items:flex-end">
        <div>
          <div style="opacity:.7;font-size:11px"><?= t('expiry_date') ?></div>
          <div style="font-size:16px;font-weight:700"><?= e($license['expiry_date']) ?></div>
        </div>
        <?= statusBadge($licSt,lang()) ?>
      </div>
    </div>
    <?php $licFile = !empty($license['file_path']) && $license['file_path'] !== 'uploads/placeholder.pdf'; ?>
    <?php if($licFile): ?>
    <a href="<?= BASE_URL.'/'.e($license['file_path']) ?>" target="_blank" class="btn btn-secondary" style="width:100%;margin-bottom:16px">🔗 <?= t('view_document') ?></a>
    <?php endif; ?>
    <?php endif; ?>

    <?php foreach($errors as $e): ?><div class="alert alert-danger"><?= e($e) ?></div><?php endforeach; ?>

    <form method="POST" enctype="multipart/form-data">
      <?= csrfField() ?>
      <input type="hidden" name="action" value="upload">
      <div class="form-group">
        <label class="form-label"><?= t('expiry_date') ?> *</label>
        <input type="date" name="expiry_date" class="form-control" value="<?= e($license['expiry_date']??'') ?>" required>
      </div>
      <div class="form-group">
        <label class="form-label"><?= t('upload_file') ?></label>
        <input type="file" name="doc_file" class="form-control" accept=".pdf,.jpg,.jpeg,.png">
        <div style="font-size:12px;color:var(--text-muted);margin-top:4px">PDF, JPG, PNG — 5MB max</div>
      </div>
      <button type="submit" class="btn btn-primary" style="width:100%">
        📤 <?= $license?t('save'):t('upload_doc') ?>
      </button>
    </form>

    <?php if($license): ?>
    <form method="POST" style="margin-top:10px" onsubmit="return confirm('<?= t('confirm') ?>?')">
      <?= csrfField() ?>
      <input type="hidden" name="action" value="delete">
      <button type="submit" class="btn btn-danger" style="width:100%">🗑 <?= t('delete') ?></button>
    </form>
    <?php endif; ?>
  </div>
</div>
</div>
<?php require __DIR__.'/../../app/Views/shared/layout_end.php'; ?>
