layui.use(['laytpl', 'table', 'jquery', 'form', 'miniTab', 'tree', 'xmSelect', 'laydate', 'upload', 'transfer'], function () {
    let $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layer = layui.layer,
        tree = layui.tree,
        laydate = layui.laydate,
        upload = layui.upload,
        xmSelect = layui.xmSelect,
        miniTab = layui.miniTab;
        transfer = layui.transfer;

    let curDateTime = date('Y-m-d H:i:s');
    /**** index begin ***/
    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',
            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: 'title', width: 200, title: '消息标题'},
                {field: 'content', minWidth: 250, title: '消息内容'},
                {field: 'type_text', width: 140, title: '消息类型'},
                {field: 'target_text', width: 140, title: '目标人群'},
                {field: 'send_at', width: 160, title: '发送日期'},
                {templet: '#row-operate', width: 150, 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;
        });

        let modifyUrl = $('#row-modify').data('url');


    }
    /*** index end ***/

    if ($('.location-add-page').length > 0) {
        function renderDate() {

            layui.use('laydate', function () {
                var laydate = layui.laydate;
                //layui多个日期渲染
                lay('.msg-send-at').each(function () {
                    laydate.render({
                        elem: this, //指定元素
                        type: 'datetime'
                        ,min: curDateTime
                    });
                });
            })

        }

        // select 监听
        form.on('submit(add_row)', function(data){
            var lay_index = ($(".msg-send-at:last").attr("lay-key"))*1 + 1;
            var html = $(".layui-row").eq(0).clone();
            $(html).find('.del-row').removeClass('layui-hide')
            $("#row").append(html);
            $(".msg-send-at:last").attr("lay-key",lay_index);
            $(".content").each(function (index) {
                    $(this).attr("name","fdata["+index+"][content]")
            });
            $(".send_at").each(function (index) {
                $(this).attr("name","fdata["+index+"][send_at]")
            });
            renderDate();
            form.render();
        });

        // 监听删除节点
        form.on('submit(del_row)', function(data){
            console.log(data)
            $(data.elem).parents('#row_one').remove();
        });
    }
    /** add and edit **/
    if ($('.location-operate-page').length > 0) {

        let receiveEle  = $('#receive-users');
        let targetList = receiveEle.data('show') ? receiveEle.data('show') : [];



       let receiveUserXm = xmSelect.render({
            el: '#receive-users',
            autoRow: true,
            radio: false,
            toolbar: { show: false },
            filterable: true,
            remoteSearch: true,
            name: 'target_list_str',
            tips: '通过手机号/昵称/姓名 查询',
            model: {
                icon: 'hidden',
            },
            prop: {
                name: 'account_desc2',
                value: 'id',
            },
            theme: {
                color: '#1e84ff',
            },
            data: targetList,
            remoteMethod: function(val, cb, show){
                //这里如果val为空, 则不触发搜索
                if(!val){
                    return cb([]);
                }
                $.ajax('/manager/worker/search-account', {
                    data: {
                        "keyword": val
                    }
                    ,dataType : 'json'
                    ,type: 'POST'
                })
                .done(function (res) {
                    cb(res.data);
                })
                .fail(function () {
                    cb([]);
                });
            }
        });

        // select 监听
        form.on('select(target)', function(data){
            let val = data.value;
            if (val == 'part') {
                $('.target-list').removeClass('layui-hide');
            } else {
                $('.target-list').addClass('layui-hide');
            }
        });
    }



});