{% extends "ui/templates/base/layout.html" %}
{% block content %}
<div class="mx-auto max-w-5xl space-y-6">
  <div>
    <h1 class="text-2xl font-bold text-slate-900">{{ title }}</h1>
    <p class="mt-1 text-sm text-slate-500">Capture notice/case details, due dates and responsibility.</p>
    <p class="mt-1 text-xs font-semibold uppercase tracking-wide text-slate-400">Active FY: {{ active_financial_year or '-' }}{% if active_assessment_year %} · AY {{ active_assessment_year }}{% endif %}</p>
  </div>
  {% if form_error %}<div class="rounded-lg bg-red-50 p-3 text-sm text-red-700">{{ form_error }}</div>{% endif %}
  <form method="post" class="space-y-5 rounded-xl bg-white p-5 shadow-sm ring-1 ring-slate-200">
    <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
    <div class="grid gap-4 md:grid-cols-3">
      <label class="space-y-1"><span class="text-sm font-medium">Client</span><select name="client_id" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm" {% if mode=='edit' %}disabled{% endif %}>{% for c in clients %}<option value="{{ c.id }}" {% if selected_client_id==c.id or (case and case.client_id==c.id) %}selected{% endif %}>{{ c.client_name }}{% if c.client_code %} ({{ c.client_code }}){% endif %}</option>{% endfor %}</select>{% if mode=='edit' %}<input type="hidden" name="client_id" value="{{ case.client_id }}">{% endif %}</label>
      <label class="space-y-1"><span class="text-sm font-medium">Engagement Link</span><select name="engagement_id" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm"><option value="">Not linked</option>{% for e in engagements %}<option value="{{ e.id }}" {% if case and case.engagement_id==e.id %}selected{% endif %}>{{ e.financial_year }} - {{ e.catalogue.service_name if e.catalogue else e.service_catalogue_id }}</option>{% endfor %}</select></label>
      <label class="space-y-1"><span class="text-sm font-medium">Priority</span><select name="priority" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">{% for p in case_priorities %}<option value="{{ p }}" {% if case and case.priority==p %}selected{% endif %}>{{ p|title }}</option>{% endfor %}</select></label>
    </div>
    <div class="grid gap-4 md:grid-cols-3">
      <label class="space-y-1"><span class="text-sm font-medium">Department</span><select name="department" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">{% for d in departments %}<option value="{{ d }}" {% if case and case.department==d %}selected{% endif %}>{{ d }}</option>{% endfor %}</select></label>
      <label class="space-y-1"><span class="text-sm font-medium">Case Type</span><select name="case_type" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">{% for t in case_types %}<option value="{{ t }}" {% if case and case.case_type==t %}selected{% endif %}>{{ t }}</option>{% endfor %}</select></label>
      <label class="space-y-1"><span class="text-sm font-medium">Status</span><select name="status" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">{% for s in case_statuses %}<option value="{{ s }}" {% if case and case.status==s %}selected{% endif %}>{{ s|replace('_',' ')|title }}</option>{% endfor %}</select></label>
    </div>
    <label class="block space-y-1"><span class="text-sm font-medium">Title</span><input name="title" value="{{ case.title if case else '' }}" required class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm"></label>
    <div class="grid gap-4 md:grid-cols-4">
      <label class="space-y-1"><span class="text-sm font-medium">Reference No</span><input name="reference_no" value="{{ case.reference_no if case else '' }}" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm"></label>
      <label class="space-y-1"><span class="text-sm font-medium">DIN / Ack No</span><input name="din_ack_no" value="{{ case.din_ack_no if case else '' }}" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm"></label>
      <label class="space-y-1"><span class="text-sm font-medium">Notice Date</span><input type="date" name="notice_date" value="{{ case.notice_date if case and case.notice_date else '' }}" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm"></label>
      <label class="space-y-1"><span class="text-sm font-medium">Due Date</span><input type="date" name="due_date" value="{{ case.due_date if case and case.due_date else '' }}" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm"></label>
    </div>
    <div class="grid gap-4 md:grid-cols-3">
      <label class="space-y-1"><span class="text-sm font-medium">Financial Year</span><input name="financial_year" value="{{ case.financial_year if case else active_financial_year or '' }}" placeholder="2025-26" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm"></label>
      <label class="space-y-1"><span class="text-sm font-medium">Assessment Year</span><input name="assessment_year" value="{{ case.assessment_year if case else active_assessment_year or '' }}" placeholder="2026-27" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm"></label>
      <label class="space-y-1"><span class="text-sm font-medium">Period</span><input name="period_label" value="{{ case.period_label if case else '' }}" placeholder="Apr 2025 / Q1 / FY" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm"></label>
    </div>
    <div class="grid gap-4 md:grid-cols-3">
      {% for field,label in [('assigned_partner_user_id','Partner'),('assigned_manager_user_id','Manager'),('assigned_staff_user_id','Staff')] %}
      <label class="space-y-1"><span class="text-sm font-medium">{{ label }}</span><select name="{{ field }}" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm"><option value="">Not assigned</option>{% for u in assignable_users %}<option value="{{ u.id }}" {% if case and case[field]==u.id %}selected{% endif %}>{{ u.full_name or u.login_id or u.email }}</option>{% endfor %}</select></label>
      {% endfor %}
    </div>
    <label class="block space-y-1"><span class="text-sm font-medium">Issue Summary</span><textarea name="issue_summary" rows="4" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">{{ case.issue_summary if case else '' }}</textarea></label>
    <label class="block space-y-1"><span class="text-sm font-medium">Remarks</span><textarea name="remarks" rows="3" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm">{{ case.remarks if case else '' }}</textarea></label>
    <div class="flex justify-end gap-3"><a href="/notice-cases" class="rounded-lg border border-slate-300 px-4 py-2 text-sm">Cancel</a><button class="rounded-lg bg-brand-600 px-4 py-2 text-sm font-semibold text-white">Save</button></div>
  </form>
</div>
{% endblock %}
