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

    var $node = {
        //设备序列号
        serialNumberInput: $('input[name="serialNumber"]'),
        //  有效期开始日期
        beginDateInput: $('input[name="beginDate"]'),
        //  有效期截至日期
        endDateInput: $('input[name="endDate"]'),
        createDate: $('#create_date'),
        //处理状态
        handleStatusSelect: $('#handle_status')
    }

    //日期时间范围
    laydate.render({
        elem: '#create_date'
        , type: 'datetime'
        , range: true
        , min: '1990-1-1 00:00:00'
        , done: function (value, date, endDate) {
            var dateArray = value.split(" - ");
            $("#begin_date").val(dateArray[0]);
            $("#end_date").val(dateArray[1]);
        }
    });

    //  初始化表单
    var layuiTable = layui.common.layuiTable.render(layui.table,{
        elem: '#data_table'
        ,url: '/device/instruct/pageFace?&publishData=' + userId
        ,method: 'POST'
        ,cols: [[
            { field: 'serialNumber', title: '设备序列号',align:'center' }
            ,{ field: 'instructDescribe', title: '指令描述',align:'center' }
            ,{ field: 'createTime', title: '推送时间',align:'center',width:180,templet:'<div>{{ d.createTime ? layui.util.toDateString(d.createTime) : "" }}</div>' }
            ,{ field: 'lastModifyTime', title: '响应时间',align:'center',width:180,templet:'<div>{{ d.lastModifyTime ? layui.util.toDateString(d.lastModifyTime) : "" }}</div>' }
            ,{ field: 'status', title: '状态',align:'center',width:100,templet:function (entity) {
                    var result = undefined;
                    switch (entity['status']) {
                        case 1:
                            result = '待消费';
                            break;
                        case 2:
                            result = '已消费';
                            break;
                        default:
                            result = '异常';
                            break;
                    }
                    return result;
                }
            }
            ,{ field: 'errorMsg', title: '异常信息',align:'center' }
            ,{ field: 'publishData', title: '推送数据',align:'center' }
            ,{ field: 'handleStatus', title: '处理状态',align:'center',sort:true, templet:function (entity) {
                    var result = undefined;
                    switch (entity['handleStatus']) {
                        case 1:
                            result = '待处理';
                            break;
                        case 2:
                            result = '已处理';
                            break;
                        case 3:
                            result = '处理失败';
                            break;
                        default:
                            result = '';
                            break;
                    }
                    return result;
                }
            }
            ,{ field: 'resendTime', title: '最近重发时间',align:'center',width:180,templet:'<div>{{ d.lastResendTime ? layui.util.toDateString(d.lastResendTime) : "" }}</div>' }
            ,{ title: '操作',width:180, align: 'center',toolbar: '#table-operate'}
        ]]
    });


    //  操作栏事件绑定
    layui.table.on('tool(data_table_event)',function (obj) {
        switch (obj.event) {
            case 'resend' :
                layer.confirm('是否重发该指令?', {icon: 7, title:'提示'},function(index) {
                    $.ajax({
                        url : '/device/instruct/resend/' + obj.data.id,
                        type : 'GET',
                        success: function (result) {
                            if(result.data){
                                layer.alert('重发成功');
                                // layuiTable.reload();
                                location.reload();
                            }else {
                                layer.alert('重发失败')
                            }
                        }
                    });
                });
                break;
            default:
                break;
        }
    });

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

    //监听重置查询
    form.on('submit(reset)', function () {
        $node.handleStatusSelect.val(''),
            $node.createDate.val(''),
            $node.serialNumberInput.val(''),
            $node.beginDateInput.val(''),
            $node.endDateInput.val(''),
            form.render('select'),
            reloadTable();
    });

    //  重新加载表格
    var reloadTable = function (obj) {
        //重新加载表格
        layuiTable.reload({
            where: {
                serialNumber: $node.serialNumberInput.val(),
                handleStatus:$node.handleStatusSelect.val(),
                beginDate: $node.beginDateInput.val(),
                endDate: $node.endDateInput.val(),
                // //排序
                // sortField: obj ? obj.field : '',
                // sortOrder: obj ? obj.type : '',
            }
            //重新从第1页开始
            , page: {
                curr: 1
            },
            initSort: obj
        });
    };

    //监听刷新
    layui.form.on('submit(refresh)', function(){
        location.reload();
    });
});
