@extends('layouts.admin_app')
@section('header')
@endsection
@section('content')
    <style>
        .table th {
            white-space: nowrap;
            /* 禁止文字換行 */
            text-align: center;
            /* 內容置中 */
            overflow: hidden;
            /* 隱藏超出內容 */
            text-overflow: ellipsis;
            /* 超出用省略號顯示 */
        }

        .table td {
            white-space: nowrap;

        }

        .table th {
            width: auto;
            /* 可根據內容自適應，但不要超出 */
        }

        .table {
            width: 100%;
            /* 撐滿父容器 */
        }

        table {
            position: static !important;
        }
    </style>
    <div class="row">
        <div class="col-md-12">
            <div class="card-body">
                <!-- Basic Breadcrumb -->
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item">
                            <a href="javascript:void(0);">首頁</a>
                        </li>
                        <li class="breadcrumb-item active">會員列表</li>
                    </ol>
                </nav>
            </div>
        </div>
        <!-- Content wrapper -->
        <div class="content-wrapper">
            <!-- Content  MemberList-->
            <div class="container-xxl flex-grow-1 container-p-y">
                <div class="row g-6 mb-6">
                    <div class="col-sm-6 col-xl-3">
                        <div class="card">
                            <div class="card-body">
                                <div class="d-flex justify-content-between">
                                    <div class="me-1">
                                        <p class="text-heading mb-1">會員共計</p>
                                        <div class="d-flex align-items-center">
                                            <h4 class="mb-1 me-2">21,459</h4>
                                            <p class="text-success mb-1">(+29%)</p>
                                        </div>
                                        <small class="mb-0">Total Users</small>
                                    </div>
                                    <div class="avatar">
                                        <div class="avatar-initial bg-label-primary rounded-3">
                                            <div class="ri-group-line ri-26px"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6 col-xl-3">
                        <div class="card">
                            <div class="card-body">
                                <div class="d-flex justify-content-between">
                                    <div class="me-1">
                                        <p class="text-heading mb-1">普通會員</p>
                                        <div class="d-flex align-items-center">
                                            <h4 class="mb-1 me-1">4,567</h4>
                                            <p class="text-success mb-1">(+18%)</p>
                                        </div>
                                        <small class="mb-0">Last week analytics</small>
                                    </div>
                                    <div class="avatar">
                                        <div class="avatar-initial bg-label-danger rounded-3">
                                            <div class="ri-user-add-line ri-26px"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6 col-xl-3">
                        <div class="card">
                            <div class="card-body">
                                <div class="d-flex justify-content-between">
                                    <div class="me-1">
                                        <p class="text-heading mb-1">白銀會員</p>
                                        <div class="d-flex align-items-center">
                                            <h4 class="mb-1 me-1">19,860</h4>
                                            <p class="text-danger mb-1">(-14%)</p>
                                        </div>
                                        <small class="mb-0">Last week analytics</small>
                                    </div>
                                    <div class="avatar">
                                        <div class="avatar-initial bg-label-success rounded-3">
                                            <div class="ri-user-follow-line ri-26px"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6 col-xl-3">
                        <div class="card">
                            <div class="card-body">
                                <div class="d-flex justify-content-between">
                                    <div class="me-1">
                                        <p class="text-heading mb-1">白金會員</p>
                                        <div class="d-flex align-items-center">
                                            <h4 class="mb-1 me-1">237</h4>
                                            <p class="text-success mb-1">(+42%)</p>
                                        </div>
                                        <small class="mb-0">Last week analytics</small>
                                    </div>
                                    <div class="avatar">
                                        <div class="avatar-initial bg-label-warning rounded-3">
                                            <div class="ri-user-search-line ri-26px"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                @php
                $pageTotal = 0;
                @endphp
                <!-- Users List Table -->
                <div class="card">
                    <div class="card-header border-bottom">
                        <h5 class="card-title mb-0">Filters</h5>
                        <div class="d-flex justify-content-between align-items-center row gx-5 pt-4 gap-5 gap-md-0">
                            <div class="col-md-4 user_role"></div>
                            <div class="col-md-4 user_plan"></div>
                            <div class="col-md-4 user_status"></div>
                        </div>
                    </div>
                    <div class="card-datatable table-responsive">
                        <table class="datatables-users table">
                            <thead>
                                <tr>
                                    <th>縮 排</th>
                                    <th>會員id</th>
                                    <th>會員名稱</th>
                                    <th>手機號碼</th>
                                    <th>會員等級</th>
                                    <th>兌換碼</th>
                                    <th>操 作</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($data as $item)
                                    <tr>
                                        {{-- 縮排 --}}
                                        <td>
                                            <a data-bs-toggle="collapse" class="chgIcon"
                                                href="#promocode-{{ $item->id }}" role="button">
                                                <i class="ri-triangle-fill ri-rotate-90 " style="color:9FCFF8;"></i> </a>
                                        </td>
                                        <td class="text-center">{{ $item->id }}</td>
                                        <td class="sorting_1">
                                            <div class="d-flex justify-content-start align-items-center user-name">
                                                <div class="avatar-wrapper">
                                                    <div class="avatar avatar-sm me-3">
                                                        <img src="{{ $item->avatar ?? '' }}" alt="Avatar"
                                                            class="rounded-circle">
                                                    </div>
                                                </div>
                                                <div class="d-flex flex-column">
                                                    <a href="#" class="text-truncate text-heading">
                                                        <span class="fw-medium">{{ $item->name }}</span></a>
                                                    <small>{{ $item->email }}</small>
                                                </div>
                                            </div>
                                        </td>
                                        <td>{{ $item->phone }}</td>
                                        <td class="text-center">
                                            <span class="text-truncate d-flex align-items-center text-heading"
                                                id="user-{{ $item->id }}">
                                                @if ($item->level == 0)
                                                    <i class="ri-user-line ri-22px text-danger me-2"></i>
                                                @elseif($item->level == 1)
                                                    <i class="ri-user-line ri-22px text-info me-2"></i>
                                                @elseif($item->level == 2)
                                                    <i class="ri-user-line ri-22px text-sucess me-2"></i>
                                                @elseif($item->level == 9)
                                                    <i class="ri-user-line ri-22px text-warning me-2"></i>
                                                @endif
                                                {{ $item->Level_Name }}
                                            </span>
                                        </td>
                                        <td>
                                            @if (count($item->getPromoCode) == 1)
                                                <span class="text-danger" style="font-size:20px">
                                                    {{ $item->getPromoCode[0]->code }}
                                                </span>
                                            @endif
                                            @php
                                            $pageTotal +=  count($item->getPromoCode) ;
                                            @endphp
                                            @if (count($item->getPromoCode) > 1)
                                                <a class="text-danger" style="font-size:20px" data-bs-toggle="collapse"
                                                    href="#multiCollapseExample-{{ $item->id }}" role="button"
                                                    aria-expanded="false" aria-controls="multiCollapseExample1">
                                                    {{ $item->getPromoCode[0]->code }} ({{ count($item->getPromoCode) }}
                                                    筆)

                                                </a>
                                            @endif
                                        </td>
                                        <td>
                                            <!-- Icon Dropdown -->
                                            <div class="btn-group">
                                                <button type="button"
                                                    class="btn btn-info btn-icon rounded-pill dropdown-toggle hide-arrow"
                                                    data-bs-toggle="dropdown">
                                                    <i class="ri-user-line"></i>
                                                </button>
                                                <ul class="dropdown-menu">
                                                    <li><a class="dropdown-item" href="javascript:void(0);"
                                                            onclick="changeLevel({{ $item->id }},0)">
                                                            <i class="ri-user-line ri-22px text-danger me-2"></i>
                                                            普通會員</a>
                                                    </li>
                                                    <li><a class="dropdown-item" href="javascript:void(0);"
                                                            onclick="changeLevel({{ $item->id }},1)">
                                                            <i class="ri-user-line ri-22px text-info me-2"></i>
                                                            白銀會員</a></li>
                                                    <li><a class="dropdown-item" href="javascript:void(0);"
                                                            onclick="changeLevel({{ $item->id }},2)">
                                                            <i class="ri-user-line ri-22px text-sucess me-2"></i>
                                                            白金會員</a></li>
                                                    <li>
                                                        <hr class="dropdown-divider">
                                                    </li>
                                                    <li><a class="dropdown-item" href="javascript:void(0);"
                                                            onclick="changeLevel({{ $item->id }},9)">
                                                            <i class="ri-user-line ri-22px text-warning me-2"></i>
                                                            俺是管理員</a></li>
                                                </ul>
                                            </div>

                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="7">
                                            <div class="collapse multi-collapse" id="promocode-{{ $item->id }}">
                                                <div
                                                    class="d-grid d-sm-flex p-4 border justify-content-center table-responsive text-nowrap">
                                                    <table class="table">
                                                        <thead>
                                                            <tr>
                                                                <th>優惠碼</th>
                                                                <th>給予</th>
                                                                <th>優惠碼</th>
                                                                <th>給予</th>
                                                                <th>優惠碼</th>
                                                                <th>給予</th>
                                                                <th>優惠碼</th>
                                                                <th>給予</th>
                                                                <th>優惠碼</th>
                                                                <th>給予</th>
                                                            </tr>
                                                        </thead>
                                                        <tbody>
                                                            @php
                                                            $i=1;
                                                            $promoCodes = $item->getPromoCode()->paginate(30); // 每頁30筆優惠碼
                                                        @endphp
                                                        @foreach ($promoCodes as $val)
                                                                @if ($i == 1)
                                                                    <tr>
                                                                @endif
                                                                <td class="text-danger"> {{ $val->code }}</td>
                                                                <td  class="text-info">{{ $val->give_to }}</td>
                                                                @if ($i == 5)
                                    </tr>
                                    @php $i = 0 @endphp
                                @endif
                                @php $i++; @endphp
                                @endforeach
                            </tbody>
                            <tfoot>
                                <th colspan="10">
                                    @if($promoCodes->count()>29)
                                    {{ $promoCodes->links() }}
                                    @endif
                                </th>
                            </tfoot>
                        </table>
                        {{-- <span class="text-danger " style="font-size:20px">
                                                    @foreach ($item->getPromoCode as $key => $val)
                                                        @if ($key % 8 == 0)
                                                    <br>
                                                        @endif
                                                        {{ $val->code }}
                                                    @endforeach
                                                </span> --}}
                    </div>
                </div>
                </td>

                </tr>
                @endforeach
                </tbody>
                <tfoot>
                    <tr>
                        <th colspan="3">本頁兌換碼計數</th>
                        <th colspan="4">總兌換碼計數</th>
                    </tr>
                    <tr>
                        <th colspan="3" class="text-start-center"><h3 class="mb-1 me-2 text-info">{{ $pageTotal }}</h3></th>
                        <th colspan="4" class="text-start-center"><h3 class="mb-1 me-2 text-info">{{  $total}} </h3></th>
                    </tr>
                </tfoot>
                </table>
            </div>
        </div>
        <div class="content-backdrop fade"></div>
        {{ $data->links() }}
    </div>
    <!-- Content wrapper -->
    </div>
    <!-- / Layout page -->
    </div>
    <div class="alert alert-solid-primary d-flex align-items-center" role="alert">
        <span class="alert-icon rounded">
            <i class="ri-user-follow-line ri-22px"></i>
        </span>
        點選縮排可以看詳細派發,點擊操作可更改會員等級！
    </div>
    </div>
