/**
 * Created by wangwei on 2017-2-14.
 */

function getTotalContractCount(contextPath){

    $.ajax({
        type: 'GET',
        url: contextPath + '/csh/contract/totalCount/query',
        contentType: "application/json; charset=utf-8",
        success: function (datas) {
            if (datas[0]) {
                $("#con-contract-total-count").append( datas[0].contractCount);
                getInceptReceiptContractCount(datas[0].contractCount,contextPath);
                //getInceptNotReceiptCount(datas[0].contractCount);
                //getTerminatingCount(datas[0].contractCount);
                //getSignNotInceptCount(datas[0].contractCount);
                //getTerminateCount(datas[0].contractCount);
            } else {
                $("#con-contract-total-count").append('0.00%');
            }
        }
    });
}

function getInceptReceiptContractCount(conContractTotal,contextPath){
    $.ajax({
        type: 'GET',
        //url: contextPath + '/csh/contract/diffContractStatusCount/query',
        url:contextPath + '/con/conHomepage/statisticalChart/query',
        contentType: "application/json; charset=utf-8",
        success: function (datas) {
            /*var inceptReceipt=0;
            var inceptNotReceipt=0;
            var terminating=0;
            var signNotIncept=0;*/


            var conNewCount = 0;
            var conSignCount = 0;
            var conInceptCount = 0;
            var conEndCount = 0;
            var conCancelCount = 0;
            var conChangeCount = 0;

            var conNewCountPercent = 0;
            var conSignCountPercent = 0;
            var conInceptCountPercent = 0;
            var conEndCountPercent = 0;
            var conCancelCountPercent = 0;
            var conChangeCountPercent = 0;





            /*for(i=0;i<datas.length;i++){
                if(datas[i].contract_status=="INCEPT_RECEIPT"){
                    $("#Incept-Receipt-Contract-Count").append( (datas[i].contractCount/conContractTotal*100).toFixed(2)+'%');
                    inceptReceipt=datas[i].contractCount;
                }
                if(datas[i].contract_status=="INCEPT_NOT_RECEIPT"){
                    $("#incept-Not-Receipt-Count").append( (datas[i].contractCount/conContractTotal*100).toFixed(2)+'%');
                    inceptNotReceipt=datas[i].contractCount;
                }
                if(datas[i].contract_status=="TERMINATING"){
                    $("#terminating-Count").append( (datas[i].contractCount/conContractTotal*100).toFixed(2)+'%');
                    terminating=datas[i].contractCount;
                }
                if(datas[i].contract_status=="SIGN_NOT_INCEPT"){
                    $("#sign-Not-Incept-Count").append( (datas[i].contractCount/conContractTotal*100).toFixed(2)+'%');
                    signNotIncept=datas[i].contractCount;
                }

            }
            var others=conContractTotal-inceptReceipt-inceptNotReceipt-terminating-signNotIncept;
            $("#terminate-Count").append( (others/conContractTotal*100).toFixed(2)+'%');*/

            for (i = 0; i < datas.length; i++) {
                if (datas[i].contractStatus == "NEW") {
                    //$("#new-Count").html(datas[i].contractCountPercent + '%');
                    conNewCountPercent = datas[i].contractCountPercent;
                    conNewCount = datas[i].contractCount;
                }
                if (datas[i].contractStatus == "SIGN") {
                    //$("#sign-Count").html(datas[i].contractCountPercent + '%');
                    conSignCountPercent = datas[i].contractCountPercent;
                    conSignCount = datas[i].contractCount;
                }
                if (datas[i].contractStatus == "INCEPT") {
                    //$("#incept-Count").html(datas[i].contractCountPercent + '%');
                    conInceptCountPercent =  datas[i].contractCountPercent;
                    conInceptCount = datas[i].contractCount;
                }
                if (datas[i].contractStatus == "PENDING") {
                    //$("#change-Count").html(datas[i].contractCountPercent + '%');
                    conChangeCountPercent = datas[i].contractCountPercent;
                    conChangeCount = datas[i].contractCount;
                }
                if (datas[i].contractStatus == "TERMINATE") {
                    //$("#terminate-Count").html(datas[i].contractCountPercent + '%');
                    conEndCountPercent = datas[i].contractCountPercent;
                    conEndCount = datas[i].contractCount;
                }
                if (datas[i].contractStatus == "CANCEL") {
                    //$("#cancel-Count").html(datas[i].contractCountPercent + '%');
                    conCancelCountPercent=datas[i].contractCountPercent;
                    conCancelCount = datas[i].contractCount;
                }
            }
            $("#new-Count").html(conNewCountPercent + '%');
            $("#sign-Count").html(conSignCountPercent + '%');
            $("#incept-Count").html(conInceptCountPercent + '%');
            $("#change-Count").html(conChangeCountPercent + '%');
            $("#terminate-Count").html(conEndCountPercent + '%');
            $("#cancel-Count").html(conCancelCountPercent + '%');


            //合同总数
            /*var data = [
                {
                    data: [[1, inceptReceipt]],
                    color: '#58b0ff'
                },
                {
                    data: [[1, inceptNotReceipt]],
                    color: '#fd6d4c'
                },
                {
                    data: [[1, terminating]],
                    color: '#e9598d'
                },
                {
                    data: [[1, signNotIncept]],
                    color: '#a5d868'
                },
                {
                    data: [[1, others]],
                    color: '#ffcf48'
                }
            ];*/
            var data = [
                {
                    label: "新建",
                    value: conNewCount
                    //color: '#58b0ff'
                },
                {
                    label: "签约",
                    value: conSignCount
                    //color: '#fd6d4c'
                },
                {
                    label: "起租",
                    value: conInceptCount
                    //color: '#e9598d'
                },
                {
                    label: "变更",
                    value: conChangeCount
                    //color: '#a5d868'
                },
                {
                    label: "结束",
                    value: conEndCount
                    //color: '#ffcf48'
                },
                {
                    label: "取消",
                    value: conCancelCount
                    // color: '#00b3b3'
                }
            ];

            baseInfoContractPie.init('row-second-middle-top-body-foot1', data);

            /*var placeholder = $("#row-second-middle-top-body-foot1");
            placeholder.unbind();

            $.plot(placeholder, data, {
                series: {
                    pie: {
                        innerRadius: 0.45,
                        show: true,
                        stroke: {
                            width: 4
                        }
                    }
                }
                ,
                grid: {
                    clickable: true
                }
            });
            //pie的点击事件
            $("#row-second-middle-top-body-foot1").bind("plotclick", function pieClick(event, pos, obj){
                if (!obj) {
                    return;
                }
                //根据颜色获取点击的是什么合同
               var objArray = {"#58b0ff": 'inceptReceipt', "#fd6d4c":"inceptNotReceipt","#e9598d":"terminating","#a5d868":"signNotIncept","#ffcf48":"others"};
               var color = obj.series.color;
               alert(objArray[color]+'合同清单待开发');


            });*/
        }
    });
}



