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

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'fullname', title: __('Fullname')},
                        {field: 'user.username', title: __('User.username')},
                        {field: 'user.avatar', title: __('User.avatar'), formatter: Table.api.formatter.image},
                        {field: 'user.mobile', title: __('User.mobile')},
                        {field: 'address', title: __('Address')},
                        {field: 'pre_date', title: __('Pre_date'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'pre_time', title: __('Pre_time')},
                        {field: 'status', title: __('status'), formatter: Table.api.formatter.flag,searchList: {0: __('in_audit'), 1: __('adopt'),2: __('reject')}},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons: [
                            {
                                name: 'ajax',
                                text: __('通过'),
                                title: __('通过'),
                                classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                icon: 'fa',
                                url: 'sell/adopt',
                                confirm: '确认通过',
                                success: function (data, ret) {
                                    Layer.alert(ret.msg);
                                    $(".btn-refresh").trigger("click");
                                    //如果需要阻止成功提示,则必须使用return false;
                                    //return false;
                                },
                                error: function (data, ret) {
                                    Layer.alert(ret.msg);
                                    return false;
                                },visible: function (row) {
                                    if(row.status == 1){
                                        return false;//隐藏
                                    }else{
                                        return true;
                                    }
                                }
                            },
                            {
                                name: 'ajax',
                                text: __('驳回'),
                                title: __('驳回'),
                                classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                icon: 'fa',
                                url: 'sell/reject',
                                confirm: '确认驳回',
                                success: function (data, ret) {
                                    Layer.alert(ret.msg);
                                    $(".btn-refresh").trigger("click");
                                    //如果需要阻止成功提示,则必须使用return false;
                                    //return false;
                                },
                                error: function (data, ret) {
                                    Layer.alert(ret.msg);
                                    return false;
                                },visible: function (row) {
                                    if(row.status == 2){
                                        return false;//隐藏
                                    }else{
                                        return true;
                                    }
                                }
                            },
                            {
                                name: 'staff',
                                text: '指派',
                                title: '指派',
                                icon: 'fa fa-list',
                                classname: 'btn btn-xs btn-success btn-dialog',
                                url: "appoint/index",
                                // url:function(row){
                                //     return 'appoint/appoint_index?s_id='+row.id;
                                // },
                                visible: function (row) {
                                    if(row.status == 1){
                                        return true;
                                    }else{
                                        return false;//隐藏
                                    }
                                }
                            }
                        ]}
                    ]
                ]
            });

            // 为表格绑定事件
            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;
});