var app = angular.module('myApp.controllers', []);
app.controller('configController', function ($scope, $commonbox) {
    var ht = $(".main-content").css("height");
    $(".main-left").css("height", ht);
});
app.controller('listController', function ($scope) {
    $scope.changeInfo = function () {
        $('#refuseBox').modal('show');
    }

});
app.controller('summaryController', function ($scope) {

});
/**
 * listController   报告清单
 */
app.controller("listctr", function ($scope, $http, $Popupbox,$Scorebox) {
    $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
        var cht = $(".main-content").css("height");
        var lht=$(".main-left").css("height");
        var chts=cht.split("px");
        var lhts=lht.split("px");
        if(parseInt(lhts[0])<parseInt(chts[0])){
            $(".main-left").css("height",cht);
        }
        /*if(parseInt(lhts[0])>parseInt(chts[0])){
            $(".main-left").css("height","100%");
        }*/
    });
    $scope.model=[{
        policyname:'全部',
        value:0
    },{
        policyname:'寿险',
        value:2
    },{
        policyname:'产险',
        value:1
    },{
        policyname:'健康险',
        value:3
    }];
    $scope.branch=function(){
        $http({
            method: 'post',
            url: '/icc-front/compass/branch.do',
            params:{
                type:$scope.channel1
            }
        }).then(
            function successCallback(response) {
                if (response.data!=null) {
                    $scope.branchs=response.data;
                }
            }, function errorCallback(response) {
            }
        );

    };
    $scope.exportExcel=function() {
        var str="?";
        if($scope.branchNametemp1!=null&&$scope.branchNametemp1!=undefined){
            str+="branchName="+$scope.branchNametemp1+"&";
        }
        if($scope.channeltemp1!=null&$scope.channeltemp1!=undefined){
            str+="channel="+$scope.channeltemp1+"&";
        }
         if($scope.policyHoldertemp1!=null&&$scope.policyHoldertemp1!=undefined){
             str+="policyHolder="+$scope.policyHoldertemp1+"&";
         }
         if($scope.policyNotemp1!=null&&$scope.policyNotemp1!=undefined){
             str+="policyNo="+$scope.policyNotemp1+"&";
         }
         if($scope.claimNotemp1!=null&&$scope.claimNotemp1!=undefined){
             str+="claimNo="+$scope.claimNotemp1+"&";
         }
         if($scope.IdNnumbertemp1!=null&&$scope.IdNnumbertemp1!=undefined){
             str+="IdNnumber="+$scope.IdNnumbertemp1+"&";
         }
         if($scope.score1temp1!=null&&$scope.score2temp1!=undefined){
             str+="score1="+$scope.score1temp1+"&";
             str+="score2="+$scope.score2temp1+"&";
         }

         if($scope.datet1temp1!=null&&$scope.datet2temp1!=null){
             str+="date1="+$scope.datet1temp1+"&";
             str+="date2="+$scope.datet2temp1+"&";
         }
        window.location.href="/icc-front/compass/exportExcel.do"+str;
       /* window.location.href="/icc-front/compass/exportExcel.do?filename="+$scope.filename;*/
    };
    $scope.listquery = function () {
        var date1=$("#date1").val();
        var date2=$("#date2").val();

        var date1time=new Date(date1);
        var date2time=new Date(date2);

        var type="^[0-9]*[0-9][0-9]*$";
        var r=new RegExp(type);

        var score1=$("#score1").val();
        var score2=$("#score2").val();
        var s1=parseInt(score1);
        var s2=parseInt(score2);
        var flag=(score1!=""&&score2!="")?true:false;

        var ishave=false;
        //赔案号
        var policyNumber1=$("#policyNumber1").val();
        var claimNo1=$("#claimNo1").val();
        var IdNnumber1=$("#IdNnumber1").val();

        if((policyNumber1!=""||claimNo1!=""||IdNnumber1!="")&&($("#date1").val() == ""||$("#date1").val() == "NaN")&&($("#date2").val() == ""||$("#date2").val() == "NaN")){
            ishave=true;
        }
        if (!ishave&&(date1 == "" ||date2== "" || date1== undefined || date2== undefined)) {
            $Popupbox.show("结案起止日期是必选项，请选择正确日期！")
        }else if(!ishave&&(date1time.getTime()>=date2time.getTime())){
            $Popupbox.show("请选择正确的时间区间！")
        }
        else if((score1==""&&score2!="")||(score1!=""&&score2=="")){
            $Scorebox.show("请填写完整的分值区间！");
        }
        else if(flag&&(s1<0||s2>1000)){
            $Scorebox.show("请填写0到1000之间的分值区间！");
        }else if(flag&&(s1>s2||s2==0)){
            $Scorebox.show("请填写正确的分值区间！");
        }
        else  if(flag&&(!r.test(s1)||!r.test(s2))){
                $Scorebox.show("分值区间请填写正整数！");
            }
        else {

            $scope.branchNametemp1=$scope.branchName1;
            $scope.channeltemp1=$scope.channel1;
            $scope.policyHoldertemp1=$scope.policyHolder1;
            $scope.policyNotemp1=$scope.policyNumber1;
            $scope.claimNotemp1=$scope.claimNo1;
            $scope.IdNnumbertemp1=$scope.IdNnumber1;
            $scope.score1temp1=(($scope.score1==undefined||$scope.score1=="")?"0":$scope.score1);
            $scope.score2temp1=(($scope.score2==undefined||$scope.score2=="")?"0":$scope.score2);
            $scope.datet1temp1=(date1==""?"":formatDate(date1time,1));
            $scope.datet2temp1=(date2==""?"":formatDate(date2time,1));
            pagenationForListreport($scope, $http,$Popupbox,date1time,date2time);
    /*        $http({

                method: 'post',
                url: '/icc-front/compass/list.do',
                params: {
                    branchName: $scope.branchName1,
                    policyNo: $scope.policyNumber1,
                    channel: $scope.channel1,
                    policyHolder: $scope.policyHolder1,
                    claimNo: $scope.claimNo1,
                    IdNnumber: $scope.IdNnumber1,
                    ScoreInterval:($scope.score1==undefined?"0":$scope.score1)+","+($scope.score2==undefined?"0":$scope.score2),
                    enddate:(date1==""?"":formatDate(date1time,1))+ "," +(date2==""?"":formatDate(date2time,1))
                }
            }).then(function successCallback(response) {
                if (response.data.records.length == 0) {
                    $Popupbox.show("没有查询到相关数据！");
                }
                else {
                    $scope.filename=response.data.filename;
                    //分页
                    pagenation($scope, $http, response);
                }
            }, function errorCallback(response) {
            });*/
        }
    };
});
/**
 * ConfigController   配置列表
 */
