@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-palette"></i>
                    </span>
                    {{ __('auto.添加主题配置') }}
                </h3>
            </div>
            <div class="row">
                <div class="col-12 grid-margin">
                    <div class="card">
                        <div class="card-body">
                            <form id="themeForm">
                                <div class="form-group">
                                    <label>{{ __('auto.主题KEY') }}</label>
                                    <input type="text" class="form-control" name="ThemeKey" required>
                                </div>
                                <div class="form-group">
                                    <label>{{ __('auto.绑定的Region (多个用逗号分隔)') }}</label>
                                    <div class="region-checkboxes">
                                        @foreach($regions as $region)
                                            <div class="form-check">
                                                <label class="form-check-label">
                                                    <input type="checkbox" class="form-check-input" name="region_ids[]" value="{{$region->id}}">
                                                    {{$region->RegionID}} (ID: {{$region->id}})
                                                </label>
                                            </div>
                                        @endforeach
                                    </div>
                                </div>
                                <button type="submit" class="btn btn-gradient-primary mr-2">{{ __('auto.提交') }}</button>
                                <a href="{{ url('admin/web_theme_config') }}" class="btn btn-light">{{ __('auto.取消') }}</a>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        $('#themeForm').on('submit', function(e) {
            e.preventDefault();

            var checkedIds = [];
            $('input[name="region_ids[]"]:checked').each(function() {
                checkedIds.push($(this).val());
            });

            var formData = {
                ThemeKey: $('input[name="ThemeKey"]').val(),
                BindRegions: checkedIds.join(',')
            };

            $.ajax({
                url: '{{ url("admin/web_theme_config/add") }}',
                type: 'POST',
                data: formData,
                success: function(res) {
                    if (res.code === 200) {
                        alert('添加成功');
                        window.location.href = '{{ url("admin/web_theme_config") }}';
                    } else {
                        alert(res.msg || '添加失败');
                    }
                }
            });
        });
    </script>
@endsection
