define(['table', 'upload','form'], function (Table,Upload,Form) {
    var recharge = {
        //Do setup work hereAction
        index: function () {
            window.networkOption=Config.networkList;
            window.statuskOption=Config.statusList;
            
            Table.api.init({
                extend: {
                    index_url: 'recharge/select',
                    add_url: 'recharge/insert',
                    edit_url: 'recharge/update',
                    del_url: 'recharge/delete',
                    multi_url: 'recharge/multi',
                    //dragsort_url: 'recharge/weigh',
                    table: 'recharge',
                }
            });

            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: false,
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {
                            field: 'id',
                            title: 'ID',
                            filter: "number",
                            sortable: true,    // 是否排序
                            visible:false,
                        },
                        {
                            field: 'user.username',
                            title: '用户',
                            filter: "string",
                        },
                        {
                            title: "金额",
                            field: "amount",
                            filter: "number",
                            formatter:Table.api.formatter.number,
                            sortable: true    // 是否排序
                        },
                        {
                            title: "网络",
                            field: "network",
                            sortable: true,
                            filter: "select",
                            filterOption:"networkOption"
                        },
                        {
                            title: "地址",
                            field: "address",
                            formatter: Table.api.formatter.address
                        },
                        {
                            title: "入账地址",
                            field: "from",
                            filter: "string",
                            visible:false,
                            formatter: Table.api.formatter.address
                        },
                        {
                            title: "转账金额",
                            field: "real_amount",
                            sortable: true,
                            filter: "string",
                            formatter:Table.api.formatter.number,
                            visible:false
                        },
                        {
                            title: "txid",
                            field: "txid",
                            filter: "string",
                            visible:false,
                            formatter: Table.api.formatter.txid
                        },
                        {
                            title: "转账时间",
                            field: "pay_time",
                            sortable: true,
                            formatter:Table.api.formatter.datetime,
                            visible:false
                        },
                        {
                            title: "result",
                            field: "result",
                            filter: "string",
                            visible:false
                        },
                        {
                            title: "reason",
                            field: "reason",
                            filter: "string",
                            visible:false
                        },
                        {
                            title: "状态",
                            field: "status",
                            sortable: true,
                            formatter:Table.api.formatter.status,
                            searchList:window.statuskOption,
                            operate:false,
                            // formatter: function (v,d) {
                            //     for (let i = 0; i < window.statuskOption.length; i++) {
                            //         if(v===window.statuskOption[i].value){
                            //             return '<span class="status-'+v+'">'+window.statuskOption[i].label+'</span>';
                            //         }
                            //     }
                            // },
                            filter: "select",
                            filterOption:"statuskOption"
                        },
                        {
                            title: "memo",
                            field: "memo",
                            visible: false,
                        },
                        {
                            title: "创建时间",
                            field: "created_at",
                            sortable: true,
                        },
                        {
                            title: "更新时间",
                            field: "updated_at",
                            sortable: true,
                            visible: false,
                        },
                        {
                            field: 'operate', title: '操作', 
                            table: table, events: Table.api.events.operate, 
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        update:function(){
            this.bindevent();
        },
        insert:function(){
            this.bindevent();
        },
        setForm:function(obj){
            console.log(obj);
            if(obj.from){
                $('[name=from]').val(obj.from);
            }
            if(obj.transfer_at){
                $('[name=pay_time]').val(Table.api.formatter.datetime(obj.transfer_at));
            }
            if(obj.real_amount){
                $('[name=real_amount]').val(obj.real_amount);
            }
            $('[name=result]').val(obj.result);
        },
        bindevent:function(){
            var form = $('form');
            Form.api.bindevent(form);
            $('#gettxid').on('click',function(){
                var btn_loading = $(this).find('i');
                var v = $('[name=txid]').val();
                if(v){
                    btn_loading.show();
                    if(v.substr(0,2) == '0x'){
                        $.getJSON('index/txid?txid='+v,function(res){
                            btn_loading.hide();
                            recharge.setForm({
                                from:res.data.from,
                                transfer_at:res.data.timestamp,
                                real_amount:res.data.real_amount,
                                result:res.data.status == '0x1' ? 'SUCCESS' : 'FAIL'
                            });
                        });
                    }else{
                        $.getJSON('https://apilist.tronscan.org/api/transaction-info?hash='+v,function(res){
                            btn_loading.hide();
                            if(res.contractRet){
                                recharge.setForm({
                                    from:res.tokenTransferInfo ? res.tokenTransferInfo.from_address : '',
                                    transfer_at:parseInt(res.timestamp/1000),
                                    real_amount:res.tokenTransferInfo ? res.tokenTransferInfo.amount_str/1000000 : 0,
                                    result:res.contractRet
                                });
                            }else{
                                recharge.setForm({
                                    from:null,
                                    transfer_at:null,
                                    real_amount:null,
                                    result:"FAIL"
                                });
                            }
                        });
                    }
                }
            });
        }
    };
    return recharge
});