"use strict";
layui.extend({
    common: 'js/common',
    orgTree: 'layui/lay/mymodules/org-tree',
}).use(['jquery', 'layer', 'common', 'table', 'orgTree', 'form',], function () {
    //  弹窗插件
    var layer = layui.layer;

    var table = layui.table;
    //  Jquery插件
    var $ = layui.jquery;
    //  引入公共模块
    var common = layui.common;
    //引入表单
    var form = layui.form;
    var dropdown = layui.dropdown;
    var $node = {
        nameInput: $('#name'),
        serialNumberInput: $('#serial_number'),
        onLineStatusSelect: $('#on_line_status'),

        id: $('input[name="id"]'),
    };

    //  数据
    var Data = {
        //  当前设备厂商
        deviceFactory: $('#device_factory').val(),
        //  当前设备型号
        deviceModelNumber: $('#device_model_number').val()
    };

    //  初始化表单
    var layuiTable = common.layuiTable.render(table, {
            elem: '#table_id',
            url: '/permissions/appPackage/getPackagePageByFactory?deviceFactory=' + Data.deviceFactory + '&deviceModelNumber=' + Data.deviceModelNumber,
            cellMinWidth: 80, //全局定义常规单元格的最小宽度，
            method: "get",
            cols: [[
                {type: 'radio'},
                {field: 'version', title: '版本号', align: 'center'},
                {field: 'createDate', title: '上传时间', align: 'center', minWidth: 130},
                {field: 'deviceFactory', title: '设备厂商', align: 'center'},
                {field: 'deviceModelNumber', title: '设备型号', align: 'center'},
                {field: 'description', title: '版本说明', align: 'center'},
                {
                    field: 'isEnable', title: '启用状态', align: 'center', templet: function (d) {
                        if (d.isEnable == '1') {
                            return "启用"
                        } else if (d.isEnable == '2') {
                            return "禁用"
                        } else {
                            return "未知"
                        }
                    }
                },
            ]]
            , page: true
        })
    ;

    var selectPkgId = null;
    table.on('radio(table_id)', function (obj) {
        if (obj.checked) {
            selectPkgId = obj.data.id;
            if (obj.data.isEnable === 1) { //只有选中启用状态的升级包，才允许提交，否则都禁用提交
                $('#submit_btn').removeClass('layui-btn-disabled').removeAttr('disabled', 'disabled');
            } else {
                $('#submit_btn').addClass("layui-btn-disabled").attr("disabled", true);
            }
        }
    });
    form.on('submit(selectConfirm)', function (data) {
        layer.confirm('是否确认提交？', {icon: 7, title: '提示'}, function (index) {
            common.timer($('button[lay-filter="selectConfirm"]'));
            var params = {};
            //  选中升级包ID
            params['id'] = selectPkgId;
            params['deviceId'] = Array.from(window.parent.selectDeviceIds).join();
            common.sendAsyncRequest("/permissions/appPackage/deviceSelectPackageUpgrade", 'POST', params, callbackFn);
        });
    });
    //取消按钮
    $('#cancel_btn').on("click", function () {
        //获取当前弹窗出层的名字
        var index = parent.layer.getFrameIndex(window.name);
        //关闭弹窗层
        parent.layer.close(index);
    });

    //回调函数
    function callbackFn(result) {
        if (result.code == '200') {
            layer.msg("操作成功", {time: 1000, shade: [0.1, '#000', true]}, function () {
                // 父页面刷新
                parent.location.reload();
                //获取当前弹窗出层的名字
                var mylay = parent.layer.getFrameIndex(window.name);
                //关闭弹窗层
                parent.layer.close(mylay);
            });
        } else {//添加失败
            layer.msg("操作失败");
        }
    }

    //  重新加载表格
    var selectedItem = undefined;
    var reloadTable = function () {
        //  重新加载表格
        layuiTable.reload({
            //  设定异步数据接口的额外参数
            where: {
                name: $node.nameInput.val(),
                serialNumber: $node.serialNumberInput.val(),
                onlineStatus: $node.onLineStatusSelect.val(),
                selectedId: selectedItem ? selectedItem.id : ''
            }
            //  重新从第1页开始
            , page: {
                curr: 1
            }
        });
    };
});