app.controller("configctr", function ($rootScope,$scope, $http,$commonbox,$Popupbox) {
    $http.post('/icc-front/config/configInit.do').success(function (data) {
        if(data.iccinterface=='1'){
            $("#iccinterface").html("关闭接口");
        }else{
            $("#iccinterface").html("开启接口");
        }
        if(data.iccscore=='1'){
            $("#iccscore").html("关闭评分计算");
        }else{
            $("#iccscore").html("开启评分计算");
        }
        $("#maxnum1").val(data.maxnum1);
        $("#maxnum2").val(data.maxnum2);
        $("#middlenum1").val(data.middlenum1);
        $("#middlenum2").val(data.middlenum2);
        $("#minnum1").val(data.minnum1);
        $("#minnum2").val(data.minnum2);
    }).error(function (err) {

    });
    $rootScope.configscore=function(){
        var iccscore=$("#iccscore").html();
        if("关闭评分计算"==iccscore){
            iccscore='0';
        }else{
            iccscore='1';
        }
        $.ajax({
            type: 'POST',
            url: "/icc-front/config/switch.do",
            data:{
                iccscore:iccscore
            },
            datatype: "json",
            success:function (data) {
                if(data.resCode=='1'){
                    if(iccscore=='1'){
                        $("#iccscore").html("关闭评分计算");
                    }else{
                        $("#iccscore").html("开启评分计算");
                    }
                }
            },
            error:function (data) {

            }
        });
    };

    $scope.configsave = function () {
        var type="^[0-9]*[0-9][0-9]*$";
        var r=new RegExp(type);
        var maxnum1=$("#maxnum1").val();
        var maxnum2=$("#maxnum2").val();
        var middlenum1=$("#middlenum1").val();
        var middlenum2=$("#middlenum2").val();
        var minnum1=$("#minnum1").val();
        var minnum2=$("#minnum2").val();

        if(maxnum1==''||maxnum2==''||middlenum1==''||middlenum2==''||minnum1==''||minnum2==''){
            $Popupbox.show("不能填写空值！");
            return;
        }
        if((parseInt(maxnum1)>=parseInt(maxnum2))||(parseInt(middlenum1)>=parseInt(middlenum2))||(parseInt(minnum1)>=parseInt(minnum2))){
            $Popupbox.show("最大值不能小于或等于最小值！");
            return;
        }
        if(parseInt(minnum2)!=parseInt(middlenum1)||parseInt(middlenum2)!=parseInt(maxnum1)){
            $Popupbox.show("请配置从低到高不间断的数值区间！");
            return;
        }
        if(parseInt(minnum1)<0||parseInt(maxnum2)>1000){
            $Popupbox.show("请配置从0到1000的数值区间！");
            return;
        }
        if(!r.test(maxnum1)||!r.test(maxnum2)||!r.test(middlenum1)||!r.test(middlenum2)||!r.test(minnum1)||!r.test(minnum2)){
            $Popupbox.show("请配置正整数！");
            return;
        }
        $http({
            method: 'post',
            url: '/icc-front/config/ConfigSave.do',
            params: {
                maxnum1: maxnum1,
                maxnum2:maxnum2,
                middlenum1:middlenum1,
                middlenum2:middlenum2,
                minnum1:minnum1,
                minnum2:minnum2
            }
        }).then(function successCallback(response) {
    if(response.data.resCode=='1'){
        $commonbox.show('配置成功！');
    }else{
        $commonbox.show('配置失败！');
    }
}, function errorCallback(response) {
});
}
});
/**
 * SummaryController    统计报告
 */
