/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * Special Declaration: These technical material reserved as the technical secrets by AGREE
 * TECHNOLOGY have been protected by the "Copyright Law" "ordinances on Protection of Computer
 * Software" and other relevant administrative regulations and international treaties. Without
 * the written permission of the Company, no person may use (including but not limited to the
 * illegal copy, distribute, display, image, upload, and download) and disclose the above
 * technical documents to any third party. Otherwise, any infringer shall afford the legal
 * liability to the company.
 */
/**
 * Created by huang.dy on 2016/2/18.
 */
define(['echarts', 'echarts/chart/radar'], function (ECharts, Radar) {
    return {
        init: function (myCharts_, mainNode, title, subTitle, names, values, others) {
            if (names.length === 0 || values.length === 0) {
                return myCharts_;
            }
            var myCharts = myCharts_;
            if (!myCharts) {
                myCharts = ECharts.init(mainNode);
            }
            myCharts.clear();
            var axisLabelArr = new Array(), tempArr = new Array();
            var tooltipHeight = mainNode.clientHeight - 30;
            var indicators = [];
            for (var i = 0; i < names.length; i++) {
                indicators.push({text: names[i]});
            }
            var option = {
                title: {
                    text: title ? title : "",
                    subtext: subTitle ? subTitle : ""
                },
                tooltip: {
                    trigger: 'axis',
                    backgroundColor: 'rgba(246, 246, 246, 0.7)',
                    formatter: function (params, ticket, callback) {
                        var name = params[0].indicator;
                        var index = names.indexOf(name);
                        var width = 320;
                        //1.标题部分
                        var res = "<div style='background-color: #3572B1;margin-top:-5px;margin-left:-5px;margin-right:-5px;width: " + (width + 10) + "px;height: 38px;' ><span style='color:#fff;position: absolute;left: 8px;top:10px; '>" + params[0].indicator + "</span></div> <br/>";
                        //2.内容部分
                        if (others && others.length !== 0) {
                            res += "<div style='background-color: rgba(220,220,220,0.5);font-size:16px;margin-top:-22px;margin-left:-5px;margin-right:-5px;margin-bottom:-4px;width: " + (width + 10) + "px;height: " + (tooltipHeight - 38) + "px;overflow: auto;'>";
                            var tableStyle = "margin-top:-10px;margin-left:8px;width:" + (width - 30) + "px; border:solid 0.1em #E2DFD2;";
                            var thTdStyle_1 = "width: " + (width - 30) / 2 + "px; border:solid 0.1em #E2DFD2;text-align: center;padding:0;";
                            var thTdStyle_2 = "width: " + (width - 30) / 3 + "px; border:solid 0.1em #E2DFD2;text-align: center;padding:0;";
                            //2.1.描述
                            res += "<div style='margin-top:5px;margin-left:8px;width:" + (width - 30) + "px;'>" + others[index].description + "</div>";
                            //2.2.表格1(开柜数量、排队等待人数)
                            res += "<br/><table cellspacing='0'cellpadding='0' style='" + tableStyle + "'>   <thead> <tr><th  style='" + thTdStyle_1 + "'>开柜数量</th> <th  style='" + thTdStyle_1 + "'>排队等待人数</th></tr> </thead>" +
                                "<tr><td  style='" + thTdStyle_1 + "'>" + others[index].windowNum + "</td><td  style='" + thTdStyle_1 + "'>" + others[index].waitInLineNum + "</td></tr>" + "</table>";
                            //2.3.表格2（其他数据）
                            if (others[index].otherDesc) {
                                var other = others[index].otherDesc;
                                var otherArr = other.split(";");
                                res += "<br/><table cellspacing='0'cellpadding='0' style='" + tableStyle + "margin-bottom:10px;'>   <thead> <tr><th  style='" + thTdStyle_2 + "'>队列名称</th> <th  style='" + thTdStyle_2 + "'>等待人数</th> <th  style='"
                                    + thTdStyle_2 + "'>平均等待时间</th></tr> </thead>";
                                for (var i = 0; i < otherArr.length; i++) {
                                    var arr = otherArr[i].split(",");
                                    if (arr && arr.length === 3) {
                                        res += "<tr><td  style='" + thTdStyle_2 + "'>" + arr[0] + "</td><td  style='" + thTdStyle_2 + "'>" + arr[1] + "</td><td  style='" + thTdStyle_2 + "'>" + arr[2] + "</td></tr>";
                                    }
                                }
                                res += "</table>";
                            }
                            res += "</div>";
                        }
                        return res;
                    },
                    textStyle: {
                        color: '#000'
                    }
                },
                legend: {
                    show: false,
                    x: 'left',
                    selectedMode: false,//图例开关，默认开启
                    data: [title]
                },
                toolbox: {
                    show: false
                },
                calculable: true,
                polar: [
                    {
                        indicator: indicators,
                        center: ['50%', '50%'],
                        radius: '80%',//150,
                        startAngle: -93,//90,
                        splitNumber: 4,//分割段数,默认5
                        name: {
                            formatter: /*'【{value}】'*/function (value) {
                                var index = names.indexOf(value);
                                if (values[index] === 0) {
                                    return value + "(当前网点)";
                                }
                                return value + "(步行" + others[index].walkingTime + ")";
                            },
                            textStyle: {color: '#000'}
                        },
                        scale: true,
                        type: 'circle',
                        axisLine: {            // 坐标轴线
                            show: false,        // 默认显示，属性show控制显示与否
                            lineStyle: {       // 属性lineStyle控制线条样式
                                color: '#fff',
                                width: 2,
                                type: 'solid'
                            }
                        },
                        axisLabel: {           // 坐标轴文本标签，详见axis.axisLabel
                            show: true,
                            formatter: function (value) {
                                if (axisLabelArr.indexOf(value) >= 0) {
                                    tempArr.push(value);
                                    if ((tempArr.length / axisLabelArr.length) === values.length - 1) {
                                        axisLabelArr = new Array();
                                        tempArr = new Array();
                                    }
                                    return "";
                                } else {
                                    axisLabelArr.push(value);
                                    return value + "千米";
                                }
                            },
                            textStyle: {       // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                                color: '#fff'
                            }
                        },
                        splitArea: {
                            show: true,
                            areaStyle: {
                                color: "#34BEEF"//['rgba(250,0,250,0.3)', 'rgba(0,200,200,0.3)']
                            }
                        },
                        splitLine: {
                            show: true,
                            lineStyle: {
                                width: 2,
                                color: '#fff'
                            }
                        }
                    }
                ],
                series: [
                    {
                        name: title,
                        type: 'radar',
                        itemStyle: {
                            emphasis: {//强调样式（悬浮时样式）
                                // color: 各异,
                                lineStyle: {
                                    width: 0
                                }
                            },
                            normal: {
                                lineStyle: {
                                    type: 'dashed',
                                    width: 1
                                }
                            }
                        },
                        draggable: false,
                        symbol: 'emptyCircle',
                        symbolSize: 10,
                        data: [
                            {
                                value: values,
                                name: title
                            }
                        ]
                    }
                ]
            };

            // 为echarts对象加载数据
            myCharts.setOption(option);
            return myCharts;
        }
    };
});