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

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

                     cancel_url: 'school/classes/order/service_order/cancel',
                    user_confirmation_url: 'school/classes/order/service_order/user_confirmation',
                    shop_confirmation_url: 'school/classes/order/service_order/shop_confirmation',
                    admin_confirmation_url: 'school/classes/order/service_order/admin_confirmation',
                    import_url: 'school/classes/order/service_order/import',
                    table: 'school_classes_service_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: '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',visible:false, 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: 'reason', title: __('Reason'), operate: 'LIKE'},
                        {field: 'classes_order_id',visible:false, title: __('Classes_order_id')},
                        {field: 'user_id',visible:false, title: __('User_id')},
                        {field: 'classes_order_detail_id',visible:false, title: __('Classes_order_detail_id')},
                        {field: 'classes_lib_id',visible:false, title: __('Classes_lib_id')},
                        {field: 'manystore_id',visible:false, title: __('Manystore_id')},
                        {field: 'shop_id',visible:false, title: __('Shop_id')},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"4":__('Status 4'),"7":__('Status 7'),"-3":__('Status -3')}, formatter: Table.api.formatter.status},
                        {field: 'service_stauts', title: __('Service_stauts'), searchList: {"1":__('Service_stauts 1'),"4":__('Service_stauts 4'),"7":__('Service_stauts 7'),"10":__('Service_stauts 10'),"-3":__('Service_stauts -3')}, formatter: Table.api.formatter.normal},
                        {field: 'sales_type', title: __('Sales_type'), searchList: {"-3":__('Sales_type -3'),"1":__('Sales_type 1'),"4":__('Sales_type 4'),"7":__('Sales_type 7'),"10":__('Sales_type 10')}, formatter: Table.api.formatter.normal},
                        {field: 'reject_reason', title: __('Reject_reason'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'reject_images', title: __('Reject_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'auto_price', title: __('Auto_price'), operate:'BETWEEN'},
                        {field: 'loss_proportion', title: __('Loss_proportion'), operate:'BETWEEN'},
                        {field: 'auto_loss_price', title: __('Auto_loss_price'), operate:'BETWEEN'},
                        {field: 'tbc_price', title: __('Tbc_price'), operate:'BETWEEN'},
                        {field: 'c_price', title: __('C_price'), operate:'BETWEEN'},
                        {field: 'real_refundprice', title: __('Real_refundprice'), operate:'BETWEEN'},
                        {field: 'sub_refundprice', title: __('Sub_refundprice'), operate:'BETWEEN'},


                        {field: 'platform', title: __('Platform'), searchList: {"miniapp":__('Platform miniapp')}, formatter: Table.api.formatter.normal},
                        {field: 'pay_type', title: __('Pay_type'), searchList: {"yue":__('Pay_type yue'),"wechat":__('Pay_type wechat')}, formatter: Table.api.formatter.normal},
                        {field: 'refundtime', title: __('Refundtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'rejecttime', title: __('Rejecttime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'handletime', title: __('Handletime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'confirmtime', title: __('Confirmtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'checkouttime', title: __('Checkouttime'), 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: 'schoolclassesorder.order_no',visible:false, title: __('Schoolclassesorder.order_no'), operate: 'LIKE'},
                        {field: 'schoolclassesorder.pay_no',visible:false, title: __('Schoolclassesorder.pay_no'), operate: 'LIKE'},
                        // {field: 'lib.title', title: __('Lib.title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'lib.headimage', title: __('Lib.headimage'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'manystore.nickname',visible:false, title: __('Manystore.nickname'), operate: 'LIKE'},
                        {field: 'manystoreshop.name',visible:false, title: __('Manystoreshop.name'), operate: 'LIKE'},
                        {field: 'manystoreshop.logo',visible:false, title: __('Manystoreshop.logo'), operate: 'LIKE'},

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


                                {
                                    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 == '1' && row.service_stauts == "1";
                                    }
                                },


                                {
                                    name: 'order_log',
                                    text: __('售后日志'),
                                    title: __('售后日志'),
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    icon: 'fa fa-list',
                                    url: order_log_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },
                                {
                                    name: 'order_detail',
                                    text: __('课程信息'),
                                    title: __('课程信息'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-list',
                                    dropdown : '更多',
                                    url: order_detail_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.paytime;
                                    // }
                                },
                                {
                                    name: 'order',
                                    text: __('课程订单查看'),
                                    title: __('课程订单查看'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-list',
                                    dropdown : '更多',
                                    url: order_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },
                                {
                                    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/service_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/service_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/service_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/classes/order/service_order/admin_confirmation').submit();
            });
            Controller.api.bindevent();
        },
        user_confirmation: function(){
            $("#user_confirmation").on('click', function() {
                $("#user_confirmation-form").attr("action",'school/classes/order/service_order/user_confirmation').submit();
            });
            Controller.api.bindevent();
        },
        shop_confirmation: function(){
            $("#shop_confirmation").on('click', function() {
                $("#shop_confirmation-form").attr("action",'school/classes/order/service_order/shop_confirmation').submit();
            });
            Controller.api.bindevent();
        },

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


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

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


       var generrate = {
        listen:function () {
            this.offlineListen();
            // this.priceListen();
        },
        offlineListen:function () {
            var that = this;
            // console.log($("input:radio[name='row[address_type]']").val())
            // this.setOfflineType($("input:radio[name='row[address_type]']").val());
            $("input:radio[name='row[status]']").change(function (){
                that.setOfflineType($(this).val());
            });
        },
        setOfflineType:function (val) {
            switch (val) {
                case 'yes':
                    $('#status_no').hide();
                    $('#status_yes').show();
                    break;
                case 'no':
                    $('#status_yes').hide();
                    $('#status_no').show();
                    break;
            }
        }
    }

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

    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;
});