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

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

                    auth_yes_url: 'score/log/auth_yes',
                    auth_no_url: 'score/log/auth_no',

                    table: 'score_log',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                asyndownload: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'score_event_id', title: __('Score_event_id'),visible: false},
                        {field: 'score_employee_id', title: __('Score_employee_id'),visible: false},
                        {field: 'user_id', title: __('User_id'),visible: false},
                        {field: 'score_team_id', title: __('Score_team_id'),visible: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'score', title: __('Score'), operate: false},
                        {field: 'scoreevent.desc', title: __('Event.desc'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'desc', title: __('Desc'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'reason', title: __('Reason'), operate: 'LIKE'},
                        {field: 'employee.name', title: __('Employee.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'team.name', title: __('Team.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},


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



                        {field: 'authtime', title: __('Authtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {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: 'scoreevent.name', title: __('Event.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

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


            // 批量审核成功
            $(document).on("click", ".btn-auth_yes", function () {
               let ids = Table.api.selectedids(table);
                ids  = ($.isArray(ids) ? ids.join(",") : ids);

                //先提示，确认是否提交，确认提交再调用接口
                Layer.confirm('确定要批量审核通过吗？', function (index) {
                    //调用接口
                    let url = $.fn.bootstrapTable.defaults.extend.auth_yes_url;
                    options = {url: url, data: { ids: ids}};
                    Fast.api.ajax(options, function (data, ret) {
                        table.trigger("uncheckbox");
                        // var success = $(element).data("success") || $.noop;
                        //提交成功提示信息
                        Fast.api.success(ret.msg);

                        // if (typeof success === 'function') {
                        //     if (false === success.call(element, data, ret)) {
                        //         return false;
                        //     }
                        // }
                        table.bootstrapTable('refresh');
                    }, function (data, ret) {
                        // var error = $(element).data("error") || $.noop;
                        //提交失败提示信息
                        Fast.api.error(ret.msg);
                        // if (typeof error === 'function') {
                        //     if (false === error.call(element, data, ret)) {
                        //         return false;
                        //     }
                        // }
                    });
                });



            });
            // 批量审核失败
            $(document).on("click", ".btn-auth_no", function () {
                let ids = Table.api.selectedids(table);
                ids  = ($.isArray(ids) ? ids.join(",") : ids);
                //调用接口
                Layer.confirm('确定要批量审核不通过吗？', function (index) {
                    //调用接口
                    let url = $.fn.bootstrapTable.defaults.extend.auth_no_url;
                    options = {url: url, data: { ids: ids}};
                    Fast.api.ajax(options, function (data, ret) {
                        table.trigger("uncheckbox");
                        // var success = $(element).data("success") || $.noop;
                        //提交成功提示信息
                        Fast.api.success(ret.msg);

                        // if (typeof success === 'function') {
                        //     if (false === success.call(element, data, ret)) {
                        //         return false;
                        //     }
                        // }
                        table.bootstrapTable('refresh');
                    }, function (data, ret) {
                        // var error = $(element).data("error") || $.noop;
                        //提交失败提示信息
                        Fast.api.error(ret.msg);
                        // if (typeof error === 'function') {
                        //     if (false === error.call(element, data, ret)) {
                        //         return false;
                        //     }
                        // }
                    });
                });


            });


        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'score/log/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'score/log/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'score/log/destroy',
                                    refresh: true
                                }
                            ],
                            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]"));
            }
        }
    };
    return Controller;
});
