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

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'school/classes/hourorder/order/index' + location.search,
                    add_url: 'school/classes/hourorder/order/add'+ location.search,
                    edit_url: 'school/classes/hourorder/order/edit'+ location.search,
                    examine_url: 'school/classes/hourorder/order/examine',
                    verification_url: 'school/classes/hourorder/order/verification',
                    del_url: 'school/classes/hourorder/order/del',
                    multi_url: 'school/classes/hourorder/order/multi',
                    import_url: 'school/classes/hourorder/order/import',
                    table: 'school_classes_hour_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: 'schoolclassesorderdetail.feel', title: __('是否免费'), searchList: {"0":__('付费课程'),"1":__('免费课程')}, formatter: Table.api.formatter.normal},


                        {field: 'status', title: __('Status'), searchList: {"-3":__('Status -3'),"-1":__('Status -1'),"0":__('Status 0'),"3":__('Status 3')}, formatter: Table.api.formatter.status},


                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {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: '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: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'classes_order_id',visible:false, title: __('Classes_order_id')},
                        {field: 'classes_lib_spec_id',visible:false, title: __('Classes_lib_spec_id')},
                        {field: 'user_id', 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: 'limit_num', title: __('Limit_num')},

                        {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: 'sign_num',visible:false, title: __('Sign_num')},
                        // {field: 'verification_num',visible:false, title: __('Verification_num')},
                        {field: 'verification_user_id',visible:false, title: __('Verification_user_id')},
                        {field: 'reservation_time', title: __('Reservation_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'finish_time', title: __('Finish_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'cancel_time', title: __('Cancel_time'), 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: 'manystore_id', title: __('Manystore_id'),visible:false},
                        {field: 'shop_id', title: __('Shop_id'),visible:false},

                        {field: 'operate', title: __('Operate'), table: table , buttons: [
                                {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 == 0){
                                            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 == '-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',
                                    dropdown : '更多',
                                    icon: 'fa fa-list',
                                    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: 'schoolclasseslibspec.name',visible:false, title: __('Schoolclasseslibspec.name'), operate: 'LIKE'},
                                               // {field: 'schoolclasseslib.title',visible:false, title: __('Schoolclasseslib.title'), operate: 'LIKE'},
                        // {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/hourorder/order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            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/hourorder/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/hourorder/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/hourorder/order/examine').submit();
            });
            Controller.api.bindevent();
        },
        verification: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {

                //老师必须是上面机构中的
                $("#c-classes_lib_spec_id").data("params", function (obj) {
                    //obj为SelectPage对象
                    return {custom: {classes_lib_id: $("#c-classes_lib_id").val(),status: '1'}};
                });


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

    var order_log_url = function (row,dom) {
        return 'school/classes/hourorder/order_log/index?classes_hour_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 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;
});