
layui.config({
    base: '../../lib/layui/lay/mymodules/'
    ,version: '101100'
}).use('eleTree');
layui.extend({
    admin: '{/}../../static/js/admin'
});
layui.use(['jquery','eleTree','table','laypage','admin'], function(){
    var $= layui.jquery,
        eleTree	= layui.eleTree,
        laypage	= layui.laypage,
        table	= layui.table;
    var addQRcode = '../../pages/QRcode/addQRcode.html';
    var updateHtml = '../../pages/editQRcode/editQRcode.html';
    var QRcodeDetaile = '../../pages/QRcoddetails/indexDetaile.html';
    var Synthesis = '../../pages/QRcode/Synthesis.html';
    // 部门
    var belpID = 'root';
    sessionStorage.setItem("ztreeDepid",'root');
    delpFun();
    function delpFun(){
        sessionStorage.setItem("depID",'root');
        $.ajax({
            url:'http://apptest.jieweizhineng.com/api/section/getlist?userStaffNum='+getUserStaffNum(),
            method:'get',
            dataType: 'json',
            data: {

            },
            success:function (res) {
                if(res.code === 0){
                    var data = [res.data];
                    eleTree.render({
                        elem: '.ele1',
                        data:data ,
                        defaultExpandAll:true,
                        // showCheckbox: true,
                    });
                    eleTree.on("nodeClick(data)",function(d) {
                        belpID =  d.data.currentData.id;
                        sessionStorage.setItem("depID",belpID);
                        sessionStorage.setItem("ztreeDepid",d.data.currentData.id);
                        $('.stetaName').val("");
                        var info={
                            statusContent: '',
                            departmentId: d.data.currentData.id,
                            deleteStatus: 'false',
                        };
                        getStateList(info);
                    })
                }else{
                    layer.msg('请检查网路', {icon: 2});
                }
            },
            error:function (err) {
                layer.msg('请检查网路', {icon: 2});
            }
        })

    }
    var info={
        title: $('.stetaName').val(),
        departmentId: belpID,
        deleteStatus: 'false',
    };
    getStateList(info);
    function getStateList(res) {
        var index = layer.load(2);
        table.render({
            elem: '#stateRemind',
            url: 'http://apptest.jieweizhineng.com/api/swagger/api/page/v1/getPageList',
            id: 'idQRcode',
            method: 'get',
            loading: true,
            page:true,
            cols: [[
                {field: 'title', title: '二维码名称', align: 'center'}
                , {field: 'createDate', title: '创建日期', align: 'center'}
                , {title: ' 操作', align: 'center', toolbar: '#barDemo'}
            ]],
            where: {
                title: res.title,
                departmentId: res.departmentId,
                deleteStatus: res.deleteStatus,
            },
            request: {
                pageName: 'pageNumber' //页码的参数名称，默认：page
                ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            response: {
                statusName: 'code'
                , statusCode: 200,
                countName: 'count',
                dataName: 'data'
            },
            done: function (res, curr, count) {
                layer.close(index);
                if (res.code === '200') {
                    $('.fr').html('共有数据：'+res.count+' 条')
                }
            }
        });
    }
    // 查询
    $(".seachSteta").click(function () {
        var checkbox = $("#checkboxT").prop('checked'),
            root='',
            info;
        if(checkbox == true){
            root = '';
            info={
                title: $('.stetaName').val(),
                departmentId: root,
                deleteStatus: 'false',
            };
        }else {
            info={
                title: $('.stetaName').val(),
                departmentId: belpID,
                deleteStatus: 'false'
            };
        }
        getStateList(info)
    });
    // 生成uid
    function creatuuid(len, radix) { // len位数，radix基数
        var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
        var uuid = [], i;
        radix = radix || chars.length;
        if (len) {
            for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
        } else {
            var r;
            uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
            uuid[14] = '4';
            for (i = 0; i < 36; i++) {
                if (!uuid[i]) {
                    r = 0 | Math.random()*16;
                    uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                }
            }
        }
        return uuid.join('');
    }
    // 监听状态table
    table.on('tool(stateItme)', function(obj) {
        if( obj.event === "move"){
            sessionStorage.setItem("statusId",obj.data.id);
            moveDep('选择部门信息','http://apptest.jieweizhineng.com/api/swagger/api/page/v1/movePage/','400','500',table,'idQRcode');
        } else if (obj.event === "copy"){
            var content ={
                _method:'put'
            };
            layer.alert('确认要复制记录模板吗？',{icon: 8}, function(index){
                $.ajax({
                    url: 'http://apptest.jieweizhineng.com/api/swagger/api/page/v1/copyPage/'+obj.data.id,
                    method: 'post',
                    dataType: 'json',
                    data: content,
                    success: function (res) {
                        if (res.code === '200'){
                            layer.msg('复制成功', {icon: 1});
                            table.reload('idQRcode');
                        } else{
                            layer.msg('复制失败', {icon: 2});
                            table.reload('idQRcode');
                        }
                        layer.close(index);
                    },
                    error:function () {
                        layer.msg('请检查网络', {icon: 2});
                        layer.close(index);
                    }
                });
            });
        }else if(obj.event === 'QRcode'){
            layer.open({
                type: 2,
                id: 'Synthesis',
                title: '合成二维码',
                shadeClose: true,
                scrollbar: true,
                shade: 0.3,
                maxmin: true,
                skin: 'oaBtn',
                area: ['450px', '450px'],
                content: Synthesis,
                success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);
                    body.find('.SynthesisQrcord').attr('src',"http://apptest.jieweizhineng.com/api/qrcode?id="+obj.data.id);
                }
            });
        }else if (obj.event === "edit"){
            var objID =obj.data;
            var delobj=[];
            sessionStorage.setItem("QRcodeDetaileID",obj.data.id);
            layer.open({
                type: 2,
                id: 'updateQRcode',
                title:'修改二维码',
                shadeClose: true,
                scrollbar:true,
                shade: 0.3,
                maxmin: true,
                skin: 'oaBtn',
                area: ['100%', '100%'],
                content: updateHtml,
                success: function (layero, index) {
                    var indexk = layer.load(2);
                    var body = layer.getChildFrame('body', index);
                    body.find('#pageFlag').val('true');
                    var iframeWindow = window['layui-layer-iframe'+ index];
                    $.ajax({
                        url: 'http://apptest.jieweizhineng.com/api/swagger/api/page/v1/getPageEdit/'+obj.data.id,
                        type: 'get',
                        dataType: 'json',
                        success: function (res) {
                            var indexk = layer.load(2);
                            var b = new Base64();
                            body.find('.edit_head_title').html(res.data.page.title);
                            var newArray =res.data.pageItemList;
                            // console.log(res,'title');
                            // for (var j = 0; j < res.data.pageItemList.length; j++) {
                            //     if (res.data.pageItemList[j].pageRelation.zIndex == j+1){
                            //         newArray.push(res.data.pageItemList[j])
                            //     }
                            // }
                            for (var i = 0; i < newArray.length; i++) {
                                // 文本
                                if (newArray[i].textVo != null) {
                                    var str = b.decode(newArray[i].textVo.dataContent);
                                    var _modeltemp = '';
                                    var data_id =creatuuid(12,36);
                                    _modeltemp = body.find('#temp-1').html();
                                    body.find('.module_box').children('div.b-g').last().before(_modeltemp);
                                    body.find(".reisze-now").css("height",$(".reisze-now").height()+250+'px');
                                    body.find('.module_box').children('.sortable-dragging').last().attr("dataid",data_id);
                                    body.find("#module_box .sortable-dragging").each(function(i,p){
                                        $(this).attr("sort",i+1);
                                        $(this).attr("module",i+1);
                                    });
                                    iframeWindow.editPureTextTitleName(data_id,newArray[i].textVo.title,newArray[i].textVo.id,newArray[i].pageRelation.id,str);
                                }
                                // 图文
                                if (newArray[i].imageText != null) {
                                    var str = b.decode(newArray[i].imageText.dataContent);
                                    var _modeltemp = '';
                                    var data_id =creatuuid(12,36);
                                    _modeltemp = body.find('#temp-2').html();
                                    body.find('.module_box').children('div.b-g').last().before(_modeltemp);
                                    body.find(".reisze-now").css("height",$(".reisze-now").height()+250+'px');
                                    body.find('.module_box').children('.sortable-dragging').last().attr("dataid",data_id);
                                    body.find("#module_box .sortable-dragging").each(function(i,p){
                                        $(this).attr("sort",i+1);
                                        $(this).attr("module",i+1)
                                    });
                                    iframeWindow.edituploadImage(data_id,newArray[i].imageText.title,newArray[i].imageText.imageUrl,newArray[i].imageText.id,newArray[i].pageRelation.id,str);
                                }
                                // 图集
                                if (newArray[i].pictureList != null) {
                                    var _modeltemp = '';
                                    var data_id =creatuuid(12,36);
                                    _modeltemp = body.find('#temp-3').html();
                                    body.find('.module_box').children('div.b-g').last().before(_modeltemp);
                                    body.find(".reisze-now").css("height",$(".reisze-now").height()+250+'px');
                                    body.find('.module_box').children('.sortable-dragging').last().attr("dataid",data_id);
                                    body.find("#module_box .sortable-dragging").each(function(i,p){
                                        $(this).attr("sort",i+1);
                                        $(this).attr("module",i+1)
                                    });
                                    iframeWindow.edituploadImageAtlas(data_id,newArray[i].pictureList,newArray[i].pageRelation.businessId,newArray[i].pageRelation.id);
                                }
                                // 文件
                                if (newArray[i].fileList != null) {
                                    var _modeltemp = '';
                                    var data_id =creatuuid(12,36);
                                    _modeltemp = body.find('#temp-4').html();
                                    body.find('.module_box').children('div.b-g').last().before(_modeltemp);
                                    body.find(".reisze-now").css("height",$(".reisze-now").height()+250+'px');
                                    body.find('.module_box').children('.sortable-dragging').last().attr("dataid",data_id);
                                    body.find("#module_box .sortable-dragging").each(function(i,p){
                                        $(this).attr("sort",i+1);
                                        $(this).attr("module",i+1)
                                    });
                                    iframeWindow.edituploadFile(data_id,newArray[i].fileList,newArray[i].pageRelation.businessId,newArray[i].pageRelation.id)
                                }
                                // 链接
                                if (newArray[i].linksList !=null) {
                                    var _modeltemp = '';
                                    var data_id =creatuuid(12,36);
                                    _modeltemp = body.find('#temp-5').html();
                                    body.find('.module_box').children('div.b-g').last().before(_modeltemp);
                                    body.find(".reisze-now").css("height",$(".reisze-now").height()+250+'px');
                                    body.find('.module_box').children('.sortable-dragging').last().attr("dataid",data_id);
                                    body.find("#module_box .sortable-dragging").each(function(i,p){
                                        $(this).attr("sort",i+1);
                                        $(this).attr("module",i+1)
                                    });
                                    iframeWindow.editlinkFun(data_id,newArray[i].linksList,newArray[i].pageRelation.businessId,newArray[i].pageRelation.id)
                                }
                                // 视频
                                if (newArray[i].videoList !=null) {
                                    var _modeltemp = '';
                                    var data_id =creatuuid(12,36);
                                    _modeltemp = body.find('#temp-6').html();
                                    body.find('.module_box').children('div.b-g').last().before(_modeltemp);
                                    body.find(".reisze-now").css("height",$(".reisze-now").height()+250+'px');
                                    body.find('.module_box').children('.sortable-dragging').last().attr("dataid",data_id);
                                    body.find("#module_box .sortable-dragging").each(function(i,p){
                                        $(this).attr("sort",i+1);
                                        $(this).attr("module",i+1)
                                    });
                                    iframeWindow.editaddVideo(data_id,newArray[i].videoList[0].videoUrl,newArray[i].pageRelation.businessId,newArray[i].pageRelation.id)
                                }
                                // 设备铭牌editDevice
                                if (newArray[i].equipmentList !=null) {
                                    var _modeltemp = '';
                                    var data_id =creatuuid(12,36);
                                    _modeltemp = body.find('#temp-7').html();
                                    body.find('.module_box').children('div.b-g').last().before(_modeltemp);
                                    body.find(".reisze-now").css("height",$(".reisze-now").height()+250+'px');
                                    body.find('.module_box').children('.sortable-dragging').last().attr("dataid",data_id);
                                    body.find("#module_box .sortable-dragging").each(function(i,p){
                                        $(this).attr("sort",i+1);
                                        $(this).attr("module",i+1)
                                    });
                                    iframeWindow.editDevice(data_id,newArray[i].equipmentList,newArray[i].pageRelation.businessId,newArray[i].pageRelation.id)
                                }
                                // 记录模版
                                if (newArray[i].pageRecordList !=null) {
                                    var _modeltemp = '';
                                    var data_id =creatuuid(12,36);
                                    _modeltemp = body.find('#temp-8').html();
                                    body.find('.module_box').children('div.b-g').last().before(_modeltemp);
                                    body.find(".reisze-now").css("height",$(".reisze-now").height()+250+'px');
                                    body.find('.module_box').children('.sortable-dragging').last().attr("dataid",data_id);
                                    body.find("#module_box .sortable-dragging").each(function(i,p){
                                        $(this).attr("sort",i+1);
                                        $(this).attr("module",i+1)
                                    });
                                    iframeWindow.editRecord(data_id,newArray[i].pageRecordList,newArray[i].pageRelation.businessId,newArray[i].pageRelation.id)
                                }
                            }
                            layer.close(indexk);
                        },
                        error:function () {
                            layer.msg('请检查网络', {icon: 2});
                        }
                    });

                },
                cancel: function(index, layero){
                    table.reload('idQRcode');
                }
            })
        } else if (obj.event === "delete"){
            var content ={
                _method:'DELETE'
            };
            layer.alert('确认要删除吗？',{icon: 8}, function(index){
                $.ajax({
                    url: 'http://apptest.jieweizhineng.com/api/swagger/api/page/v1/removePage/'+obj.data.id,
                    method: 'post',
                    dataType: 'json',
                    data: content,
                    success: function (res) {
                        if (res.code === '200'){
                            layer.msg('删除成功', {icon: 1});
                            table.reload('idQRcode');
                        } else{
                            layer.msg('删除失败', {icon: 2});
                            table.reload('idQRcode');
                        }
                        layer.close(index);
                    },
                    error:function () {
                        layer.msg('请检查网络', {icon: 2});
                        layer.close(index);
                    }
                });
            });
        } else if (obj.event === 'dataile') {
            sessionStorage.setItem('QrcodeIds',obj.data.id);
            layer.open({
                type: 2,
                id: 'QRcodeDetaile',
                title: '二维码详情',
                shadeClose: true,
                scrollbar: true,
                shade: 0.3,
                maxmin: true,
                skin: 'oaBtn',
                area: ['375px', '667px'],
                content: QRcodeDetaile,
                success: function (layero, index) {

                }
            })
        }
    });
    // 添加状态
    // 打开添加弹窗
    $(".addState").click(function () {
        layer.open({
            type: 2,
            id: 'addQRcodeID',
            title:'添加二维码',
            shadeClose: true,
            scrollbar:true,
            shade: 0.3,
            maxmin: true,
            skin: 'oaBtn',
            area: ['100%', '100%'],
            content: addQRcode,
            success: function (layero, index) {
                var body = layer.getChildFrame('body', index);
            },
            cancel: function(index, layero){
                table.reload('idQRcode');
            }
        })
    })
});