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

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'people_num', title: __('人数'), operate: false},
                        {field: 'people_names', title: __('成员'), operate: false},
                        {field: 'opertime', title: __('统计时间查询'), operate:'RANGE', addclass:'datetimerange', autocomplete:false,formatter:function (value, row, index) {
                                return row.opertime ? row.opertime : '未查询';
                            }},
                        {field: 'score_count', title: __('积分统计（根据具体统计时间筛选）'),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: '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: 'select2',
                                    text: __('队员详情'),
                                    title: __('队员详情'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: employee_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_team_id='+row.id;
    }
    var employee_url= function (row,dom) {
        return 'score/employee/index?team_id='+row.id;
    }

    return Controller;
});
