"use strict";
layui.extend({
    common    :   'js/common'
}).use(['jquery','layer','common','table','laydate','form'],function () {
    var $ = layui.jquery;
    var taskType = $('input[name="taskType"]').val();
    //  引入表单
    var form = layui.form;


    //日期时间范围
    layui.laydate.render({
        elem: '#date_range'
        ,format: 'yyyy-MM-dd'
        ,max: 0 //最小日期为当天
    });

    //  初始化表单
    var layuiTable = layui.common.layuiTable.render(layui.table,{
        elem: '#data_table'
        ,url: '/ad/face/early_warning/pagingQuery?&taskType=' + taskType
        ,cols: [[
            { field: 'deviceName', title: '设备名称',align:'center' }
            ,{ field: 'serialNumber', title: '序列号',align:'center' }
            ,{ field: 'installPath', title: '安装位置',align:'center',width:130 }
            ,{ field: 'type', title: '预警类型',align:'center',width:130 }
            ,{ field: 'expect', title: '预警值',align:'center',width:130 }
            ,{ field: 'value', title: '播放次数',align:'center',width:130 }
            ,{ field: 'createTime', title: '创建时间',align:'center',width:180,templet:'<div>{{ layui.util.toDateString(d.createTime) }}</div>' }
            ,{ field: 'endTime', title: '时间',align:'center',width:180,templet:function (entity) {
                var result = layui.util.toDateString(entity['endTime'],'yyyy-MM-dd') + '&nbsp;&nbsp;';
                if (entity['type'] === 2){
                    result += '第' + entity['weekNum'] + '周';
                }
                return result;
            }}
            ,{ field: 'endFlag', title: '状态',align:'center',width:100,templet:'#end_flag' }
        ]]
    });

    //  重新加载表格
    var reloadTable = function() {
        var where = {
            deviceName  :   $('input[name="deviceName"]').val(),
            serialNumber  :   $('input[name="serialNumber"]').val(),
            time :   $('input[name="time"]').val()
        };
        layuiTable.reload({ where: where ,page: { curr: 1 } });
    };

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

    //监听重置
    form.on('submit(reset)', function(){
        $('input[name="deviceName"]').val("");
        $('input[name="serialNumber"]').val("");
        $('input[name="time"]').val("");
        // 刷新
        form.render();
        reloadTable();
    });


});
