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/statisticsAdWatch/showStatisticsAdWatchList' //模拟接口
        ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，
        ,method:'post'
        , cols: [[
             { field: 'id',align:'center', title: '序号',templet:'<div>{{d.LAY_INDEX }}</div>'}
            , { field: 'adName',align:'center', title: '广告名称'}
            , { field: 'adCode',align:'center', title: '广告编号'}
            , { field: 'adTypeName',align:'center', title: '广告类别'}
            , { field: 'residentialName',align:'center', title: '所属小区'}
            , { field: 'installPath',align:'center', title: '设备位置'}
            , { field: 'userName',align:'center', title: '观看用户',templet:function(d){
                    if(d.userType>=0){
                        return d.userName==null?"":d.userName;
                    }else{
                        return "访客";
                    }
                }}
            , { field: 'watchDate',align:'center', title: '观看时间' }
         ]]
       , id: 'table-id'
       , page: true
       , limit: 20
    });
    var startDate,endDate;


    /**
     * 查询
     */
    var queryTableFn=function() {
        var adCode = $('input[name="adCode"]').val();
        var adName = $('input[name="adName"]').val();
        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: {
                adCode: adCode,
                adName: adName,
                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');
    });

    //日期时间范围选择
    laydate.render({
        elem: '#laydate-range-datetime'
        ,type: 'datetime'
        ,range:true //或 range: '~' 来自定义分割字符
        ,done: function(value, date, endDate){
            //date得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
            if(value){
                var dates=value.split(" - ");
                startDate=dates[0];
                endDate=dates[1];
            }else{
                startDate="";
                endDate="";
            }
        }
    });

    //  初始化省市区
    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(){
        //清空条件
        $('input[name="adCode"]').val("");
        $('input[name="adName"]').val("");
        $('#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();
    });

    //监听导出
    form.on('submit(export)', function(){
        window.location.href = "/statistics/statisticsAdWatch/exportAdWatch";
    });
});

