<?php
if ( ! defined( 'ABSPATH' ) ) exit;

// 注意这里的类名，要和主文件里 YFJ_Module_ + ucfirst('bazi_cesuan') 对应
class YFJ_Module_Bazi_hepan extends YFJ_Base_Module {

    public function __construct() {
        $this->module_id = 'bazi_hepan';             // 1. 修改模块 ID
        $this->shortcode = 'yfj_bazi_hepan';         // 2. 修改短代码
        $this->api_endpoint = '/v1/Bazi/hepan';      // 3. 配置对应的真实 API 接口

        // 🚀 新增：声明本模块支持英文能力
        $this->supported_langs = ['zh-cn', 'zh-tw', 'en-us'];

        parent::__construct();
    }

    /**
     * 专属数据拦截与加工引擎 (重写基类方法)
     * 在这里可以对 API 返回的原始数据进行格式化、过滤或追加二次计算的自定义数据。
     */
    protected function process_api_data($raw_data) {
        // 1. 确保数据是数组格式，防止报错
        if (is_array($raw_data)) {
            //如果 API 没返回格局，给个默认的兜底值
            if (!isset($raw_data['base_info']['zhengge'])) {
                $raw_data['base_info']['zhengge'] = '未入格';
            }
        }

        //将处理后的数据返回给 views/result.php 进行渲染
        return $raw_data;
    }

