@extends('layouts.admin')

@section('content')
  @php
    $statusLabel = (int) ($userdata->status ?? 0) === 1 ? 'Active' : 'Blocked';
    $locationText = collect([$cityName, $stateName, $postalCode])->filter()->implode(', ');
    $summaryCards = [
        'Institute ID' => '#'.$userdata->id,
        'Login Email' => $userdata->email ?: '-',
        'Current Status' => $statusLabel,
    ];
  @endphp

  <div class="card-header">
    <h4>Edit Institute</h4>
  </div>
  <div class="card-body">
    @include('admin.partials.flash-messages')

    <div class="portal-page-shell">
      <div class="portal-hero-card">
        <div class="portal-hero-main">
          <div class="portal-hero-copy">
            <h5>{{ $userdata->name }}</h5>
            <p>Update institute identity, address, affiliation and branding while keeping the existing account flow unchanged.</p>
            <div class="portal-chip-list">
              <span class="portal-chip">{{ $userdata->mobileno ?: 'Mobile not available' }}</span>
              <span class="portal-chip">{{ $locationText ?: 'Location not available' }}</span>
              <span class="portal-chip">{{ $statusLabel }}</span>
            </div>
            <div class="portal-hero-actions">
              <a href="{{ route('admin.changepassword', ['userid' => $userdata->id]) }}" class="btn btn-dark btn-sm">Change Password</a>
              <a href="{{ route('admin.institutedetails') }}" class="btn btn-outline-primary btn-sm">Back to Institutes</a>
            </div>
          </div>
          <img src="{{ $profileImageUrl }}" alt="{{ $userdata->name }}" class="portal-hero-image">
        </div>
      </div>

      <div class="portal-summary-grid">
        @foreach($summaryCards as $label => $value)
          <div class="portal-summary-card">
            <span>{{ $label }}</span>
            <strong>{{ $value }}</strong>
          </div>
        @endforeach
      </div>

      <form action="{{ route('admin.updateinstitute') }}" method="post" class="portal-form-shell">
        @csrf
        <input type="hidden" name="editid" id="editid" value="{{ $userdata->id }}">

        <div class="portal-form-section">
          <h6>Institute Identity</h6>
          <p>Core account details are shown here. Email and login mobile stay locked for stability.</p>
          <div class="portal-form-grid">
            <div class="form-group portal-form-group">
              <label for="institutename">Institute Name</label>
              <input type="text" class="form-control" id="institutename" name="institutename" value="{{ old('institutename', $userdata->name) }}" maxlength="200" placeholder="Institute Name" required>
            </div>
            <div class="form-group portal-form-group">
              <label for="emailid">Email Address</label>
              <input type="email" class="form-control" id="emailid" value="{{ $userdata->email }}" disabled>
            </div>
            <div class="form-group portal-form-group">
              <label for="mobilenumber">Mobile Number</label>
              <input type="text" class="form-control" id="mobilenumber" value="{{ $userdata->mobileno }}" disabled>
            </div>
          </div>
        </div>

        <div class="portal-form-section">
          <h6>Address and Affiliation</h6>
          <p>These details help identify the institute profile in admin reports and public sections.</p>
          <div class="portal-form-grid">
            <div class="form-group portal-form-group portal-form-group-full">
              <label for="addressname">Address</label>
              <input type="text" class="form-control" id="addressname" name="addressname" value="{{ old('addressname', $userdata->address) }}" maxlength="500" placeholder="Institute address" required>
            </div>
            <div class="form-group portal-form-group portal-form-group-full">
              <label for="affiliatedetail">Affiliated Details</label>
              <input type="text" class="form-control" id="affiliatedetail" name="affiliatedetail" value="{{ old('affiliatedetail', $affiliateDetail) }}" maxlength="500" placeholder="Board or affiliation details">
            </div>
            <div class="form-group portal-form-group">
              <label for="statename">State</label>
              <input type="text" class="form-control" id="statename" name="statename" value="{{ old('statename', $stateName) }}" maxlength="200" placeholder="State Name" required>
            </div>
            <div class="form-group portal-form-group">
              <label for="cityname">City</label>
              <input type="text" class="form-control" id="cityname" name="cityname" value="{{ old('cityname', $cityName) }}" maxlength="200" placeholder="City Name" required>
            </div>
            <div class="form-group portal-form-group">
              <label for="postalcode">Postal Code</label>
              <input type="text" class="form-control" id="postalcode" name="postalcode" value="{{ old('postalcode', $postalCode) }}" maxlength="10" placeholder="Postal Code" required oninput="this.value = this.value.replace(/\D/g, '').slice(0, 10);">
            </div>
          </div>
        </div>

        <div class="portal-form-section">
          <h6>Branding</h6>
          <p>Refresh logo or monogram without affecting institute access.</p>
          <div class="portal-upload-shell">
            <div id="targetLayer" class="portal-upload-preview">
              <img src="{{ $profileImageUrl }}" alt="{{ $userdata->name }}">
              <input type="hidden" name="photoid" id="photoid" value="{{ $userdata->profileimh }}">
            </div>
            <div class="loading" style="display:none; text-align:center;">
              <img src="{{ asset('public/admin/loadspinner.gif') }}" style="width:80px;" alt="Loading">
            </div>
            <div class="portal-form-actions">
              <p class="portal-inline-note">Existing image remains safe until you replace it.</p>
              <div>
                <a id="newuploadphoto" href="#" class="btn btn-outline-primary">Upload New Logo</a>
                <button type="submit" class="btn btn-primary">Update Institute</button>
              </div>
            </div>
          </div>
        </div>
      </form>
    </div>
  </div>

  @include('admin.partials.attachment-upload-form')
@endsection