app.controller("collectctr", function ($scope, $http, $Popupbox,$rootScope,$Scorebox) {

    $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
        var cht = $(".main-content").css("height");
        var lht=$(".main-left").css("height");
        var chts=cht.split("px");
        var lhts=lht.split("px");
        if(parseInt(lhts[0])<parseInt(chts[0])){
            $(".main-left").css("height",cht);
        }
        if(parseInt(lhts[0])>parseInt(chts[0])){
            $(".main-left").css("height","100%");
        }
    });

    $scope.model=[{
        policyname:'全部',
        value:0
    },{
        policyname:'寿险',
        value:2
    },{
        policyname:'产险',
        value:1
    },{
        policyname:'健康险',
        value:3
    }];
    $scope.branch=function(){
        $http({
            method: 'post',
            url: '/icc-front/compass/branch.do',
            params:{
                type:$scope.channel2
            }
        }).then(
            function successCallback(response) {
                if (response.data!=null) {
                    $scope.branchs=response.data;
                }
            }, function errorCallback(response) {
            }
        );

    };
    $scope.exportExcel=function() {
        var str="?";
        if($scope.branchNametemp2!=null&&$scope.branchNametemp2!=undefined){
            str+="branchName="+$scope.branchNametemp2+"&";
        }
        if($scope.channeltemp2!=null&$scope.channeltemp2!=undefined){
            str+="channel="+$scope.channeltemp2+"&";
        }
        if($scope.policyHoldertemp2!=null&&$scope.policyHoldertemp2!=undefined){
            str+="policyHolder="+$scope.policyHoldertemp2+"&";
        }
        if($scope.policyNotemp2!=null&&$scope.policyNotemp2!=undefined){
            str+="policyNo="+$scope.policyNotemp2+"&";
        }
        if($scope.date1temp2!=null&&$scope.datet2temp2!=null){
            str+="date1="+$scope.date1temp2+"&";
            str+="date2="+$scope.datet2temp2+"&";
        }
        window.location.href="/icc-front/summary/export.do"+str;
    };
    $scope.Summaryquery = function () {
        var date1=$("#date1").val();
        var date2=$("#date2").val();

        var date1time=new Date(date1);
        var date2time=new Date(date2);

        var  policyNumber2=$("#policyNumber2").val();
        var  ishave=false;
        if(policyNumber2!=""&&(date1==""&&date2=="")){
            ishave=true;
        }
        if (!ishave&&(date1== ""||date2 == "")) {
            $Popupbox.show("结案起止日期是必选项，请选择正确日期！");
        }else if(!ishave&&(date1time.getTime()>date2time.getTime())){
            $Popupbox.show("请选择正确的日期顺序")
        }
        else {
            $scope.branchNametemp2=$scope.branchName2;
            $scope.channeltemp2=$scope.channel2;
            $scope.policyHoldertemp2=$scope.policyHolder2;
            $scope.policyNotemp2=$scope.policyNumber2;
            $scope.date1temp2=(date1==""?"":formatDate(date1time,0));
            $scope.datet2temp2=(date2==""?"":formatDate(date2time,0));
            //查询
            $http({
                method: 'post',
                url: '/icc-front/summary/query.do',
                params: {
                    branchName: $scope.branchName2,
                    policyNo: $scope.policyNumber2,
                    channel: $scope.channel2,
                    policyHolder: $scope.policyHolder2,
                    date1:(date1==""?"":formatDate(date1time,0)),
                    date2:(date2==""?"":formatDate(date2time,0))
                }
            }).then(function successCallback(response) {
                if (response.data.records.length == 0) {
                    $Popupbox.show("没有查询到相关数据！");
                } else {
                    //汇总行数据
                    pagenation($scope, $http, response);
                    $scope.highcaseSummary=response.data.summary.highcaseSummary;
                    $scope.highproportionSummary=response.data.summary.highproportionSummary;
                    $scope.middlecaseSummary=response.data.summary.middlecaseSummary;
                    $scope.middleproportionSummary=response.data.summary.middleproportionSummary;
                    $scope.lowcaseSummary=response.data.summary.lowcaseSummary;
                    $scope.lowproportionSummary=response.data.summary.lowproportionSummary;
                    $scope.totalcase=response.data.summary.totalcase;
                    $scope.filename=response.data.filename;
                }
            }, function errorCallback(response) {
            });
        }
    }
});

