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.on('load-success.bs.table', function (e, data) {
			    //这里可以获取从服务端获取的JSON数据
			    console.log(data);
			    //这里我们手动设置底部的值
			    $("#total").text(data.total);
			});

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                height: 750,
                sortName: '',
                escape: false,
                pagination: false,
				search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true, operate: false},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('拉黑')}, operate: false},
                        // {field: 'group.name', title: __('Group')},
                        // {field: 'username', title: __('Username'), operate: 'LIKE'},
						{field: 'nickname', title: __('Nickname'), align: 'left', formatter:function (value, row, index) {
						        return value.toString().replace(/(&|&)nbsp;/g, ' ');
						    }
						},
                        // {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: 'idcard_front', title: __('Idcard_front'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'is_work', title: __('Is_work'), formatter: Table.api.formatter.status, searchList: {0: __('否'), 1: __('是')}},
                        {field: 'factory.factory_shortname', title: __('入职工厂'), operate: 'LIKE'},
                        {field: 'join_time', title: __('入职日期'), formatter: Table.api.formatter.datetime, operate: false},
                        {field: 'log_days_month', title: __('本月打卡天数'), operate: false},
                        {field: 'work_hours_month', title: __('本月打卡工时'), operate: false},
                        {field: 'is_complete', title: __('Is_complete'), formatter: Table.api.formatter.status, searchList: {0: __('否'), 1: __('是')}},
                        // {field: 'work_subsidy', title: __('Work_subsidy'), operate: false},
                        {field: 'recruit_subsidy', title: __('Recruit_subsidy'), operate: false},
                        {field: 'recruit_subsidy2', title: __('Recruit_subsidy2'), operate: false},
                        {field: 'work_subsidy_month', title: __('Work_subsidy_month'), operate: false},
                        {field: 'recruit_subsidy_month', title: __('Recruit_subsidy_month'), operate: false},
                        {field: 'lower_num', title: __('Lower_num'), operate: false},
                        // {field: 'level', title: __('Level'), operate: 'BETWEEN', sortable: true},
                        // {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        // {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},
                        //操作栏,默认有编辑、删除或排序按钮,可自定义配置buttons来扩展按钮
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'user_work_log',
                                    text: __('打卡记录'),
                                    title: __('打卡记录'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'user/work_log?user_id={id}',
                                },
                                {
                                    name: 'detail',
                                    text: __('查看下级'),
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'user/user_recruit_subsidy_log?user_id={id}'
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});