import * as echarts from "echarts";
import { handelLineType,dataLength, chartsWidth, emptyData,handelSwitchs, fetchBtcPrice, handelZoom, fetchEthPrice, handelProfitrate, handelWatermark, handelSmaData, getDateArray } from "../../util/processingData"
export default {
    data() {
        return {
            lengthTimer:null,
            arhLoading: false,
            option: {
                legend: {
                    right: 100,
                    //处理legengd过长
                    // formatter: function (name) {

                    //     return echarts.format.truncateText(name, 60, '14px Microsoft Yahei', '…');
                    // },
                },
                tooltip: {
                    trigger: "axis",
                    //解决tooltip显示不全问题1 
                    confine: true,
                    textStyle: {
                        align: "left",
                    },
                    animation: false,
                    formatter: function (params) {
                        const date = new Date(params[0].value[0]);
                        const year = date.getFullYear();
                        const month = date.getMonth() + 1;
                        const day = date.getDate();
                        let time =  `${year}-${month}-${day}`;
                        var res =time                       

                        for (let i = 0; i < params.length; i++) {
                            res += `</br>${params[i].marker} ${params[i].seriesName} &nbsp&nbsp&nbsp&nbsp <span style="font-weight: bold">${params[i].value[1]}</span>`

                        }
                        return res;
                    },
                    axisPointer: {
                        animation: false,
                        snap: true,
                        label: {
                            precision: 2, //坐标轴保留的位数
                        },
                        type: "cross", //cross shadow
                        crossStyle: {
                            //十字轴横线
                            // opacity: "0",
                            width: 0.5,
                        },
                        lineStyle: {
                            // opacity: 0,
                        },
                    },
                },

                xAxis: {
                    type: "time",
                    boundaryGap: false,
                    data: [],
                    axisTick: {
                        //去除刻度
                        show: false,
                    },
                    axisLine: {
                        //去除轴线
                        show: false,
                    },
                },
                yAxis: [
                    {
                        // name: this.$t(`home.figure`),
                        position: "left",
                        type: "value",
                        axisLabel: {
                            // formatter:function(value){

                            //     return `${value/1000} k`
                            // }
                        },
                     
                    },
                    {
                        position: "right",
                        // type: "log",
                        splitNumber: "5",
                        show: true,
                    },
                    {
                        position: "right",
                        // type: "log",
                        splitNumber: "5",
                        show: false,
                    },
                    {
                        position: "right",
                        // type: "log",
                        splitNumber: "5",
                        show: false,
                    },
                ],
                dataZoom: [
                    {
                        type: "inside",
                        start: 10,
                        end: 100,
                        maxSpan: 100,
                        minSpan: 2,
                        animation: false,
                    },
                    {
                        start: 0,
                        end: 20,
                        // showDetail: false,
                    },
                ],
                series: [
                    {
                        name: "arh999",
                        type: "line",
                        smooth: false, //线条是否圆滑
                        symbol: "circle",
                        symbolSize: 5,
                        showSymbol: false,
                        itemStyle: {
                            color: "#f7931a",
                            borderColor: "rgba(221,220,107,0.1)",
                            borderWidth: 12,
                        },
                        lineStyle: {
                            //线条样式#f7931a
                            color: "#f7931a",
                            width: "1",
                        },
                        data: [],
                        markArea: {
                            silent: true,
                            data: [
                                [
                                    {
                                        yAxis: "0.45",
                                        itemStyle: {
                                            color: "#fde7e7",
                                        },
                                    },
                                    {
                                        yAxis: "0",
                                    },
                                ],

                            ],
                        },
                        yAxisIndex: 0,

                    },
                    {
                        name: "arh99x",
                        type: "line",
                        smooth: false, //线条是否圆滑
                        symbol: "circle",
                        symbolSize: 5,
                        showSymbol: false,
                        itemStyle: {
                            color: "#306f67",
                            borderColor: "rgba(221,220,107,0.1)",
                            borderWidth: 12,
                        },
                        lineStyle: {
                            //线条样式#e43961
                            color: "#306f67",
                            width: "1",
                        },
                        yAxisIndex: 0,
                        data: [],
                    },
                    {
                        name: "Price[BTC]",
                        type: "line",
                        smooth: false, //线条是否圆滑
                        symbol: "circle",
                        symbolSize: 5,
                        showSymbol: false,
                        itemStyle: {
                            color: "#888888",
                            borderColor: "rgba(221,220,107,0.1)",
                            borderWidth: 12,
                        },
                        lineStyle: {
                            //线条样式
                            color: "#888888",
                            width: "1",
                        },
                        yAxisIndex: 1,
                        data: [],
                        // markLine: {
                        //     symbol: ["none", "none"], //去掉箭头
                        //     itemStyle: {
                        //         normal: {
                        //             lineStyle: { type: "solid", color: "rgba(0,0,0,0.01)" },
                        //         },
                        //     },
                        //     //  lineStyle:{
                        //     //   opacity:0
                        //     // },
                        //     label: {
                        //         formatter: "{b}",
                        //         fontSize: 10,
                        //         color: "rgba(0,0,0,0.8)",
                        //         align: "left",
                        //     },
                        //     data: [
                        //         {
                        //             name: "$0",
                        //             yAxis: 0,
                        //         },
                        //         {
                        //             name: "$10k",
                        //             yAxis: 10000,
                        //         },
                        //         {
                        //             name: "$20K",
                        //             yAxis: 20000,
                        //         },
                        //         {
                        //             name: "$30K",
                        //             yAxis: 30000,
                        //         },
                        //         {
                        //             name: "$40K",
                        //             yAxis: 40000,
                        //         },
                        //         {
                        //             name: "$50K",
                        //             yAxis: 50000,
                        //         },
                        //         {
                        //             name: "$60K",
                        //             yAxis: 60000,
                        //         },
                        //         {
                        //             name: "$70K",
                        //             yAxis: 70000,
                        //         },
                        //         {
                        //             name: "$80K",
                        //             yAxis: 80000,
                        //         },
                        //         {
                        //             name: "$90K",
                        //             yAxis: 90000,
                        //         },
                        //         {
                        //             name: "$100K",
                        //             yAxis: 100000,
                        //         },

                        //     ],
                        // },
                    },
                    // {
                    //     name: "Price [ETH]",
                    //     type: "line",
                    //     smooth: false, //线条是否圆滑
                    //     symbol: "circle",
                    //     symbolSize: 5,
                    //     showSymbol: false,
                    //     itemStyle: {
                    //         color: "#627eea",
                    //         borderColor: "rgba(221,220,107,0.1)",
                    //         borderWidth: 12,
                    //     },
                    //     lineStyle: {
                    //         //线条样式
                    //         color: "#627eea",
                    //         width: "1",
                    //     },
                    //     yAxisIndex: 2,
                    //     data: [],
                    // },
                    {
                        name: "定投线",
                        type: "line",
                        smooth: false, //线条是否圆滑
                        symbol: "circle",
                        symbolSize: 5,
                        showSymbol: false,
                        itemStyle: {
                            color: "#2c2f90",
                            borderColor: "rgba(221,220,107,0.1)",
                            borderWidth: 12,
                        },
                        lineStyle: {
                            //线条样式#e43961
                            color: "#2c2f90",
                            width: "2",
                        },
                        yAxisIndex: 0,
                        data: [],
                    },
                    {
                        name: "抄底线",
                        type: "line",
                        smooth: false, //线条是否圆滑
                        symbol: "circle",
                        symbolSize: 5,
                        showSymbol: false,
                        itemStyle: {
                            color: "#f5839e",
                            borderColor: "rgba(221,220,107,0.1)",
                            borderWidth: 12,
                        },
                        lineStyle: {
                            //线条样式#e43961
                            color: "#f5839e",
                            width: "2",
                        },
                        yAxisIndex: 0,
                        data: [],
                    },

                ],
            },
            dayOption: {
                legend: {
                    right: 100,
                    formatter: function (name) {
                        return name;
                    },
                },
                tooltip: {
                    trigger: "axis",
                    //解决tooltip显示不全问题1 
                    confine: true,
                    textStyle: {
                        align: "left",
                    },
                    animation: false,
                    // formatter: function (params) {
                    //     var res = params[0].axisValueLabel;

                    //     for (let i = 0; i <= params.length - 1; i++) {
                    //         res += `</br>${params[i].marker} <span style="font-weight: bold">${params[i].value[1]}</span> ${params[i].seriesName}`;
                    //     }
                    //     return res;
                    // },
                    axisPointer: {
                        animation: false,
                        snap: true,
                        label: {
                            precision: 2, //坐标轴保留的位数
                        },
                        type: "cross", //cross shadow
                        crossStyle: {
                            //十字轴横线
                            // opacity: "0",
                            width: 0.5,
                        },
                        lineStyle: {
                            // opacity: 0,
                        },
                    },
                },

                xAxis: {
                    // type: "time",
                    boundaryGap: false,
                    data: [],
                    axisTick: {
                        //去除刻度
                        show: false,
                    },
                    axisLine: {
                        //去除轴线
                        show: false,
                    },
                    axisLabel: {
                        formatter: function (value) {
                            let hours = value.split(" ")[1].split(":")[0];
                            let min = value.split(" ")[1].split(":")[1];
                            return `${hours}:${min}`;
                        },
                    },
                },
                yAxis: [
                    {
                        // name: this.$t(`home.figure2`),
                        position: "left",
                        type: "value",
                        // min: `dataMin`,
                        // max: `dataMax`,
                        // axisLabel:{
                        //     formatter:function(value){
                        //         let data = (value/10000).toFixed(3)
                        //         return `${data} W`
                        //     }
                        // }
                        min:`dataMin`,    
                        max:`dataMax`,      
                    },
                    {
                        position: "right",
                        type: "value",
                        // min: `dataMin`,
                        // max: `dataMax`,
                        show: true,
                    },
                    {
                        position: "right",
                        type: "log",
                        min: `dataMin`,
                        max: `dataMax`,
                        // splitNumber: "5",
                        show: false,
                    },
                ],
                dataZoom: [
                    {
                        type: "inside",
                        start: 10,
                        end: 100,
                        maxSpan: 100,
                        minSpan: 2,
                        animation: false,
                    },
                    {
                        start: 0,
                        end: 20,
                        // showDetail: false,
                    },
                ],
                series: [
                    {
                        name: "arh99",
                        type: "line",
                        smooth: false, //线条是否圆滑
                        symbol: "circle",
                        symbolSize: 5,
                        showSymbol: false,
                        itemStyle: {
                            color: "#f7931a",
                            borderColor: "rgba(221,220,107,0.1)",
                            borderWidth: 12,
                        },
                        lineStyle: {
                            //线条样式
                            color: "#f7931a",
                            width: "1",
                        },
                        data: [],
                    },
                    {
                        name: "arh99x",
                        type: "line",
                        smooth: false, //线条是否圆滑
                        symbol: "circle",
                        symbolSize: 5,
                        showSymbol: false,
                        itemStyle: {
                            color: "#e43961",
                            borderColor: "rgba(221,220,107,0.1)",
                            borderWidth: 12,
                        },
                        lineStyle: {
                            //线条样式
                            color: "#e43961",
                            width: "1",
                        },
                        data: [],
                        yAxisIndex: 0,
                    },


                ],
            },
            timeParams: {
                req: "arh99",
                start: "",
                end: ""
            },
            dayParams: {
                req: "arh99day",
                start: "",
                end: ""
            },
            arhData: [],
            arhDayData: [],
            radius: "BTC",
            timer: "",
            switch3: "",
            btcFlag: null,
            ethFlag: null,
            fatherSmaData: [],
            start: "",
            end: "",
            sma: "",
            zoomStart: 0,
            zoomEnd: 0,
        }
    },
    watch: {
        radius: {
            handler(val) {
                // if (val=="BTC") {
                //     this.radius = "BTC"
                // }else if (val=="ETH") {
                //     this.radius = "ETH"
                // }
                this.fetchArh(this.timeParams.req, val)
                this.fetchArhDay(this.dayParams.req, val)
            },
            deep: true,
            immediate: true,
        }

    },
    mounted() {

        this.radius = localStorage.getItem("radius");
        //监听localstorage变化
        window.addEventListener("setItem", () => {
            this.radius = localStorage.getItem("radius");
        });
        if (this.timer) {
            clearInterval(this.timer)
        }
        //5分钟请求一次页面
        this.timer = setInterval(() => {
            this.fetchArh(this.timeParams.req, this.radius)
            this.fetchArhDay(this.dayParams.req, this.radius)
        }, 300000)

        this.switch3 = handelSwitchs(this.option, this.switch3)
        this.ethFlag = JSON.parse(localStorage.getItem("ethFlag"));
        this.btcFlag = JSON.parse(localStorage.getItem("btcFlag"));
        this.$addStorageEvent(1, "zoomStart", this.zoomStart);
        this.$addStorageEvent(1, "zoomEnd", this.zoomEnd);

        window.addEventListener("setItem", () => {
            this.ethFlag = JSON.parse(localStorage.getItem("ethFlag"));
            this.btcFlag = JSON.parse(localStorage.getItem("btcFlag"));
            this.zoomStart = localStorage.getItem("zoomStart")
            this.zoomEnd = localStorage.getItem("zoomEnd")

        })
    },
    methods: {
        //初始化arh999图表
        inChart() {
            if (this.myChart == null) {
                this.myChart = echarts.init(document.getElementById("arhBox"));
            }
            this.option = handelZoom(this.myChart, this.option, this, this.zoomStart, this.zoomEnd)

            this.myChart.setOption(this.option);
            chartsWidth("arhBox", this);
            window.addEventListener("resize", () => {
                if (this.myChart) this.myChart.resize();
            });
        },
        //初始化arh999day图表
        inChartDay() {
            if (this.myChartDay == null) {
                this.myChartDay = echarts.init(document.getElementById("arhDayBox"));
            }
            this.dayOption = handelZoom(this.myChartDay, this.dayOption, this, this.zoomStart, this.zoomEnd)

            this.myChartDay.setOption(this.dayOption);
            chartsWidth("arhDayBox", this);
            window.addEventListener("resize", () => {
                this.myChartDay.resize();
            });
        },
        //请求arh999渲染数据
        async fetchArh(req, radius, start, end, sma) {
            this.arhLoading = true
            const { data } = await this.$API.getMa370(req, radius, start, end, sma)
            this.arhData = data.data
            //空数据处理
            let noData = emptyData(data.data, this.option.series, this.inChart, this)
            if (noData) {
                this.arhLoading = false
                return
            }
            this.start = this.arhData[0].date.split("T")[0]
            this.end = this.arhData[this.arhData.length - 1].date.split("T")[0]
            //添加水印
            this.option = handelWatermark(this.option)
            //根据渲染的数据时间渲染对应的价格线和移动平均线
            this.$nextTick(() => {
                //移动平均线没值 才渲染正常价格数据 
                if (this.start && !this.fatherSmaData[0]) {
                    this.handelSonBtcPrice("BTC", true)
                }
                //点击时间 移动平均线有值 就渲染查询移动平均线
                if (this.fatherSmaData[0] && this.start) {
                    this.handelSonSma(this.fatherSmaData, this.start, this.end)
                }
                //渲染ETH
                if (this.ethFlag) {
                    this.$addStorageEvent(1, "ethFlag", JSON.stringify(this.ethFlag));
                    this.handelSonEthPrice("ETH", this.ethFlag)
                }
            })


            let arh99ValueList = []
            let arh99xValueList = []
            let investmentList = []
            let bottomLine = []
            this.arhData.forEach(item => {
                arh99ValueList.push(Number(item.arh99.toFixed(2)))
                arh99xValueList.push(Number(item.arh99x.toFixed(2)))
                investmentList.push(1.2)
                bottomLine.push(0.45)
            })

            this.option.series[0].data = handelProfitrate(this.arhData, arh99ValueList)
            this.option.series[1].data = handelProfitrate(this.arhData, arh99xValueList)
            this.option.series[3].data = handelProfitrate(this.arhData, investmentList)
            this.option.series[4].data = handelProfitrate(this.arhData, bottomLine)

            this.inChart()
            this.arhLoading = false

             //判定页面条数是否一致
             if (this.lengthTimer) {
                clearTimeout(this.lengthTimer)
            }
            this.lengthTimer = setTimeout(() => {

                this.$addStorageEvent(1, "lengthFlag", dataLength(this.option, this.option.series[0].data.length), );
                this.$addStorageEvent(1, "lengthFlag", dataLength(this.dayOption, this.dayOption.series[0].data.length), );

            }, 20000)


        },
        //请求arh999day渲染数据
        async fetchArhDay(req, radius, start, end) {
            // this.arhLoading = true
            const { data } = await this.$API.getMa370(req, radius, start, end)
            this.arhDayData = data.data
             //空数据处理
             let noData = emptyData(data.data, this.dayOption.series, this.inChartDay, this)
             if (noData) {
                 this.arhLoading = false
                 return
             }
            this.dayOption.xAxis.data.length = 0
            this.dayOption.series[0].data.length = 0
            this.dayOption.series[1].data.length = 0
            //添加水印
            this.dayOption = handelWatermark(this.dayOption)
            this.arhDayData.forEach(item => {
                let year = item.date.split("T")[0]
                let hour = item.date.split("T")[1].split(".")[0]
                let time = `${year} ${hour}`
                this.dayOption.xAxis.data.push(time)
                this.dayOption.series[0].data.push([time, Number(item.arh99.toFixed(4))])
                this.dayOption.series[1].data.push([time, Number(item.arh99x.toFixed(4))])

            })
            this.inChartDay()
            // this.arhLoading = false
        },

        //父级时间
        handelSonTime(start, end) {
            this.timeParams.start = start;
            this.timeParams.end = end;
            this.fetchArh(this.timeParams.req, this.radius, this.timeParams.start, this.timeParams.end, this.sma)
            if (this.radius == "BTC") {
                // this.handelSonEthPrice()
            }
        },
        //点击对数/线性按钮切换
        handelLineSwitch() {
            this.option = handelLineType(this.option)
            this.dayOption = handelLineType(this.dayOption)
            this.switch3 = handelSwitchs(this.option, this.switch3)
            this.myChart.setOption(this.option);
            this.myChartDay.setOption(this.dayOption);

        },
        //父级点击BTC价格按钮
        handelSonBtcPrice(radius, flag) {
            this.btcFlag = flag
            fetchBtcPrice("BTC", this.option.series[2], this.start, this.end, this.inChart, this, this.btcFlag)

        },
        //父级点击ETH价格按钮
        handelSonEthPrice(radius, flag) {
            this.ethFlag = flag
            this.$addStorageEvent(1, "ethFlag", JSON.stringify(this.ethFlag));
            fetchEthPrice("ETH", this.option.series[3], this.start, this.end, this.inChart, this, this.ethFlag)
        },
        //父级选择Sma
        handelSonSma(smaDataList) {
            this.sma = smaDataList
            this.fetchArh(this.timeParams.req, this.radius, this.timeParams.start, this.timeParams.end, this.sma)
            this.$addStorageEvent(1, "sma", this.sma);


            // this.fatherSmaData = smaDataList
            // handelSmaData(smaDataList, this.option.series[2], this.inChart, this.start, this.end,)

        },

    },
    beforeDestroy() {
        // console.log("销毁咯 清除定时器");
        clearInterval(this.timer)
        if (this.lengthTimer) {
            clearTimeout(this.lengthTimer)
        }
    }




}