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

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'school/activity/order/order/index' + location.search,
                    freecancel_url: 'school/activity/order/order/freecancel',//免费和待支付取消订单
                    paidcancel_url: 'school/activity/order/order/paidcancel',//付费取消订单
                    add_url: 'school/activity/order/order/add',
                    edit_url: 'school/activity/order/order/edit',
                    del_url: 'school/activity/order/order/del',
                    multi_url: 'school/activity/order/order/multi',
                    import_url: 'school/activity/order/order/import',
                    verification_url: 'school/activity/order/order/verification',
                    refund_url: 'school/activity/order/order/refund',
                    shop_confirmation_url: 'school/activity/order/order/shop_confirmation',
                    after_sales_url: 'school/activity/order/order/after_sales',
                    admin_confirmation_url: 'school/activity/order/order/admin_confirmation',

                    table: 'school_activity_order',
                }
            });

            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: 'order_no', title: __('Order_no'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'activity.title', title: __('Activity.title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'activity.images', title: __('Activity.images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},

                        {field: 'status', title: __('Status'), searchList: {"-3":__('Status -3'),"0":__('Status 0'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5'),"6":__('Status 6'),"7":__('Status 7'),"9":__('Status 9')}, formatter: Table.api.formatter.status},


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



                        {field: 'pay_no', title: __('Pay_no'),visible:false,  operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'user_id',visible:false,  title: __('User_id')},
                        {field: 'activity_id',visible:false, title: __('Activity_id')},
                        {field: 'activity_order_detail_id',visible:false, title: __('Activity_order_detail_id')},
                        {field: 'beforeprice', title: __('Beforeprice'), operate:'BETWEEN'},
                        {field: 'totalprice', title: __('Totalprice'), operate:'BETWEEN'},
                        {field: 'num', title: __('购买人数'), operate:'BETWEEN'},
                        {field: 'detail.price', title: __('购买单价'), operate:'BETWEEN'},

                        {field: 'payprice', title: __('Payprice'), operate:'BETWEEN'},
                        {field: 'pay_type', title: __('Pay_type'), searchList: {"yue":__('Pay_type yue'),"wechat":__('Pay_type wechat')}, formatter: Table.api.formatter.normal},
                                                {field: 'before_status',visible:false, title: __('Before_status'), searchList: {"-3":__('Before_status -3'),"0":__('Before_status 0'),"2":__('Before_status 2'),"3":__('Before_status 3'),"4":__('Before_status 4'),"6":__('Before_status 6'),"9":__('Before_status 9')}, formatter: Table.api.formatter.status},
                        {field: 'server_status', title: __('Server_status'), searchList: {"0":__('Server_status 0'),"3":__('Server_status 3'),"6":__('Server_status 6')}, formatter: Table.api.formatter.status},

                        {field: 'suspend_status', title: __('Suspend_status'), searchList: {"0":__('Suspend_status 0'),"1":__('Suspend_status 1'),"2":__('Suspend_status 2')}, formatter: Table.api.formatter.status},

                        {field: 'canceltime',visible:false, title: __('Canceltime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'paytime',visible:false, title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'auth_time',visible:false, title: __('Auth_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'reservation_time',visible:false, title: __('Reservation_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'finishtime',visible:false, title: __('Finishtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'refundtime',visible:false, title: __('Refundtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'total_refundprice',visible:false, title: __('Total_refundprice'), operate:'BETWEEN'},
                        {field: 'real_refundprice',visible:false, title: __('Real_refundprice'), operate:'BETWEEN'},
                        {field: 'sub_refundprice', title: __('Sub_refundprice'), operate:'BETWEEN'},
                        {field: 'platform', title: __('Platform'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'reason',visible:false, title: __('Reason'), operate: 'LIKE'},
                        {field: 'auth_reason',visible:false, title: __('售后申请原因'), operate: 'LIKE'},
                        {field: 'auth_status', title: __('Auth_status'), searchList: {"0":__('Auth_status 0'),"1":__('Auth_status 1'),"2":__('Auth_status 2')}, formatter: Table.api.formatter.status},
                        {field: 'auth_user_id',visible:false, title: __('Auth_user_id')},
                        {field: 'auth_type',visible:false, title: __('Auth_type'), operate: 'LIKE'},
                        {field: 'refund_no',visible:false, title: __('Refund_no'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'refund_error', title: __('Refund_error'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'refundsendtime',visible:false, title: __('Refundsendtime'), 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: 'detail.title' ,visible:false , title: __('Detail.title'), 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}


                        {field: 'operate', title: __('Operate'), table: table , buttons: [

                                {
                                    name: 'after_sales',
                                    text: __('帮用户发起售后'),
                                    title: __('帮用户发起售后'),
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    icon: 'fa fa-sign-in',
                                    url: $.fn.bootstrapTable.defaults.extend.after_sales_url,
                                    callback: function (data) {

                                    },
                                    visible: function (row) {
                                        //只有付费订单有售后
                                        return (row.status == '2' || row.status == '3'|| row.status == '9')  && row.detail.feel == '0';
                                    }
                                },


                                {
                                    name: 'shop_confirmation',
                                    text: __('代商家处理售后'),
                                    title: __('代商家处理售后'),
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    icon: 'fa fa-sign-in',
                                    url: $.fn.bootstrapTable.defaults.extend.shop_confirmation_url,
                                    callback: function (data) {

                                    },
                                    visible: function (row) {
                                        return row.status == '4';
                                    }
                                },
                                {
                                    name: 'admin_confirmation',
                                    text: __('处理挂起单'),
                                    title: __('处理挂起单'),
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    icon: 'fa fa-sign-in',
                                    url: $.fn.bootstrapTable.defaults.extend.admin_confirmation_url,
                                    callback: function (data) {

                                    },
                                    visible: function (row) {
                                        return row.status == '7';
                                    }
                                },



                                {name: 'freecancel',
                                    text: '取消订单',
                                    icon: 'fa fa-user-times',
                                    classname: 'btn btn-xs btn-warning btn-magic btn-ajax',
                                    url: $.fn.bootstrapTable.defaults.extend.freecancel_url,
                                    confirm: '确认取消订单',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg );
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                    visible: function (row) {
                                        //非免费订单只有未支付可取消
                                        if(row.status == '0' && row.detail.feel == '0'){
                                            return true;
                                        }
                                        //免费订单未支付和进行中都可取消
                                        if((row.status == '0' || row.status == '2'|| row.status == '3' )&& row.detail.feel == '1'){
                                            return true;
                                        }
                                        return false;
                                    }},


                                {name: 'paidcancel',
                                    text: '取消订单',
                                    icon: 'fa fa-user-times',
                                    classname: 'btn btn-xs btn-warning btn-magic btn-ajax',
                                    url: $.fn.bootstrapTable.defaults.extend.paidcancel_url,
                                    confirm: '确认取消订单',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg );
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                    visible: function (row) {
                                        //非免费订单只有未支付可取消
                                        if((row.status == '2'|| row.status == '3' )&& row.detail.feel == '0'){
                                            return true;
                                        }

                                        return false;
                                    }},


                                // {name: 'verification',
                                //     text: '预约核销',
                                //     icon: 'fa fa-sign-in',
                                //     classname: 'btn btn-xs btn-warning btn-magic btn-ajax',
                                //     url: $.fn.bootstrapTable.defaults.extend.verification_url,
                                //     confirm: '预约核销',
                                //     success: function (data, ret) {
                                //         Layer.alert(ret.msg );
                                //         $(".btn-refresh").trigger("click");
                                //     },
                                //     error: function (data, ret) {
                                //         Layer.alert(ret.msg);
                                //         return false;
                                //     },
                                //     visible: function (row) {
                                //         //显示条件 只能待入住订单
                                //         if(row.status == '3'){
                                //             return true;
                                //         }
                                //         return false;
                                //     }},

                                {name: 'refund',
                                    text: '退款重试(若失败双击本行查看原因)',
                                    icon: 'fa fa-sign-in',
                                    classname: 'btn btn-xs btn-danger  btn-magic btn-ajax',
                                    url: $.fn.bootstrapTable.defaults.extend.refund_url,
                                    confirm: '（本功能主要适用平台退款因账户原因失败需要重新操作的订单）重新发起退款，如果是第三方支付请等待回调！',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg );
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                    visible: function (row) {
                                        //显示条件 只能待入住订单
                                        if(row.status == '5'){
                                            return true;
                                        }
                                        return false;
                                    }
                                },


                                {
                                    name: 'order_detail',
                                    text: __('下单锁定的活动信息'),
                                    title: __('下单锁定的活动信息'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-calendar',
                                    dropdown : '更多',
                                    url: order_detail_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.paytime;
                                    // }
                                },
                                {
                                    name: 'order_code',
                                    text: __('核销管理'),
                                    title: __('核销管理'),
                                        icon: 'fa fa-calendar',
                                        classname: 'btn btn-xs btn-warning btn-magic btn-dialog',
                                    // dropdown : '更多',
                                    url: order_code_url,
                                    callback: function (data) {

                                    },
                                    visible: function (row) {
                                        return row.paytime || ( row.status > '0');
                                    }
                                },


                                {
                                    name: 'order_log',
                                    text: __('订单日志'),
                                    title: __('订单日志'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-list',
                                    dropdown : '更多',
                                    url: order_log_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },

                                {
                                    name: 'activity',
                                    text: __('线上活动查看'),
                                    title: __('线上活动查看'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-leanpub',
                                    dropdown : '更多',
                                    url: activity_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.paytime;
                                    // }
                                },

                                {
                                    name: 'settle_log',
                                    text: __('资金结算记录'),
                                    title: __('资金结算记录'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-calendar',
                                    dropdown : '更多',
                                    url: settle_log_url,
                                    callback: function (data) {

                                    },
                                    visible: function (row) {
                                        return row.sub_refundprice > 0;
                                    }
                                },



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


                    ]
                ]
            });

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



            //自定义Tab筛选条件
            $('.panel-heading .nav-custom-condition a[data-toggle="tab"]', table.closest(".panel-intro")).on('shown.bs.tab', function (e) {
                var value = $(this).data("value");
                var options = table.bootstrapTable('getOptions');
                var queryParams = options.queryParams;
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    //这一行必须要存在,否则在点击下一页时会丢失搜索栏数据
                    params = queryParams(params);

                    //如果希望追加搜索条件,可使用
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    if (value!=="") {
                        //这里可以自定义多个筛选条件
                        filter.server_status = value;
                        op.server_status = '=';
                    }else{
                        //console.log("111111111111111111");
                        //选全部时要移除相应的条件
                        delete filter.server_status;
                        delete op.server_status;
                    }

                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);

                    //如果希望忽略搜索栏搜索条件,可使用
                    //params.filter = JSON.stringify(value?{admin_id: value}:{});
                    //params.op = JSON.stringify(value?{admin_id: '='}:{});
                    return params;
                };

                table.trigger("uncheckbox");
                table.bootstrapTable('refresh', {pageNumber: 1});
                return false;
            });


            //自定义Tab筛选条件
            $('.panel-heading .nav2-custom-condition a[data-toggle="tab"]', table.closest(".panel-intro")).on('shown.bs.tab', function (e) {
                var value = $(this).data("value");
                var options = table.bootstrapTable('getOptions');
                var queryParams = options.queryParams;
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    //这一行必须要存在,否则在点击下一页时会丢失搜索栏数据
                    params = queryParams(params);

                    //如果希望追加搜索条件,可使用
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    if (value!=="") {
                        //这里可以自定义多个筛选条件
                        filter.auth_status = value;
                        op.auth_status = '=';
                    }else{
                        //console.log("111111111111111111");
                        //选全部时要移除相应的条件
                        delete filter.auth_status;
                        delete op.auth_status;
                    }

                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);

                    //如果希望忽略搜索栏搜索条件,可使用
                    //params.filter = JSON.stringify(value?{admin_id: value}:{});
                    //params.op = JSON.stringify(value?{admin_id: '='}:{});
                    return params;
                };

                table.trigger("uncheckbox");
                table.bootstrapTable('refresh', {pageNumber: 1});
                return false;
            });



            //自定义Tab筛选条件
            $('.panel-heading .nav3-custom-condition a[data-toggle="tab"]', table.closest(".panel-intro")).on('shown.bs.tab', function (e) {
                var value = $(this).data("value");
                var options = table.bootstrapTable('getOptions');
                var queryParams = options.queryParams;
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    //这一行必须要存在,否则在点击下一页时会丢失搜索栏数据
                    params = queryParams(params);

                    //如果希望追加搜索条件,可使用
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    if (value!=="") {
                        //这里可以自定义多个筛选条件
                        filter.suspend_status = value;
                        op.suspend_status = '=';
                    }else{
                        //console.log("111111111111111111");
                        //选全部时要移除相应的条件
                        delete filter.suspend_status;
                        delete op.suspend_status;
                    }

                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);

                    //如果希望忽略搜索栏搜索条件,可使用
                    //params.filter = JSON.stringify(value?{admin_id: value}:{});
                    //params.op = JSON.stringify(value?{admin_id: '='}:{});
                    return params;
                };

                table.trigger("uncheckbox");
                table.bootstrapTable('refresh', {pageNumber: 1});
                return false;
            });

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'school/activity/order/order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            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: 'school/activity/order/order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'school/activity/order/order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

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

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        admin_confirmation: function(){
            $("#admin_confirmation").on('click', function() {
                $("#admin_confirmation-form").attr("action",'school/activity/order/order/admin_confirmation').submit();
            });
            Controller.api.bindevent();
        },
        shop_confirmation: function(){
            $("#shop_confirmation").on('click', function() {
                $("#shop_confirmation-form").attr("action",'school/activity/order/order/shop_confirmation').submit();
            });
            Controller.api.bindevent();
        },

        after_sales: function(){
            $("#after_sales").on('click', function() {
                $("#after_sales-form").attr("action",'school/activity/order/order/after_sales').submit();
            });
            Controller.api.bindevent();
        },


        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };




    var activity_url = function (row,dom) {
        return 'school/activity/activity/index?id='+row.activity_id;
    }

    var order_log_url = function (row,dom) {
        return 'school/activity/order/order_log/index?activity_order_id='+row.id;
    }

    var order_detail_url = function (row,dom) {
        return 'school/activity/order/order_detail/index?activity_order_id='+row.id;
    }
    var order_code_url = function (row,dom) {
        return 'school/activity/order/order_code/index?activity_order_id='+row.id;
    }

    var settle_log_url = function (row,dom) {
        return 'school/activity/order/settle_log/index?activity_order_id='+row.id;
    }




    return Controller;
});
