const percent = 'percent';
let intFields = ['flowerLength','yield'];
//农事颜色数组
let rongShiColors = ["#548235","#4472C4","#FFD966","#C5E0B4","#F8CBAD",'#3FD0F9','#4962FC','#d223e7','#fe8104','#3FD0F9'];

function getTwoMonthDate(){
    let now = new Date();

    // 生成日期数组
    let dateArray = [];
    for(let i = 0; i < 60; i++) {
        let date = new Date(now.getTime());
        date.setDate(date.getDate() - i);
        dateArray.push(date.toISOString().slice(0,10));
    }
    dateArray = dateArray.reverse();
    return dateArray;
}
function getDatesBetween(startDate, endDate) {
    const start = new Date(startDate);
    const end = new Date(endDate);

    const dates = [];

    let current = start;

    while (current <= end) {
        dates.push(new Date(current).toISOString().slice(0,10));
        current.setDate(current.getDate() + 1);
    }

    return dates;
}
function getTwoMonthDateStr(){
    let now = new Date();
    let date = new Date(now.getTime());
    // 生成日期数组
    date.setDate(date.getDate() - 60);
    let str1 = date.toISOString().slice(0,10);;
    let str2 = now.toISOString().slice(0,10);
    return [str1,str2];
}
function getLegendData(nsType){
    let arr = [];
    for(let i=0;i<nsType.length;i++){
        let item = nsType[i];
        arr.push({label:item.name, color:rongShiColors[i]})
      }
    return arr;
}
function getXAxis(){
   let date = getTwoMonthDate();
   return date;
}

function getLegend(names){
    let legend = {
        "orient":"horizontal",
        "right":50,
        "top":0,
        "itemWidth":20,
        "itemHeight":20,
        "align":"right",
        "width":"100%",
        "itemGap":5,
        "show":false,
        "data":[]
    }
    legend.data = names;
    return legend;
}

function getSeries(currentPeriod,targetName,targetField,list){
    let nsType = currentPeriod.nsType;
    let id = currentPeriod.id;
    let periodMame = currentPeriod.name;
    let series = [];

    let serie = {'name':targetName,"type":"line","symbolSize":5,"data":[],"field":targetField}
    series.push(serie);

    list.forEach(item=>{
        if(item.periodId == id){
            series.find(function(serie){
                if(item[serie.field]){
                    let subItem = {"id":item.id,"value":item[serie.field],"createDate":item.createDate,"img":item.img};
                    serie.data.push(subItem);
                }
            })
        }
    })
    return series;
}

function getMaxValue(targetField,periodId,list){
    if(intFields.indexOf(targetField) == -1){
        return 1;
    }
    let maxValue = 0;

    list.forEach(item=>{
        if(item.id == periodId && item[targetField] > maxValue){
            maxValue = item[targetField];
        }
    })
    return maxValue;
}

function getYAxisParams(targetField,currentPeriod,list,percent){
    let periodId = currentPeriod.id;
    let maxValue = getMaxValue(targetField,periodId,list);

    let nsType = currentPeriod.nsType;
    let nzNames = [...nsType].map(item=>item.name);
    let nzLength = nzNames.length == 0?2:nzNames.length;
    let numberInterval = Math.ceil(maxValue/nzLength/3);
    let interval = percent?0.1:numberInterval;
    let max = numberInterval*nzLength*3+numberInterval//1.1*maxValue;
    let min = -interval*3-3*interval*(nzLength-1);
    // let max = -min;
    if(percent){
        max = 1;
    }
    let d = -3*interval;
    let v = -2*interval;
    let nzv = [];
    for(let j = 0;j < nzLength;j++){
        nzv.push(v+d*j)
    }
    return [min,max,interval,nzNames,nzv,Math.ceil(maxValue/numberInterval)];
}

function getNongDataTotal(list){
    let newList = [];
    for(let i = 0;i<list.length;i++){
        let subItem = list[i];
        let a = newList.find(function(item){
            return item.actionName == subItem.actionName &&
            item.nsTypeId == subItem.nsTypeId &&
            item.endDate == subItem.endDate &&
            item.startDate == subItem.startDate &&
            item.periodName == subItem.periodName; //&&
            // item.createdName == subItem.createdName &&
            // item.executorName == subItem.executorName
        });
        if(!a){
            newList.push(subItem);
        }
    }
    return newList;
}

function getNSData(list,currentPeriod,nzv){
    let nsSeries = [];
    //农事类型
    let nsType = currentPeriod.nsType;
    let id = currentPeriod.id;
    let periodName = currentPeriod.name;

    nsType.forEach((item,index)=>{
        let serie = {
            "type":"line",
            "symbol":"circle",
            "symbolSize":6,
            "lineStyle":{"color":"#00000000"},
            "itemStyle":{"borderWidth":1,"borderColor":"#000000","color":rongShiColors[index]},
            "data":getTemplate(),
            "name":item.name
        }
        nsSeries.push(serie);
    })
    list.forEach(item=>{
        if(item.periodName == periodName){
            let createDate = item.startDate;
            let endDate = item.endDate;
            let nsTypeId = item.nsTypeId;
            nsType.forEach(function(item1,index){
                if(item1.id == nsTypeId){
                    let value = nzv[index];
                    let itemStyle  = {"color":"#ffffff"};
                    let serie = nsSeries[index];
                    let sData = serie.data.find(item=>item.createDate == createDate);
                    let eData = serie.data.find(item=>item.createDate == endDate);
                    debugger;
                    if(sData){
                        delete sData.mid;
                        sData.value = value;
                        //开始的白色圆点
                        sData.itemStyle = itemStyle;
                    }
                    if(eData){
                        delete eData.mid;
                        //结束的默认颜色圆点
                        eData.value = value;
                    }
                }
            })
        }
    });
    debugger;
    return nsSeries;
}

function getTemplate(){
    let arr = getTwoMonthDate();
    let list = [];
    arr.forEach(function(item,index){
       let option =  {"value":null,"mid":"mid",createDate:item};
       list.push(option);
    })
    return list;
}
export {
    getXAxis,
    getLegend,
    getSeries,
    getMaxValue,
    getYAxisParams,
    getLegendData,
    getNongDataTotal,
    getTwoMonthDateStr,
    getNSData
}
