var ExcelData;

function ConfirmImport() {
    $.ajax({
        type: "POST",
        url: "/Admin/RightDic/Import?r=" + Math.random(1),
        data: "ExcelData=" + encodeURIComponent(JSON.stringify(ExcelData)),
        dataType: "text",
        success: function (data) {
            if (data == "success") {
                layer.msg("导入成功", {
                    time: 2000
                }, function () {
                    window.location.reload();
                });
            } else {
                layer.msg(data);
            }
        }
    });
}

var table;
var iconindex;
layui.config({
    base: '/layuiadmin/' //静态资源所在路径
}).extend({
    myexcel: 'layui/lay/modules/excel',
    index: 'lib/index' //主入口模块
}).use(['index', 'table', 'excel', 'laydate'], function () {
    var $ = layui.$,
        form = layui.form;
    table = layui.table;

    //- 筛选条件-日期
    var laydate = layui.laydate;


    //excel导入
    var excel = layui.excel;
    $('#ExcelFile').change(function (e) {
        var files = e.target.files;
        excel.importExcel(files, {}, function (data) {
            ExcelData = data[0].Sheet1;
        });
    });

    //监听单元格编辑
    table.on('edit(LAY-list-manage)', function (obj) {
        var value = obj.value //得到修改后的值
            ,
            data = obj.data //得到所在行所有键值
            ,
            field = obj.field; //得到字段
        console.log(field);
        if (field == "sort") {
            $.ajax({
                type: "POST",
                url: "/Admin/RightDic/Sort?r=" + Math.random(1),
                data: "Id=" + data.id + "&Sort=" + value,
                dataType: "text",
                success: function (data) {
                    table.reload('LAY-list-manage');
                }
            });
        }
    });

    //列表数据
    table.render({
        elem: '#LAY-list-manage',
        url: '/Admin/RightDic/IndexData' //模拟接口
            ,
        cols: [
            [{
                type: 'checkbox',
                fixed: 'left'
            }, {
                field: 'Icon',
                fixed: 'left',
                title: '图标',
                width: 60,
                templet: '#iconShow',
                event: 'icon'
            }, {
                field: 'name',
                title: '标题'
            }, {
                field: 'MainMenu',
                fixed: 'right',
                title: '快捷方式',
                width: 120,
                templet: '#switchMainMenu'
            }, {
                field: 'MainStat',
                fixed: 'right',
                title: '统计',
                width: 120,
                templet: '#switchMainStat'
            }, {
                field: 'MainDataList',
                fixed: 'right',
                title: '主界面数据列表',
                width: 150,
                templet: '#switchMainDataList'
            }, {
                field: 'sort',
                fixed: 'right',
                title: '排序',
                width: 80,
                edit: 'text'
            }, {
                title: '操作',
                align: 'center',
                fixed: 'right',
                width: 180,
                toolbar: '#table-list-tools'
            }]
        ],
        limit: 1000,
        height: 'full-60',
        text: '对不起，加载出现异常！',
        done: function (res, curr, count) {
            $(".layui-none").text("无数据");
        }
    });

    //监听工具条
    table.on('tool(LAY-list-manage)', function (obj) {
        var data = obj.data;        
        if (obj.event === 'icon') {
            var iconlist = ['layui-icon-heart-fill', 'layui-icon-heart', 'layui-icon-light', 'layui-icon-time', 'layui-icon-bluetooth', 'layui-icon-at', 'layui-icon-mute', 'layui-icon-mike', 'layui-icon-key', 'layui-icon-gift', 'layui-icon-email', 'layui-icon-rss', 'layui-icon-wifi', 'layui-icon-logout', 'layui-icon-android', 'layui-icon-ios', 'layui-icon-windows', 'layui-icon-transfer', 'layui-icon-service', 'layui-icon-subtraction', 'layui-icon-addition', 'layui-icon-slider', 'layui-icon-print', 'layui-icon-export', 'layui-icon-cols', 'layui-icon-screen-restore', 'layui-icon-screen-full', 'layui-icon-rate-half', 'layui-icon-rate', 'layui-icon-rate-solid', 'layui-icon-cellphone', 'layui-icon-vercode', 'layui-icon-login-wechat', 'layui-icon-login-qq', 'layui-icon-login-weibo', 'layui-icon-password', 'layui-icon-username', 'layui-icon-refresh-3', 'layui-icon-auz', 'layui-icon-spread-left', 'layui-icon-shrink-right', 'layui-icon-snowflake', 'layui-icon-tips', 'layui-icon-note', 'layui-icon-home', 'layui-icon-senior', 'layui-icon-refresh', 'layui-icon-refresh-1', 'layui-icon-flag', 'layui-icon-theme', 'layui-icon-notice', 'layui-icon-website', 'layui-icon-console', 'layui-icon-face-surprised', 'layui-icon-set', 'layui-icon-template-1', 'layui-icon-app', 'layui-icon-template', 'layui-icon-praise', 'layui-icon-tread', 'layui-icon-male', 'layui-icon-female', 'layui-icon-camera', 'layui-icon-camera-fill', 'layui-icon-more', 'layui-icon-more-vertical', 'layui-icon-rmb', 'layui-icon-dollar', 'layui-icon-diamond', 'layui-icon-fire', 'layui-icon-return', 'layui-icon-location', 'layui-icon-read', 'layui-icon-survey', 'layui-icon-face-smile', 'layui-icon-face-cry', 'layui-icon-cart-simple', 'layui-icon-cart', 'layui-icon-next', 'layui-icon-prev', 'layui-icon-upload-drag', 'layui-icon-upload', 'layui-icon-download-circle', 'layui-icon-component', 'layui-icon-file-b', 'layui-icon-user', 'layui-icon-find-fill', 'layui-icon-loading', 'layui-icon-loading-1', 'layui-icon-add-1', 'layui-icon-play', 'layui-icon-pause', 'layui-icon-headset', 'layui-icon-video', 'layui-icon-voice', 'layui-icon-speaker', 'layui-icon-fonts-del', 'layui-icon-fonts-code', 'layui-icon-fonts-html', 'layui-icon-fonts-strong', 'layui-icon-unlink', 'layui-icon-picture', 'layui-icon-link', 'layui-icon-face-smile-b', 'layui-icon-align-left', 'layui-icon-align-right', 'layui-icon-align-center', 'layui-icon-fonts-u', 'layui-icon-fonts-i', 'layui-icon-tabs', 'layui-icon-radio', 'layui-icon-circle', 'layui-icon-edit', 'layui-icon-share', 'layui-icon-delete', 'layui-icon-form', 'layui-icon-cellphone-fine', 'layui-icon-dialogue', 'layui-icon-fonts-clear', 'layui-icon-layer', 'layui-icon-date', 'layui-icon-water', 'layui-icon-code-circle', 'layui-icon-carousel', 'layui-icon-prev-circle', 'layui-icon-layouts', 'layui-icon-util', 'layui-icon-templeate-1', 'layui-icon-upload-circle', 'layui-icon-tree', 'layui-icon-table', 'layui-icon-chart', 'layui-icon-chart-screen', 'layui-icon-engine', 'layui-icon-triangle-d', 'layui-icon-triangle-r', 'layui-icon-file', 'layui-icon-set-sm', 'layui-icon-reduce-circle', 'layui-icon-add-circle', 'layui-icon-404', 'layui-icon-about', 'layui-icon-up', 'layui-icon-down', 'layui-icon-left', 'layui-icon-right', 'layui-icon-circle-dot', 'layui-icon-search', 'layui-icon-set-fill', 'layui-icon-group', 'layui-icon-friends', 'layui-icon-reply-fill', 'layui-icon-menu-fill', 'layui-icon-log', 'layui-icon-picture-fine', 'layui-icon-face-smile-fine', 'layui-icon-list', 'layui-icon-release', 'layui-icon-ok', 'layui-icon-help', 'layui-icon-chat', 'layui-icon-top', 'layui-icon-star', 'layui-icon-star-fill', 'layui-icon-close-fill', 'layui-icon-close', 'layui-icon-ok-circle', 'layui-icon-add-circle-fine'];
            var content = '';
            content += '<li style="width: 40px; height:40px; line-height:40px; text-align:center; float: left;" onclick="setIcon(\'' + data.id + '\', \'\')">无</li>';
            for (var i = 0; i < iconlist.length; i++) {
                content += '<li style="width: 40px; height:40px; line-height:40px; text-align:center; float: left;" onclick="setIcon(\'' + data.id + '\', \'' + iconlist[i] + '\')"><i class="layui-icon ' + iconlist[i] + '" style="font-size:20px;"></i></li>';
            }
            iconindex = layer.open({
                type: 1,
                title: false,
                closeBtn: 0,
                shadeClose: true,
                skin: 'yourclass',
                area: ['600px', 'auto'],
                content: '<div><ul>' + content + '</ul></div>'
            });
        } else if (obj.event === 'edit') {
            var tr = $(obj.tr);
            var perContent = layer.open({
                type: 2,
                title: '编辑',
                content: 'Edit?Id=' + data.id + '',
                maxmin: true,
                area: ['500px', '450px'],
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index],
                        submitID = 'LAY-list-front-submit',
                        submit = layero.find('iframe').contents().find('#' + submitID);

                    setTimeout(function () {
                        layero.find('iframe').contents().find('.layui-tab-item').each(function (i) {
                            var errObj = $(this).find('.layui-form-danger');
                            if (errObj.length > 0) {
                                iframeWindow.element.tabChange('mytabbar', String(i + 1));
                                submit.click();
                            }
                        });
                    }, 300);


                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                        var field = data.field; //获取提交的字段
                        var userdata = "";
                        for (var prop in field) {
                            userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
                        }
                        //提交 Ajax 成功后，静态更新表格中的数据
                        //$.ajax({});

                        $.ajax({
                            type: "POST",
                            url: "/Admin/RightDic/EditPost?r=" + Math.random(1),
                            data: userdata,
                            dataType: "text",
                            success: function (data) {
                                layer.close(index); //关闭弹层
                                if (data == "success") {
                                    table.reload('LAY-list-manage'); //数据刷新
                                } else {
                                    layer.msg(data);
                                }
                            }
                        });
                    });

                    submit.trigger('click');
                },
                success: function (layero, index) {

                }
            });
            layer.full(perContent);
        } else if (obj.event === 'right') {
            var tr = $(obj.tr);
            var perContent = layer.open({
                type: 2,
                title: '权限配置',
                content: 'RightSet?Id=' + data.id + '',
                maxmin: true,
                area: ['500px', '450px'],
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index],
                        submitID = 'LAY-list-front-submit',
                        submit = layero.find('iframe').contents().find('#' + submitID);

                    setTimeout(function () {
                        layero.find('iframe').contents().find('.layui-tab-item').each(function (i) {
                            var errObj = $(this).find('.layui-form-danger');
                            if (errObj.length > 0) {
                                iframeWindow.element.tabChange('mytabbar', String(i + 1));
                                submit.click();
                            }
                        });
                    }, 300);


                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                        var field = data.field; //获取提交的字段
                        var userdata = "";
                        for (var prop in field) {
                            userdata += prop + "=" + encodeURIComponent(field[prop]) + "&";
                        }
                        var RightList = '';
                        layero.find('iframe').contents().find('input[type=checkbox][name=RightString]:checked').each(function (i) {
                            RightList += $(this).val() + ',';
                        });
                        userdata += '&RightList=' + RightList;

                        //提交 Ajax 成功后，静态更新表格中的数据
                        //$.ajax({});

                        $.ajax({
                            type: "POST",
                            url: "/Admin/RightDic/RightSetPost?r=" + Math.random(1),
                            data: userdata,
                            dataType: "text",
                            success: function (data) {
                                if (data == "success") {
                                    layer.msg('设置成功');
                                    layer.close(index); //关闭弹层
                                    // table.reload('LAY-list-manage'); //数据刷新
                                } else {
                                    layer.msg(data);
                                }
                            }
                        });
                    });

                    submit.trigger('click');
                },
                success: function (layero, index) {

                }
            });
            layer.full(perContent);
        }
    });


    //监听搜索
    form.on('submit(LAY-list-front-search)', function (data) {
        var field = data.field;

        //执行重载
        table.reload('LAY-list-manage', {
            where: field
        });
    });
    form.on('submit(LAY-list-front-searchall)', function (data) {
        table.reload('LAY-list-manage', {
            where: null
        });
    });
    form.on('switch(MainMenu)', function (obj) {
        $.ajax({
            type: "POST",
            url: "/Admin/RightDic/SetMainMenu?r=" + Math.random(1),
            data: "Id=" + obj.value + "&value=" + obj.elem.checked,
            dataType: "text",
            success: function (data) {
            }
        });
    });
    form.on('switch(MainStat)', function (obj) {
        $.ajax({
            type: "POST",
            url: "/Admin/RightDic/SetMainStat?r=" + Math.random(1),
            data: "Id=" + obj.value + "&value=" + obj.elem.checked,
            dataType: "text",
            success: function (data) {
            }
        });
    });
    form.on('switch(MainDataList)', function (obj) {
        $.ajax({
            type: "POST",
            url: "/Admin/RightDic/SetMainDataList?r=" + Math.random(1),
            data: "Id=" + obj.value + "&value=" + obj.elem.checked,
            dataType: "text",
            success: function (data) {
            }
        });
    });

    //事件
    var active = {

    };

    $('.layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
});


function setIcon(id, icon) {
    $.ajax({
        type: "POST",
        url: "/Admin/RightDic/SetIcon?r=" + Math.random(1),
        data: "Id=" + id + "&value=" + icon,
        dataType: "text",
        success: function (data) {
            layer.close(iconindex);
            table.reload('LAY-list-manage');
        }
    });
}