// packageB/pages/AI-test-skin/history_record/history_record.js
import * as echarts from '../../../components/ec-canvas/echarts';

const app = getApp();
const request = app.request;
const os = app.globalData;
const setting = os.setting;
const ut = require('../../../../utils/util');
let initChart = null;
const option = {
    backgroundColor: 'white',
    tooltip: {},
    grid: {
        // left:'20%',
        top: '30%',
        // right:'10%'
    },
    legend: {
        icon: 'circle',
        itemWidth: 5,
        itemHeight: 5,
        selected: {
            '综合': true,
            '毛孔': false,
            '黑头': false,
            '色斑': false,
            '皱纹': false,
            '痤疮': false,
            '敏感度': false,
            '黑眼圈': false,
        },
        padding: [20, 50],
        data: [
            {
                name: '综合',
                // textStyle: {
                //     backgroundColor: "#999",
                // }
            },
            {
                name: '毛孔',
            }, {
                name: '黑头',
            },
            {
                name: '色斑',
            },
            {
                name: '皱纹',
            },
            {
                name: '痤疮',
            },
            {
                name: '敏感度',
            },
            {
                name: '黑眼圈',
            },
        ]
    },
    xAxis: {
        type: 'category',
        data: ["02/11", "02/16", "02/17"]
    },
    yAxis: {
        type: 'value',
        min: 0,
        max: 100,
        splitNumber: 4,
        interval: 25,
    },
    series: [
        {
            name: '综合',
            type: 'bar',
            itemStyle: {
                color: '#84e1eb',
                borderRadius: 3,
            },
            barWidth: 8,
            data: [5, 20, 36, 10, 10, 20]
        },
        {
            name: '毛孔',
            type: 'line',
            lineStyle: {
                color: "#fbb11f"
            },
            itemStyle: {
                color: '#fbb11f',
            },
            data: [5, 20, 36, 10, 10, 20]
        },
        {
            name: '黑头',
            type: 'line',
            lineStyle: {
                color: "#e9552b"
            },
            itemStyle: {
                color: '#e9552b',
            },
            data: [50, 60, 70]
        },
        {
            name: '色斑',
            type: 'line',
            lineStyle: {
                color: "#38ce87"
            },
            itemStyle: {
                color: '#38ce87',
            },
            data: [50, 55, 60]
        },
        {
            name: '皱纹',
            type: 'line',
            lineStyle: {
                color: "#d67bf0"
            },
            itemStyle: {
                color: '#d67bf0',
            },
            data: [80, 85, 90]
        },
        {
            name: '痤疮',
            type: 'line',
            lineStyle: {
                color: "#f85b95"
            },
            itemStyle: {
                color: '#f85b95',
            },
            data: [30, 35, 40]
        },
        {
            name: '敏感度',
            type: 'line',
            lineStyle: {
                color: "#af78fc"
            },
            itemStyle: {
                color: '#af78fc',
            },
            data: [20, 26, 10]
        },
        {
            name: '黑眼圈',
            type: 'line',
            lineStyle: {
                color: "#688eff"
            },
            itemStyle: {
                color: '#688eff',
            },
            data: [50, 60, 20]
        }
    ]
};
Page({

    /**
     * 页面的初始数据
     */
    data: {
        arr1: ['综合', '毛孔', '黑头', '色斑', '皱纹', '痤疮', '敏感度', '黑眼圈'],
        ec: {
            onInit: function (canvas, width, height) {
                initChart = echarts.init(canvas, null, {
                    width: width,
                    height: height,
                });
                canvas.setChart(initChart);
                return initChart;
            }
        },
        history: [],
        page: 1,
        no_more: 0,
        isLoading: 0,
        is_get: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getHistoryData();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        
    },

    time_sort(data) {
        let arr = this.data.history;
        for (let i = 0; i < data.length; i++) {
            let history = data[i];
            let id = history.id;
            let img_remark = history.img_remark;
            let now = new Date(history['addtime'] * 1000);
            let year = now.getFullYear(); //年
            let month = (now.getMonth() + 1).toString().padStart(2, 0); //月
            let day = (now.getDate()).toString().padStart(2, 0); //日
            let hour = (now.getHours()).toString().padStart(2, 0); //时
            let minute = (now.getMinutes()).toString().padStart(2, 0); //分
            let history_time = `${year}年${month}月`;
            let time = `${year}/${month}/${day} ${hour}:${minute}`;
            let time_obj = {
                history_time,
                time_arr: new Array(),
            };
            let obj = {
                time,
                id,
                img_remark,
            };
            if (arr.length > 0) {
                let index = arr.findIndex(item => {
                    return item['history_time'] === history_time;
                    
                })
                if (index > -1) {
                    arr[index]['time_arr'].push(obj);
                } else {
                    time_obj['time_arr'].push(obj);
                    arr.push(time_obj);
                }
            } else {
                time_obj['time_arr'].push(obj);
                arr.push(time_obj);
            }
        }
        function _sort_by_history_time (property){
            return function(a,b){
                let val1 = a[property];
                let val2 = b[property];
                if(val2 > val1){
                    return 1
                }else if(val2 < val1){
                    return -1
                }else{
                    return 0
                }
            }
        }
        arr.sort(_sort_by_history_time('history_time'));
        return arr;
    },
    bindscrolltolower(){
        console.log(111);
    },

    async getHistoryData() {
        if (this.data.no_more) return false;
        if (this.data.isLoading) return false;
        this.data.isLoading = 1;
        let history = this.data.history;
        let url = `/api/weshop/face/storeSkinface/page`;
        const { data: res } = await request.promiseGet(url, {
            isShowLoading: true,
            data: {
                store_id: setting.stoid,
                user_id: os.user_id,
                page: this.data.page,
                pageSize:20
            }
        });
        // initChart.setOption(option);
        if (res.code == 0 && res.data.pageData.length > 0) {
            history = this.time_sort(res.data.pageData);
            if(res.data.pageData.length >= res.data.total){
                this.setData({
                    no_more: 1,
                })
            }
            this.setData({
                history,
                isLoading: 0,
                is_get: 1,
            })
        } else {
            if (res.data.page > 1) {
                this.setData({
                    history,
                    no_more: 1,
                    isLoading: 0,
                    is_get: 1,
                })
            } else {
                this.setData({
                    history: [],
                    is_get: 1,
                })
            }
        }

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        getApp().check_can_share();
    },
    go_result(e) {
        let id = e.currentTarget.dataset.id;
        let url =`/packageD/pages/AI-test-skin/success_result/success_result?id=${id}`;
        app.goto(url);
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.data.page++;
        this.getHistoryData();
    },

    /**
     * 用户点击右上角分享
     */
     onShareAppMessage: function () {   	  getApp().globalData.no_clear=1

    }
})