vatModule.controller('VatModelAnalysisController', ['$scope', '$translate', '$uibModal', 'vatSessionService', 'modelConfigurationService', '$compile', '$timeout', '$q', 'SweetAlert', '$http', 'apiConfig',
    function ($scope, $translate, $uibModal, vatSessionService, modelConfigurationService, $compile, $timeout, $q, SweetAlert, $http, apiConfig) {
        'use strict';

        //由 project 决定的在本页面中固定的变量
        $scope.industryId = vatSessionService.project.industryID;
        $scope.organizationId = vatSessionService.project.organizationID;
        $scope.projectId = vatSessionService.project.id;
        $scope.projectYear = vatSessionService.project.year;
        $scope.projectPeriod = vatSessionService.month;
        $scope.serviceTypeId = vatSessionService.project.serviceTypeID;

        var xArray = ['1月', '2月', '3月', '4月', '5月', '6月',
            '7月', '8月', '9月', '10月', '11月', '12月'];
        var colors = ['#ffb600', '#dc6900', '#a32020'];

        //增值税负担率
        function getBurdenRateData(chart) {
            var yArray = ['应纳税额', '主营业务收入', '占比'];
            $http.get('/reportAnalysis/burdenRateData/' + $scope.projectId, apiConfig.createVat())
                .success(function (res) {
                    if (res && 0 === res.code) {
                        var option = {
                            title: {
                                text: '增值税负担率'
                            },
                            color: colors,
                            tooltip: {
                                trigger: 'axis',
                                axisPointer: {
                                    type: 'cross'
                                }
                            },
                            grid: {
                                right: '20%'
                            },
                            toolbox: {
                                feature: {
                                    dataView: {show: false, readOnly: false},
                                    restore: {show: false},
                                    saveAsImage: {show: true}
                                }
                            },
                            legend: {
                                type: 'scroll',
                                bottom: 10,
                                data: yArray
                            },
                            xAxis: [
                                {
                                    type: 'category',
                                    axisTick: {
                                        alignWithLabel: true
                                    },
                                    data: xArray
                                }
                            ],
                            yAxis: [
                                {
                                    type: 'value',
                                    name: '金额',
                                    // min: 0,
                                    // max: 250,
                                    position: 'right',
                                    axisLine: {
                                        lineStyle: {
                                            color: colors[2]
                                        }
                                    },
                                    axisLabel: {
                                        formatter: function (val) {
                                            return formatNum(val);
                                        }
                                    }
                                },
                                {
                                    type: 'value',
                                    name: '占比',
                                    // min: 0,
                                    // max: 25,
                                    position: 'left',
                                    axisLine: {
                                        lineStyle: {
                                            color: colors[2]
                                        }
                                    },
                                    axisLabel: {
                                        formatter: function (val) {
                                            return (val * 100) + ' %';
                                        }
                                    }
                                }
                            ],
                            series: [
                                {
                                    name: yArray[0],
                                    type: 'bar',
                                    itemStyle: {
                                        color: colors[1]
                                    },
                                    data: res.data.vatAmount
                                },
                                {
                                    name: yArray[1],
                                    type: 'bar',
                                    itemStyle: {
                                        color: colors[0]
                                    },
                                    data: res.data.income
                                },
                                {
                                    name: yArray[2],
                                    type: 'line',
                                    yAxisIndex: 1,
                                    itemStyle: {
                                        color: colors[2]
                                    },
                                    lineStyle: {
                                        color: colors[2]
                                    },
                                    data: res.data.rate
                                }
                            ]
                        };
                        chart.setOption(option);
                    }

                })
        }

        //增值税申报表收入与会计收入差异离散度分析
        function getDispersion(chart) {
            $http.get('/reportAnalysis/burdenRateData/' + $scope.projectId, apiConfig.createVat())
                .success(function (res) {
                    if (res && 0 === res.code) {
                        var data = [];
                        for (var i in xArray) {
                            data.push([xArray[i], res.data.rate[i]]);
                        }
                        var option = {
                            title: {
                                text: '增值税申报表收入与会计收入差异离散度分析'
                            },
                            xAxis: [
                                {
                                    type: 'category',
                                    axisTick: {
                                        alignWithLabel: true
                                    },
                                    data: xArray
                                }
                            ],
                            yAxis: {},
                            series: [{
                                // symbolSize: 20,
                                itemStyle: {
                                    color: '#dc6900'
                                },
                                data: data,
                                type: 'scatter'
                            }],
                            tooltip: {
                                trigger: 'axis',
                                axisPointer: {
                                    type: 'cross'
                                }
                            },
                            toolbox: {
                                feature: {
                                    dataView: {show: false, readOnly: false},
                                    restore: {show: false},
                                    saveAsImage: {show: true}
                                }
                            }
                        };
                        chart.setOption(option);
                    }

                })
        }

        //进项税额占主营业务成本的比例
        function getIncomeRateData(chart) {
            var yArray = ['进项税额', '主营业务成本', '占比'];
            $http.get('/reportAnalysis/incomeRate/' + $scope.projectId, apiConfig.createVat())
                .success(function (res) {
                    if (res && 0 === res.code) {
                        var option = {
                            title: {
                                text: '进项税额占主营业务成本比例'
                            },
                            color: colors,
                            tooltip: {
                                trigger: 'axis',
                                axisPointer: {
                                    type: 'cross'
                                }
                            },
                            grid: {
                                right: '20%'
                            },
                            toolbox: {
                                feature: {
                                    dataView: {show: false, readOnly: false},
                                    restore: {show: false},
                                    saveAsImage: {show: true}
                                }
                            },
                            legend: {
                                type: 'scroll',
                                bottom: 10,
                                data: yArray
                            },
                            xAxis: [
                                {
                                    type: 'category',
                                    axisTick: {
                                        alignWithLabel: true
                                    },
                                    data: xArray
                                }
                            ],
                            yAxis: [
                                {
                                    type: 'value',
                                    name: '金额',
                                    // min: 0,
                                    // max: 250,
                                    position: 'right',
                                    axisLine: {
                                        lineStyle: {
                                            color: colors[2]
                                        }
                                    },
                                    axisLabel: {
                                        formatter: function (val) {
                                            return formatNum(val);
                                        }
                                    }
                                },
                                {
                                    type: 'value',
                                    name: '占比',
                                    // min: 0,
                                    // max: 25,
                                    position: 'left',
                                    axisLine: {
                                        lineStyle: {
                                            color: colors[2]
                                        }
                                    },
                                    axisLabel: {
                                        formatter: function (val) {
                                            return (val * 100) + ' %';
                                        }
                                    }
                                }
                            ],
                            series: [
                                {
                                    name: yArray[0],
                                    type: 'bar',
                                    itemStyle: {
                                        color: colors[1]
                                    },
                                    data: res.data.vatAmount
                                },
                                {
                                    name: yArray[1],
                                    type: 'bar',
                                    itemStyle: {
                                        color: colors[0]
                                    },
                                    data: res.data.income
                                },
                                {
                                    name: yArray[2],
                                    type: 'line',
                                    yAxisIndex: 1,
                                    itemStyle: {
                                        color: colors[2]
                                    },
                                    data: res.data.rate
                                }
                            ]
                        };
                        chart.setOption(option);
                    }

                })
        }

        //进项税转出额波动率
        function getIncomeVolatilityData(chart) {
            var yArray = ['本期进项税额转出额', '进项税额', '占比'];
            $http.get('/reportAnalysis/incomeVolatility/' + $scope.projectId, apiConfig.createVat())
                .success(function (res) {
                    if (res && 0 === res.code) {
                        var option = {
                            title: {
                                text: '进项税转出额波动率'
                            },
                            color: colors,
                            tooltip: {
                                trigger: 'axis',
                                axisPointer: {
                                    type: 'cross'
                                }
                            },
                            grid: {
                                right: '20%'
                            },
                            toolbox: {
                                feature: {
                                    dataView: {show: false, readOnly: false},
                                    restore: {show: false},
                                    saveAsImage: {show: true}
                                }
                            },
                            legend: {
                                type: 'scroll',
                                bottom: 10,
                                data: yArray
                            },
                            xAxis: [
                                {
                                    type: 'category',
                                    axisTick: {
                                        alignWithLabel: true
                                    },
                                    data: xArray
                                }
                            ],
                            yAxis: [
                                {
                                    type: 'value',
                                    name: '金额',
                                    // min: 0,
                                    // max: 250,
                                    position: 'right',
                                    axisLine: {
                                        lineStyle: {
                                            color: colors[2]
                                        }
                                    },
                                    axisLabel: {
                                        formatter: function (val) {
                                            return formatNum(val);
                                        }
                                    }
                                },
                                {
                                    type: 'value',
                                    name: '占比',
                                    // min: 0,
                                    // max: 25,
                                    position: 'left',
                                    axisLine: {
                                        lineStyle: {
                                            color: colors[2]
                                        }
                                    },
                                    axisLabel: {
                                        formatter: function (val) {
                                            return (val * 100) + ' %';
                                        }
                                    }
                                }
                            ],
                            series: [
                                {
                                    name: yArray[0],
                                    type: 'bar',
                                    itemStyle: {
                                        color: colors[1]
                                    },
                                    data: res.data.vatAmount
                                },
                                {
                                    name: yArray[1],
                                    type: 'bar',
                                    itemStyle: {
                                        color: colors[0]
                                    },
                                    data: res.data.income
                                },
                                {
                                    name: yArray[2],
                                    type: 'line',
                                    yAxisIndex: 1,
                                    itemStyle: {
                                        color: colors[2]
                                    },
                                    data: res.data.rate
                                }
                            ]
                        };
                        chart.setOption(option);
                    }

                })
        }

        //增值税应税收入、增值税免、抵、退办法出口收入 占会计收入比例
        function getVatIncomeRateData(chart) {
            var yArray = ['增值税应税收入', '增值税免、抵、退出口收入', '其余'];
            $http.get('/reportAnalysis/vatIncomeRate/' + $scope.projectId + "/" + $scope.projectPeriod, apiConfig.createVat())
                .success(function (res) {
                    if (res && 0 === res.code) {
                        var option = {
                            title: {
                                text: '应税收入、出口收入比例'
                            },
                            color: colors,
                            tooltip: {
                                trigger: 'axis',
                                axisPointer: {
                                    type: 'cross'
                                }
                            },
                            grid: {
                                right: '20%'
                            },
                            toolbox: {
                                feature: {
                                    dataView: {show: false, readOnly: false},
                                    restore: {show: false},
                                    saveAsImage: {show: true}
                                }
                            },
                            legend: {
                                type: 'scroll',
                                bottom: 5,
                                data: yArray
                            },
                            series: [
                                {
                                    name: '占比',
                                    type: 'pie',
                                    radius: '55%',
                                    center: ['50%', '60%'],
                                    data: [
                                        {value: res.data.vatAmount[0], name: '增值税应税收入'},
                                        {value: res.data.income[0], name: '增值税免、抵、退出口收入'},
                                        {value: res.data.rate[0], name: '其余'}
                                    ],
                                    itemStyle: {
                                        emphasis: {
                                            shadowBlur: 10,
                                            shadowOffsetX: 0,
                                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                                        }
                                    }
                                }
                            ]
                        };
                        chart.setOption(option);
                    }

                })
        }

        //应税收入、出口收入占会计收入比例及趋势
        function getVatIncomeLineData(chart) {
            var yArray = ['应税收入', '出口收入','会计收入','应税占比','出口占比'];
            $http.get('/reportAnalysis/vatIncomeLine/' + $scope.projectId, apiConfig.createVat())
                .success(function (res) {
                    if (res && 0 === res.code) {
                        var option = {
                            title: {
                                text: '应税收入、出口收入占会计收入比例及趋势'
                            },
                            color: colors,
                            tooltip: {
                                trigger: 'axis',
                                axisPointer: {
                                    type: 'cross'
                                }
                            },
                            grid: {
                                right: '20%'
                            },
                            toolbox: {
                                feature: {
                                    dataView: {show: false, readOnly: false},
                                    restore: {show: false},
                                    saveAsImage: {show: true}
                                }
                            },
                            legend: {
                                type: 'scroll',
                                bottom: 10,
                                data: yArray
                            },
                            xAxis: [
                                {
                                    type: 'category',
                                    axisTick: {
                                        alignWithLabel: true
                                    },
                                    data: xArray
                                }
                            ],
                            yAxis: [
                                {
                                    type: 'value',
                                    name: '金额',
                                    // min: 0,
                                    // max: 250,
                                    position: 'right',
                                    axisLine: {
                                        lineStyle: {
                                            color: colors[2]
                                        }
                                    },
                                    axisLabel: {
                                        formatter: function (val) {
                                            return formatNum(val);
                                        }
                                    }
                                },
                                {
                                    type: 'value',
                                    name: '占比',
                                    // min: 0,
                                    // max: 250,
                                    position: 'left',
                                    axisLine: {
                                        lineStyle: {
                                            color: colors[2]
                                        }
                                    },
                                    axisLabel: {
                                        formatter: function (val) {
                                            return (val * 100) + ' %';
                                        }
                                    }
                                }
                            ],
                            series: [
                                {
                                    name: yArray[0],
                                    type: 'bar',
                                    itemStyle: {
                                        color: '#eb8c00'
                                    },
                                    data: res.data.val1
                                },
                                {
                                    name: yArray[1],
                                    type: 'bar',
                                    itemStyle: {
                                        color: '#dc6900'
                                    },
                                    data: res.data.val2
                                },
                                {
                                    name: yArray[2],
                                    type: 'bar',
                                    itemStyle: {
                                        color: colors[0]
                                    },
                                    data: res.data.val3
                                },
                                {
                                    name: yArray[3],
                                    type: 'line',
                                    yAxisIndex: 1,
                                    itemStyle: {
                                        color: colors[2]
                                    },
                                    data: res.data.rate1
                                },
                                {
                                    name: yArray[4],
                                    type: 'line',
                                    yAxisIndex: 1,
                                    itemStyle: {
                                        color: '#602320'
                                    },
                                    data: res.data.rate2
                                }
                            ]
                        };
                        chart.setOption(option);
                    }

                })
        }

        //已抵扣进项税波动率
        function getDeductionData(chart) {
            var yArray = ['占比', '基准值'];
            $http.get('/reportAnalysis/deduction/' + $scope.projectId, apiConfig.createVat())
                .success(function (res) {
                    if (res && 0 === res.code) {
                        var option = {
                            title: {
                                text: '已抵扣进项税波动率'
                            },
                            color: colors,
                            tooltip: {
                                trigger: 'axis',
                                axisPointer: {
                                    type: 'cross'
                                }
                            },
                            grid: {
                                right: '20%'
                            },
                            toolbox: {
                                feature: {
                                    dataView: {show: false, readOnly: false},
                                    restore: {show: false},
                                    saveAsImage: {show: true}
                                }
                            },
                            legend: {
                                type: 'scroll',
                                bottom: 10,
                                data: yArray
                            },
                            xAxis: [
                                {
                                    type: 'category',
                                    axisTick: {
                                        alignWithLabel: true
                                    },
                                    data: xArray
                                }
                            ],
                            yAxis: [
                                {
                                    type: 'value',
                                    name: '占比',
                                    // max: 25,
                                    position: 'left',
                                    axisLine: {
                                        lineStyle: {
                                            color: colors[2]
                                        }
                                    },
                                    axisLabel: {
                                        formatter: function (val) {
                                            return (val * 100) + ' %';
                                        }
                                    }
                                }
                            ],
                            series: [
                                {
                                    name: yArray[1],
                                    type: 'line',
                                    itemStyle: {
                                        color: '#968c6d'
                                    },
                                    data: ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1']
                                },
                                {
                                    name: yArray[0],
                                    type: 'bar',
                                    itemStyle: {
                                        color: colors[1]
                                    },
                                    data: res.data.rate
                                }
                            ]
                        };
                        chart.setOption(option);
                    }

                })
        }

        function formatNum(strNum) {
            if (strNum.length <= 3) {
                return strNum;
            }
            if (!/^(\+|-)?(\d+)(\.\d+)?$/.test(strNum)) {
                return strNum;
            }
            var a = RegExp.$1,
                b = RegExp.$2,
                c = RegExp.$3;
            var re = new RegExp();
            re.compile("(\\d)(\\d{3})(,|$)");
            while (re.test(b)) {
                b = b.replace(re, "$1,$2$3");
            }
            return a + "" + b + "" + c;
        }

        function init() {
            var width = $("div .row")[0].offsetWidth;
            $('div[name="p_chart"]').css("width", (width / 2) - 80);
            $('div[name="p_chart"]').css("height", 400);

            var burdenRateDiv = echarts.init(document.getElementById('burdenRateDiv'));
            var discreteAnalysisDiv = echarts.init(document.getElementById('discreteAnalysisDiv'));
            var incomeRateDiv = echarts.init(document.getElementById('incomeRateDiv'));
            var incomeVolatilityDiv = echarts.init(document.getElementById('incomeVolatilityDiv'));
            // var vatIncomeRateDiv = echarts.init(document.getElementById('vatIncomeRateDiv'));
            var vatIncomeLineDiv = echarts.init(document.getElementById('vatIncomeLineDiv'));
            var deductionDiv = echarts.init(document.getElementById('deductionDiv'));

            getBurdenRateData(burdenRateDiv);
            getDispersion(discreteAnalysisDiv);
            getIncomeRateData(incomeRateDiv);
            getIncomeVolatilityData(incomeVolatilityDiv);
            // getVatIncomeRateData(vatIncomeRateDiv);
            getVatIncomeLineData(vatIncomeLineDiv);
            getDeductionData(deductionDiv);
        }

        (function () {
            init();

        })();
    }
]);