layui.extend({
    common    :   'js/common',
    region    :   'layui/lay/mymodules/region'
}).use(['jquery','table','common','laydate','form','region'], function () {
    var $ = layui.jquery;
    var table = layui.table;
    var common=layui.common;
    var laydate=layui.laydate;
    var  form=layui.form;
    common.layuiTable.render(table,{
         elem: '#table-id'
        , url: '/statistics/statisticsUser/statisticsUserCountList' //模拟接口
        ,method:'post'
        ,totalRow: true
        , cellMinWidth: 10 //全局定义常规单元格的最小宽度，
        , cols: [[
             { field: 'id',align:'center', title: '序号',templet:'<div>{{d.LAY_INDEX }}</div>',totalRowText: '合计'}
            , { field: 'name', title: '小区',align:'center'}
            , { field: 'orgPidName', title: '物业',align:'center'}
            , { field: 'region', title: '区域',align:'center'}
            , { field: 'userCount', title: '总住户',totalRow: true,align:'center'}
            , { field: 'manSexUserCount', title: '男性住户',totalRow: true,align:'center'}
            , { field: 'womanSexUserCount', title: '女性住户',totalRow: true,align:'center'}
            , { field: 'unknownSexUserCount', title: '未知性别',totalRow: true,align:'center'}
            , { field: 'tenantUserCount', title: '租客',totalRow: true,align:'center'}
            , { field: 'lastMonthAddUserCount', title: '上月新增住户',totalRow: true,align:'center'}
        ]]
       , id: 'table-id'
        , page: true
        , limit: 10
        , done: function(res, curr, count){
             //格式化合计行数字
            var $currentTotalTable=$(".layui-table-total table");
            if($currentTotalTable){
                var $userCount= $currentTotalTable.find("td[data-field='userCount'] div");
                $userCount.text(Number($userCount.text()).toFixed(0));
                var $manSexUserCount=$currentTotalTable.find("td[data-field='manSexUserCount'] div");
                $manSexUserCount.text(Number($manSexUserCount.text()).toFixed(0));
                var $womanSexUserCount=$currentTotalTable.find("td[data-field='womanSexUserCount'] div");
                $womanSexUserCount.text(Number($womanSexUserCount.text()).toFixed(0));
                var $unknownSexUserCount=$currentTotalTable.find("td[data-field='unknownSexUserCount'] div");
                $unknownSexUserCount.text(Number($unknownSexUserCount.text()).toFixed(0));
                var $tenantUserCount=$currentTotalTable.find("td[data-field='tenantUserCount'] div");
                $tenantUserCount.text(Number($tenantUserCount.text()).toFixed(0));
                var $lastMonthAddUserCount=$currentTotalTable.find("td[data-field='lastMonthAddUserCount'] div");
                $lastMonthAddUserCount.text(Number($lastMonthAddUserCount.text()).toFixed(0));
            }
        }
    });

    /**
     * 查询
     */
    var queryTableFn=function() {
        var regionProvince= $('input[name="regionProvince"]').val();
        var regionCity= $('input[name="regionCity"]').val();
        var regionArea=$('input[name="regionArea"]').val();
        var residentialId=$('#org_id :selected').val();
        var orgPid=$('#org_pid :selected').val();
        //执行重载
        table.reload('table-id', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: {
                regionProvince:regionProvince,
                regionCity:regionCity,
                regionArea:regionArea,
                orgPid:orgPid,
                id:residentialId
            }
        });
    }
    var  active = {
        query: function(){
            queryTableFn();
        },
        reset: function(){
            //清空条件
            $('input[name="regionProvince"]').val("");
            $('input[name="regionCity"]').val("");
            $('input[name="regionArea"]').val("");
            $('#region').val("");
            //清空下拉框
            $('#org_id').val("");
            $('#org_pid').val("");
            // 刷新select标签
            form.render('select');
            queryTableFn();
        }, //统计
        statisticsUserCount:function () {
            // 总住户数，上月新增住户数，租客数，已认证小程序住户数
            common.sendAsyncRequest("/statistics/statisticsUser/statisticsUserCount","POST",null,function(result)  {
                var resultMap=result.data;
                if(resultMap){
                    //总住户数
                    var userTotalCount=resultMap['userTotalCount'];
                    if(userTotalCount){
                        $("#userTotalCount").text(userTotalCount);
                    }
                    //上月新增住户数
                    var lastMonthNewUserCount=resultMap['lastMonthNewUserCount'];
                    if(lastMonthNewUserCount){
                        $("#lastMonthNewUserCount").text(lastMonthNewUserCount);
                    }
                    //租客数
                    var tenantUserCount=resultMap['tenantUserCount'];
                    if(tenantUserCount){
                        $("#tenantUserCount").text(tenantUserCount);
                    }
                    //已认证小程序住户数
                    var weiXinUserCount=resultMap['weiXinUserCount'];
                    if(weiXinUserCount){
                        $("#weiXinUserCount").text(weiXinUserCount);
                    }
                }
            });
        },//导出
        export:function () {
            window.location.href = "/statistics/statisticsUser/exportUser";
        }

    };

    $(document).on('click','.layui-btn', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
        return false;
    });
    //日期年份选择
    laydate.render({
        elem: '#laydate-range-year'
        ,type: 'year'
    });
    //  初始化省市区
    layui.region.init({
        //  元素节点
        elem: '#region_body',
        //  触发此元素时显示
        trigger: '#region',
        //  选中省的回调
        provinceCallbackFn: function(data) {
            var address = data.province.text;
            $('#region').val(address);
            $('input[name="regionProvince"]').val(data.province.id);
        },
        //  选中市的回调
        cityCallbackFn: function(data) {
            var address = data.province.text + '-' + data.city.text;
            $('#region').val(address);
            $('input[name="regionCity"]').val(data.city.id);
        },
        //  选中区域的回调
        areaCallbackFn: function(data) {
            var address = data.province.text + '-' + data.city.text + '-' + data.county.text;
            $('#region').val(address);
            $('input[name="regionArea"]').val(data.county.id);
        },
        //  关闭时的回调
        hideCallbackFn: function(data) {
        },
        //清空回调
        clearFn : function(){
            $node.regionInput.val('');
            $node.provinceInput.val('');
            $node.cityInput.val('');
            $node.areaInput.val('');
        }
    });
    // 初始化下拉列表
    var initSelectFn = function(){
        //小区下拉框
        common.sendAsyncRequest("/estate/agency/findResidentialListByCondition",'GET',{},function(result){
            var node = $('#org_id');
            //  清空元素
            node.html('');
            node.append('<option value="">全部</option>');
            result.list.forEach(function(item) {
                node.append('<option value="' + item.id + '">' + item.name + '</option>');
            });
            //重新渲染
            form.render('select');
            node.next().find('dl dd').eq(0).click();
            $('#parent_name').val( node.next().find('dl dd').eq(0).click().text());
        });
        //物业下拉框
        common.sendAsyncRequest("/estate/agency/findOrgListByCondition",'GET',{},function(result){
            var node = $('#org_pid');
            //  清空元素
            node.html('');
            node.append('<option value="">全部</option>');
            result.list.forEach(function(item) {
                node.append('<option value="' + item.id + '">' + item.name + '</option>');
            });
            //重新渲染
            form.render('select');
            node.next().find('dl dd').eq(0).click();
            $('#org_pid_name').val( node.next().find('dl dd').eq(0).click().text());
        });
    };
    //初始化
    var init=function(){
        //统计住户
        active.statisticsUserCount();
        // 小区下拉列表
        initSelectFn();

    }
    //初始化数据
    init();
});

