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

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'type_id', title: __('分类id'),operate:false},
                        {field: 'examname', title: __('Examname'),operate:'like'},
                        {field: 'workname', title: __('学业名称'),operate:'like'},
                        {field: 'class_hour', title: __('Class_hour'),operate:false},
                        {field: 'image', title: __('Image'),operate:false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'yinimage', title: __('印章图片'),operate:false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'pass_mark', title: __('及格分数'),operate:false},
                        {field: 'dan_num', title: __('单选题数量'),operate:false},
                        {field: 'dan_score', title: __('单选题分值'),operate:false},
                        {field: 'duo_num', title: __('多选题数量'),operate:false},
                        {field: 'duo_score', title: __('多选题分值'),operate:false},
                        {field: 'pan_num', title: __('判断题数量'),operate:false},
                        {field: 'pan_score', title: __('判断题分值'),operate:false},
                        {field: 'examdurationtime', title: __('考试时长/分钟'),operate:false},
                        {field: 'expirationtime', title: __('Expirationtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'pass_list',
                                    text: __('通过列表'),
                                    title: __('通过列表'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: '',
                                    url: $.fn.bootstrapTable.defaults.extend.pass_list_url+'?study_id={id}&type=1',
                                    extend: "data-area='[\"90%\",\"95%\"]'",
                                },
                                {
                                    name: 'pass_list',
                                    text: __('未通过列表'),
                                    title: __('未通过列表'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: '',
                                    url: $.fn.bootstrapTable.defaults.extend.pass_list_url+'?study_id={id}&type=2',
                                    extend: "data-area='[\"90%\",\"95%\"]'",
                                },
                                {
                                    name: 'class_list',
                                    text: __('培训课程列表'),
                                    title: __('培训课程列表'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: $.fn.bootstrapTable.defaults.extend.class_list_url+'?study_id={id}',
                                    extend: "data-area='[\"90%\",\"95%\"]'",
                                },
                            ], formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        pass_list: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'study/pass_list' + location.search,
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'updatetime',
                exportTypes: ['excel'],
                exportOptions: {
                    // type: 'excel',
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    }
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'user.id', title: __('Id'), sortable: true},
                        // {field: 'group.name', title: __('Group')},
                        {field: 'user.username', title: __('用户名'), operate:'LIKE'},
                        {field: 'user.nickname', title: __('真实姓名'), operate:'LIKE'},
                        {field: 'study.id', title: __('报考科目'), searchList: Config.studyList,formatter: Table.api.formatter.flag},
                        {field: 'score', title: __('成绩'), operate:false},
                        {field: 'is_pass', title: __('是否通过'), searchList: {"1":"通过","2":'未通过'},formatter: Table.api.formatter.flag},
                        {field: 'user.password', title: __('密码'), operate:false},
                        {field: 'user.card', title: __('身份证'), operate:false},
                        {field: 'user.phone', title: __('手机号'), operate:false},
                        {field: 'user.sex', title: __('性别'), searchList: {"1":"男","2":'女'},formatter: Table.api.formatter.flag},
                        {field: 'user.work_address', title: __('工作地点'), operate:'LIKE'},
                        {field: 'user.image', title: __('头像'), operate:false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'user.studynum', title: __('学号'), operate:false},
                        {field: 'user.expirationtime', title: __('有效期'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('考试时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'user.status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});