layui.use(['jquery', 'laytpl', 'table', 'miniTab'], function () {
    let $ = layui.jquery,
        table = layui.table,
        layer = layui.layer,
        miniTab = layui.miniTab;

    /**** index begin ***/
    //index页面
    if ($('.location-index-page').length > 0) {
        miniTab.listen();

        // 渲染表格
        let listUrl = $('#table-container').data('url');
        let insTb = table.render({
            elem: '#table-container',
            toolbar: '#toolbar-tpl',
            defaultToolbar: [],
            url: listUrl,
            method: 'post',
            request: {
                pageName: 'page',
                limitName: 'size',
            },
            parseData: function (res) {
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.list //解析数据列表
                };
            },
            page: true,
            limit: 100,
            limits: [100, 200],
            cols: [[
                {type: 'checkbox'},
                {field: 'title', minWidth: 200, title: '字段标题'},
                {field: 'name', minWidth: 200, title: '字段名称'},
                {field: 'remark', minWidth: 200, title: '备注'},
                {templet: '#row-status', field: 'status', align: 'center', title: '状态'},
                {templet: '#row-require', field: 'is_require', align: 'center', title: '必填'},
                {templet: '#row-operate', align: 'center', title: '操作'}
            ]],
            done: function () {
                Tools.setInsTb(insTb);
            }
        });

        table.on('toolbar(table-container)', function (obj) {
            let layEvent = obj.event;
            let insTb = Tools.getInsTb();

            // toolbar 删除
            if (layEvent === 'del') {
                let checkStatus = table.checkStatus('table-container');
                if (checkStatus.data.length <= 0) {
                    layer.msg('请先选择数据');
                    return false;
                }
                let selected = checkStatus.data;
                let ids = [];
                let url = $($(this).context).data('href')
                $.each(selected, function (index, val) {
                    ids.push(val.id);
                })

                delRow(url, ids, insTb);
                return false;
            }

            // toolbar 刷新
            if (layEvent === 'refresh') {
                refreshTab(insTb);
                return false;
            }

            // toolbar 添加
            if (layEvent === 'add') {
                let url = $($(this).context).data('href')
                let title = $($(this).context).data('title')
                openLayer(url, title);
                return false;
            }

            // toolbar 搜索显示与隐藏
            if (layEvent === 'search') {
                let search = $('.table-search-fieldset');
                if (search.hasClass('div-show')) {
                    search.css('display', 'none').removeClass('div-show');
                } else {
                    search.css('display', 'block').addClass('div-show');
                }
                return false;
            }

            if (layEvent === 'sync') {
                let url = $($(this).context).data('href')
                let index = layer.load(2);
                $.post(url, function (res) {
                    layer.close(index);
                    layer.msg(res.msg)
                    if (res.code === 0) {
                        insTb.reload();
                    }
                })
                return false;
            }
        });
    }
    /*** index end ***/
});