@extends('layouts.app')
@section('title', $lead ? 'Edit Lead' : 'Add Lead')
@section('page-title', $lead ? 'Edit Lead' : 'Add New Lead')

@section('content')
@include('partials.employee-achievement', ['employees' => $employees])
<div class="page-hd">
  <div><h2>{{ $lead ? 'Edit Lead' : 'Add New Lead' }}</h2><p>Required fields marked *</p></div>
  <div class="page-hd-right"><a href="{{ route('leads.index') }}" class="btn btn-out btn-sm">Cancel</a></div>
</div>

<div class="form-card">
  <h3>Lead Information</h3>
  <form method="POST" action="{{ $lead ? route('leads.update',$lead) : route('leads.store') }}">
    @csrf
    @if($lead) @method('PUT') @endif

    <div class="form-row fr2">
      <div class="field">
        <label>Full Name <span class="req">*</span></label>
        <input type="text" name="name" value="{{ old('name', $lead->name ?? '') }}" placeholder="Rajesh Kumar" required>
        @error('name')<div style="color:#dc2626;font-size:11px;margin-top:4px">{{ $message }}</div>@enderror
      </div>
      <div class="field">
        <label>Company</label>
        <input type="text" name="company" value="{{ old('company', $lead->company ?? '') }}" placeholder="TechWave Solutions">
      </div>
    </div>

    <div class="form-row fr2">
      <div class="field">
        <label>Phone / WhatsApp <span class="req">*</span></label>
        <input type="text" name="phone" value="{{ old('phone', $lead->phone ?? '') }}" placeholder="+91 98765 43210" required>
        @error('phone')<div style="color:#dc2626;font-size:11px;margin-top:4px">{{ $message }}</div>@enderror
      </div>
      <div class="field">
        <label>Email</label>
        <input type="email" name="email" value="{{ old('email', $lead->email ?? '') }}" placeholder="rajesh@techwave.in">
      </div>
    </div>

    <div class="form-row fr3">
      <div class="field">
        <label>Source <span class="req">*</span></label>
        <select name="source" required>
          <option value="">Select</option>
          @foreach($sources as $s)
          <option value="{{ $s }}" {{ old('source', $lead->source ?? '')===$s?'selected':'' }}>{{ $s }}</option>
          @endforeach
        </select>
        @error('source')<div style="color:#dc2626;font-size:11px;margin-top:4px">{{ $message }}</div>@enderror
      </div>
      <div class="field">
        <label>Assign To <span class="req">*</span></label>
        <select name="assigned_to" required>
          <option value="">Select Employee</option>
          @foreach($employees as $e)
          <option value="{{ $e->id }}" {{ old('assigned_to', $lead->assigned_to ?? '')==$e->id?'selected':'' }}>{{ $e->name }}</option>
          @endforeach
        </select>
        @error('assigned_to')<div style="color:#dc2626;font-size:11px;margin-top:4px">{{ $message }}</div>@enderror
      </div>
      <div class="field">
        <label>Priority</label>
        <select name="priority">
          @foreach(['High','Medium','Low'] as $p)
          <option value="{{ $p }}" {{ old('priority', $lead->priority ?? 'Medium')===$p?'selected':'' }}>{{ $p }}</option>
          @endforeach
        </select>
      </div>
    </div>

    <div class="form-row fr3">
      <div class="field">
        <label>Service</label>
        <select name="service">
          <option value="">Select</option>
          @foreach($services as $s)
          <option value="{{ $s }}" {{ old('service', $lead->service ?? '')===$s?'selected':'' }}>{{ $s }}</option>
          @endforeach
        </select>
      </div>
      <div class="field">
        <label>Budget (Rs)</label>
        <input type="text" name="budget" value="{{ old('budget', $lead->budget ?? '') }}" placeholder="1,25,000">
      </div>
      <div class="field">
        <label>Stage</label>
        <select name="stage">
          @foreach($stages as $s)
          <option value="{{ $s }}" {{ old('stage', $lead->stage ?? 'New')===$s?'selected':'' }}>{{ $s }}</option>
          @endforeach
        </select>
      </div>
    </div>

    <div class="form-row fr1">
      <div class="field">
        <label>Note / Requirement {{ !$lead ? '<span class="req">*</span>' : '' }}</label>
        <textarea name="note" placeholder="What the customer needs, how they enquired, key details...">{{ old('note') }}</textarea>
        @error('note')<div style="color:#dc2626;font-size:11px;margin-top:4px">{{ $message }}</div>@enderror
      </div>
    </div>

    <div style="display:flex;gap:8px;margin-top:6px">
      <button type="submit" class="btn btn-pri">
        <svg viewBox="0 0 24 24"><polyline points="20 6 9 17 4 12"/></svg>
        {{ $lead ? 'Update Lead' : 'Save Lead' }}
      </button>
      <a href="{{ route('leads.index') }}" class="btn btn-out">Cancel</a>
    </div>

    @if($errors->any())
    <div class="fmsg fmsg-err" style="margin-top:12px">Please fix the errors above.</div>
    @endif
  </form>
</div>
@endsection
