layui.extend({
    common    :   'js/common',
}).use(['jquery','common'], function () {
    var $ = layui.jquery;
    //  弹窗插件
    var layer = layui.layer;
    var common = layui.common;

    // 项目id
    var projectId = $('#projectId').val();
    //小区id
    var residentialId = $("#residentialId").val();

    //参数
    var params ={};

    //区安装记录中审核通过的设备数量
    params['residentialId'] = residentialId;
    params['checkState'] = 1;
    common.sendAsyncRequest("/implement/device/installation/findByResidentialId",'POST',params,function (res){
        var list = res.list;
        var deviceInstallationIds = new Array();
        var pillar = 0;
        var wallHung = 0;
        var rainCover = 0;
        for(var i in list){
            deviceInstallationIds.push(list[i].id);
            if (list[i].deviceType==0){
                ++pillar;
            } else if (list[i].deviceType == 1){
                ++wallHung;
            } else if (list[i].deviceType == 2){
                ++rainCover;
            }
        }
        var deviceCount = pillar + wallHung + rainCover;
        $('#pillar').text(pillar);
        $('#wallHung').text(wallHung);
        $('#rainCover').text(rainCover);
        $('span[name="deviceCount"]').text(deviceCount);
        if(deviceInstallationIds.length>0){
            getDeviceTestCount(deviceInstallationIds);
        }
    });

    //小区定位记录中审核通过的数量
    params = {};
    params['projectId'] = projectId;
    common.sendAsyncRequest("/implement/position/findByProjectId",'POST',params,function (res){
        var position = res.data;
        var positionCount = 0;
        if (position){
            if (position.pillar) {
                positionCount = positionCount + position.pillar==null?0 : position.pillar;
            }
            if (position.wallHung) {
                positionCount = positionCount + position.wallHung==null?0 : position.wallHung;
            }
            if (position.rainCover) {
                positionCount = positionCount + position.rainCover==null?0 : position.rainCover;
            }
            getInstallationReviewCount(position.id);
        }
        $("#positionCount").text(positionCount);
    });

    //获取小区信息
    params = {};
    params['residentialId'] = residentialId;
    common.sendAsyncRequest("/implement/project/findResidentialById",'POST',params,function (res){
        var residential = res.data;
        if(residential){
            var totalUnitNumber = 0;
            var totalRoomNumber = 0;
            var totalUserNumber = 0;
            var unitNumber = 0;
            var roomNumber = 0;
            var userNumber = 0;
            var userRate = 0;
            if (residential.totalUnitNumber){
                totalUnitNumber = residential.totalUnitNumber
            }
            if (residential.totalRoomNumber) {
                totalRoomNumber = residential.totalRoomNumber
            }
            if (residential.totalUserNumber){
                totalUserNumber = residential.totalUserNumber
            }
            if (residential.unitNumber) {
                unitNumber = residential.unitNumber
            }
            if (residential.roomNumber) {
                roomNumber = residential.roomNumber
            }
            if (residential.userNumber) {
                userNumber = residential.userNumber
            }
            if (totalUserNumber != 0 && userNumber!=0){
                userRate = (userNumber / totalUserNumber * 100).toFixed(2);
            }
            $("#totalUnitNumber").text(totalUnitNumber);
            $("#totalRoomNumber").text(totalRoomNumber);
            $("#totalUserNumber").text(totalUserNumber);
            $("#unitNumber").text(unitNumber);
            $("#roomNumber").text(roomNumber);
            $("#userNumber").text(userNumber);
            $("#userRate").text(userRate);
        }
    });


    function getDeviceTestCount(deviceInstallationIds) {
        params = {};
        params['deviceInstallationIds'] = deviceInstallationIds.toString();
        common.sendAsyncRequest("/implement/device/test/findByDeviceInstallationIds",'POST',params,function (res){
            var deviceTestCount = 0;
            var list = res.list;
            if (list) {
                deviceTestCount = list.length;
            }
            $('span[name="deviceTestCount"]').text(deviceTestCount);
        });
    }
    
    function getInstallationReviewCount(positionId) {
        params = {};
        params['positionId'] = positionId;
        common.sendAsyncRequest("/implement/environmental/review/findByPositionId",'POST',params,function (res){
            var environmentalReview = res.data;
            var installationReviewCount = 0;
            if(environmentalReview){
                if (environmentalReview.pillar) {
                    installationReviewCount = installationReviewCount + environmentalReview.pillar;
                }
                if (environmentalReview.wallHung) {
                    installationReviewCount = installationReviewCount + environmentalReview.wallHung;
                }
                if (environmentalReview.rainCover) {
                    installationReviewCount = installationReviewCount + environmentalReview.rainCover;
                }
            }
            $('#installationReviewCount').text(installationReviewCount);
        });
    }
});