    /**
     * 沙盒模式下的模拟数据
     */
    protected function get_demo_data($post_data) {
        //获取当前后台设置的语言
        $lang = get_option('yfj_language', 'zh-cn');

        // 1. 把你的完整 JSON 粘贴到单引号里面（要考虑到zh-cn 和 zh-tw）
        $demo_json_zh_cn = '{"errcode":0,"errmsg":"请求成功","notice":"本次测算结果仅供娱乐使用，请勿用于封建迷信和违法用途。","data":{"male":{"bazi":["戊辰","乙卯","壬戌","丙午"],"gl_year":"1988","gl_month":"3","gl_day":"8","gl_hours":"12","sex":"乾造","name":"张三","gongli":"1988年3月8日12时20分","kw":"子丑","tg_cg_god":["七杀","伤官","日元","偏财"],"dz_cg":["戊|乙|癸","乙","戊|辛|丁","丁|己"],"dz_cg_god":["七杀|伤官|劫财","伤官","七杀|正印|正财","正财|正官"],"day_cs":["墓","死","冠带","胎"],"na_yin":["大林木","大溪水","大海水","天河水"]},"female":{"bazi":["癸未","壬戌","壬午","丙午"],"gl_year":"2003","gl_month":"11","gl_day":"5","gl_hours":"12","sex":"坤造","name":"李四","gongli":"2003年11月5日12时20分","kw":"申酉","tg_cg_god":["劫财","比肩","日元","偏财"],"dz_cg":["己|丁|乙","戊|辛|丁","丁|己","丁|己"],"dz_cg_god":["正官|正财|伤官","七杀|正印|正财","正财|正官","正财|正官"],"day_cs":["养","冠带","胎","胎"],"na_yin":["杨柳木","大海水","杨柳木","天河水"]},"minggong":{"male_fengshui":"东四命","female_fengshui":"东四命","score":"10","male_minggong":"震","female_minggong":"离","description":"此项满分为10分。以东四命与西四命之说来合，如果相合，那么在选择合作场地时，应选与自己命宫相合的方位。","detail_description":"甲方命卦为震，适宜合作场地方位为东方。乙方命卦为离，适宜合作场地方位为南方。两者命卦方位均属于：东四命。说明两人，命宫相合，两个合作伙伴相遇时，他们的专长与技能完美契合，形成了一场无往不胜的合作盛宴。就如同四命之间互相帮助，共同修正特定的煞星威力，他们能够相互支持，共同解决问题，降低不利因素的影响，实现合作的最大化效益。在这个紧密的合作关系中，他们之间的机遇不断滋长，相互增强，促进彼此之间的理解和信任，从而共同实现成功与幸福。这种合作关系不仅为双方带来了事业上的成功，也为他们的合作项目和事业发展打下了坚实的基础，为双方的未来带来了更广阔的机遇和福利。"},"nianqitongzhi":{"male_nian_zhi":"辰","male_nian_zhi_desc":"木","female_nian_zhi":"未","female_nian_zhi_desc":"火","description":"此项满分为20分。如寅卯辰会东方木气，虎兔龙合作生意成功的机缘就大于其它属相；巳午未会南方火气，蛇马羊合作生意成功的机缘就大于其它属相；申酉戌会西方金气，猴鸡狗合作生意成功的机缘就大于其它属相；亥子丑会北方水气，猪鼠牛合作生意成功机缘就大于其它属相。","detail_description":"甲方年支为辰，乙方年支为未。此局为未拱辰。年支代表生意宫，此局拱合，意味着双方合作伙伴会在团队的建议下，同意此次合作。合作伙伴的年支拱合，代表着双方能够相互尊重，建立起牢固的合作关系，并培养出彼此合作信任。这是通过协调和妥协的方式解决彼此的不同意见而达成的。因此，这种合作关系会非常稳固，双方合作伙伴也会因此而享受到合作的成功。","score":"10"},"yueling":{"score":"20","male_yue_zhi":"卯","female_yue_zhi":"戌","description":"此项满分为20分，如果此项分数越高，说明双方合作的财运运势越好。","detail_description":"甲方月支为卯，乙方月支为戌。此局为卯戌合化火。月支代表财帛宫，此局全六合，财帛宫展现了合作中令人振奋的财富运势，预示着合作伙伴将迎来丰厚的财务回报。在这个合作中，双方将共同开创财富之路，取得显著的经济成就。财帛宫的能量会为合作伙伴带来财富和机遇的滚滚而来，不仅会增加现有财富，还会拓展财务渠道，带来新的商机和收益。关键在于双方充分利用这些机会，以智慧和勇气迎接挑战，持续不断地努力，共同实现财富的持续增长和稳健发展。"},"rigan":{"score":"0","detail_description":"甲方日干为壬。五行信息：五行属水，谓之纯阳之水；乙方日干为壬。五行信息：五行属水，谓之纯阳之水。双方日干五行阴阳不和，代表着恋人之间可能会面临一些挑战，初期的机缘较小。然而，正是因为这些困难，他们有机会更加珍惜彼此的相遇。尽管开始时机缘不济，但凭借着彼此的坚持和努力，他们或许会创造出一段克服困难的美丽爱情，让彼此的感情更加深厚。","male_yue_zhi":"壬","female_yue_zhi":"壬","description":"此项满分为20分。如果日干五行相同，一阴一阳的组合的双方恋爱的机缘最大，如甲日干逢乙日干，庚日干逢辛日干之类。"},"tiangan":{"score":"10","male_yue_zhi":"壬","female_yue_zhi":"壬","description":"此项满分为20分，如果此项分数越高，说明双方的总体运势越好。","detail_description":"甲方日天干为壬，乙方日天干为壬。此局为平。日干代表命宫，主运势，此局平，甲方与乙方的运势呈现出安稳与和谐的状态。他们之间的运势关系如同平静的湖面，没有波澜，一切都在和谐相处之中。甲方与乙方或许因为相互理解和信任，能够在相处中保持平稳的节奏，共同朝着共同的目标前进。在这样的局势下，他们将能够以平和的心态处理各种事务，共同推动项目的进展。"},"jiankang":{"score":"5","detail_description":"在合作过程中，甲乙双方需要密切关注身体健康。身体是事业的根本，保持身体健康是持续发展的关键。合作之际，不妨相互关心，合理安排工作时间，适时休息，保证充足睡眠，定期进行体检，保持良好的饮食习惯和适度的运动。只有保持身体健康，才能充满活力地投入到合作中，共同创造更加美好的未来。","description":"此项满分为10分，如果此项分数越高，说明双方的健康运势越好。"},"all_score":55,"male_sx":"龙","female_sx":"羊","male_xz":"双鱼座","female_xz":"天蝎座"}}';
        $demo_json_zh_tw = '{"errcode":0,"errmsg":"请求成功","notice":"本次测算结果仅供娱乐使用，请勿用于封建迷信和违法用途。","data":{"male":{"bazi":["戊辰","乙卯","壬戌","丙午"],"gl_year":"1988","gl_month":"3","gl_day":"8","gl_hours":"12","sex":"乾造","name":"張三","gongli":"1988年3月8日12時20分","kw":"子丑","tg_cg_god":["七殺","傷官","日元","偏財"],"dz_cg":["戊|乙|癸","乙","戊|辛|丁","丁|己"],"dz_cg_god":["七殺|傷官|劫財","傷官","七殺|正印|正財","正財|正官"],"day_cs":["墓","死","冠帶","胎"],"na_yin":["大林木","大溪水","大海水","天河水"]},"female":{"bazi":["癸未","壬戌","壬午","丙午"],"gl_year":"2003","gl_month":"11","gl_day":"5","gl_hours":"12","sex":"坤造","name":"李四","gongli":"2003年11月5日12時20分","kw":"申酉","tg_cg_god":["劫財","比肩","日元","偏財"],"dz_cg":["己|丁|乙","戊|辛|丁","丁|己","丁|己"],"dz_cg_god":["正官|正財|傷官","七殺|正印|正財","正財|正官","正財|正官"],"day_cs":["養","冠帶","胎","胎"],"na_yin":["楊柳木","大海水","楊柳木","天河水"]},"minggong":{"male_fengshui":"東四命","female_fengshui":"東四命","score":"10","male_minggong":"震","female_minggong":"離","description":"此項滿分為10分。以東四命與西四命之說來合，如果相合，那麽在選擇合作場地時，應選與自己命宮相合的方位。","detail_description":"甲方命卦為震，適宜合作場地方位為東方。乙方命卦為離，適宜合作場地方位為南方。兩者命卦方位均屬於：東四命。說明兩人，命宮相合，兩個合作夥伴相遇時，他們的專長與技能完美契合，形成了一場無往不勝的合作盛宴。就如同四命之間互相幫助，共同修正特定的煞星威力，他們能夠相互支持，共同解決問題，降低不利因素的影響，實現合作的最大化效益。在這個緊密的合作關系中，他們之間的機遇不斷滋長，相互增強，促進彼此之間的理解和信任，從而共同實現成功與幸福。這種合作關系不僅為雙方帶來了事業上的成功，也為他們的合作項目和事業發展打下了堅實的基礎，為雙方的未來帶來了更廣闊的機遇和福利。"},"nianqitongzhi":{"male_nian_zhi":"辰","male_nian_zhi_desc":"木","female_nian_zhi":"未","female_nian_zhi_desc":"火","description":"此項滿分為20分。如寅卯辰會東方木氣，虎兔龍合作生意成功的機緣就大於其它屬相；巳午未會南方火氣，蛇馬羊合作生意成功的機緣就大於其它屬相；申酉戌會西方金氣，猴雞狗合作生意成功的機緣就大於其它屬相；亥子丑會北方水氣，豬鼠牛合作生意成功機緣就大於其它屬相。","detail_description":"甲方年支為辰，乙方年支為未。此局為未拱辰。年支代表生意宮，此局拱合，意味著雙方合作夥伴會在團隊的建議下，同意此次合作。合作夥伴的年支拱合，代表著雙方能夠相互尊重，建立起牢固的合作關系，並培養出彼此合作信任。這是通過協調和妥協的方式解決彼此的不同意見而達成的。因此，這種合作關系會非常穩固，雙方合作夥伴也會因此而享受到合作的成功。","score":"10"},"yueling":{"score":"20","male_yue_zhi":"卯","female_yue_zhi":"戌","description":"此項滿分為20分，如果此項分數越高，說明雙方合作的財運運勢越好。","detail_description":"甲方月支為卯，乙方月支為戌。此局為卯戌合化火。月支代表財帛宮，此局全六合，財帛宮展現了合作中令人振奮的財富運勢，預示著合作夥伴將迎來豐厚的財務回報。在這個合作中，雙方將共同開創財富之路，取得顯著的經濟成就。財帛宮的能量會為合作夥伴帶來財富和機遇的滾滾而來，不僅會增加現有財富，還會拓展財務渠道，帶來新的商機和收益。關鍵在於雙方充分利用這些機會，以智慧和勇氣迎接挑戰，持續不斷地努力，共同實現財富的持續增長和穩健發展。"},"rigan":{"score":"0","detail_description":"甲方日干為壬。五行資訊：五行属水，谓之純陽之水；乙方日干為壬。五行資訊：五行属水，谓之純陽之水。雙方日干五行陰陽不和，代表著戀人之間可能會面臨一些挑戰，初期的機緣較小。然而，正是因為這些困難，他們有機會更加珍惜彼此的相遇。盡管開始時機緣不濟，但憑借著彼此的堅持和努力，他們或許會創造出一段剋服困難的美麗愛情，讓彼此的感情更加深厚。","male_yue_zhi":"壬","female_yue_zhi":"壬","description":"此項滿分為20分。如果日干五行相同，一陰一陽的組合的雙方戀愛的機緣最大，如甲日干逢乙日干，庚日干逢辛日干之類。"},"tiangan":{"score":"10","male_yue_zhi":"壬","female_yue_zhi":"壬","description":"此項滿分為20分，如果此項分數越高，說明雙方的總體運勢越好。","detail_description":"甲方日天干為壬，乙方日天干為壬。此局為平。日干代表命宮，主運勢，此局平，甲方與乙方的運勢呈現出安穩與和諧的狀態。他們之間的運勢關系如同平靜的湖面，沒有波瀾，一切都在和諧相處之中。甲方與乙方或許因為相互理解和信任，能夠在相處中保持平穩的節奏，共同朝著共同的目標前進。在這樣的局勢下，他們將能夠以平和的心態處理各種事務，共同推動項目的進展。"},"jiankang":{"score":"5","detail_description":"在合作過程中，甲乙雙方需要密切關註身體健康。身體是事業的根本，保持身體健康是持續發展的關鍵。合作之際，不妨相互關心，合理安排工作時間，適時休息，保證充足睡眠，定期進行體檢，保持良好的飲食習慣和適度的運動。只有保持身體健康，才能充滿活力地投入到合作中，共同創造更加美好的未來。","description":"此項滿分為10分，如果此項分數越高，說明雙方的健康運勢越好。"},"all_score":55,"male_sx":"龍","female_sx":"羊","male_xz":"雙魚座","female_xz":"天蠍座"}}';
        $demo_json_en_us = '{"errcode":0,"errmsg":"请求成功","notice":"本次测算结果仅供娱乐使用，请勿用于封建迷信和违法用途。","data":{"male":{"bazi":["WuChen","GuiHai","DingMao","BingWu"],"gl_year":"1988","gl_month":"11","gl_day":"8","gl_hours":"12","sex":"Male","name":"Third Lunar Month","gongli":"1988Year11Month8Day12Hour20minute","kw":"XuHai","tg_cg_god":["Hurting Officer","Seven Killings","Day Stem","Rob Wealth"],"dz_cg":["Wu|Yi|Gui","Ren|Jia","Yi","Ding|Ji"],"dz_cg_god":["Hurting Officer|Indirect Resource|Seven Killings","Proper Authority|Direct Resource","Indirect Resource","Equal Shoulders|Eating God"],"day_cs":["Decline","Conception","Illness","Taking Office"],"na_yin":["Great Forest Wood","Ocean Water","Furnace Fire","Heavenly River Water"]},"female":{"bazi":["WuChen","GuiHai","DingMao","BingWu"],"gl_year":"1988","gl_month":"11","gl_day":"8","gl_hours":"12","sex":"Female","name":"Fourth Lunar Month","gongli":"1988Year11Month8Day12Hour20minute","kw":"XuHai","tg_cg_god":["Hurting Officer","Seven Killings","Day Stem","Rob Wealth"],"dz_cg":["Wu|Yi|Gui","Ren|Jia","Yi","Ding|Ji"],"dz_cg_god":["Hurting Officer|Indirect Resource|Seven Killings","Proper Authority|Direct Resource","Indirect Resource","Equal Shoulders|Eating God"],"day_cs":["Decline","Conception","Illness","Taking Office"],"na_yin":["Great Forest Wood","Ocean Water","Furnace Fire","Heavenly River Water"]},"minggong":{"male_fengshui":"East Four Auspicious Positions","female_fengshui":"East Four Auspicious Positions","score":"10","male_minggong":"Zhen","female_minggong":"Zhen","description":"This item is scored out of 10 points. According to the theory of Eastern and Western Four Pillars of Destiny, if the elements are in harmony, then when choosing a cooperation venue, one should select a direction that aligns with their own Destiny Palace.","detail_description":"The destiny hexagram of Party A is Zhen, The suitable cooperative venue direction is East direction. The destiny hexagram of Party B is Zhen, The suitable cooperative venue direction is East direction. Both hexagrams of the two parties are in the direction of：East Four Auspicious Positions. This means that when two individuals have aligned destinies, their encounter is like a symphony of perfect harmony. Their expertise and skills seamlessly complement each other, creating an unstoppable force in their collaboration. Much like the interaction between the Four Pillars of Destiny, they assist each other, mitigating the influence of unfavorable factors and maximizing the benefits of their cooperation. Within this close-knit partnership, opportunities flourish, trust deepens, and success becomes a shared reality. This collaboration not only brings success in their endeavors but also lays a solid foundation for future projects and career growth. It opens doors to broader opportunities and benefits for both parties, ensuring a prosperous and fulfilling future."},"nianqitongzhi":{"male_nian_zhi":"Dragon","male_nian_zhi_desc":"Wood","female_nian_zhi":"Dragon","female_nian_zhi_desc":"Wood","description":"This item has a total score of 20 points. For example, if someone\'s Chinese zodiac sign is associated with the Eastern Wood Qi (such as Tiger, Rabbit, or Dragon), their chances of success in business cooperation are greater compared to other zodiac signs. Similarly, those associated with the Southern Fire Qi (such as Snake, Horse, or Goat) have greater chances of success in business cooperation. Likewise, individuals associated with the Western Metal Qi (such as Monkey, Rooster, or Dog) or the Northern Water Qi (such as Pig, Rat, or Ox) also have greater chances of success in business cooperation.","detail_description":"Party A\'s yearly earthly branch is Dragon, and party B\'s yearly earthly branch isDragon. This situation is Dragon Dragon self-injury.Year branch represents the business palace, and in this situation, it is self-destructive, implying that the teams of both collaborating partners may hinder this collaboration. The self-destructive year branch of the collaborating partners suggests that friction and disputes may arise during the collaboration, with significant differences in thinking and even goals. It is suggested that both collaborating partners establish common goals, attempt communication and mutual understanding to overcome difficulties in collaboration, and move towards a better future together. By establishing common values and goals for collaboration, both parties can foster the development of their partnership and achieve a win-win situation.","score":"5"},"yueling":{"score":"5","male_yue_zhi":"Pig","female_yue_zhi":"Pig","description":"This item is scored out of 20 points. A higher score indicates better financial prospects for the collaboration between both parties.","detail_description":"Party A\'s monthly earthly branch is Pig, and party B\'s monthly earthly branch is Pig. This situation is Pig Pig self-injury。The monthly branch represents the wealth palace, and the current situation suggests a self-punishing alignment. This self-punishing alignment implies internal financial challenges and conflicts that the partners may face. There might be disputes, differences, or disharmony in the cooperation, affecting financial aspects. Issues such as fund losses, contractual disputes, or poor management may occur. Overcoming this predicament requires mutual communication, understanding, and compromise between the partners, seeking common solutions to problems. Only through the efforts of both parties, overcoming internal conflicts and difficulties, can they rebuild a stable cooperative relationship and achieve financial growth and success."},"rigan":{"score":"0","detail_description":"Party A\'s daily heavenly stem is Ding. The five elements information：Belongs to Fire, known as pure Yin Fire, and party B\'s daily heavenly stem is Ding. The five elements information：Belongs to Fire, known as pure Yin Fire. The incompatible Yin-Yang attributes of the two parties\' heavenly stems suggest that lovers may face some challenges, with limited opportunities in the initial stages. However, it is precisely these difficulties that offer them the chance to cherish their encounter even more. Despite the initial lack of favorable circumstances, with perseverance and effort, they may create a beautiful love story overcoming obstacles, deepening their bond along the way.","male_yue_zhi":"Ding","female_yue_zhi":"Ding","description":"This item is scored out of 20 points. If the heavenly stems of both parties are the same, the combination of one Yin and one Yang holds the greatest potential for love between them, such as when the heavenly stem of A encounters the heavenly stem of B, or when the heavenly stem of G encounters the heavenly stem of X, and so forth."},"tiangan":{"score":"10","male_yue_zhi":"Ding","female_yue_zhi":"Ding","description":"This item is scored out of 20 points, and the higher the score, the better the overall luck of the cooperation between the two parties.","detail_description":"Party A\'s daily heavenly stem is Ding, and party B\'s daily heavenly stem is Ding. This situation is steady.The daily heavenly stem represents the palace of destiny and main fortune. In this situation of neutrality, the cooperative prospects between Party A and Party B are characterized by stability and harmony. Their collaboration is like the calm surface of a lake, without ripples, everything is in harmonious coexistence. Party A and Party B may maintain a steady rhythm in their cooperation due to mutual understanding and trust, moving forward towards common goals. In such a situation, they will be able to handle various affairs in cooperation with a peaceful mindset, jointly driving the progress of the project."},"jiankang":{"score":"10","detail_description":"Party A and Party B both prioritize their physical health, supporting each other and jointly promoting each other\'s well-being. They care for each other and create a healthy working environment together, striving to maintain physical health while encouraging each other to maintain good lifestyle habits. They collaboratively develop health plans, engage in regular exercise, focus on dietary nutrition, ensure adequate sleep, and pursue the perfect balance of physical and mental health. With mutual encouragement and support, their physical health is ensured, providing a solid foundation for the stable development of their partnership.","description":"This criterion is scored out of 10 points. A higher score indicates better health prospects for both parties."},"all_score":40,"male_sx":"Dragon","female_sx":"Dragon","male_xz":"Scorpio","female_xz":"Scorpio"}}';

        if ($lang == 'zh-cn') {
            $demo_json = $demo_json_zh_cn;
        } elseif ($lang == 'zh-tw') {
            $demo_json = $demo_json_zh_tw;
        } else {
            $demo_json = $demo_json_en_us;
        }

        // 2. 解析为 PHP 数组
        $demo_array_json = json_decode($demo_json, true);

        // 3. 【核心提取】剥离外层的 errcode 状态，只提取 data 节点内容
        $data = $demo_array_json['data'] ?? [];

        // 5. 返回处理好的干净数据给 process_api_data
        return $data;
    }
}