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

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

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'user_id', title: __('User_id')},
                        {field: 'user_id', title: __('User_id'), operate: false,formatter: function (value,row,index) {
                            if(row){
                                console.log(row);
                                var html="id："+row.user_id;
                                html  = row.agent.realname?html+"<br/> 姓名：" + row.agent.realname:html;
                                html  = row.agent.mobile?html+"<br/> 电话：" + row.agent.mobile:html;
                                return html;
                            } else {
                                return "id：" + row.user_id;
                            }

                        }},
                        // {field: 'contract_id', title: __('Contract_id')},
                        {field: 'order_id', title: __('订单记录'), operate: false,formatter: function (value,row,index) {
                            if(row){
                                var html="id："+row.order_id;
                                html  = row.order.total_price?html+"<br/> 订单总金额：" + row.order.total_price:html;
                                html  = row.order.num?html+"<br/> 认购设备数量：" + row.order.num:html;
                                return html;
                            } else {
                                return "id：" + row.order_id;
                            }
                        }},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), 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);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
