@extends('base.base')
@section('base')
    <!-- 内容区域 -->
    <div class="main-panel">
        <div class="content-wrapper">
            <div class="page-header">
                <h3 class="page-title">
                     <span class="page-title-icon bg-gradient-primary text-white mr-2">
                        <i class="mdi mdi-settings"></i>
                    </span>
                    {{ __('auto.投放渠道管理') }}
                </h3>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="#">{{ __('auto.渠道管理') }}</a></li>
                        <li class="breadcrumb-item active" aria-current="page">{{ __('auto.投放渠道管理') }}</li>
                    </ol>
                </nav>
            </div>
            <div class="row">
                <div class="col-lg-12 grid-margin stretch-card">
                    <div class="card">
                        <div class="card-body">
                            <h4 class="card-title">{{ __('auto.投放渠道管理') }}</h4>
                            <form class="well form-inline margin-top-20" method="get" action='/admin/web_channel_config/list'>
                                <span style="padding-left: 5px">{{ __('auto.渠道号：') }}</span>
                                <input type="text" name="Channel" value="{{$channel}}" class="form-control">&nbsp;&nbsp;
                                <span style="padding-left: 5px">{{ __('auto.包名：') }}</span>
                                <input type="text" name="PackageName" value="{{$packageName}}" class="form-control">&nbsp;&nbsp;
                                <input type="submit" class="btn btn-sm btn-gradient-dark btn-icon-text" value="{{ __('auto.搜索') }}"/>&nbsp;&nbsp;
                                <a href="/admin/web_channel_config/list" class="btn btn-sm btn-gradient-warning btn-icon-text">{{ __('auto.清空') }}&{{ __('auto.刷新') }}</a>&nbsp;&nbsp;
                            </form>
                            <br>
{{--                            <button type="button" class="btn btn-sm btn-gradient-success btn-icon-text" onclick="add()">--}}
{{--                                <i class="mdi mdi-plus btn-icon-prepend"></i>--}}
{{--                                {{ __('auto.添加配置') }}--}}
{{--                            </button>--}}
                            <button type="button" class="btn btn-sm btn-gradient-primary btn-icon-text" onclick="quickCreateChannel()">
                                <i class="mdi mdi-plus btn-icon-prepend"></i>
                                {{ __('auto.快速创建渠道') }}
                            </button>
                            <br><br>
                            <table class="table table-bordered">
                                <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>{{ __('auto.渠道号') }}</th>
                                    <th>{{ __('auto.包名') }}</th>
                                    <th>RegionID</th>
                                    <th>{{ __('auto.网站地址') }}</th>
                                    <th>{{ __('auto.备注') }}</th>
                                    <th>StateNo</th>
                                    <th>Platform</th>
                                    <th>PlatformToken</th>
                                    <th>BonusArr</th>
                                    <th>{{ __('auto.操作') }}</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($list as $k=>$v)
                                    <tr>
                                        <td>{{$v->ID}}</td>
                                        <td>{{$v->Channel}}</td>
                                        <td>{{$v->PackageName}}</td>
                                        <td>
                                            @php
                                                $regionInfo = $regionMap[$v->RegionID] ?? null;
                                            @endphp
                                            @if($regionInfo && $regionInfo['has_logo'])
                                                <div style="background-color: #000; display: inline-block; padding: 2px; width: 100%; text-align: center;">
                                                    @if(!empty($regionInfo['is_svg']))
                                                        <object data="{{$regionInfo['logo']}}" type="image/svg+xml" style="max-height: 30px; width: 100%; height: auto; pointer-events: none;" title="{{$v->RegionID}}">
                                                            <img src="{{$regionInfo['logo']}}" style="max-height: 30px; width: auto;" alt="Logo" title="{{$v->RegionID}}">
                                                        </object>
                                                    @else
                                                        <img src="{{$regionInfo['logo']}}" style="max-height: 30px; width: auto;" alt="Logo" title="{{$v->RegionID}}">
                                                    @endif
                                                </div>
                                            @else
                                                <span style="color: {{$regionInfo['color'] ?? ''}}">{{$v->RegionID}}</span>
                                            @endif
                                        </td>
                                        <td>
                                            @php
                                                $channelUrl = $channelUrls[$v->ID] ?? '';
                                            @endphp
                                            @if($channelUrl)
                                                <a href="javascript:void(0);"
                                                   onclick="copyToClipboard('{{$channelUrl}}', this)"
                                                   style="color: #007bff; text-decoration: none; cursor: pointer;"
                                                   title="{{ __('auto.点击复制') }}">
                                                    {{$channelUrl}}
                                                </a>
                                            @else
                                                <span style="color: #999;">-</span>
                                            @endif
                                        </td>
                                        <td contentEditable="true" onblur="remarks(this, {{$v->ID}})" style="cursor: text;">{{$v->Remarks}}</td>
                                        <td>{{$v->StateNo}}</td>
                                        <td>{{$v->PlatformName}} ({{$v->PlatformID}})</td>
                                        <td>
                                            @if(!empty($v->PlatformToken))
                                                <span style="color: #28a745;">{{ __('auto.已配置') }}</span>
                                            @else
                                                <span style="color: #dc3545;">{{ __('auto.未配置') }}</span>
                                            @endif
                                        </td>
                                        <td>{{$v->BonusArr}}</td>
                                        <td>
                                            <button class="btn btn-sm btn-gradient-dark" onclick="edit({{$v->ID}})">
                                                {{ __('auto.修改') }}
                                            </button>
                                        </td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                            <div class="box-footer clearfix" id="pages">
                                {{ __('auto.总共') }} <b>{{ $list->total() }}</b> {{ __('auto.条,分为') }}<b>{{ $list->lastPage() }}</b>{{ __('auto.页') }}
                                {!! $list->appends(['Channel' => $channel, 'PackageName' => $packageName])->links() !!}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        function add() {
            layer.open({
                type: 2,
                title: '{{ __('auto.添加配置') }}',
                shadeClose: true,
                shade: 0.8,
                area: ['70%', '80%'],
                content: '/admin/web_channel_config/add'
            });
        }

        function edit(id) {
            layer.open({
                type: 2,
                title: '{{ __('auto.修改配置') }}',
                shadeClose: true,
                shade: 0.8,
                area: ['70%', '80%'],
                content: '/admin/web_channel_config/update/' + id
            });
        }

        function copyToClipboard(text, element) {
            // 创建临时文本域
            var tempInput = document.createElement('textarea');
            tempInput.value = text;
            document.body.appendChild(tempInput);
            tempInput.select();

            try {
                // 执行复制
                document.execCommand('copy');

                // 显示复制成功提示
                layer.msg('{{ __('auto.复制成功') }}', {icon: 1, time: 1000});

                // 短暂改变链接颜色表示已复制
                var originalColor = element.style.color;
                element.style.color = '#28a745';
                setTimeout(function() {
                    element.style.color = originalColor;
                }, 300);
            } catch (err) {
                layer.msg('{{ __('auto.复制失败') }}', {icon: 2});
            }

            // 移除临时文本域
            document.body.removeChild(tempInput);
        }

        // 快速创建渠道
        function quickCreateChannel() {
            layer.open({
                type: 2,
                title: '{{ __('auto.快速创建渠道') }}',
                shadeClose: true,
                shade: 0.8,
                area: ['60%', '70%'],
                content: '/admin/channel/quick_create_channel',
                end: function() {
                    window.location.reload();
                }
            });
        }

        // 修改备注
        function remarks(obj, id) {
            var remark = $(obj).html();
            myRequest("/admin/web_channel_config/remarks/" + id, "post", {remark}, function (res) {
                layer.msg(res.msg);
                setTimeout(function () {
                    // window.location.reload();
                }, 1500);
            });
        }
    </script>
@endsection
