@extends('layouts.app')
@section('title', '定期契約マスタ - 編集')

@section('content')
<style>
  .rc-edit,
  .rc-edit .card,
  .rc-edit .form-control,
  .rc-edit .btn,
  .rc-edit .breadcrumb {
    font-size: 13px;
  }
</style>

<div class="rc-edit">
  <div class="content-header">
    <div class="container-fluid">
      <div class="row mb-1">
        <div class="col-lg-6"><h1 class="m-0 text-dark">編集</h1></div>
        <div class="col-lg-6">
          <ol class="breadcrumb float-sm-right text-sm">
            <li class="breadcrumb-item"><a href="{{ route('home') }}">ホーム</a></li>
            <li class="breadcrumb-item"><a href="{{ route('regularcontracts') }}">定期契約マスタ</a></li>
            <li class="breadcrumb-item active">編集</li>
          </ol>
        </div>
      </div>
    </div>
  </div>

  <section class="content">
    <div class="container-fluid">
      <div class="card">
        <div class="card-header d-flex align-items-center">
          <div class="d-flex align-items-center">
            <button form="edit-form" type="submit" class="btn btn-default btn-sm mr-2 js-confirm-submit">登録</button>
            <button type="button" class="btn btn-default btn-sm" id="btnReceipt">領収書発行</button>
          </div>
          <form id="del-form" method="post" action="{{ route('regularcontracts_delete') }}" class="m-0 ml-auto">
            @csrf
            <input type="hidden" name="ids[]" value="{{ $row->contract_id }}">
            <button type="submit" class="btn btn-default btn-sm">削除</button>
          </form>
        </div>
        {{-- 本体フォーム（登録はこの form を submit） --}}
        <form id="edit-form" method="post" action="{{ route('regularcontracts_edit', ['contract_id'=>$row->contract_id]) }}">
          @csrf
          @include('admin.regularcontracts._form', array_merge(
            ['isEdit'=>1,'isInfo'=>0],
            (array) $row
          ))
        </form>
      </div>
    </div>
  </section>
</div>

<script>
  // 領収書発行はとりあえずダミー動作
  document.getElementById('btnReceipt')?.addEventListener('click', function(){
    alert('領収書発行の処理をここに実装してください。');
  });

  document.addEventListener('DOMContentLoaded', function () {
    $('.js-confirm-submit').on('click', function (e) {
      e.preventDefault();
      const $button = $(this);
      $.confirm({
        title: '確認ダイアログ。',
        content: '登録してよろしいですか？はい/いいえ',
        buttons: {
          ok: {
            text: "はい",
            btnClass: 'btn-primary',
            keys: ['enter'],
            action: function () {
              const formId = $button.attr('form');
              if (formId) {
                $('#' + formId).trigger('submit');
              } else {
                $button.closest('form').trigger('submit');
              }
            }
          },
          いいえ: function () {}
        }
      });
    });
  });
</script>
@endsection