@endsection
@section('scripts')
    <script>
        $(".chgIcon").click(function() {
            var icon = $(this).find("i"); // 只找當前被點擊 <a> 內的 <i>

            if (icon.hasClass("ri-rotate-90")) {
                icon.removeClass("ri-rotate-90").addClass("ri-rotate-180");
                icon.css("color", "#DC30BC"); // 變色
            } else {
                icon.removeClass("ri-rotate-180").addClass("ri-rotate-90");
                icon.css("color", "#9FCFF8"); // 恢復原色
            }
        });

        function changeLevel(id, lv) {
        Swal.fire({
            title: "確定",
            text: "你即將修改此用戶權限！",
            icon: "warning",
            showCancelButton: true,
            confirmButtonText: "確定",
            cancelButtonText: "取消"
        }).then((result) => {
            if (result.isConfirmed) {
                $.ajax({
                    type: "patch",
                    url: "{{ route('member.changelevel') }}",
                    data: {
                        id: id,
                        level: lv
                    },
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    success: function(response) {
                        if (response.status == 'success') {
                            let name = response.name;
                            let view = '';
                            if (lv == 0) {
                                view = '<i class="ri-user-line ri-22px text-danger me-2"></i>' + name;
                            } else if (lv == 1) {
                                view = '<i class="ri-user-line ri-22px text-info me-2"></i>' + name;
                            } else if (lv == 2) {
                                view = '<i class="ri-user-line ri-22px text-sucess me-2"></i>' + name;
                            } else if (lv == 9) {
                                view = '<i class="ri-user-line ri-22px text-warning me-2"></i>' + name;
                            }
                            $("#user-" + id).html(view);
                            Swal.fire("成功", "修改成功", "success");
                        }
                    }
                });
            } else if (result.dismiss === Swal.DismissReason.cancel) {
                Swal.fire("取消", "使用者取消操作", "error");
            }
        });
    }
    </script>
    <script src="{{ asset('assets/vendor/js/dropdown-hover.js') }}"></script>
@endsection
