import { findDataInfo ,documentsList} from '../../api/documentManagement'
export default {
    data() {
        return {
            menuList: [
                {
                    path: "/AccessMiningPool",
                    name: "course.NEXAcourse",
                    value: "nexa",
                    imgUrl: `${this.$baseApi}img/nexa.png`,
                    show: false,
                },
                {
                    path: "",
                    name: "course.RXDcourse",
                    value: "rxd",
                    show: true,
                },


            ],
            activeCoin: "nexa",
            params: {
                coin: "nexa",
            },
            activeItem: {
                path: "nexaAccess",
                value: "nexa",
                label: "nexa",
                img: require("../../assets/img/currency-nexa.png"),
                imgUrl:  `${this.$baseApi}img/nexa.png`,
                show: true,
                name: "course.NEXAcourse",
                amount: 10000,
                tcp: "stratum+tcp://nexa.m2pool.com:33333",
                ssl: "stratum+ssl://nexa.m2pool.com:33335",
                Step1: "course.Step1",
                accountContent1: "course.accountContent1",
                accountContent2: "course.accountContent2",
                Step2: "course.Step2",
                bindWalletText1: "course.bindWalletText1",
                bindWalletText2: "course.bindWalletText2",
                walletAddr: "https://nexa.org/",
                bindWalletText3: "course.bindWalletText3",
                bindWalletText4: "course.bindWalletText4",
                exchangeAddr1: "https://www.mxc.com/",
                exchangeAddr1Label: "MEXC",
                exchangeAddr2: "https://www.coinex.com/zh-hans/",
                exchangeAddr2Label: "CoinEx",
                bindWalletText5: "course.bindWalletText5",
                bindWalletText6: "course.bindWalletText6",
                bindWalletText7: "course.bindWalletText7",
                bindWalletText8: "course.bindWalletText8",
                Step3: "course.Step3",
                miningIncome1: "course.miningIncome1",
                miningIncome2: "course.miningIncome2",
                GPU: "bzminer、lolminer、Rigel、WildRig",
                ASIC: "course.dragonBall",
                Step4: "course.Step4",
                parameter: "course.parameter",
                parameter2: "course.parameter2",
                parameter3: "course.parameter3",
                parameter4: "course.parameter4",
                parameter5: "course.parameter5",
                parameter6: "course.parameter6",
                parameter7: "course.parameter7",
                parameter8: "course.parameter8",




            },
            pageTitle: "course.NEXAcourse",
            currencyList: [
                {
                    path: "nexaAccess",
                    value: "nexa",
                    label: "nexa",
                    img: require("../../assets/img/currency-nexa.png"),
                    imgUrl:  `${this.$baseApi}img/nexa.png`,
                    show: true,
                    name: "course.NEXAcourse",
                    amount: 10000,
                    tcp: "stratum+tcp://nexa.m2pool.com:33333",
                    ssl: "stratum+ssl://nexa.m2pool.com:33335",
                    Step1: "course.Step1",
                    accountContent1: "course.accountContent1",
                    accountContent2: "course.accountContent2",
                    Step2: "course.Step2",
                    bindWalletText1: "course.bindWalletText1",
                    bindWalletText2: "course.bindWalletText2",
                    walletAddr: "https://nexa.org/",
                    bindWalletText3: "course.bindWalletText3",
                    bindWalletText4: "course.bindWalletText4",
                    exchangeAddr1: "https://www.mxc.com/",
                    exchangeAddr1Label: "MEXC",
                    exchangeAddr2: "https://www.coinex.com/zh-hans/",
                    exchangeAddr2Label: "CoinEx",
                    bindWalletText5: "course.bindWalletText5",
                    bindWalletText6: "course.bindWalletText6",
                    bindWalletText7: "course.bindWalletText7",
                    bindWalletText8: "course.bindWalletText8",
                    Step3: "course.Step3",
                    miningIncome1: "course.miningIncome1",
                    miningIncome2: "course.miningIncome2",
                    GPU: "bzminer、lolminer、Rigel、WildRig",
                    ASIC: "course.dragonBall",
                    Step4: "course.Step4",
                    parameter: "course.parameter",
                    parameter2: "course.parameter2",
                    parameter3: "course.parameter3",
                    parameter4: "course.parameter4",
                    parameter5: "course.parameter5",
                    parameter6: "course.parameter6",
                    parameter7: "course.parameter7",
                    parameter8: "course.parameter8",

                },
                {
                    path: "grsAccess",
                    value: "grs",
                    label: "grs",
                    imgUrl:`${this.$baseApi}img/grs.svg`,
                    show: true,
                    name: "course.GRScourse",
                    amount: 1,
                    tcp: "",
                    ssl: ""


                },
                {
                    path: "monaAccess",
                    value: "mona",
                    label: "mona",
                    imgUrl: `${this.$baseApi}img/mona.svg`,
                    name: "course.MONAcourse",
                    amount: 1,
                    tcp: "",
                    show: true,
                    ssl: ""
                },
                {
                    path: "dgbsAccess",
                    value: "dgbs",
                    // label: "dgb-skein-pool1",
                    label: "dgb(skein)",
                    img: require("../../assets/img/currency/DGB.svg"),
                    imgUrl: `${this.$baseApi}img/dgb.svg`,
                    show: false,
                    name: "course.dgbsCourse",
                    amount: 1,
                    tcp: "",
                    ssl: ""
                },
                {
                    path: "dgbqAccess",
                    value: "dgbq",
                    // label: "dgb(qubit-pool1)",
                    label: "dgb(qubit)",
                    imgUrl: `${this.$baseApi}img/dgb.svg`,
                    show: false,
                    name: "course.dgbqCourse",
                    amount: 1,
                    tcp: "",
                    ssl: ""
                },
                {
                    path: "dgboAccess",
                    value: "dgbo",
                    // label: "dgb-odocrypt-pool1",
                    label: "dgb(odocrypt)",
                    imgUrl: `${this.$baseApi}img/dgb.svg`,
                    show: false,
                    name: "course.dgboCourse",
                    amount: 1,
                    tcp: "",
                    ssl: ""
                },
                {
                    path: "rxdAccess",
                    value: "rxd",
                    label: "radiant",
                    img: require("../../assets/img/currency/rxd.png"),
                    imgUrl: `${this.$baseApi}img/rxd.png`,
                    show: true,
                    name: "course.RXDcourse",
                    amount: 100,
                    tcp: "stratum+tcp://rxd.m2pool.com:33370",
                    ssl: "stratum+ssl://rxd.m2pool.com:33375",
                    Step1: "course.Step1",
                    accountContent1: "course.accountContent1",
                    accountContent2: "course.accountContent2",
                    Step2: "course.Step2",
                    bindWalletText1: "course.bindWalletText1",
                    bindWalletText2: "course.bindWalletText2",
                    walletAddr: "https://radiantblockchain.org/",
                    bindWalletText3: "course.bindWalletText3",
                    bindWalletText4: "course.bindWalletText4",
                    exchangeAddr1: "https://www.mxc.com/",
                    exchangeAddr1Label: "MEXC",
                    exchangeAddr2: "https://www.coinex.com/zh-hans/",
                    exchangeAddr2Label: "CoinEx",
                    bindWalletText5: "course.bindWalletText5",
                    bindWalletText6: "course.bindWalletText6",
                    bindWalletText7: "course.bindWalletText7",
                    bindWalletText8: "course.bindWalletText8",
                    Step3: "course.Step3",
                    miningIncome1: "course.rxdIncome1",
                    miningIncome2: "course.miningIncome2",
                    GPU: "lolminer",
                    ASIC: "course.dragonBallA11Move",
                    Step4: "course.Step4",
                    parameter: "course.parameter",
                    parameter2: "course.parameter2",
                    parameter3: "course.parameter3",
                    parameter4: "course.parameter4",
                    parameter5: "course.parameter5",
                    parameter6: "course.parameter6",
                    parameter7: "course.parameter7",
                    parameter8: "course.parameter8",


                },
                // {
                //     path: "enxAccess",
                //     value: "enx",
                //     label: "Entropyx(enx)",
                //     imgUrl:`${this.$baseApi}img/enx.svg`,
                //     show: true,
                //     name: "course.ENXcourse",
                //     amount: 5000,
                //     tcp: "",
                //     ssl: ""


                // },
                {
                    path: "alphminingPool",
                    value: "alph",
                    label: "Alephium(alph)",
                    imgUrl:`${this.$baseApi}img/alph.svg`,
                    show: true,
                    name: "course.alphCourse",
                    amount: 1,
                    tcp: "",
                    ssl: ""


                },


            ],
            currencyPath:  `${this.$baseApi}img/nexa.png`,
            openAPI: true,
            imgUrl:  `${this.$baseApi}img/nexa.png`,
            activeName: "1",
            currentRoutePath:"",

            listParams:{
                type:"1",//挖矿教程
                lang:this.$i18n.locale,
                pageNum:1,
                pageSize:50
              },
              tutorialLoading:false,
              navList:[],
              DetailsParams:{
                id:"",
                lang:this.$i18n.locale,
              },
              info:"",
              activeId:"",
              contentShow:false
        }
    },

    watch: {
        '$route': {
          immediate: true,
          handler(to) {
            
            
            this.currentRoutePath =to.path.split(`/`)[3]
           
     
            //  this.activeItem = this.currencyList.find(item => { return item.path == this.currentRoutePath })
            //  this.$addStorageEvent(1, `activeItem`, JSON.stringify( this.activeItem))
            
          
          
            }
        }
      },
    
    mounted() {
        // 优先级：路由参数 > localStorage > 默认
        let coinFromRoute = this.$route.params && this.$route.params.coin;
        let localActiveCoin = localStorage.getItem('activeCoin') ? JSON.parse(localStorage.getItem('activeCoin')) : null;
        let targetCoin = coinFromRoute || localActiveCoin || (this.currencyList[0] && this.currencyList[0].value);

        this.activeCoin = targetCoin;
        this.activeItem = this.currencyList.find(item => item.value === targetCoin) || this.currencyList[0];
        this.params.coin = targetCoin;

        // 如果是路由参数，记忆到localStorage
        if (coinFromRoute) {
            localStorage.setItem('activeCoin', JSON.stringify(coinFromRoute));
        }

        this.fetchAllList(this.listParams);
    },
    methods: {
        async fetchAllList(params){
            this.setLoading('tutorialLoading', true);
          const res = await documentsList(params)
          console.log(res,"res");
          
          if (res && res.code === 200) {
            // 按照id升序排序，确保最小id在最上面
            const sortedRows = (res.rows || []).slice().sort((a, b) => Number(a.id) - Number(b.id))
            this.navList = sortedRows

            const item = this.navList.find(item => { return item.coin == this.params.coin })
          
            
            if (item && item.coin) {
                 this.DetailsParams.id= item.id
                 this.DetailsParams.coin = item.coin
                this.clickJump(item)
               
            }else if (this.$route.query.id) {
                this.DetailsParams.id = this.$route.query.id
               
            }else{
                this.DetailsParams.id = this.navList[0] && this.navList[0].id;                
            }
            
            if (this.DetailsParams.id) {
                this.fetchProblemDetails(this.DetailsParams)
            }
            
          }
        
        },

        async fetchProblemDetails(params) {
            this.setLoading('tutorialLoading', true);
            const res = await findDataInfo(params)
            if(res && res.code === 200){
           
                this.info = res.data.content || ""
                this.activeId = res.data.id
                this.contentShow = true
            }
            this.setLoading('tutorialLoading', false);
        },

        backMenu(){
            this.contentShow = false
        },



        // isActiveRoute(item) {
        //     // 直接使用完整的路径进行比较
        //     return this.currentRoutePath.includes(`AccessMiningPool/${item.path}`);
        //   },
        toggleDropdown(e) {
            if (!e) return;
            const menuItem = e.currentTarget;
            const dropdown = menuItem.querySelector(".dropdown");
            const arrow = menuItem.querySelector(".arrow");

            if (dropdown) {
                dropdown.classList.toggle("show");
                arrow?.classList.toggle("up");
            }
        },
        changeMenuName(e, item) {

            console.log(`走移动端了？`);
            

            if (!e) return;
            if (!item.path) return; // 添加路径检查

            e.stopPropagation(); // 阻止事件冒泡

           this.currentRoutePath = item.path
            // 更新 activeItem
            this.activeItem = item;
            this.activeCoin = item.value;
            this.$addStorageEvent(1, 'activeItem', JSON.stringify(item));
            const menuItem = document.getElementById("menu1");
            if (!menuItem) return;
            const dropdown = menuItem.querySelector(".dropdown");
            const arrow = menuItem.querySelector(".arrow");
            dropdown?.classList.remove("show");
            arrow?.classList.remove("up");

            this.$addStorageEvent(1, "activeItemCoin", JSON.stringify(item));


            this.$addStorageEvent(1, `activeCoin`, JSON.stringify(item.value))
            // 路由跳转
            if (item.path) {
                this.$router.push(`/${this.$i18n.locale}/AccessMiningPool/${item.path}`);
            }


        },


        clickJump(item) {
           
            this.DetailsParams.id = item.id
            this.DetailsParams.coin = item.coin
            this.activeCoin = item.coin
            localStorage.setItem('activeCoin', JSON.stringify(this.activeCoin))
            this.fetchProblemDetails(this.DetailsParams)
          
            // if (!item.path) return; // 添加路径检查
            // this.activeCoin = item.value
            // this.pageTitle = item.name
            // this.imgUrl = item.imgUrl
            // this.$addStorageEvent(1, `activeCoin`, JSON.stringify(item.value))
            // // this.$router.push(item.url)
            // const lang = this.$i18n.locale;
            // this.$router.push(`/${lang}/AccessMiningPool/${item.path}`).catch(err => {
            //     if (err.name !== 'NavigationDuplicated') {
            //         console.error('Navigation failed:', err);
            //     }
            // });



        },
        clickCopy(value) {
            navigator.clipboard.writeText(value).then(() => {
                this.$message({
                    showClose: true,
                    message: this.$t(`personal.copySuccessful`),
                    type: 'success',

                });
            }).catch(err => {
                console.log('复制失败', err);
                this.$message({
                    showClose: true,
                    message: this.$t(`personal.copyFailed`),
                    type: 'error',

                });
            });
        },
        clickCurrency(item) {


            this.currencyPath = item.imgUrl
            this.params.coin = item.value
            this.activeItem = item




        },
        handelCurrencyLabel(coin) {
            let obj = this.currencyList.find(item => item.value == coin)
            if (obj) {
                return obj.label
            } else {
                return ""
            }


        }
    },


}