layui.extend({
    common    :   'js/common'
}).use(['jquery','table','layer','form','common','util'], function () {
    var $ = layui.jquery;
    var table = layui.table;
    //  弹窗插件
    var layer = layui.layer;
    //  表单插件
    var form = layui.form;
    var common = layui.common;
    //表格对象
    var layuiTable = common.layuiTable.render(table,{
        elem: '#table-id'  //页面table的id
        , url: '/ad/push/task/showAdPushTaskList?pushStatus=3' //后台接口
        ,method: 'post'
        , cellMinWidth: 80 //全局定义常规单元格的最小宽度，
        , cols: [[
            { field: 'adTitle', title: '广告名称', align: 'center'}
            , { field: 'taskType', title: '广告类型',templet:'#taskType', align: 'center'}
            , { field: 'pushType', title: '发布方式',templet:'#pushType', align: 'center'}
            , { title: '发布范围', align: 'center',align:'center',templet:function (entity){
                    var names="";
                    var pushRangeNameList=entity.adPushRangeNames;
                    for(var i in pushRangeNameList){
                        if(i==0){
                            names+=pushRangeNameList[i];
                        }else{
                            names+=","+pushRangeNameList[i];
                        }
                    }
                    return names;
                } }
            , { field: 'pushSuccess', align: 'center', title: '发布结果',templet:'#adPushResult'}
            , { field: 'pushStatus', align: 'center', title: '发布状态',templet:'#pushStatus'}
            , { field: 'estimatePushDate', align: 'center', title: '发布时间',templet:'<div>{{ d.estimatePushDate ? layui.util.toDateString(d.estimatePushDate) : "" }}</div>' }
            , { field: 'beginDate', align: 'center', title: '开播开始时间',templet:'<div>{{ d.beginDate ? layui.util.toDateString(d.beginDate) : "" }}</div>' }
            , { field: 'endDate', align: 'center', title: '开播结束时间',templet:'<div>{{ d.endDate ? layui.util.toDateString(d.endDate) : "" }}</div>' }
            , { field: '',title: '操作', toolbar: '#table-operate-id', align: 'center',width:180}
        ]]
        //  开启分页功能
        , page: true
    });

    //监听行工具事件  lay-filter="table-id"
    table.on('tool(table-id)', function(obj){
        var data = obj.data;
        if(obj.event === 'task_detail'){
            common.openDefaultIframe({
                title   :   '监控详情',
                content :   '/ad/device/details/view?taskId='+data.id
            });
        } else if(obj.event === 'manually_issued'){ //手动下发
            layer.confirm('是否手动下发广告任务？', function(index){
                console.info(data.id);
                common.sendAsyncRequest("/ad/push/manually/issued/"+data.id,'GET',"",function (result){
                    if(result.data==true){
                        layer.msg("下发成功");
                    }else{
                        layer.msg("下发失败");
                    }
                    //刷新表格
                    layuiTable.reload();
                    layer.close(index);//关闭弹窗提示
                });
            });
        }
    });

    //  重新加载表格
    var reloadTable = function(){
        var adTitle = $('#selectedAdTitle').val();
        var taskType = $('#selectedAdType').val();
        //重新加载表格
        layuiTable.reload({
            where:{
                adTitle    :   adTitle,
                taskType     :   taskType,
            }
            //重新从第1页开始
            ,page: {
                curr: 1
            }
        });
    };

    //监听查询
    form.on('submit(query)', function(){
        reloadTable();
    });

    //监听重置
    form.on('submit(reset)', function(){
        $('#selectedAdTitle').val('');
        $('#selectedAdType').val('');
        form.render('select');
        reloadTable();
    });

});

