// Copyright (c) 2023, JINGROW and Contributors
// MIT License. See license.txt

jingrow.provide("jingrow.page_theme");

jingrow.page_theme = {
    themes: {},
    
    load_css: function(css_code, style_id) {
        if (!css_code || !style_id) return;
        
        let style_element = document.getElementById(style_id);
        if (!style_element) {
            style_element = document.createElement('style');
            style_element.id = style_id;
            document.head.appendChild(style_element);
        }
        style_element.textContent = css_code;
    },
    
    load_js: function(js_code, context) {
        if (!js_code) return;
        
        try {
            if (context.pagetype) {
                var frm = context;
                eval(js_code);
            } else {
                var listview = context;
                eval(js_code);
            }
        } catch (e) {
            jingrow.show_alert({message: "模板JS错误: " + e.message, indicator: 'red'});
        }
    },
    
    load_form_theme: function(frm) {
        if (!frm || !frm.pagetype) return;
        
        jingrow.model.with_pg("PageType", frm.pagetype, function() {
            var pagetype_pg = jingrow.get_pg("PageType", frm.pagetype);
            if (!pagetype_pg || !pagetype_pg.detail_template) return;
            
            jingrow.model.with_pg("Detail Page Template", pagetype_pg.detail_template, function() {
                var template_pg = jingrow.get_pg("Detail Page Template", pagetype_pg.detail_template);
                if (!template_pg || template_pg.enabled === 0) return;
                
                if (template_pg.template_css) {
                    jingrow.page_theme.load_css(
                        template_pg.template_css,
                        `theme-form-${frm.pagetype}`
                    );
                }
                
                if (template_pg.template_js) {
                    jingrow.page_theme.load_js(template_pg.template_js, frm);
                }
            });
        });
    },
    
    load_list_theme: function(listview) {
        if (!listview || !listview.pagetype) return;
        
        jingrow.model.with_pg("PageType", listview.pagetype, function() {
            var pagetype_pg = jingrow.get_pg("PageType", listview.pagetype);
            if (!pagetype_pg || !pagetype_pg.list_template) return;
            
            jingrow.model.with_pg("List Page Template", pagetype_pg.list_template, function() {
                var template_pg = jingrow.get_pg("List Page Template", pagetype_pg.list_template);
                if (!template_pg || template_pg.enabled === 0) return;
                
                if (template_pg.template_css) {
                    jingrow.page_theme.load_css(
                        template_pg.template_css,
                        `theme-list-${listview.pagetype}`
                    );
                }
                
                if (template_pg.template_js) {
                    jingrow.page_theme.load_js(template_pg.template_js, listview);
                }
            });
        });
    }
};

$(document).ready(function() {
    jingrow.ui.form.on('*', 'onload_post_render', function(frm) {
        if (frm.pagetype) {
            jingrow.page_theme.load_form_theme(frm);
        }
    });
    
    jingrow.ui.form.on('*', 'after_save', function(frm) {
        if (frm.pagetype) {
            jingrow.page_theme.load_form_theme(frm);
        }
    });
    
    $(document).on("jingrow.ui.setup_list", function(e, data) {
        if (data && data.list_view && data.list_view.pagetype) {
            jingrow.page_theme.load_list_theme(data.list_view);
        }
    });
    
    $(document).on("form-render-complete", function(e, args) {
        if (cur_frm && cur_frm.pagetype) {
            setTimeout(function() {
                jingrow.page_theme.load_form_theme(cur_frm);
            }, 100);
        }
    });
});

window.load_page_theme = function() {
    if (cur_frm && cur_frm.pagetype) {
        jingrow.page_theme.load_form_theme(cur_frm);
        return "已尝试加载表单主题: " + cur_frm.pagetype;
    } else {
        return "当前页面不是表单或没有pagetype属性";
    }
};

(function() {
    if (jingrow.ui.form.Form) {
        var original_render_form = jingrow.ui.form.Form.prototype.render_form;
        jingrow.ui.form.Form.prototype.render_form = function(switched) {
            original_render_form.apply(this, arguments);
            
            if (this.pagetype) {
                jingrow.page_theme.load_form_theme(this);
            }
        };
    }
})();