import _ from 'lodash'

const mainOption = function (data) {
    function getDayOptions () {
        const result = []
        for (var i = 1; i <= data.length; i++) {
            result.push({
                dataset: [
                    {
                        id: 'sumA',
                        fromDatasetId: 'raw',
                        transform: [
                            {
                                type: 'filter',
                                config: { dimension: 'lineNum', '<=': i }
                            }
                        ]
                    },
                    {
                        id: 'sum2',
                        fromDatasetId: 'raw',
                        transform: [
                            {
                                type: 'filter',
                                config: { dimension: 'lineNum', '>=': i }
                            }
                        ]
                    }
                ]
            })
        }

        return result
    }


    return {
        title: {
            text: '',
            show: false
        },
        timeline: {
            bottom: -6,
            axisType: 'category',
            autoPlay: true,
            playInterval: 2000,
            left: 0,
            right: 0,
            data: data.map(p => p.lineNum),
            label: {
                formatter: function (v, i) {
                    return data[i].date
                }
            }
        },
        backgroundColor: 'transparent',
        grid: {
            top: 12,
            bottom: 50,
            show: true,
            backgroundColor: 'transparent',
            borderWidth: 0
        },
        xAxis: {
            type: 'value',
            splitLine: { show: false },
            axisLabel: {
                show: false
            },
            boundaryGap: false
        },
        yAxis: {
            type: 'value',
            name: '数量'
        },
        dataset: [
            {
                id: 'raw',
                dimensions: ['lineNum', 'date', 'total', 'fzqk', 'gcyj', 'zjy'],
                source: data
            },
            {
                id: 'sumA',
                fromDatasetId: 'raw',
                transform: [
                    {
                        type: 'filter',
                        config: { dimension: 'lineNum', '<=': 0 }
                    }
                ]
            },
            {
                id: 'sum2',
                fromDatasetId: 'raw',
                transform: [
                    {
                        type: 'filter',
                        config: { dimension: 'lineNum', '>=': 0 }
                    }
                ]
            }
        ],
        series: [
            {
                id: 'sumA',
                type: 'line',
                name: '合计',
                datasetIndex: 1,
                encode: {
                    x: 'lineNum',
                    y: 'total'
                }
            },
            {
                id: 'fzqkA',
                type: 'line',
                name: '邻井复杂',
                datasetIndex: 1,
                encode: {
                    x: 'lineNum',
                    y: 'fzqk',
                    itemId: 'lineNum'
                }
            },
            {
                id: 'gcyjA',
                type: 'line',
                name: '工程预警',
                datasetIndex: 1,
                encode: {
                    x: 'lineNum',
                    y: 'gcyj',
                    itemId: 'lineNum'
                }
            },
            {
                id: 'zjyA',
                type: 'line',
                name: '钻井液',
                datasetIndex: 1,
                encode: {
                    x: 'lineNum',
                    y: 'zjy',
                    itemId: 'lineNum'
                }
            },
            {
                id: 'sum2',
                type: 'line',
                name: '合计',
                datasetIndex: 2,
                lineStyle: {
                    type: 'dotted'
                },
                encode: {
                    x: 'lineNum',
                    y: 'total',
                    itemId: 'lineNum'
                }
            },
            {
                id: 'fzqk2',
                type: 'line',
                name: '邻井复杂',
                datasetIndex: 2,
                lineStyle: {
                    type: 'dotted'
                },
                encode: {
                    x: 'lineNum',
                    y: 'fzqk',
                    itemId: 'lineNum'
                }
            },
            {
                id: 'gcyj2',
                type: 'line',
                name: '工程预警',
                datasetIndex: 2,
                lineStyle: {
                    type: 'dotted'
                },
                encode: {
                    x: 'lineNum',
                    y: 'gcyj',
                    itemId: 'lineNum'
                }
            },
            {
                id: 'zjy2',
                type: 'line',
                name: '钻井液',
                datasetIndex: 2,
                lineStyle: {
                    type: 'dotted'
                },
                encode: {
                    x: 'lineNum',
                    y: 'zjy',
                    itemId: 'lineNum'
                }
            }
        ],
        options: getDayOptions()
    }
}

const subOption = function (data = []) {
    return {
        legend: {
            show: false,
            top: 'bottom'
        },
        dataset: [
            {
                id: 'raw',
                dimensions: ['name', 'number'],
                source: data
            }
        ],
        backgroundColor: 'transparent',
        grid: {
            show: true,
            backgroundColor: 'transparent',
            borderWidth: 0
        },
        toolbox: {
            show: false,
            feature: {
                mark: { show: true },
                dataView: { show: true, readOnly: false },
                restore: { show: true },
                saveAsImage: { show: true }
            }
        },
        series: [
            {
                name: '报警来源统计',
                type: 'pie',
                radius: ['10%', '85%'],
                center: ['50%', '50%'],
                label: {
                    show: true,
                    position: 'inside',
                    formatter: '{b} {@number}'
                },
                roseType: 'area',
                itemStyle: {
                    borderRadius: 8
                },
                datasetIndex: 0,
                encode: {
                    itemName: 'name',
                    value: 'number'
                }
            }
        ]
    }
}

export default {
    mainOption,
    subOption
}
