# apps/core/admin.py
from django import forms
from django.contrib import admin

from ..models.sitemap import SitemapConfig
from .mixins import SingletonAdminMixin


class SitemapConfigForm(forms.ModelForm):
    content_models = forms.MultipleChoiceField(
        label="系统模型", widget=forms.CheckboxSelectMultiple, required=False)

    user_app_models = forms.MultipleChoiceField(
        label="用户应用模型", widget=forms.CheckboxSelectMultiple, required=False)

    class Meta:
        model = SitemapConfig
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # 动态设置选项
        self.fields[
            'content_models'].choices = SitemapConfig.get_content_models_choices(
            )
        self.fields[
            'user_app_models'].choices = SitemapConfig.get_user_space_choices(
            )

        # 初始化已选值
        if self.instance.content_models:
            self.initial['content_models'] = self.instance.content_models
        if self.instance.user_app_models:
            self.initial['user_app_models'] = self.instance.user_app_models

    def save(self, commit=True):
        instance = super().save(commit=False)
        # 转换表单数据到JSON存储格式
        instance.content_models = self.cleaned_data['content_models']
        instance.user_app_models = self.cleaned_data['user_app_models']
        if commit:
            instance.save()
        return instance


@admin.register(SitemapConfig)
class SitemapConfigAdmin(SingletonAdminMixin, admin.ModelAdmin):
    form = SitemapConfigForm
    fieldsets = (
        ('', {
            'description': '''
                <div class="alert alert-warning">
                    站点地图地址：
                    <ul>
                        <li><strong>索引模式：</strong>host/sitemap.xml</li>
                        <li><strong>普通模式：</strong>host/sitemap.xml，host/sitemap_1.xml...</li>
                    </ul>
                    
                    
                </div>
            ''',
            'fields': (),
        }),
        ('模型选择', {
            'description': '''
                <div class="alert alert-info">
                    选择需要加入站点地图的模型
                </div>
            ''',
            'fields': ('content_models', 'user_app_models'),
        }),
        ('配置参数', {
            'fields': ('protocol','changefreq', 'priority', 'paginate_by'),
        }),
        ('索引模式', {
            'description': '''
                <div class="alert alert-info">
                    <p>索引模式</p>
                    <pre class="p-2 bg-white"><code>
&lt;sitemapindex xmlns=&quot;http://www.sitemaps.org/schemas/sitemap/0.9&quot;&gt;
&lt;sitemap&gt;
&lt;loc&gt;http://127.0.0.1:8000/sitemap-content_category.xml&lt;/loc&gt;
&lt;/sitemap&gt;
&lt;sitemap&gt;
&lt;loc&gt;http://127.0.0.1:8000/sitemap-content_article.xml&lt;/loc&gt;
&lt;/sitemap&gt;
&lt;sitemap&gt;
&lt;loc&gt;http://127.0.0.1:8000/sitemap-content_article_2.xml&lt;/loc&gt;
&lt;/sitemap&gt;
&lt;sitemap&gt;
&lt;loc&gt;http://127.0.0.1:8000/sitemap-content_article_3.xml&lt;/loc&gt;
&lt;/sitemap&gt;
&lt;sitemap&gt;
&lt;loc&gt;http://127.0.0.1:8000/sitemap-software_docs_document.xml&lt;/loc&gt;
&lt;/sitemap&gt;
&lt;/sitemapindex&gt;
                    </code></pre>
                    <p>普通模式</p>
                    <pre class="p-2 bg-white"><code>
&lt;urlset xmlns=&quot;http://www.sitemaps.org/schemas/sitemap/0.9&quot; xmlns:xhtml=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;url&gt;
&lt;loc&gt;http://127.0.0.1:8000/dy/331.html&lt;/loc&gt;
&lt;changefreq&gt;daily&lt;/changefreq&gt;
&lt;priority&gt;0.5&lt;/priority&gt;
&lt;/url&gt;
&lt;url&gt;
&lt;loc&gt;http://127.0.0.1:8000/dy/330.html&lt;/loc&gt;
&lt;changefreq&gt;daily&lt;/changefreq&gt;
&lt;priority&gt;0.5&lt;/priority&gt;
&lt;/url&gt;
&lt;url&gt;
&lt;loc&gt;http://127.0.0.1:8000/jsj/328.html&lt;/loc&gt;
&lt;changefreq&gt;daily&lt;/changefreq&gt;
&lt;priority&gt;0.5&lt;/priority&gt;
&lt;/url&gt;
&lt;/urlset&gt;
                    </code></pre>
                </div>
            ''',
            'fields': ('enable_index', ),
        }),
    )