var baseInfoContractPie = function () {
    return {
        init: function (element_v, datas) {
            var Donut = Morris.Donut({
                element: element_v,
                data: datas,
                colors: ['#2EC3E8', '#FA6A4D', '#E75B8D', '#A0D469', '#FFCD55', '#14A9CC'],
                formatter: function (y) {
                    return y;
                }
            });
            Donut.handlers = Donut.handlers || {};
            Donut.handlers.click = Donut.handlers.click || [];
            Donut.handlers.click.push(function (arg, arg1) {
                if (arg1.label == "新建") {
                    top.openTab('CON030A','合同综合查询',contextPath+'/cont/CON030A/contract_comprehensive_query.view?contractStatus=新建、',null,null,'Y');
                }
                else if (arg1.label == "签约") {
                    top.openTab('CON030A','合同综合查询',contextPath+'/cont/CON030A/contract_comprehensive_query.view?contractStatus=签约、',null,null,'Y');
                }
                else if (arg1.label == "取消") {
                    top.openTab('CON030A','合同综合查询',contextPath+'/cont/CON030A/contract_comprehensive_query.view?contractStatus=取消、',null,null,'Y');
                }
                else if (arg1.label == "起租") {
                    top.openTab('CON030A','合同综合查询',contextPath+'/cont/CON030A/contract_comprehensive_query.view?contractStatus=起租、',null,null,'Y');
                }
                else if (arg1.label == "变更") {
                    top.openTab('CON030A','合同综合查询',contextPath+'/cont/CON030A/contract_comprehensive_query.view?contractStatus=暂挂、',null,null,'Y');
                }
                else if (arg1.label == "结束") {
                    top.openTab('CON030A','合同综合查询',contextPath+'/cont/CON030A/contract_comprehensive_query.view?contractStatus=正常结清、',null,null,'Y');
                }
            });
        }
    };
}();



function stockCountAndMonthNewCount(contextPath){
    $.ajax({
        type: 'GET',
        url: contextPath+'/csh/contract/stockCountAndMonthNewCount/query',
        contentType: "application/json; charset=utf-8",
        success: function (datas) {
            var radio=1;
            if (datas[0]) {
                radio=(datas[0].stockContractCount/(datas[0].stockContractCount+datas[0].newContractCount));
                //con-percent-color-1
            }
            var width1=(350*radio).toFixed(0);
            var width2=350-width1;
            $('#con-percent-color-1').width(width1);
            $('#con-percent-color-2').width(width2);
            //初始化完成后添加click事件
            $('#con-percent-color-1').click(function(){
                alert('TOTAL合同清单待开发！');
            });
            $('#con-percent-color-2').click(function(){
                alert('MONTH合同清单待开发！');
            });
        }
    });

}



function conContractInit(contextPath) {
    getTotalContractCount(contextPath);
    stockCountAndMonthNewCount(contextPath);
}


