layui.config({
    base: '../../js/layuiModules/',   // 第三方模块所在目录
    version: 'v1.6.4' // 插件版本号
}).extend({
    soulTable: 'notice,layNotify',	// 模块
});
let form;
var tree;
var table ;
var notice;
var layNotify;

layui.use(['table', 'form','notice', 'layNotify'], function () {
    //getData();
    table = layui.table;
    form = layui.form;
    tree = layui.tree;
    layNotify = layui.layNotify;
    notice = layui.notice;
    form.on('submit(formDemo)', function (data) {

    });
   getVoltageLevel(form, "");
    createTable();

    form.on('submit(formDemo)', function (data) {
        var tableData = table.cache.demo; //
        var tf = true;
        tableData.forEach(item => {
            if(tf){
                let id = item.id;
                let choseNum = $("#"+id+"_choseId").val()
                let dictKey = item.dictKey;
                console.log("choseNum="+choseNum);
                item.choseNum = choseNum;
            }
        });
            let resourceName = $("#teamType").val();
            let volLevelIds = $("#volLevelIds").find("option:selected").text();//获取选中文本
            if(resourceName == ""){
                layer.msg("请填写团队类型！", {icon: 2});
                return;
            }
            if(volLevelIds == "请选择电压等级"){
                layer.msg("请选择电压等级！", {icon: 2});
                return;
            }

            $.ajax({
                type: 'POST',
                async: false, // 默认异步true,false表示同步
                url: ctxPath + "/teamSet/getTeamByLevelAndName",// 请求地址
                dataType: 'json', // 服务器返回数据类型
                data: {"resourceName":resourceName,
                        "volLevel":volLevelIds,
                        "resourceType":"3"}, //获取提交的表单字段
                success: function (data) {
                    var objData = data.data;
                    if(objData=="允许添加"){
                        $.ajax({
                            type: 'POST',
                            async: false, // 默认异步true,false表示同步
                            url: ctxPath + "/teamSet/addBranchOfficeTeamSet",// 请求地址
                            dataType: 'json', // 服务器返回数据类型
                            data: {"stringKey":JSON.stringify(tableData),
                                   "resourceName":resourceName,
                                   "volLevelIds":volLevelIds,
                                    "resourceType":"3",
                                    'type':'1'}, //获取提交的表单字段
                            success: function (data) {
                                var status = data.data;
                                if("添加成功"==status){
                                    //layer.msg("数据添加成功！", {icon: 1});
                                    reloadTip("添加","添加成功",'success',layNotify);
                                    setTimeout(function(){
                                        parent.layer.closeAll();
                                    }, 2000);

                                }else{
                                   // layer.msg("数据添加失败！", {icon: 2});
                                    reloadTip("添加","添加失败",'error',layNotify);
                                    setTimeout(function(){
                                        parent.layer.closeAll();
                                    }, 2000);
                                    parent.layer.closeAll();
                                }
                            }
                        });
                    }else{
                        layer.msg("团队名称已存在！", {icon: 2});
                    }
                }
            });


    });


    $("#typeManage").click(function () {
        var index = layer.open({
            type: 2,
            title: '分类管理',
            content: 'untiTeamAdd.html',
            area: ['62%', '90%'],
            maxmin: false,
            closeBtn: true,
            btn: ['确定', '取消'],
            yes: function(index, layero){
                var formSubmit = layer.getChildFrame('form', index);
                //var submited = formSubmit.find('button')[0];
                let submited = formSubmit.find('button.subBtn');
                submited.click();
                var myIframe = window[layero.find('iframe')[0]['name']];
                myIframe.addUnitTableMessage();
                return false;
            },
            end: function(){
                createTable();
            }
        });
    });

});



function createTable() {
    $.ajax({
        type: 'POST',
        async: false, // 默认异步true,false表示同步
        url: ctxPath + "/selectUtil/getDataDictByDickVal",// 请求地址
        dataType: 'json', // 服务器返回数据类型
        data: {"dictKey":"org_type"}, //获取提交的表单字段
        success: function (data) {
            let objData= data
            //渲染表格
            table.render({
                elem: '#demo',
                cols: [[ //表头
                    {field: 'dictKey', title: '岗位',width:'200',align: 'center'},
                    {field: 'nums', title: '数量', width:'200',templet:function (d) {
                            var choseNum = d.choseNum;
                            var choseId = d.id+"_choseId";
                            var opt = '<input id="'+choseId+'" type="number" value="'+choseNum+'"  name="price_min" placeholder="" autocomplete="off" class="layui-input " min="0" step="1" lay-affix="number">';
                            return opt;
                        }
                    }
                    // {field: 'nums', title: '数量', width:'200',edit: 'number',align: 'center'}

                ]],
                data: objData,
                done: function (res, curr, count) {
                    //merge(res);
                },
            });
        }
    });
}