/**
 * analysisController    分析报告
 */
app.controller("analysisCtr", function ($scope, $http, $rootScope,$Popupbox) {

    $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
        var radios=$(":radio");
        var list=$scope.items;
        for(var i=0;i<list.length;i++){
            var name=list[i].radioname;
            var radio=list[i].radio;
            for(var j=0;j<radios.length;j++){
                if(name==radios[j].name&&radio==radios[j].value){
                    radios[j].checked=true;
                }
            }
        }
    });

    $scope.checkvalue=function(){
        if($("#myform :checked").length!=$scope.backCount) {
            showPopup(350,130,'请选择正确的反馈信息！');
            return;
        }
            var params = $("#myform").serialize();
            $.ajax({
                type: "POST",
                url: "/icc-front/Assessment/insert.do",
                data: params,
                success: function (msg) {
                    if(msg.resCode=='1'){
                        $scope.isfeedBack=true;
                        showPopup(350,130,'反馈成功！');
                    }else{
                        showPopup(350,130,'反馈失败！');
                    }

                }
            });

    };

    $rootScope.listone = function (outputId) {
        $http({
            method: 'post',
            url: '/icc-front/compass/listone.do',
            params: {
                outputId: outputId
            }
        }).then(function successCallback(response) {
            if(response.data.resCode='1'){
                //雷达图
                var data1=response.data.mapradar.claim_behavior;
                colorChange("claim_behavior",data1);
                var data2=response.data.mapradar.medical_treatment;
                colorChange("medical_treatment",data2);
                var data3=response.data.mapradar.disease_diagnosis;
                colorChange("disease_diagnosis",data3);
                var data4=response.data.mapradar.drug_project;
                colorChange("drug_project",data4);
                var data5=response.data.mapradar.diagnosis_project;
                colorChange("diagnosis_project",data5);


                var myChart;
                var domMain = document.getElementById('showecharts');
                var option = {
                    tooltip: {},
                    name: {
                        show: true,
                        formatter: null,
                        textStyle: {
                            //设置颜色
                            color: '#272923'
                        }
                    },
                    radar: {
                        splitNumber: 3,
                        indicator: [
                            {name: '索赔行为', max: 10},
                            {name: '医疗行为', max: 10},
                            {name: '疾病诊断', max: 10},
                            {name: '药品项目', max: 10},
                            {name: '诊疗项目', max: 10}
                        ]
                    },
                    series: [{
                        type: 'radar',
                        areaStyle: {
                            type: 'default'
                        },
                        data: [
                            {
                                value: [data1, data2, data3, data4, data5],
                                name: '风险等级评分',
                                itemStyle: {
                                    normal: {
                                        color: '#72abe5',
                                        areaStyle: {
                                            color: '#72abe5'
                                        }
                                    }
                                }
                            }
                        ]
                    }]
                };
                myChart = echarts.init(domMain);
                myChart.setOption(option, true);
                //
                $scope.levelscore = response.data.mapMsg.levelscore;
                $scope.temper = response.data.mapMsg.levelscore;
                $(".temper-inner").width(Math.ceil($scope.temper / 1000 * 494)+ 'px');
                /* $scope.level = response.data.mapMsg.level;*/
                $scope.level =response.data.mapMsg.level;
                $scope.name = response.data.mapMsg.name;
                $scope.age = response.data.mapMsg.age;
                if (response.data.mapMsg.sex ==1) {
                    $scope.sex ="男";
                    $("#agesvg").attr("src", "../compass/app/img/nan.svg"+"?t=" + Math.random());
                }
                else{
                    $scope.sex ="女";
                    $("#agesvg").attr("src", "../compass/app/img/nv.svg"+"?t=" + Math.random());
                }
                $scope.policydate = response.data.mapMsg.policydate;
                $scope.claimdate = response.data.mapMsg.claimdate;
                $scope.effective_policydate = response.data.mapMsg.effective_policydate;
                $scope.outputId = response.data.mapMsg.outputId;
                $scope.claimNo_one = response.data.mapMsg.claimNo_one;
                $scope.backCount = response.data.mapMsg.backCount;
                $scope.business_source = response.data.mapMsg.business_source;
                $scope.branch = response.data.mapMsg.branch;
                $scope.outDetailId = response.data.mapMsg.outDetailId;
                $scope.othertext=response.data.mapMsg.othertext;
                $scope.createdate=response.data.mapMsg.createdate;
               /* $("#othertext").val(response.data.mapMsg.othertext);
                $("#othertext").html(response.data.mapMsg.othertext);*/
                $scope.items = response.data.ratelist;

            }else{
                $Popupbox.show("没有查询到相关数据！");
            }
        }, function errorCallback(response) {
        });
    };

});


