@extends('layouts.app')

@section('title', 'メール送信テンプレート')

@section('content')
<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <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 active">メール送信テンプレート</li>
                </ol>
            </div>
        </div>
    </div>
</div>

<section class="content">
    <div class="container-fluid">
        @if(Session::has('success'))
            <div class="alert alert-success alert-dismissible" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                {{ Session::get('success') }}
            </div>
            @elseif(Session::has('error'))
            <div class="alert alert-danger alert-dismissible">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                <h4><i class="icon fa fa-ban"></i>入力内容に不備があります:</h4>
                {!! Session::get('error') !!}
            </div>
            @endif

            @if ($errors->any())
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <h4>入力内容に不備があります:</h4>
                <ul class="mb-0">
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
                <button type="button" class="close" data-dismiss="alert" aria-label="閉じる">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            @endif

            <!-- ▼ 絞り込みフィルター -->
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">絞り込みフィルター</h3>
                    </div>

                    <div class="card-body">
                        <form action="{{ route('mail_templates') }}" method="POST" id="list-form" class="form-horizontal">
                            @csrf
                            <input type="hidden" name="sort" value="{{ $sort ?? '' }}">
                            <input type="hidden" name="sort_type" value="{{ $sort_type ?? '' }}">

                            <table class="table table-borderless mb-0" style="width: 100%;">
                                <tbody>
                                    {{-- 行1 --}}
                                    <tr>
                                        <th style="width: 18%; text-align: left;">メールテンプレートID</th>
                                        <td style="width: 32%;">
                                            <input type="text" name="mail_template_id"
                                                value="{{ old('mail_template_id', $mail_template_id ?? '') }}"
                                                class="form-control input-sm" placeholder="123456">
                                        </td>

                                        <th style="width: 18%; text-align: left;">使用プログラムID</th>
                                        <td style="width: 32%;">
                                            <input type="text" name="pg_id"
                                                value="{{ old('pg_id', $pg_id ?? '') }}"
                                                class="form-control input-sm" placeholder="123456">
                                        </td>
                                    </tr>

                                    {{-- 行2 --}}
                                    <tr>
                                        <th style="text-align: left;">エリアマネージャー同報</th>
                                        <td>
                                            <select name="mgr_cc_flag" class="form-control input-sm">
                                                <option value="">全て</option>
                                                <option value="1" {{ old('mgr_cc_flag', $mgr_cc_flag ?? '') == '1' ? 'selected' : '' }}>同報する</option>
                                                <option value="0" {{ old('mgr_cc_flag', $mgr_cc_flag ?? '') == '0' ? 'selected' : '' }}>同報しない</option>
                                            </select>
                                        </td>

                                        <th style="text-align: left;">使用フラグ</th>
                                        <td>
                                            <select name="use_flag" class="form-control input-sm">
                                                <option value="">全て</option>
                                                <option value="1" {{ old('use_flag', $use_flag ?? '') == '1' ? 'selected' : '' }}>使用する</option>
                                                <option value="0" {{ old('use_flag', $use_flag ?? '') == '0' ? 'selected' : '' }}>使用しない</option>
                                            </select>
                                        </td>
                                    </tr>

                                    {{-- 行3 --}}
                                    <tr>
                                        <th style="text-align: left;">件名</th>
                                        <td colspan="3">
                                            <input type="text" name="subject"
                                                value="{{ old('subject', $subject ?? '') }}"
                                                class="form-control input-sm" placeholder="キーワード...">
                                        </td>
                                    </tr>
                                </tbody>
                            </table>

                            {{-- ボタン --}}
                            <div class="form-group col-12 text-left mt-3">
                                <button type="submit" name="action" value="filter" class="btn btn-default mr10">絞り込み</button>
                                <button type="submit" name="action" value="reset" class="btn btn-default">解除</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <!-- ▲ 絞り込みフィルター -->


    <!-- ▼ ツールバー -->
    <div class="container-fluid mb20">
        <button type="button" class="btn btn-sm btn-default mr10" onclick="location.href='{{ route('mail_templates_add') }}'">新規</button>
        <button type="button" class="btn btn-sm btn-default mr10" id="delete">削除</button>        
        <div class="d-flex justify-content-end">
            {{ $templates->appends(request()->all())->links('pagination') }}
        </div>
    </div>
    <!-- ▲ ツールバー -->

    <!-- ▼ テーブル -->
    <div class="col-lg-12 mb20">
        <div class="table-responsive">
            <form action="{{ route('mail_templates_delete') }}" method="POST" id="form_delete">
                @csrf
                <table class="table table-bordered dataTable text-nowrap">
                    <thead class="thead-light">
                        <tr>
                            {{-- チェック + 編集ボタン --}}
                            <th style="width:140px;" class="text-left">
                                <input type="checkbox" onclick="$('input[name*=\'pk\']').prop('checked', this.checked);">
                            </th>
                            {{-- ソート --}}
                            <th class="sorting {{ ($sort=='mail_template_id') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="mail_template_id"><span>メールテンプレートID</span></th>
                            <th class="sorting {{ ($sort=='pg_id') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="pg_id"><span>使用プログラムID</span></th>
                            <th><span>内部ID</span></th>
                            <th><span>エリアマネージャー同報</span></th>
                            <th><span>BCCアドレス</span></th>
                            <th class="sorting {{ ($sort=='use_flag') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="use_flag"><span>使用フラグ</span></th>
                            <th><span>備考</span></th>
                            <th class="sorting {{ ($sort=='subject') ? ($sort_type=='asc'?'sorting_asc':'sorting_desc') : '' }}" sort="subject"><span>件名</span></th>
                            <th><span>本文</span></th>
                        </tr>
                    </thead>

                    <tbody class="bg-white">
                        @foreach($templates as $item)
                            <tr>
                                <td class="align-middle" style="background-color:#faebd7;">
                                    <div class="d-flex align-items-center">
                                        <input type="checkbox" class="m-0 checkbox" name="pk[]" value="{{ $item->mail_template_id }}">
                                        <a href="{{ route('mail_templates_edit', ['id' => $item->mail_template_id]) }}" class="btn btn-sm btn-default ml-2">編集</a>
                                    </div>
                                </td>
                                <td class="sm-item text-left align-middle">{{ $item->mail_template_id }}</td>
                                <td class="sm-item text-left align-middle">{{ $item->pg_id }}</td>
                                <td class="sm-item text-left align-middle">{{ $item->internal_id }}</td>
                                <td class="sm-item text-left align-middle">{{ $item->mgr_cc_flag ? '同報する' : '同報しない' }}</td>
                                <td class="sm-item text-left align-middle">{{ $item->bcc_adrs }}</td>
                                <td class="sm-item text-left align-middle">{{ $item->use_flag ? '使用する' : '使用しない' }}</td>
                                <td class="sm-item text-left align-middle">{{ $item->memo }}</td>
                                <td class="sm-item text-left align-middle">{{ $item->subject }}</td>
                                <td class="sm-item text-left align-middle">{{ Str::limit($item->text, 30) }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </form>
        </div>
    </div>
    <!-- ▲ テーブル -->
</section>

<form action="{{ route('mail_templates_export') }}" method="GET" id="form_export"></form>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            document.querySelectorAll('th.sorting').forEach(function (th) {
                th.addEventListener('click', function () {
                    const sort = this.getAttribute('sort');
                    const url = new URL(window.location.href);
                    const currentSort = url.searchParams.get('sort');
                    const currentType = url.searchParams.get('sort_type') || 'asc';

                    const newType = (currentSort === sort && currentType === 'asc') ? 'desc' : 'asc';
                    url.searchParams.set('sort', sort);
                    url.searchParams.set('sort_type', newType);

                    window.location.href = url.toString();
                });
            });
        });
    </script>

@endsection
