course_store.js 8.3 KB
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: true,
                showExport: false,
                commonSearch: false,
                searchFormVisible: false,
                clickToSelect: false,
                dblClickToEdit: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'course.name', title: __('Course_id')},
                        {field: 'store.name', title: __('Store_id')},
                        {field: 'date', title: __('Date'), operate: 'RANGE', addclass: 'datetimerange'},
                        {field: 'time_start', title: __('Time_start')},
                        {field: 'count', title: __('Count')},
                        {field: 'notify_count', title: __('Notify_count')},
                        {field: 'sign_count', title: __('Sign_count')},
                        {field: 'time_end', title: __('Time_end')},
                        {field: 'status', title: __('Status'), formatter: function (value) {
                                var color = 'red';
                                if (value === 'new') {
                                    color = 'green';
                                }
                                if (value === 'cancel') {
                                    color = 'black';
                                }
                                return '<span style="color:' + color + '">' + __(value) + '</span>';
                            }},
                        {
                            field : 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name     : 'publish',
                                    text     : '发布',
                                    title    : __('Publish'),
                                    icon     : 'fa fa-check',
                                    classname: 'btn btn-xs btn-primary btn-publish',
                                }, {
                                    name     : 'detail',
                                    text     : __('Detail'),
                                    icon     : 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                }
                            ],
                            events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that               = $.extend({}, this);
                                that.buttons[0].extend = 'data-publish-id="' + row.id + '" data-publish-status="' + row.status + '"';
                                that.buttons[1].url    = 'course_sign/index?ids=' + row.id;
                                var table              = $(that.table).clone(true);
                                if (row.status === 'confirmed') {
                                    $(table).data("operate-edit", null);
                                    $(table).data("operate-del", null);
                                    that.buttons[0].text = '取消';
                                }
                                if (row.status === 'cancel') {
                                    $(table).data("operate-edit", null);
                                    $(table).data("operate-del", null);
                                    $(table).data("operate-publish", null);
                                }
                                if (row.status === 'new') {
                                    $(table).data("operate-detail", null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

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

            //发布
            $(document).on("click",".btn-publish",function (e) {
                var id = $(this).data('publish-id');
                var status = $(this).data('publish-status');
                e.stopPropagation();
                e.preventDefault(); //事件冒泡阻止
                var that    = this;
                var table   = $(that).closest('table');
                var options = table.bootstrapTable('getOptions');
                if(status === 'confirmed'){
                    Layer.open({title: '取消', content: '是否取消此课程', btn: '确定',yes:function (row) {
                            $.ajax({
                                type    : "POST",
                                url     : "course_store/publish",
                                data    : {
                                    id   : id
                                },
                                dataType: "json",
                                success : function (data) {
                                    // console.log(data);
                                    if (data.code === 1) {
                                        Layer.msg('取消成功');
                                        Layer.closeAll();
                                        $('.btn-refresh').trigger('click')
                                    } else {
                                        Layer.msg('取消失败');
                                        Layer.closeAll()
                                    }

                                }
                            })
                        }})
                }
                if(status === 'new'){
                    Layer.open({title: '发布', content: '是否发布此课程', btn: '确定',yes:function (row) {
                            $.ajax({
                                type    : "POST",
                                url     : "course_store/publish",
                                data    : {
                                    id   : id
                                },
                                dataType: "json",
                                success : function (data) {
                                    // console.log(data);
                                    if (data.code === 1) {
                                        Layer.msg('发布成功');
                                        Layer.closeAll();
                                        $('.btn-refresh').trigger('click')
                                    } else {
                                        Layer.msg('发布失败');
                                        Layer.closeAll()
                                    }

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

            $(document).on("dp.change", "#add-form .datetimepicker", function () {
                $(this).parent().prev().find("input").trigger("change");
            });
            $(document).on("fa.event.appendfieldlist", "#add-form .btn-append", function (e, obj) {
                Form.events.selectpage(obj);
                Form.events.datetimepicker(obj);
            });
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },

        },
    };
    return Controller;
});