layui.use(['laytpl', 'table', 'jquery', 'form', 'miniTab', 'xmSelect'], function () {
    let $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layer = layui.layer,
        xmSelect = layui.xmSelect,
        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: [{ //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '搜索'
                , layEvent: 'search'
                , icon: 'layui-icon-search'
            }],
            url: listUrl,
            method: 'post',
            even: true,
            limits: [10,20,50,100,200,500,1000],
            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,
            cols: [[
                // {type: 'checkbox'},
                { field : "business_name",  title: '商家名称'},
                { field : "type_name",  title: '商家类型'},
                {field : "contact_name" ,  title: '联系人'},
                {field : "contact_phone" ,  title: '联系电话'},
                {field : "business_address" ,  title: '地址'},
                {templet: '#row-business-license',  title: '执照',  style: 'height: 90px;'},
                {templet: '#row-status',  title: '状态',  style: 'height: 90px;'},
                {templet: '#row-operate', minWidth: 350, field: 'right', align: 'center', title: '操作', fixed: 'right'}
            ]],
            done: function () {
                Tools.setInsTb(insTb);
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('table-container', {
                page: {curr: 1}
                , where: data.field
            }, 'data');

            return false;
        });

        //监听工具条
        table.on('tool(table-container)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;
            let url = $($(this).context).data('href');
            let title = $(obj.tr.context).data('title');

            if (layEvent === 'wait') {
                state= $($(this).context).data('state') ;

                if(state == 1){
                    $.post(url, {state:state}, function (res) {
                        layer.msg(res.msg);
                        if (res.code === 0) {
                            insTb.reload();
                        }
                    })
                }else{
                    layer.open({
                        id:1,
                        type: 1,
                        title:'驳回请求',
                        skin:'layui-layer-rim',
                        area:['450px', '250px'],

                        content: ' <div class="layui-form-item" style="margin-top: 20px"> ' +
                            '<label class="layui-form-label">原因</label>' +
                            ' <div class="layui-inline"> ' +
                            '<input type="text" name="reason" id="reason" class="layui-input" placeholder="请简述原因"> ' +
                            '</div>'
                            +'</div>'
                        ,
                        btn:['保存','取消'],
                        btn1: function (index,layero) {
                            var reason  =$("#reason").val() ;
                            $.post(url, {state:state,reason:reason}, function (res) {
                                layer.msg(res.msg);
                                if (res.code === 0) {
                                    insTb.reload();
                                }
                            })
                            layer.close(index);
                        },
                        btn2:function (index,layero) {
                            layer.close(index);
                        }

                    });
                }

                return false;
            }
        });

    }
    /*** index end ***/

});