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

    var $node = {
        //卡号
        cardNoInput: $('input[name="cardNo"]'),
        //  有效期开始日期
        beginDateInput: $('input[name="beginDate"]'),
        //  有效期截至日期
        endDateInput: $('input[name="endDate"]'),
        createDate: $('#create_date'),
        //处理状态
        handleStatusSelect: $('#handle_status'),
        //指令描述
        instructCodeSelect: $('#instruct_code')
    }

    //日期时间范围
    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/pagingQuery?&serialNumber=' + serialNumber
        ,method: 'POST'
        ,cols: [[
            { field: 'instructCode', title: '指令编号',align:'center' }
            ,{ field: 'instructDescribe', title: '指令描述',align:'center' }
            , {
                field: 'createTime',
                title: '推送时间',
                align: 'center',
                width: 90,
                templet: '<div>{{ d.createTime ? layui.util.toDateString(d.createTime) : "" }}</div>'
            }
            , {
                field: 'lastModifyTime',
                title: '响应时间',
                align: 'center',
                width: 90,
                templet: '<div>{{ d.lastModifyTime ? layui.util.toDateString(d.lastModifyTime) : "" }}</div>'
            }
            , {
                field: 'status', title: '消费状态', align: 'center', width: 100, sort: true, 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: 'bizStatus', title: '业务状态', align: 'center', templet: function (entity) {
                    var result = undefined;
                    switch (entity['bizStatus']) {
                        case 100:
                            result = '升级包下载中';
                            break;
                        case 101:
                            result = '升级包已下载';
                            break;
                        case 102:
                            result = '升级包下载失败';
                            break;
                        case 200:
                            result = '设备更新中';
                            break;
                        case 201:
                            result = '设备已更新';
                            break;
                        case 202:
                            result = '设备更新失败';
                            break;
                        case 300:
                            result = '设备重启中';
                            break;
                        case 301:
                            result = '设备重启成功';
                            break;
                        case 302:
                            result = '设备重启失败';
                            break;
                        case 400:
                            result = '素材下载中';
                            break;
                        case 401:
                            result = '素材下载完成';
                            break;
                        case 402:
                            result = '素材下载失败';
                            break;
                        default:
                            result = '';
                            break;
                    }
                    return result;
                }
            }
            , {
                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: 'invalid', title: '是否过期作废', align: 'center', sort: true, templet: function (entity) {
                    var result = undefined;
                    switch (entity['invalid']) {
                        case 1:
                            result = '正常';
                            break;
                        case 2:
                            result = '已过期作废';
                            break;
                        default:
                            result = '';
                            break;
                    }
                    return result;
                }
             }
            , {
                field: 'resendTime',
                title: '最近重发时间',
                align: 'center',
                width: 90,
                templet: '<div>{{ d.lastResendTime ? layui.util.toDateString(d.lastResendTime) : "" }}</div>'
            }
            ,{field: 'xqName', title: '小区名称',align:'center'}
            ,{field: 'cardNo', title: '卡号',align:'center'}
            ,{ title: '操作',width:180, align: 'center',toolbar: '#table-operate'}
        ]]
    });

    //排序
    layui.table.on('sort(data_table_event)',function (obj) {
        layuiTable.reload({
            where:{
                //排序
                sortField   :   obj?obj.field:'',
                sortOrder   :   obj?obj.type:'',
            }
            //重新从第1页开始
            ,page: {
                curr: 1
            },
            initSort:obj
        });
    });

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

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

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

    //  头工具栏事件
    layui.table.on('toolbar(data_table_event)', function(obj){
        // switch(obj.event){
        //     default:
        //         break;
        // }
    });

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

    //  操作栏事件绑定
    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(resendList)', function () {
        layer.confirm('是否确认重发？', {icon: 7, title: '提示'}, function (index) {
            $.ajax({
                url: '/device/instruct/resend/list',
                type: 'POST',
                contentType: "application/json;charset=utf-8",
                data: serialNumber,
                success: function (result) {
                    if (result.code == '200') {
                        if (result.data) {
                            layer.msg("指令下发成功");
                            location.reload();
                        } else {
                            layer.msg("指令下发失败");
                        }
                    } else {
                        layer.msg(result.msg);
                    }
                }
            });
        });
    });
});
