"use strict";
layui.extend({
    common    :   'js/common',
}).use(['jquery','laytpl','common','util','form','element','layer'],function(){
    var $ = layui.jquery;
    var form = layui.form;
    var layer = layui.layer;

    //  页面元素
    var $node = {
        //  Id  表单
        idInput: $('input[name="id"]')
    };

    $.ajax({
        url     :   '/device/manage/get/' + $node.idInput.val(),
        success :   function(result){
            var data = result.data;
            $.ajax({
                url: '/permissions/dict/getByCode/deviceFactory',
                type    :   'GET',
                success :   function(result){
                    if (result.list){
                        data.deviceFactoryDict = result.list
                    }
                },
                error : function (result) {
                    let s = result
                }
            });
            //获取设备配置
            $.ajax({
                url     :   '/device/options/getByDeviceId/' + $node.idInput.val(),
                type    :   'GET',
                success :   function(result){
                    if (result.data){
                        data.options = result.data
                    }
                },
                error : function (result) {
                    let s = result
                }
            });
            //获取流量卡信息
            if(data.flowCardId){
                $.ajax({
                    url     :   '/device/flowCard/get/' + data.flowCardId,
                    success :   function(result){
                        if (result.data){
                            data.flowCardNum = result.data.cardNum;
                            data.flowCardBeginDate = result.data.beginDate;
                            data.flowCardEndDate = result.data.endDate;
                        }
                    }
                });
            }
            //  数据渲染
            layui.laytpl($('#device_info_template').html()).render({ data : data }, function(htmlText){
                $('#device_info').html(htmlText);
            });
            var userId = data['createAccountId'];
            if (null != userId){
            //  获取用户信息
            $.ajax({
                url     :   '/permissions/account/get/' + userId,
                success :   function(result){
                    data.createAccountName = result.data.loginName;
                    //  数据渲染
                    layui.laytpl($('#device_info_template').html()).render({ data : data }, function(htmlText){
                        $('#device_info').html(htmlText);
                    });
                }
            });
            }
        }
    });

    //监听刷新
    form.on('submit(refresh)', function(){
        location.reload();
    });
    form.on('submit(obtainInfo)', function(){
        $.ajax({
            url: '/device/manage/info/obtain',
            type: 'POST',
            data: {
                id: $node.idInput.val()
            },
            success: function (result) {
                if (result.code === '200') {
                    layer.msg("远程获取设备信息指令已下发");
                } else {
                    layer.msg(result.msg);
                }
            }
        });
    });
});
