@if(Session::has('success'))
  <div class="alert alert-success alert-dismissible">{{ Session::get('success') }}</div>
@elseif(Session::has('error'))
  <div class="alert alert-danger alert-dismissible">{{ Session::get('error') }}</div>
@endif

<section class="content">
  <div class="container-fluid">
    <div class="card">
      <div class="card-body">
        {{-- 利用契約ID --}}
        @if($isEdit || $isInfo)
          <div class="row mb-3">
            <label class="col-md-2 col-form-label">利用契約ID</label>
            <div class="col-md-10">
              <input type="text" class="form-control" value="{{ $term->terms_id ?? '' }}" readonly>
            </div>
          </div>
        @endif

        {{-- 市区ID --}}
        <div class="row mb-3">
          <label class="col-md-2 col-form-label">市区ID<span class="text-danger">*</span></label>
          <div class="col-md-10">
            <select name="city_id" class="form-control" @if($isInfo) disabled @endif>
              <option value="">都市を選択</option>
              @foreach($cities as $id => $city_name)
                <option value="{{ $id }}" @if(old('city_id', $term->city_id ?? '') == $id) selected @endif>{{ $city_name }}</option>
              @endforeach
            </select>
          </div>
        </div>

        {{-- 使用中 --}}
        <div class="row mb-3">
          <label class="col-md-2 col-form-label">使用中</label>
          <div class="col-md-10 pt-2">
            <div class="form-check form-check-inline">
              <input class="form-check-input" type="radio" name="use_flag" value="1" @if(($term->use_flag ?? '') == 1) checked @endif @if($isInfo) disabled @endif>
              <label class="form-check-label">使用中</label>
            </div>
            <div class="form-check form-check-inline">
              <input class="form-check-input" type="radio" name="use_flag" value="0" @if(($term->use_flag ?? '') == 0) checked @endif @if($isInfo) disabled @endif>
              <label class="form-check-label">過去のバージョン</label>
            </div>
          </div>
        </div>

        {{-- リビジョン --}}
        <div class="row mb-3">
          <label class="col-md-2 col-form-label">リビジョン<span class="text-danger">*</span></label>
          <div class="col-md-10">
            <input type="text" name="terms_revision" class="form-control"
                  value="{{ old('terms_revision', $term->terms_revision ?? '') }}"
                  @if($isInfo) readonly @endif>
          </div>
        </div>

        {{-- 契約内容 --}}
        <div class="row mb-3">
          <label class="col-md-2 col-form-label">契約内容<span class="text-danger">*</span></label>
          <div class="col-md-10">
            <textarea name="terms_text" rows="5" class="form-control" @if($isInfo) readonly @endif>{{ old('terms_text', $term->terms_text ?? '') }}</textarea>
          </div>
        </div>

        {{-- 備考 --}}
        <div class="row mb-3">
          <label class="col-md-2 col-form-label">備考</label>
          <div class="col-md-10">
            <input type="text" name="memo" class="form-control"
                  value="{{ old('memo', $term->memo ?? '') }}" @if($isInfo) readonly @endif>
          </div>
        </div>

        {{-- 使用開始日 --}}
        <div class="row mb-3">
          <label class="col-md-2 col-form-label">使用開始日</label>
          <div class="col-md-10">
            <input type="date" name="start_date" class="form-control"
                  value="{{ old('start_date', $term->start_date ?? '') }}" @if($isInfo) readonly @endif>
          </div>
        </div>

        {{-- 登録・削除 ボタン --}}
        <div class="text-left mt-4">
            @if($isInfo)
                {{-- 詳細画面：編集に遷移 --}}
                <a href="{{ route('terms_edit', ['id' => $term->terms_id]) }}" class="btn btn-lg btn-success">
                    編集
                </a>
            @else
                {{-- 新規／編集 共通フォーム --}}
                <button type="submit" class="btn btn-lg btn-success">
                    {{ $isEdit ? '更新' : '登録' }}
                </button>

                {{-- 編集時のみ削除可能 --}}
                @if($isEdit && isset($term->terms_id))
                    <form id="delete-form" method="POST" action="{{ route('terms_delete', ['id' => $term->terms_id]) }}" style="display:inline;">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn btn-lg btn-danger ml-2"
                                onclick="return confirm('削除してよろしいですか？')">削除</button>
                    </form>
                @endif
            @endif
        </div>
      </div>
    </div>
  </div>
</section>
