define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},


                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'group.name', title: __('Group')},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'realname', title: __('真实姓名'), operate: 'LIKE'},
                        {field: 'work', title: __('职业'), operate: 'LIKE'},
                        {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'level', title: __('Level'), operate: 'BETWEEN', sortable: true},
                        {field: 'gender', title: __('Gender'), visible: false, searchList: genderListJson, formatter: Table.api.formatter.normal},
                        {field: 'score', title: __('Score'), operate: 'BETWEEN', sortable: true},
                        {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},


                        {field: 'operate', title: __('Operate'), table: table , buttons: [
                                {
                                    name: 'user_auth',
                                    text: __('设置成机构认证'),
                                    title: __('设置成机构认证'),
                                    classname: 'btn   btn-dialog',
                                    icon: 'fa fa-bank',
                                    dropdown : '更多',
                                    url: manystore_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },
                                {
                                    name: 'user_auth',
                                    text: __('设置成机构授权'),
                                    title: __('设置成机构授权'),
                                    classname: 'btn   btn-dialog',
                                    icon: 'fa fa-bitbucket',
                                    dropdown : '更多',
                                    url: user_auth_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },



                                {
                                    name: 'teacher',
                                    text: __('设置成老师'),
                                    title: __('设置成老师'),
                                    classname: 'btn  btn-dialog',
                                    icon: 'fa fa-user',
                                    dropdown : '更多',
                                    url: teacher_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },



                                {
                                    name: 'verification',
                                    text: __('设置成核销员'),
                                    title: __('设置成核销员'),
                                    classname: 'btn  btn-dialog',
                                    icon: 'fa fa-user',
                                    dropdown : '更多',
                                    url: verification_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },


                                {
                                    name: 'third',
                                    text: __('三方账号信息'),
                                    title: __('三方账号信息'),
                                    classname: 'btn  btn-dialog',
                                    icon: 'fa fa-user',
                                    dropdown : '更多',
                                    url: third_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },


                                {
                                    name: 'classes_order',
                                    text: __('课程购买单'),
                                    title: __('课程购买单'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-cart-arrow-down',
                                    dropdown : '更多',
                                    url: classes_order_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },

                                {
                                    name: 'classes_hourorder',
                                    text: __('课时预约单'),
                                    title: __('课时预约单'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-calendar',
                                    dropdown : '更多',
                                    url: classes_hourorder_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },
                                {
                                    name: 'evaluate',
                                    text: __('查看用户评价'),
                                    title: __('查看用户评价'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-handshake-o',
                                    dropdown : '更多',
                                    url: evaluate_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.classes_evaluate_id;
                                    // }
                                },
                                {
                                    name: 'attachment',
                                    text: __('查看用户上传图片'),
                                    title: __('查看用户上传图片'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-file-picture-o',
                                    dropdown : '更多',
                                    url: attachment_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.classes_evaluate_id;
                                    // }
                                },

                            ], events: Table.api.events.operate, formatter: Table.api.formatter.operate},



                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            /**
             * 发放y预约订单
             * cc
             * 2020年8月5日
             */
            $(document).on('click', '.btn-changeuser', function (event) {
                var url = $(this).attr('data-url');
                if(!url) return false;
                var title = $(this).attr('title');
                var width = $(this).attr('data-width');
                var height = $(this).attr('data-height');
                var area = [$(window).width() > 800 ? (width?width:'800px') : '95%', $(window).height() > 600 ? (height?height:'600px') : '95%'];
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: area,
                    callback:function(ret){//回调方法,需要在本页面Controller中增加方法监听且调用Fast.api.close(ret)传递结果;
                    }
                };
                Fast.api.open(url,title,options);
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        changeuser: function(){
            $("#changeuser").on('click', function() {
                $("#changeuser-form").attr("action",'user/user/changeuser').submit();
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };


    var third_url = function (row,dom) {
        return 'third/index?user_id='+row.id;
    }
    //classes_order_url
     var classes_order_url = function (row,dom) {
        return 'school/classes/order/order/index?user_id='+row.id;
    }
    //classes_hourorder_url
     var classes_hourorder_url = function (row,dom) {
        return 'school/classes/hourorder/order/index?user_id='+row.id;
    }


        var user_auth_url = function (row,dom) {
        return 'manystore/user_auth/index?user_id='+row.id;
    }

        var teacher_url = function (row,dom) {
        return 'school/classes/teacher/index?user_id='+row.id;
    }

        var verification_url = function (row,dom) {
        return 'school/classes/verification/index?user_id='+row.id;
    }

    var manystore_url = function (row,dom) {
        return 'manystore/index/index?user_id='+row.id;
    }


    var evaluate_url= function (row,dom) {
        return 'school/classes/evaluate/index?user_id='+row.id;
    }

    var attachment_url = function (row,dom) {
        return 'general/attachment/index?user_id='+row.id;
    }


    return Controller;
});