layui.extend({
    common    :   'js/common',
    region    :   'layui/lay/mymodules/region'
}).use(['jquery','table','common','form','region'], function () {
    var $ = layui.jquery;
    var table = layui.table;
    var common=layui.common;
    var  form=layui.form;
    common.layuiTable.render(table,{
         elem: '#table-id'
         , url: '/statistics/statisticsAdWatch/showStatisticsAdClickMobileRankList' //模拟接口
         ,method:'post'
         , cellMinWidth: 80 //全局定义常规单元格的最小宽度，
         , cols: [[
            { field: 'adName', title: '广告名称',align:'center'}
            , { field: 'adCode', title: '广告编号',align:'center'}
            , { field: 'adTypeName', title: '所属广告类别',align:'center'}
            , { field: 'clickCount', title: '点击次数',align:'center'}
         ]]
        , id: 'table-id'
        , page: true
        , limit: 10
    });

    var startDate,endDate;
    var queryTableFn=function(){
        var selectedAdTypeId = $('#selectedAdTypeId').val();
        var regionProvince= $('input[name="regionProvince"]').val();
        var regionCity= $('input[name="regionCity"]').val();
        var regionArea=$('input[name="regionArea"]').val();
        //执行重载
        table.reload('table-id', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: {
                adTypeId:selectedAdTypeId,
                startDate:startDate,
                endDate:endDate,
                regionProvince:regionProvince,
                regionCity:regionCity,
                regionArea:regionArea
            }
        });
    }

    //获取根机构下面所有广告类型Adtype
    common.sendAsyncRequest("/ad/type/getAdTypeList","GET",{},function(result) {
        var $select = $('#selectedAdTypeId');
        result.list.forEach(function(item) {
            $select.append('<option value="' + item.id + '">' + item.name+ '</option>')
        });
        // 刷新select标签
        form.render('select');
    });

    //  初始化省市区
    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('');
        }
    });

    //监听搜索
    form.on('submit(query)', function(){
        queryTableFn();
    });

    //监听重置查询
    form.on('submit(reset)', function(){
        //清空条件
        $('#selectedAdTypeId').val("");
        $('input[name="regionProvince"]').val("");
        $('input[name="regionCity"]').val("");
        $('input[name="regionArea"]').val("");
        $('#region').val("");
        startDate="";
        endDate="";
        $("#laydate-range-datetime").val("");
        // 刷新select标签
        form.render('select');
        queryTableFn();
    });
});

