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

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'headimage', title: __('Headimage'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'address_city', title: __('Address_city'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'province', title: __('Province')},
                        {field: 'city', title: __('City')},
                        {field: 'district', title: __('District')},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'address_detail', title: __('Address_detail'), operate: 'LIKE'},
                        {field: 'longitude', title: __('Longitude'), operate: 'LIKE'},
                        {field: 'latitude', title: __('Latitude'), 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: 'sign_start_time', title: __('Sign_start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'sign_end_time', title: __('Sign_end_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: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'people_num', title: __('People_num')},
                        {field: 'item', title: __('Item'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {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: '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_demo/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            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_demo/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_demo/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {

                $(document).on("dp.change", "#add-form .datetimerange", function () {
                    $(this).parent().prev().find("input").trigger("change");
                });

                $(document).on("dp.change", "#add-form .datetimepicker", function () {
                    $(this).parent().prev().find("input").trigger("change");
                });
                $(document).on("dp.change", "#edit-form .datetimerange", function () {
                    $(this).parent().prev().find("input").trigger("change");
                });

                $(document).on("dp.change", "#edit-form .datetimepicker", function () {
                    $(this).parent().prev().find("input").trigger("change");
                });

                $(document).on("fa.event.appendfieldlist", "#add-form .btn-append", function (e, obj) {
                    // Form.api.bindevent($("form[role=form]"));
                    // // //绑定动态下拉组件
                    Form.events.selectpage(obj);
                    // // //绑定日期组件
                    Form.events.daterangepicker(obj);
                    Form.events.datetimepicker(obj);
                    // // Form.events.datetimerange(obj);
                    // Form.api.bindevent(this);
                    // //绑定上传组件
                    // Form.events.faupload(obj);

                    // //上传成功回调事件，变更按钮的背景
                    // $(".upload-image", obj).data("upload-success", function (data) {
                    //     $(this).css("background-image", "url('" + Fast.api.cdnurl(data.url) + "')");
                    // })
                });
                $(document).on("fa.event.appendfieldlist", "#edit-form .btn-append", function (e, obj) {
                    // Form.api.bindevent($("form[role=form]"));
                    // // //绑定动态下拉组件
                    Form.events.selectpage(obj);
                    // // //绑定日期组件
                    Form.events.daterangepicker(obj);
                    Form.events.datetimepicker(obj);
                    // // Form.events.datetimerange(obj);
                    // Form.api.bindevent(this);
                    // //绑定上传组件
                    // Form.events.faupload(obj);

                    // //上传成功回调事件，变更按钮的背景
                    // $(".upload-image", obj).data("upload-success", function (data) {
                    //     $(this).css("background-image", "url('" + Fast.api.cdnurl(data.url) + "')");
                    // })
                });

                $("#c-address_city").on("cp:updated", function() {
                    var citypicker = $(this).data("citypicker");
                    var province = citypicker.getCode("province");
                    var city = citypicker.getCode("city");
                    var district = citypicker.getCode("district");
                    if(province){
                        $("#province").val(province);
                    }
                    if(city){
                        $("#city").val(city);
                    }
                    if(district){
                        $("#district").val(district);
                    }
                    $(this).blur();
                });



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