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

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'school/classes/activity/order/order/index' + location.search,
                    add_url: 'school/classes/activity/order/order/add',
                    edit_url: 'school/classes/activity/order/order/edit',
                    del_url: 'school/classes/activity/order/order/del',
                    cancel_url: 'school/classes/activity/order/order/cancel',
                    examine_url: 'school/classes/activity/order/order/examine',
                    verification_url: 'school/classes/activity/order/order/verification',
                    refund_url: 'school/classes/activity/order/order/refund',



                    multi_url: 'school/classes/activity/order/order/multi',
                    import_url: 'school/classes/activity/order/order/import',
                    table: 'school_classes_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: 'pay_no', title: __('Pay_no'),visible:false, operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {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'),"9":__('Status 9')}, formatter: Table.api.formatter.status},
                        {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: 'detail.title', title: __('Detail.title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'detail.headimage', title: __('Detail.headimage'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'orderitem.name', title: __('Orderitem.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'orderitem.feel', title: __('Feel'), searchList: {"0":__('Feel 0'),"1":__('Feel 1')}, formatter: Table.api.formatter.normal},

                        {field: 'totalprice', title: __('Totalprice'), operate:'BETWEEN'},



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



                        {field: 'reason', title: __('Reason'), operate: 'LIKE'},

                        {field: 'auth_user_id',visible:false, title: __('Auth_user_id')},
                        {field: 'auth_type',visible:false, title: __('Auth_type'), operate: 'LIKE'},


                        {field: 'manystore_id', title: __('Manystore_id'),visible:false},
                        {field: 'shop_id', title: __('Shop_id')},
                        {field: 'code', title: __('Code'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'codeimage', title: __('Codeimage'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'codeoneimage', title: __('Codeoneimage'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'classes_activity_id', title: __('Classes_activity_id'),visible:false},
                        {field: 'activity_order_detail_id', title: __('Activity_order_detail_id'),visible:false},
                        {field: 'beforeprice', title: __('Beforeprice'), 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', 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: 'canceltime', title: __('Canceltime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'auth_time', title: __('Auth_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'reservation_time', title: __('Reservation_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'finishtime', title: __('Finishtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'refundtime', title: __('Refundtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'total_refundprice', title: __('Total_refundprice'), operate:'BETWEEN'},
                        {field: 'real_refundprice', 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: 'verification_user_id', title: __('Verification_user_id')},
                        {field: 'verification_type', title: __('Verification_type'), operate: 'LIKE'},
                        {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: 'manystore.nickname',visible:false, title: __('Manystore.nickname'), operate: 'LIKE'},
                        {field: 'shop.name',visible:false, title: __('Shop.name'), operate: 'LIKE'},
                        {field: 'shop.logo',visible:false, title: __('Shop.logo'), operate: 'LIKE'},
                        {field: 'activity.title',visible:false, title: __('Activity.title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'activity.headimage',visible:false, title: __('Activity.headimage'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},


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

                                {name: 'cancel',
                                    text: '取消订单',
                                    icon: 'fa fa-user-times',
                                    classname: 'btn btn-xs btn-warning btn-magic btn-ajax',
                                    url: $.fn.bootstrapTable.defaults.extend.cancel_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.orderitem.feel == '0'){
                                            return true;
                                        }
                                        //免费订单未支付和进行中都可取消
                                        if((row.status == '0' || row.status == '2'|| row.status == '3' )&& row.orderitem.feel == '1'){
                                            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: 'examine',
                                    text: __('预约审核'),
                                    title: __('预约审核'),
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    icon: 'fa fa-sign-in',
                                    url: $.fn.bootstrapTable.defaults.extend.examine_url,
                                    callback: function (data) {

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



                                {
                                    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_item',
                                    text: __('下单规格'),
                                    title: __('下单规格'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-calendar',
                                    dropdown : '更多',
                                    url: order_item_url,
                                    callback: function (data) {

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


                                {
                                    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: 'manystore',
                                    text: __('查看机构'),
                                    title: __('查看机构'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-home',
                                    dropdown : '更多',
                                    url: manystore_url,
                                    callback: function (data) {

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


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



                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'school/classes/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/classes/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/classes/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();
        },
        examine: function(){
            $("#examine").on('click', function() {
                $("#examine-form").attr("action",'school/classes/activity/order/order/examine').submit();
            });
            Controller.api.bindevent();
        },
        verification: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };


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

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

    var order_detail_url = function (row,dom) {
        return 'school/classes/activity/order/order_detail/index?classes_activity_order_id='+row.id;
    }
    var order_item_url = function (row,dom) {
        return 'school/classes/activity/order/order_item/index?classes_activity_order_id='+row.id;
    }

    var manystore_url = function (row,dom) {
        return 'manystore/index/index?shop_id='+row.shop_id;
    }


    return Controller;
});
