<?php
/* @var $this \yii\web\View */

$this->title = '同名转账';
?>
<?php $this->beginBlock('header_script'); ?>
    <link href="<?= STATIC_URL ?>/ui/css/plugins/dataTables/datatables.min.css" rel="stylesheet">
    <style>
        label.error {
            display: none !important;
        }

        #datatables {
            width: 100% !important;
        }
    </style>
<?php $this->endBlock(); ?>
<div class="row">
    <div class="col-md-3">
        <?= $this->render('/common/_left_menu.php') ?>
    </div>
    <div class="col-md-9">
        <div class="tabs-container">
            <ul class="nav nav-tabs">
                <li class="active" style="background:none;"><a data-toggle="tab" href="#tab-1"> <i class="fa fa-retweet"></i> 同名转账</a></li>
                <li style="background:none;"><a data-toggle="tab" href="#tab-2"> <i class="fa fa-th"></i> 申请记录</a></li>
            </ul>
            <div class="tab-content">
                <div id="tab-1" class="tab-pane active">
                    <div class="panel-body">
                        <br><br><br><br>
                        <form id="form">
                            <div class="row">
                                <div class="col-sm-1"></div>
                                <div class="col-sm-4">
                                    <div class="input-group">
                                                <span class="input-group-btn">
                                                    <button type="button" class="btn btn-primary "><i class="fa fa-user"></i> 转出账户</button>
                                                </span>
                                        <select name="flogin" id="flogin" class="form-control ">
                                            <option value="">请选择</option>
                                            <?php foreach ($data['logins'] as $login) { ?>
                                            <option value="<?= $login ?>"><?= $login ?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-sm-2" style="text-align:center;">
                                    <div class="btn btn-default "><i class="fa fa-retweet"></i></div>
                                </div>
                                <div class="col-sm-4">
                                    <div class="input-group">
                                                <span class="input-group-btn">
                                                    <button type="button" class="btn btn-primary "><i class="fa fa-user"></i> &nbsp;转入账户</button>
                                                </span>
                                        <input onkeyup="amountChange();" id="tlogin" type="number" name="tlogin" class="form-control ">
                                    </div>
                                </div>
                                <div class="col-sm-1"></div>
                            </div>
                            <br>
                            <div class="row">
                                <div class="col-sm-1"></div>
                                <div class="col-sm-4">
                                    <div class="input-group">
                                                <span class="input-group-btn">
                                                    <button type="button" class="btn btn-primary "><i class="fa fa-dollar"></i> &nbsp;转移金额</button>
                                                </span>
                                        <input onkeyup="amountChange();" id="amount" type="number" name="amount" class="form-control ">
                                    </div>
                                </div>
                                <div class="col-sm-7"></div>
                            </div>
                            <br>
                            <div class="row">
                                <div class="col-sm-1"></div>
                                <div class="col-sm-10">
                                    <table class="table invoice-total">
                                        <tbody>
                                        <tr>
                                            <td><strong>转出账户 :</strong></td>
                                            <td id="floginTd"></td>
                                        </tr>
                                        <tr>
                                            <td><strong>转入账户 :</strong></td>
                                            <td id="tloginTd"></td>
                                        </tr>
                                        <tr>
                                            <td><strong>转移金额 :</strong></td>
                                            <td id="amountTd"></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                    <div class="text-right">
                                        <div id="submit" class="btn btn-danger"><i class="fa fa-upload"></i> 提交申请</div>
                                    </div>

                                    <div class="well m-t"><strong>友情提示：</strong>
                                        1个工作日处理完成。
                                    </div>
                                </div>
                                <div class="col-sm-1"></div>
                            </div>

                        </form>
                        <br><br>
                    </div>
                </div>

                <div id="tab-2" class="tab-pane">
                    <div class="panel-body">
                        <div class="table-responsive" style="margin:30px 10px 0px 10px;">
                            <table id="datatables" class="table table-striped table-bordered table-hover">
                                <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>状态</th>
                                    <th>转出账户</th>
                                    <th>转入账户</th>
                                    <th>金额</th>
                                    <th>申请时间</th>
                                    <th>备注</th>
                                </tr>
                                </thead>
                            </table>
                        </div>

                    </div>
                </div>

            </div>
        </div>

    </div>

</div>

<?php $this->beginBlock('footer_script'); ?>
<script src="<?= STATIC_URL ?>/ui/js/plugins/dataTables/datatables.min.js"></script>
<script>

    $("#flogin").change(function(){
        var v=$(this).val();
        $("#floginTd").html(v);
    });

    $("#tlogin").change(function(){
        var v=$(this).val();
        var f=$("#flogin").val();
        $("#tloginTd").html(v);
        $.ajax({
            type: "GET",
            url: "<?= \yii\helpers\Url::to(['/user/transfer/is-ib-user']) ?>",
            data: {
                toLogin: v,
                fromLogin: f
            },
            beforeSend: function() {},
            success: function(data) {
                if(data.isSuccess) {
                    $('#submit').removeAttr("disabled");
                    // $('#submit').bind("click");
                } else {
                    $('#submit').attr('disabled', true);
                    toastr.error(data.msg, "提示");
                    // $('#submit').unbind("click");
                }
            },
            complete: function(XMLHttpRequest, textStatus) {},
            error: function() {
                toastr.error(data.msg, "提示");
            }
        });
    });


    function amountChange(){
        var amount=$("#amount").val();
        $("#amountTd").html("$"+amount);
    }

    $("#submit").on("click", function() {
        if($("#form").valid()){
            $.ajax({
                type: "POST",
                url: "<?= \yii\helpers\Url::to(['/user/transfer/transfer-save']) ?>",
                data: $('#form').serialize(),
                dataType: "json",
                beforeSend: function() {},
                success: function(data) {
                    if(data.code == 1) {
                        table.ajax.reload();
                        toastr.success(data.message, "提示");
                    } else {
                        toastr.error(data.message, "提示");
                    }
                },
                complete: function(XMLHttpRequest, textStatus) {},
                error: function() {
                    toastr.error(data.message, "提示");
                }
            });
        }
    });

    var table;
    $(document).ready(function() {

        $("#form").validate({
            rules: {
                "flogin": {
                    required: true
                },
                "tlogin": {
                    required: true
                },
                "amount": {
                    required: true
                }
            }
        });

        table = $('#datatables').DataTable({
            "aLengthMenu": [[20,50,100,500,999999999], [20,50,100,500,"ALL"]],
            language: {
                "sProcessing": "处理中...",
                "sLengthMenu": "显示 _MENU_ 项结果",
                "sZeroRecords": "没有匹配结果",
                "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
                "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                "sInfoPostFix": "",
                "sSearch": "搜索:",
                "sUrl": "",
                "sEmptyTable": "表中数据为空",
                "sLoadingRecords": "载入中...",
                "sInfoThousands": ",",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "上页",
                    "sNext": "下页",
                    "sLast": "末页"
                },
                "oAria": {
                    "sSortAscending": ": 以升序排列此列",
                    "sSortDescending": ": 以降序排列此列"
                }
            },
            "processing": true,
            "serverSide": true,
            "ajax": '<?= \yii\helpers\Url::to(['/user/transfer/transfer-ajax']) ?>',
            "order": [[0, "desc"]],
            "columns": [{
                "data": "id"
            },{
                "data": "type"
            },{
                "data": "from_login"
            },{
                "data": "to_login"
            },{
                "data": "amount"
            },{
                "data": "in_time"
            },{
                "data": "memo"
            }],
            columnDefs: [{
                targets: 1,
                render: function(data, type, row, meta) {
                    if(data==0){
                        return '<span class="label label-danger">处理中</span>';
                    }else if(data==1){
                        return '<span class="label label-danger">不通过</span>';
                    }else if(data==2){
                        return '<span class="label label-primary">已转账</span>';
                    }
                },
                "orderable": true
            },{
                targets: 5,
                render: function(data, type, row, meta) {
                    if(data!=0){
                        return timeStamp2String(parseInt(data));
                    }else{
                        return "";
                    }
                },
                "orderable": true
            }],


            "sPaginationType": "full_numbers",
            dom: '<"html5buttons"B>lTfgitp',
            buttons: [{
                extend: 'copy'
            },
                {
                    extend: 'csv'
                },
                {
                    extend: 'excel'
                },
                {
                    extend: 'pdf'
                },
                {
                    extend: 'print',
                    customize: function(win) {
                        $(win.document.body).addClass('white-bg');
                        $(win.document.body).css('font-size', '10px');
                        $(win.document.body).find('table').addClass('compact').css('font-size', 'inherit');
                    }
                }]

        });

        $('#datatables tbody').on('click', 'tr',
            function() {
                if ($(this).hasClass('selected')) {
                    $(this).removeClass('selected');
                } else {
                    table.$('tr.selected').removeClass('selected');
                    $(this).addClass('selected');
                }
            });

    });

</script>
<?php $this->endBlock(); ?>
