import { getLuck, getBlockInfo } from "../../api/home"
import { Debounce } from "../../utils/publicMethods";

export default {
    data() {
        return {
            luckData: {
                luck3d: "0",
                luck7d: "0",
                luck30d: 0,
                luck90d: 0,
            },
            BlockInfoData: [
                // {
                //     height:"847545",
                //     date:"2024-06-12 09:11:57",
                //     hash:"00000…9392577",
                //     reward:"3.12500000",
                //     fees:"0.40962024",
                // },
                // {
                //     height:"847545",
                //     date:"2024-06-12 09:11:57",
                //     hash:"00000…9942577",
                //     reward:"3.12500000",
                //     fees:"0.40962024",
                // },  {
                //     height:"847545",
                //     date:"2024-06-12 09:11:57",
                //     hash:"00000…9925757",
                //     reward:"3.12500000",
                //     fees:"0.40962024",
                // },  {
                //     height:"847545",
                //     date:"2024-06-12 09:11:57",
                //     hash:"00000…9925677",
                //     reward:"3.12500000",
                //     fees:"0.40962024",
                // },  {
                //     height:"847545",
                //     date:"2024-06-12 09:11:57",
                //     hash:"00000…9925877",
                //     reward:"3.12500000",
                //     fees:"0.40962024",
                // },  {
                //     height:"847545",
                //     date:"2024-06-12 09:11:57",
                //     hash:"00000…9928577",
                //     reward:"3.12500000",
                //     fees:"0.40962024",
                // },
                // {
                //     height:"847545",
                //     date:"2024-06-12 09:11:57",
                //     hash:"00000…99S28577",
                //     reward:"3.12500000",
                //     fees:"0.40962024",
                // },
                // {
                //     height:"847545",
                //     date:"2024-06-12 09:11:57",
                //     hash:"00000…A99S28577",
                //     reward:"3.12500000",
                //     fees:"0.40962024",
                // },
                // {
                //     height:"847545",
                //     date:"2024-06-12 09:11:57",
                //     hash:"00000…A9W9S28577",
                //     reward:"3.12500000",
                //     fees:"0.40962024",
                // },
                // {
                //     height:"847545",
                //     date:"2024-06-12 09:11:57",
                //     hash:"00000…A9W9SQ28577",
                //     reward:"3.12500000",
                //     fees:"0.40962024",
                // },


            ],
            currentPage: 1,
            currencyList: [
                // {
                //     value: "nexa",
                //     label: "nexa",
                //     img: require("../../assets/img/currency-nexa.png"),
                //     imgUrl: "",

                // },
                // {
                //     value: "grs",
                //     label: "grs",
                //     img: require("../../assets/img/currency/grs.svg"),
                //     imgUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/258.png",

                // },
                // {
                //     value: "mona",
                //     label: "mona",
                //     img: require("../../assets/img/currency/mona.svg"),
                //     imgUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/213.png",
                // },
                // {
                //     value: "dgb_skein",
                //     label: "dgb-skein-pool1",
                //     img: require("../../assets/img/currency/DGB.svg"),
                //     imgUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/109.png",
                // },
                // {
                //     value: "dgb_qubit",
                //     label: "dgb-qubit-pool1",
                //     img: require("../../assets/img/currency/DGB.svg"),
                //     imgUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/109.png",
                // },
                // {
                //     value: "dgb_odo",
                //     label: "dgb-odocrypt-pool1",
                //     img: require("../../assets/img/currency/DGB.svg"),
                //     imgUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/109.png",
                // },
                // // {
                // //     value: "dgb2_odo",
                // //     label: "dgb-odocrypt-pool2",
                // //     img: require("../../assets/img/currency/DGB.svg"),
                // //     imgUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/109.png",
                // // },
                // {
                //     value: "dgb_qubit_a10",
                //     label: "dgb-qubit-pool2",
                //     img: require("../../assets/img/currency/DGB.svg"),
                //     imgUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/109.png",
                // },
                // {
                //     value: "dgb_skein_a10",
                //     label: "dgb-skein-pool2",
                //     img: require("../../assets/img/currency/DGB.svg"),
                //     imgUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/109.png",
                // },
                // {
                //     value: "dgb_odo_b20",
                //     label: "dgb-odoscrypt-pool3",
                //     img: require("../../assets/img/currency/DGB.svg"),
                //     imgUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/109.png",
                // },

            ],
            BlockInfoParams: {
                coin: "nexa",
                limit: 10,
                page: 1,
            },
            params: {
                coin: "nexa",
            },
            customColor: '#C1A1FE',
            weekColor: '#F6C12B',
            monthColor: '#7DD491',
            MarchColor: '#F94280',
            ItemActive: "nexa",
            reportBlockLoading: false,
            totalSize: 0,
            LuckDataLoading: false,
            currencyPath: `${this.$baseApi}img/nexa.png`,
            transactionFeeList: [//没有交易费

                // {
                //     label:"grs",
                //     coin: "grs",
                //     feeShow: false,
                // },
                {
                    label: "mona",
                    coin: "mona",
                    feeShow: false,
                },
                {

                    label: "dgb(skein)",
                    coin: "dgbs",
                    feeShow: false,
                },
                {

                    coin: "dgbq",
                    label: "dgb(qubit)",
                    feeShow: false,
                },
                {

                    coin: "dgbo",
                    label: "dgb(odocrypt)",
                    feeShow: false,
                },


            ],
            FeeShow: true,
            activeItemCoin: {
                value: "nexa",
                label: "nexa",
                imgUrl: `${this.$baseApi}img/nexa.png`,

            },
            isInternalChange: false



        }
    },
    watch: {
        activeItemCoin: {
            handler(newVal) {
                // 防止无限循环
                if (this.isInternalChange) {
                    return;
                }
                this.handleActiveItemChange(newVal);
            },
            deep: true
        }
    },
    mounted() {


        if (this.$route.query.coin) {
            this.ItemActive = this.$route.query.coin
            this.currencyPath = this.$route.query.imgUrl
            this.params.coin = this.$route.query.coin
            this.BlockInfoParams.coin = this.$route.query.coin
            this.ItemActive = this.$route.query.coin
            this.handelCoinLabel(this.$route.query.coin)
        }

        this.getLuckData(this.params)
        this.getBlockInfoData(this.BlockInfoParams)

        this.registerRecoveryMethod('getLuckData', this.params);
        this.registerRecoveryMethod('getBlockInfoData', this.BlockInfoParams);

        let value = localStorage.getItem("activeItemCoin")
        this.activeItemCoin = JSON.parse(value)
        this.currencyList = JSON.parse(localStorage.getItem("currencyList"))
        window.addEventListener("setItem", () => {
            this.currencyList = JSON.parse(localStorage.getItem("currencyList"))
            let value = localStorage.getItem("activeItemCoin")
            this.activeItemCoin = JSON.parse(value)
        });


    },
    methods: {
        // async getLuckData(params) {
        //     this.LuckDataLoading = true
        //     const data = await getLuck(params)
        //     if (data && data.code == 200) {
        //         this.luckData= data.data
        //     }


        //     this.LuckDataLoading = false

        // },

        getLuckData: Debounce(async function (params) {
            this.setLoading('LuckDataLoading', true);
            const data = await getLuck(params)
            if (data && data.code == 200) {
                this.luckData = data.data
            }


            this.setLoading('LuckDataLoading', false);
        }, 200),
        // async getBlockInfoData(params) {
        //     this.reportBlockLoading=true
        //     const data = await getBlockInfo(params)
        //     if (!data) {
        //         this.reportBlockLoading=false
        //     }
        //     this.totalSize = data.total
        //     this.BlockInfoData = data.rows
        //     this.BlockInfoData.forEach((item,index)=>{
        //         item.date =  `${item.date.split("T")[0]} ${item.date.split("T")[1].split(`.`)[0]}`
        //     })
        //     // this.currentPage = 1
        //     // console.log(data,"获取币种信息");
        //     this.reportBlockLoading=false
        // },
        getBlockInfoData: Debounce(async function (params) {

            this.setLoading('reportBlockLoading', true);
            const data = await getBlockInfo(params)
            if (!data) {
                this.setLoading('reportBlockLoading', false);
            }
            this.totalSize = data.total
            this.BlockInfoData = data.rows
            this.BlockInfoData.forEach((item, index) => {
                item.date = `${item.date.split("T")[0]} ${item.date.split("T")[1].split(`.`)[0]}`
            })
            // this.currentPage = 1
            // console.log(data,"获取币种信息");
            this.setLoading('reportBlockLoading', false);

        }, 200),
        handleActiveItemChange(item) {
            this.currencyPath = item.imgUrl
            this.params.coin = item.value
            this.BlockInfoParams.coin = item.value
            this.ItemActive = item.value
            this.getBlockInfoData(this.BlockInfoParams)
            this.getLuckData(this.params)
            this.handelCoinLabel(item.value)
        },

        clickCurrency(item) {
            if (!item) return;
            // 设置标记，防止触发 watch

            this.luckData = {}

            this.isInternalChange = true;
            this.activeItemCoin = item;
            this.$addStorageEvent(1, `activeItemCoin`, JSON.stringify(item))

            // 在下一个事件循环中重置标记
            this.$nextTick(() => {
                this.isInternalChange = false;
            });

            // 直接调用处理方法
            this.handleActiveItemChange(item);

        },
        clickItem(item) {

            switch (this.ItemActive) {
                case `nexa`:
                    window.open(`https://explorer.nexa.org/block-height/${item.height}`)
                    break;
                case `grs`:
                    window.open(`https://chainz.cryptoid.info/grs/block.dws?${item.height}.htm`)
                    break;
                case `mona`:
                    window.open(`https://mona.insight.monaco-ex.org/insight/block/${item.hash}`)
                    break;
                case `rxd`:
                    window.open(`https://explorer.radiantblockchain.org/block-height/${item.height}`)
                    break;
                case `enx`:
                    // window.open(`https://explorer.entropyx.org/blocks/${item.hash}`)
                    break;
                case `alph`:
                    window.open(`https://explorer.alephium.org/blocks/${item.hash}`)
                    break;
                case `xmr`:
                    window.open(`https://localmonero.co/blocks/block/${item.height}`)
                    break;
                    case `sha3x`:
                        window.open(`https://explore.tari.com/blocks/${item.height}`)
                        break;
                    

                default:
                    break;
            }

            if (this.ItemActive.includes("dgb")) {
                window.open(`https://chainz.cryptoid.info/dgb/block.dws?${item.height}.htm`)
            }

        },
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            this.BlockInfoParams.limit = val
            this.BlockInfoParams.page = 1
            this.currentPage = 1
            this.getBlockInfoData(this.BlockInfoParams)
        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.BlockInfoParams.page = val
            this.getBlockInfoData(this.BlockInfoParams)
        },
        handelCoinLabel(coin) {
            let obj = {
                coin: "",
            }

            if (coin) {
                obj = this.transactionFeeList.find(item => item.coin == coin)
                this.FeeShow = false
            }
            if (!obj) {
                this.FeeShow = true
            }

        },
        handelLabel(coin) {
            if (coin.includes("dgb")) {
                return "dgb"
            } else {
                return coin
            }

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


        }
    },

}