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

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

            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: 'user.nickname', title: __('申请用户')},
                        {field: 'book.title', title: __('申请书籍')},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'email', title: __('Email'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'school', title: __('School'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'use_to', title: __('用途'), searchList: {"0":__('教材用书'),"1":__('学习参考'),"2":__('其他')}, formatter: Table.api.formatter.normal},
                        {field: 'mailing_name', title: __('Mailing_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'mailing_area', title: __('Mailing_area'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'mailing_address', title: __('Mailing_address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'mailing_phone', title: __('Mailing_phone'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'status', title: __('状态'), searchList: {"0":__('未审核'),"1":__('已寄出'),"2":__('已拒绝')}, formatter: Table.api.formatter.status},
                        {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,

                            buttons: [{
                                name: 'click',
                                text:'已寄出',
                                title: __('已寄出'),
                                classname: 'btn btn-xs btn-info btn-click',
                                url: 'books.books_level/edit?ids/{id}',
                                data: {status:0},
                                // icon: 'fa fa-leaf',
                                click: function (data,row) {
                                    var temp=table.bootstrapTable('getSelections');
                                    var id = row.id;
                                    console.log(row);
                                    return $.ajax({
                                        // url:'http://up.kuman.cn/settle/cancel',
                                        url:'books.sample_apply/update_status?ids='+id,
                                        type:'post',
                                        // dataType:'json',
                                        data:{status:1},
                                        success:function(data){
                                            if(data.code === 1){
                                                layer.msg('操作成功');
                                                window.location.reload();
                                            }else{
                                                layer.msg(data.msg);
                                            }
                                        }
                                    });
                                    // Layer.alert("点击按钮执行的事件");
                                },
                                visible: function (row) {
                                    console.log(row)
                                    if (row.status === "0"){
                                        return true;
                                    }
                                    return false;
                                },
                                refresh:true,
                            },
                                {
                                    name: 'click',
                                    text:'拒绝',
                                    title: __('拒绝'),
                                    classname: 'btn btn-xs btn-danger btn-click',
                                    url: 'books.sample_apply/edit?ids/{id}',
                                    data: {status:1},
                                    // icon: 'fa fa-leaf',
                                    click: function (data,row) {
                                        var temp=table.bootstrapTable('getSelections');
                                        var id = row.id;
                                        console.log(row);
                                        return $.ajax({
                                            // url:'http://up.kuman.cn/settle/cancel',
                                            url:'books.sample_apply/update_status?ids='+id,
                                            type:'post',
                                            // dataType:'json',
                                            data:{status:2},
                                            success:function(data){
                                                if(data.code === 1){
                                                    layer.msg('操作成功');
                                                    window.location.reload();
                                                }else{
                                                    layer.alert(data.msg);
                                                }
                                            }
                                        });
                                        // Layer.alert("点击按钮执行的事件");
                                    },
                                    visible: function (row) {
                                        // console.log(row)
                                        if (row.status === "0"){
                                            return true;
                                        }
                                        return false;
                                    },
                                    refresh:true,
                                },
                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                $(table).data("operate-edit", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            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;
});
