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

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pageSize: 30,
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id'),visible: false},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},

                        {field: 'team.name', title: __('Team.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'opertime', title: __('统计时间查询'), operate:'RANGE', addclass:'datetimerange', autocomplete:false,formatter:function (value, row, index) {
                                return row.opertime ? row.opertime : '未查询';
                            }},
                        {field: 'score_count', title: __('积分统计（根据具体统计时间筛选）'), sortable: true, formatter:function (value, row, index) {
                                // if( !row.opertime){
                                //     return "<span style='color: #0d6aad'>#请在点开【搜索栏】后选择【统计时间查询】提交查询#</span>";
                                // }
                                return `
                                    <span style="color: #0d6aad">${row.score_count}</span>
                                `;
                            },searchable:false},

                        {field: 'user.nickname', title: __('User.nickname'),visible: false, operate: 'LIKE'},
                        {field: 'user.mobile', title: __('User.mobile'), operate: 'LIKE'},
                        {field: 'user.avatar', title: __('User.avatar'),visible: false, operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},


                        {field: 'team_id', title: __('Team_id'),visible: false},
                        {field: 'status', title: __('Status'), searchList: {"entry":__('Status entry'),"exit":__('Status exit')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {field: 'operate', title: __('Operate'), table: table , buttons: [
                                {
                                    name: 'select',
                                    text: __('积分记录'),
                                    title: __('查看积分记录'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: log_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     if(row.status == '1' || row.status == '2' ) return true;
                                    //     return false;
                                    // }
                                },
                                {
                                    name: 'user',
                                    text: __('登录账户信息修改'),
                                    title: __('登录账户信息修改'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: user_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     if(row.status == '1' || row.status == '2' ) return true;
                                    //     return false;
                                    // }
                                },

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

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {

                $(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 ids = $(this).attr('data-id');
                    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.close(ret);
                        }
                    };
                    Fast.api.open(url,title,options);
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };


    var log_url = function (row,dom) {
        return 'score/log/index?score_employee_id='+row.id;
    }
    var user_url = function (row,dom) {
        return 'user/user/edit/ids/'+row.user_id;
    }



    return Controller;
});