app.controller('chargebox', function ($scope,$http,$rootScope,$compile) {

    $rootScope.listoutPutId=function(claimNo){
        $http({
            method: 'post',
            url: '/icc-front/Assessment/outputIdList.do',
            params: {
                claimNo: claimNo
            }
        }).then(function successCallback(response) {
            var map=response.data;
            var str="<table id='tbl' width='100%'></table>";
            var htmls="";
            $(".modal-body").html(str);
            for(var key in map){
                htmls+="<tr><td width='50%'><a  herf='#' "+"ng-click='listone("+key+")'"+"data-toggle= 'modal'data-target='#confirmBox'>"+map[key]+" </a></td></tr>";
            }
            var $html = $compile(htmls)($scope);
            $("#tbl").append($html);
            $("#modalbtn").click();

        });
    }, function errorCallback(response) {

    }

});

function   colorChange(idname,score){
    var str="";
    var level="";
    var color="";
    if("claim_behavior"==idname){
        str="索赔行为：";
    }else if("medical_treatment"==idname){
        str="医疗行为：";
    }else if("disease_diagnosis"==idname){
        str="疾病诊断：";
    }else if("drug_project"==idname){
        str="药品项目：";
    }else {
        str="诊疗项目：";
    }
    if(score==1){
        level="低";
        color="green";
    }else if(score>1&&score<=5){
        level="中";
        color="blue";
    }
    else{
        level="高";
        color="orange";
    }
    $("#"+idname).attr("class",color);
    $("#"+idname).html(str+level);
}

function showPopup(w,h,Msg){
    var popUp = document.getElementById("popupcontent");
    popUp.style.bottom ="400px";
    popUp.style.left ="250px";
    popUp.style.width = w + "px";
    popUp.style.height = h + "px";
    var baseText="";
    baseText+="<div role=\"document\" class=\"feedback-content\">"+Msg+"</div>";
    baseText+="<div role=\"document\" class=\"recharge-feedbackfoot\" onclick=\"hidePopup();\">确定</div>";
    popUp.innerHTML = baseText;
    popUp.style.visibility = "visible";
}
function hidePopup(){
    var popUp = document.getElementById("popupcontent");
    popUp.style.visibility = "hidden";
}
function formatDate(date,show) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    if(show==1){
    return year + "-" + formatTen(month) + "-" + formatTen(day);
    }else{
    return year + "-" + formatTen(month);
    }
}
function formatTen(num) {
    return num > 9 ? (num + "") : ("0" + num);
}

$("#loginOut").click(function(){
  $.ajax({
      url:"/icc-front/compass/logout.action",
      datatype:"json",
      type:"post",
      data:{
          unifiedlogout:true
      },
      success:function (data) {
            window.location.href="http://10.186.125.230:37001/icc-front/compass/compassIndex.do";
      }
  })

});