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

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'school/classes/order/order/index' + location.search,
                    add_url: 'school/classes/order/order/add',
                    edit_url: 'school/classes/order/order/edit',
                    del_url: 'school/classes/order/order/del',
                    cancel_url: 'school/classes/order/order/cancel',
                    after_sales_url: 'school/classes/order/order/after_sales',
                    multi_url: 'school/classes/order/order/multi',
                    import_url: 'school/classes/order/order/import',
                    table: 'school_classes_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'},
                        {field: 'status', title: __('Status'), searchList: {"-3":__('Status -3'),"0":__('Status 0'),"3":__('Status 3'),"4":__('Status 4'),"6":__('Status 6'),"9":__('Status 9')}, formatter: Table.api.formatter.status},

                        {field: 'pay_no', title: __('Pay_no'), operate: 'LIKE',visible:false},

                        {field: 'schoolclassesorderdetail.feel', title: __('是否免费'), searchList: {"0":__('付费课程'),"1":__('免费课程')}, formatter: Table.api.formatter.normal},
                        {field: 'schoolclassesorderdetail.title', title: __('Schoolclassesorderdetail.title'), operate: 'LIKE'},
                        {field: 'schoolclassesorderdetail.headimage', title: __('Schoolclassesorderdetail.headimage'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},

                        {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: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},


                        {field: 'user_id', title: __('User_id')},
                        {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: 'manystore_id', title: __('Manystore_id'),visible:false},
                        {field: 'shop_id', title: __('Shop_id'),visible:false},
                        {field: 'code', title: __('Code'),visible:false, operate: 'LIKE'},
                        // {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_lib_id', title: __('Classes_lib_id')},
                        {field: 'classes_order_detail_id',visible:false, title: __('Classes_order_detail_id')},
                        {field: 'beforeprice',visible:false, title: __('Beforeprice'), operate:'BETWEEN'},
                        {field: 'totalprice', title: __('Totalprice'), operate:'BETWEEN'},
                        {field: 'before_status',visible:false, title: __('Before_status'), searchList: {"-3":__('Before_status -3'),"0":__('Before_status 0'),"3":__('Before_status 3'),"6":__('Before_status 6'),"9":__('Before_status 9')}, formatter: Table.api.formatter.status},
                        {field: 'server_status',visible:false, title: __('Server_status'), searchList: {"0":__('Server_status 0'),"3":__('Server_status 3'),"6":__('Server_status 6')}, formatter: Table.api.formatter.status},
                        {field: 'reason',visible:false, title: __('Reason'), operate: 'LIKE'},
                        // {field: 'auth_manystore_id',visible:false, title: __('Auth_manystore_id')},
                        // {field: 'auth_opinion',visible:false, title: __('Auth_opinion'), operate: 'LIKE'},
                        // {field: 'auth_file',visible:false, title: __('Auth_file'), operate: false},
                        // {field: 'admin_id',visible:false, title: __('Admin_id')},
                        // {field: 'result_status',visible:false, title: __('Result_status'), searchList: {"0":__('Result_status 0'),"3":__('Result_status 3'),"6":__('Result_status 6')}, formatter: Table.api.formatter.status},
                        // {field: 'result_text',visible:false, title: __('Result_text'), operate: 'LIKE'},
                        // {field: 'result_file',visible:false, title: __('Result_file'), operate: false},
                        {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: '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',visible:false, title: __('Total_refundprice'), operate:'BETWEEN'},
                        {field: 'real_refundprice',visible:false, title: __('Real_refundprice'), operate:'BETWEEN'},
                        {field: 'sub_refundprice',visible:false, title: __('Sub_refundprice'), operate:'BETWEEN'},
                        {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: 'manystore.avatar',visible:false, title: __('Manystore.avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                                                {field: 'manystoreshop.name',visible:false, title: __('Manystoreshop.name'), operate: 'LIKE'},
                        {field: 'manystoreshop.image',visible:false, title: __('Manystoreshop.image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'manystoreshop.address_city',visible:false, title: __('Manystoreshop.address_city'), operate: 'LIKE'},
                        {field: 'manystoreshop.province',visible:false, title: __('Manystoreshop.province')},
                        {field: 'manystoreshop.city',visible:false, title: __('Manystoreshop.city')},
                        {field: 'manystoreshop.district',visible:false, title: __('Manystoreshop.district')},
                        {field: 'manystoreshop.address',visible:false, title: __('Manystoreshop.address'), operate: 'LIKE'},
                        {field: 'manystoreshop.address_detail',visible:false, title: __('Manystoreshop.address_detail'), operate: 'LIKE'},
                        {field: 'schoolclasseslib.title',visible:false, title: __('Schoolclasseslib.title'), operate: 'LIKE'},
                        {field: 'schoolclasseslib.headimage',visible:false, title: __('Schoolclasseslib.headimage'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                                               {field: 'admin.nickname', title: __('Admin.nickname'),visible:false, operate: 'LIKE'},
                        {field: 'admin.avatar', title: __('Admin.avatar'),visible:false, operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'classes_service_order_id', title: __('售后订单id'),visible:false},


                        {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.schoolclassesorderdetail.feel == '0'){
                                            return true;
                                        }
                                        //免费订单未支付和进行中都可取消
                                        if((row.status == '0' || row.status == '3' )&& row.schoolclassesorderdetail.feel == '1'){
                                            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: 'service_order',
                                    text: __('售后单情况'),
                                    title: __('售后单情况'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    dropdown : '更多',
                                    url: service_order_url,
                                    callback: function (data) {

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

                                {
                                    name: 'order',
                                    text: __('课时预约发起'),
                                    title: __('课时预约发起'),
                                    classname: 'btn btn-xs btn-danger btn-magic  btn-dialog',
                                    icon: 'fa fa-calendar',
                                    url: order_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: 'evaluate',
                                    text: __('查看用户评价'),
                                    title: __('查看用户评价'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-handshake-o',
                                    dropdown : '更多',
                                    url: evaluate_url,
                                    callback: function (data) {

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

                                {
                                    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 == '3' || row.status == '9')  && row.schoolclassesorderdetail.feel == '0';
                                    }
                                },
                                {
                                    name: 'classes_lib',
                                    text: __('线上课程查看'),
                                    title: __('线上课程查看'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-leanpub',
                                    dropdown : '更多',
                                    url: classes_lib_url,
                                    callback: function (data) {

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

                                //
                                // {name: 'unsetmockauth',
                                //     text: '取消加圈资格',
                                //     icon: 'fa fa-sign-in',
                                //     classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                //     url: $.fn.bootstrapTable.defaults.extend.unsetmockauth_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.mock_type == "base" &&  row.add_mock_status == "2"){
                                //             return true;
                                //         }
                                //         return false;
                                //     }},

                            ], 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/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: '130px',
                            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/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/order/order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        after_sales: function(){
            $("#after_sales").on('click', function() {
                $("#after_sales-form").attr("action",'school/classes/order/order/after_sales').submit();
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };


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

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

    var order_url = function (row,dom) {
        return 'school/classes/hourorder/order/index?classes_order_id='+row.id + '&classes_lib_id='+row.classes_lib_id;
    }

        var service_order_url = function (row,dom) {
        return 'school/classes/order/service_order/index?classes_order_id='+row.id;
    }


    var evaluate_url= function (row,dom) {
        return 'school/classes/evaluate/index?classes_order_id='+row.id + '&classes_lib_id='+row.classes_lib_id+ '&shop_id='+row.shop_id+ '&teacher_id='+row.schoolclassesorderdetail.teacher_id+ '&user_id='+row.user_id+ '&nickname='+row.user.nickname  + '&image='+row.user.avatar;
    }


    var classes_lib_url= function (row,dom) {
        return 'school/classes/classes_lib/index?id='+row.classes_lib_id+ '&shop_id='+row.shop_id+ '&teacher_id='+row.schoolclassesorderdetail.teacher_id;
    }


    return Controller;
});