define(['table','form','yntree'], function (Table,Form,YnTree1) {
    var Admin = {
        //Do setup work hereAction
        index: function () {
            Table.api.init({
                extend: {
                    index_url: 'userrole/select',
                    add_url: 'userrole/insert',
                    edit_url: 'userrole/update',
                    del_url: 'userrole/delete',
                    multi_url: 'userrole/multi',
                    dragsort_url: 'userrole/weigh',
                    table: 'userrole',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: false,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'id',
                            title: 'ID',
                            filter: "number",
                            sortable: true    // 是否排序
                        },
                        {
                            title: "父级",
                            field: "pid",
                            visible: false,
                        },
                        {
                            field: 'name', 
                            title: '角色组',
                            filter: "string",
                        },
                        {
                            field: 'max_send_msg_count', 
                            title: '最大消息数量',
                            filter: "number",
                        },
                        {
                            field: 'max_friend_count', 
                            title: '最大好友数量',
                            filter: "number",
                        },
                        {
                            field: 'max_group_join_count', 
                            title: '群组加入限制',
                            filter: "number",
                        },
                        {
                            field: 'max_gourp_create_count', 
                            title: '群组创建限制',
                            filter: "number",
                        },
                        {
                            title: "创建时间",
                            field: "created_at",
                        },
                        {
                            title: "更新时间",
                            field: "updated_at",
                        },
                        {field: 'operate', title: '操作', table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        update:function(){
            this.bindevent();
        },
        insert:function(){
            this.bindevent();
        },
        flatArrayToTree: function (items) {
            let res = []
            let getChildren = (res, pid) => {
                for (const i of items) {
                    if (i.parent === pid) {
                        const newItem = { ...i, children: [] }
                        newItem.name =newItem.text;
                        newItem.value =newItem.id;
                        newItem.checked =newItem.state.selected;
                        res.push(newItem)
                        getChildren(newItem.children, newItem.id)
                    }
                }
            }
            getChildren(res, 0)
            return res
        },
        rendertree: function (content) {
            if(Admin.ruletree){
                Admin.ruletree.destroy();
            }
            var treeData = {
                // 复选框change事件
                onchange: function (input, yntree) {
                    console.log(yntree.getValues().join(','));
                    if ($("#treeview").length > 0) {
                        $("input[name='rules']").val(yntree.getValues().join(','));
                    }
                },
                // 是否严格的遵循父子互相关联的做法
                checkStrictly: true,
                data: []
            };
            treeData.data = Admin.flatArrayToTree(content)
            var yntree = new YnTree(document.getElementById("treeview"), treeData);
            Admin.ruletree = yntree;
        },
        ruletree: null,
        bindevent: function () {
            Form.api.bindevent($("form[role=form]"));
            //渲染权限节点树
            //变更级别后需要重建节点树
            $(document).on("change", "select[name='pid']", function () {
                var pid = $(this).data("pid");
                var id = $(this).data("id");
                // if ($(this).val() == id) {
                //     $("option[value='" + pid + "']", this).prop("selected", true).change();
                //     Fast.api.error(__('Can not change the parent to self'));
                //     return false;
                // }
                $.ajax({
                    url: "userrole/tree",
                    type: 'post',
                    dataType: 'json',
                    data: {id: id, pid: $(this).val()},
                    success: function (ret) {
                        if (ret.hasOwnProperty("code")) {
                            var data = ret.hasOwnProperty("data") && ret.data != "" ? ret.data : "";
                            if (ret.code) {
                                Fast.api.error(ret.msg);
                            } else {
                                Admin.rendertree(data);
                            }
                        }
                    }, error: function (e) {
                        Fast.api.error(e.message);
                    }
                });
            });
            $("select[name='pid']").trigger("change");
        }
    };
    return Admin
});