@extends('layouts.app')
@section('content')
<main>
    <section id="" class="container mt20 mb20">
        <div class="row">
            <div class="col-12 col-lg-6 mb20">
                <div class="card border-success">
                    <div class="card-header border-success">
                        <h5 class="card-title text-success">定期契約情報</h5>
                    </div>
                    <div class="card-body">
                        <div class="slider_2-1">
                            @forelse($contracts as $contract)
                            @php
                            $now = \Carbon\Carbon::now();
                            $update_flag = $contract->contract_renewal;
                            $start_dd = $contract->update_grace_period_start_date;
                            $start_hm = $contract->update_grace_period_start_time;
                            $end_dd = $contract->update_grace_period_end_date;
                            $end_hm = $contract->update_grace_period_end_time;
                            $contract_end_dt = $contract->contract_periode ? \Carbon\Carbon::parse($contract->contract_periode) : null;
                            $periode_month = $contract_end_dt ? $contract_end_dt->month : null;
                            $periode_year = $contract_end_dt ? $contract_end_dt->year : null;
                            $bg = 'alert-warning';
                            $btn_text = '更新する';
                            $btn_active = true;

                            // 契約終了月より前は「ご契約中」
                            if ($now->lt($contract_end_dt)) {
                            $bg = 'bg-white';
                            $btn_text = 'ご契約中';
                            $btn_active = false;
                            } else {
                            // 契約終了月より後は猶予期間判定
                            if (is_numeric($start_dd) && is_numeric($end_dd)) {
                            // 開始日
                            $start_date = $contract_end_dt->format('Y-m-') . str_pad($start_dd, 2, '0', STR_PAD_LEFT);
                            $start_time = ($start_hm && preg_match('/^\d{2}:\d{2}$/', $start_hm)) ? $start_hm : '00:00';
                            $start_dt = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $start_date . ' ' . $start_time . ':00');
                            // 終了日
                            if ($start_dd < $end_dd) {
                                $end_date=$contract_end_dt->format('Y-m-') . str_pad($end_dd, 2, '0', STR_PAD_LEFT);
                                $end_time = ($end_hm && preg_match('/^\d{2}:\d{2}$/', $end_hm)) ? $end_hm : '23:59';
                                $end_dt = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $end_date . ' ' . $end_time . ':00');
                                } else {
                                $next_month_dt = $contract_end_dt->copy()->addMonth();
                                $end_date = $next_month_dt->format('Y-m-') . str_pad($end_dd, 2, '0', STR_PAD_LEFT);
                                $end_time = ($end_hm && preg_match('/^\d{2}:\d{2}$/', $end_hm)) ? $end_hm : '23:59';
                                $end_dt = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $end_date . ' ' . $end_time . ':00');
                                }
                                } else {
                                $start_dt = null;
                                $end_dt = null;
                                }
                                // 猶予期間判定
                                if ($update_flag === 0) {
                                $bg = 'bg-white';
                                $btn_text = '手続き中';
                                $btn_active = false;
                                } elseif ($update_flag === 1) {
                                $bg = 'bg-white';
                                $btn_text = '更新済';
                                $btn_active = false;
                                } elseif ($start_dt && $end_dt && $now->between($start_dt, $end_dt)) {
                                // 猶予期間内
                                if ($contract_end_dt && $now->gt($contract_end_dt)) {
                                $bg = 'alert-danger';
                                $btn_text = '更新する';
                                $btn_active = true;
                                } else {
                                $bg = 'alert-warning';
                                $btn_text = '更新する';
                                $btn_active = true;
                                }
                                } else {
                                $bg = 'bg-white';
                                $btn_text = 'ご契約中';
                                $btn_active = false;
                                }
                                }
                                // 契約終了月の場合（既存ロジック）
                                if (is_numeric($start_dd) && is_numeric($end_dd)) {
                                // 開始日
                                $start_date = $contract_end_dt->format('Y-m-') . str_pad($start_dd, 2, '0', STR_PAD_LEFT);
                                $start_time = ($start_hm && preg_match('/^\d{2}:\d{2}$/', $start_hm)) ? $start_hm : '00:00';
                                $start_dt = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $start_date . ' ' . $start_time . ':00');
                                // 終了日
                                if ($start_dd < $end_dd) {
                                    $end_date=$contract_end_dt->format('Y-m-') . str_pad($end_dd, 2, '0', STR_PAD_LEFT);
                                    $end_time = ($end_hm && preg_match('/^\d{2}:\d{2}$/', $end_hm)) ? $end_hm : '23:59';
                                    $end_dt = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $end_date . ' ' . $end_time . ':00');
                                    } else {
                                    $next_month_dt = $contract_end_dt->copy()->addMonth();
                                    $end_date = $next_month_dt->format('Y-m-') . str_pad($end_dd, 2, '0', STR_PAD_LEFT);
                                    $end_time = ($end_hm && preg_match('/^\d{2}:\d{2}$/', $end_hm)) ? $end_hm : '23:59';
                                    $end_dt = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $end_date . ' ' . $end_time . ':00');
                                    }
                                    } else {
                                    $start_dt = null;
                                    $end_dt = null;
                                    }
                                    // 以降は既存のボタン・背景色判定ロジック
                                    if ($update_flag===0) {
                                    $bg='bg-white';
                                    $btn_text='手続き中';
                                    $btn_active=false;
                                    }
                                    elseif ($update_flag===1) {
                                    $bg='bg-white';
                                    $btn_text='更新済';
                                    $btn_active=false;
                                    }
                                    elseif (!is_null($end_dt) && $end_dt->gt($start_dt)) {
                                    if ($start_dt && $now->lt($start_dt)) {
                                    $bg = 'bg-white';
                                    $btn_text = 'ご契約中';
                                    $btn_active = false;
                                    } else {
                                    // 契約終了日を過ぎていて、更新可能期間内は赤背景
                                    if ($contract_end_dt && $now->gt($contract_end_dt) && $start_dt && $end_dt && $now->between($start_dt, $end_dt)) {
                                    $bg = 'alert-danger';
                                    $btn_text = '更新する';
                                    $btn_active = true;
                                    } else {
                                    $bg = 'alert-warning';
                                    $btn_text = '更新する';
                                    $btn_active = true;
                                    }
                                    }
                                    }
                                    elseif ($start_dt && $start_dt->gt($end_dt)) {
                                    if ($now->lt($start_dt)) {
                                    $bg = 'bg-white';
                                    $btn_text = 'ご契約中';
                                    $btn_active = false;
                                    } elseif ($now->gte($start_dt) && $now->lte($contract_end_dt->copy()->endOfMonth())) {
                                    $bg = 'alert-warning';
                                    $btn_text = '更新する';
                                    $btn_active = true;
                                    } else {
                                    $bg = 'alert-danger';
                                    $btn_text = '更新する';
                                    $btn_active = true;
                                    }
                                    }
                                    @endphp
                                    <div>
                                        <div class="card {{ $bg }}">
                                            <h6 class="mt10 ml10 font-weight-bold">{{ $contract->park_name }}</h6>
                                            <table class="table table-sm">
                                                <tr>
                                                    <th>{{ $contract->psection_subject ?? '' }}</th>
                                                    <td>{{ $contract->usertype_subject1 ?? '' }}</td>
                                                </tr>
                                                <tr>
                                                    <th>{{ $contract->ptype_subject ?? '' }}</th>
                                                    <td>{{ $contract->pplace_no ?? '' }}</td>
                                                </tr>
                                                <tr>
                                                    <th>定期契約ID</th>
                                                    <td>{{ $contract->contract_id }}</td>
                                                </tr>
                                                <tr>
                                                    <th>期間</th>
                                                    <td>{{ \Carbon\Carbon::parse($contract->contract_periods)->format('Y-m-d') }}から</td>
                                                </tr>
                                                <tr>
                                                    <th class="text-center" colspan="2"><span class="h2">{{ $contract->enable_months }}</span>ヶ月</th>
                                                </tr>
                                                <tr>
                                                    <td class="text-center" colspan="2">
                                                        @if($btn_active)
                                                        <a href="{{ url('regular_contract/update/' . $contract->contract_id) }}"
                                                            class="btn {{ $bg == 'alert-warning' ? 'btn-warning' : ($bg == 'alert-danger' ? 'btn-danger' : 'btn-outline-secondary disabled') }} badge-pill">
                                                            {{ $btn_text }}
                                                        </a>
                                                        @else
                                                        <button class="btn btn-outline-secondary badge-pill disabled">{{ $btn_text }}</button>
                                                        @endif
                                                    </td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>
                                    @empty
                                    <p class="text-center">定期契約情報はありません<br>
                                        <a href="{{ url('regular_contract/create') }}" class="btn btn-block btn-lg btn-success">新規定期契約</a>
                                    </p>
                                    @endforelse
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-12 col-lg-6 mb20">
                <div class="card border-success mb20">
                    <div class="card-header border-success text-success">
                        <h5 class="card-title">シール発行</h5>
                    </div>
                    <div class="container slider_1-1">
                        @forelse($seals as $seal)
                        <article class="row">
                            <figure class="col-12 col-md-4 float-right mt50">
                                <div class="ml30 mt10 text-danger text-center">
                                    @if(!empty($seal->contract_qr_id))
                                    {!! QrCode::size(120)->generate($seal->contract_qr_id) !!}
                                    @else
                                    <div class="text-danger">QRコード<br>未発行</div>
                                    @endif
                                </div>
                            </figure>
                            <div class="col-12 col-md-8">
                                <h6 class="mt30 ml20 font-weight-bold"></h6>
                                <table class="table table-sm ml20">
                                    <tr>
                                        <th style="padding-right:8px;padding-left:8px;">{{ $seal->psection_subject ?? '' }}</th>
                                        <td style="padding-left:8px;">{{ $seal->usertype_subject1 ?? '' }}</td>
                                    </tr>
                                    <tr>
                                        <th style="padding-right:8px;padding-left:8px;">{{ $seal->ptype_subject ?? '' }}</th>
                                        <td style="padding-left:8px;">{{ $seal->pplace_no ?? '' }}</td>
                                    </tr>
                                    <tr>
                                        <th style="padding-right:8px;padding-left:8px;">定期契約ID</th>
                                        <td style="padding-left:8px;">{{ $seal->contract_id }}</td>
                                    </tr>
                                    <tr>
                                        <th class="text-center" colspan="2">
                                            <span class="h2">{{ $seal->enable_months }}</span>ヶ月
                                        </th>
                                    </tr>
                                </table>
                            </div>
                        </article>
                        @empty
                        <div class="text-center p-4">シール発行対象の契約はありません。</div>
                        @endforelse
                    </div>
                </div>
                <div id="my-information" class="card border-success">
                    <div class="card-header border-success text-success">
                        <h5 class="card-title">{{ $user_name }}さんへのxお知らせ
                            <a href="{{ route('user_information.index', ['management_code' => request()->route('management_code')]) }}" class="badge badge-secondary badge-pill float-right">お知らせ一覧を見る</a>
                        </h5>
                    </div>
                    <ul class="info-slider_1-1">
                        @if($information)
                        <li>
                            <span class="small" style="margin-right: 1em;">{{ $information->entry_date }}</span>
                            {{ str($information->user_information_history)->before("\n") }}
                        </li>
                        @else
                        <li class="text-center">お知らせはありません。</li>
                        @endif
                    </ul>
                </div>
            </div>
        </div>
    </section>
</main>
@endsection