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

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                asyndownload: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},

                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'classes_star', title: __('Classes_star'), operate:'BETWEEN'},
                        {field: 'teacher_star', title: __('Teacher_star'), operate:'BETWEEN'},
                        {field: 'shop_star', title: __('Shop_star'), operate:'BETWEEN'},
                        {field: 'message_text', title: __('Message_text'), operate:'BETWEEN'},



                        {field: 'evaluate_time', title: __('Evaluate_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'top', title: __('Top'), searchList: {"0":__('Top 0'),"1":__('Top 1')}, formatter: Table.api.formatter.normal},
                        {field: 'weigh', title: __('Weigh'), operate: false},

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


                        {field: 'shop.name', title: __('Shop.name'), 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: 'teacher.name', title: __('Teacher.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'teacher.head_image', title: __('Teacher.head_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},



                        {field: 'classes_lib_id', title: __('Classes_lib_id'),visible:false},
                        {field: 'classes_order_id', title: __('Classes_order_id'),visible:false},
                        {field: 'manystore_id', title: __('Manystore_id'),visible:false},
                        {field: 'shop_id', title: __('Shop_id'),visible:false},
                        {field: 'teacher_id', title: __('Teacher_id'),visible:false},

                        {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: 'classesorder.order_no', title: __('Order.order_no'),visible:false, operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'manystore.nickname', title: __('Manystore.nickname'),visible:false, operate: 'LIKE'},
                         {field: 'shop.logo', title: __('Shop.logo'),visible:false, operate: 'LIKE'},

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

                                {
                                    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: '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;
                                    // }
                                },
                                {
                                    name: 'order',
                                    text: __('评价的课程购买订单'),
                                    title: __('评价的课程购买订单'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-cart-arrow-down',
                                    dropdown : '更多',
                                    url: order_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },
                                {
                                    name: 'teacher',
                                    text: __('评价老师信息'),
                                    title: __('评价老师信息'),
                                    classname: 'btn  btn-xs btn-danger btn-magic  btn-dialog',
                                    icon: 'fa fa-user',
                                    url: teacher_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },
                            ], events: Table.api.events.operate, formatter: Table.api.formatter.operate},


                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {



                $("#c-classes_order_id").data("params", function (obj) {
                    //obj为SelectPage对象
                    return {custom: {shop_id: $("#c-shop_id").val(),classes_lib_id: $("#c-classes_lib_id").val(),user_id: $("#c-user_id").val()}};
                });


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

                $("#c-classes_lib_id").data("params", function (obj) {
                    //obj为SelectPage对象
                    return {custom: {shop_id: $("#c-shop_id").val(),teacher_id: $("#c-teacher_id").val()}};
                });

                //机构清除老师也要清除
                $("#c-shop_id").change(function () {
                    $("#c-teacher_id").selectPageClear();
                    $("#c-classes_lib_id").selectPageClear();
                    $("#c-classes_order_id").selectPageClear();
                });
                $("#c-teacher_id").change(function () {
                    $("#c-classes_lib_id").selectPageClear();
                    $("#c-classes_order_id").selectPageClear();
                });

                $("#c-classes_lib_id").change(function () {
                    $("#c-classes_order_id").selectPageClear();
                });

                $("#c-user_id").change(function () {
                    console.log(this);
                });




                $(document).on('click', '.btn-changeuser', function (event) {
                    var url = $(this).attr('data-url');
                    if(!url) return false;
                    var title = $(this).attr('title');
                    var width = $(this).attr('data-width');
                    var height = $(this).attr('data-height');
                    // var ids = $(this).attr('data-id');
                    var area = ['98%','98%'];
                    var options = {
                        shadeClose: false,
                        shade: [0.3, '#393D49'],
                        area: area,
                        callback:function(ret){//回调方法，需要在本页面Controller中增加方法监听且调用Fast.api.close(ret)传递结果;
                            Fast.api.close(ret);
                        }
                    };
                    Fast.api.open(url,title,options);
                });



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

    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.teacher_id;
    }

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

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

    return Controller;
});
