<?php 
/* @var $this \yii\web\View */
use yii\helpers\Url;

?>
<style>
    #symbolCount .legend > div , #symbolCount .legend > table{
        top: auto !important;
        bottom: -70px !important;
    }
</style>
<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-12">
        <h2>账户管理</h2>
        <ol class="breadcrumb">
            <li>
                <a href="/admin/dashboard">管理首页</a>
            </li>
            <li class="active">
                <strong>MT4账户管理</strong>
            </li>
        </ol>
    </div>
</div>

<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-lg-6">
            <div class="ibox float-e-margins">
                <div class="ibox-title clear">
                    <div class="btn btn-link">账户<?= $user['LOGIN'] ?>基本信息</div>
                    <div class="pull-right">
                        <div class="btn-group">
                            <?php if (empty($ib)): ?>
                                <!--<button id="setIb" class="btn btn-primary"><i class="fa fa-cogs"></i> 设置所属代理</button>-->
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
                <div class="ibox-content">
                    <?php if ($ib): ?>
                        <div class="text-center">
                            <!--<div class="btn btn-group">
                                <button id="setHadComm" class="btn btn-default"><i class="fa fa-cogs"></i> 已返佣设置</button>
                                <?php if ($isCommissionRun == 0): ?>
                                    <a class="btn btn-primary" href="<?= Url::to(['commission-run', 'login' => $user['LOGIN'], 'run' => 1]) ?>"><i class="fa fa-unlock"></i>
                                        开启自动返佣</a>
                                <?php else: ?>
                                    <a class="btn btn-danger" href="<?= Url::to(['commission-run', 'login' => $user['LOGIN'], 'run' => 0]) ?>"><i class="fa fa-lock"></i>
                                        关闭自动返佣</a>
                                <?php endif; ?>
                            </div>-->
                        </div>
                        <div class="hr-line-dashed"></div>
                    <?php endif; ?>
                    <div class="table-responsive">
                        <table class="table table-bordered">
                            <?php if ($ib): ?>
                               <!-- <tr>
                                    <th>自动返佣状态</th>
                                    <td colspan="3">
                                        <?php if ($isCommissionRun == 0) : ?>
                                            <label class="label label-danger">已关闭</label>
                                        <?php elseif ($isCommissionRun == 1) : ?>
                                            <label class="label label-primary">已启动</label>
                                        <?php endif; ?>
                                    </td>
                                </tr>-->
                            <?php endif; ?>
                            <tr>
                                <th>账户</th>
                                <td><?= $user['LOGIN'] ?></td>
                                <th>用户组</th>
                                <td><?= $user['GROUP'] ?></td>
                            </tr>
                            <tr>
                                <th>杠杆</th>
                                <td>1:<?= $user['LEVERAGE'] ?></td>
                                <th>余额</th>
                                <td><?= $user['BALANCE'] ?></td>
                            </tr>
                            <tr>
                                <th>保证金</th>
                                <td><?= $user['MARGIN'] ?></td>
                                <th>可用保证金</th>
                                <td><?= $user['MARGIN_FREE'] ?></td>
                            </tr>
                            <tr>
                                <th>保证金比例</th>
                                <td><?= $user['MARGIN_LEVEL'] ?></td>
                                <th>净值</th>
                                <td><?= $user['EQUITY'] ?></td>
                            </tr>
                            <tr>
                                <th>总交易次数</th>
                                <td><?= $historyTotalCount ?></td>
                                <th>盈利交易</th>
                                <td><?php if ($historyTotalCount != 0): ?><?= $historyWinCount ?> (<?= $historyWinPercent ?>%)<?php else: ?>0<?php endif; ?></td>
                            </tr>
                            <tr>
                                <th>亏损交易</th>
                                <td><?php if ($historyTotalCount != 0): ?><?= $historyLossCount ?> (<?= $historyLossPercent ?>%)<?php else: ?>0<?php endif; ?></td>
                                <th>最后更新时间</th>
                                <td><?= $user['MODIFY_TIME'] ?></td>
                            </tr>
                        </table>
                        <?php if ($ib): ?>
                            <!--<div class="hr-line-dashed"></div>
                            <div class="text-center">
                                <button id="delIb" class="btn btn-danger"><i class="fa fa-trash-o"></i> 取消上级所属</button>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <table class="table table-bordered">
                                <tr>
                                    <th width="120">代理姓名</th>
                                    <td><?= $ib['name'] ?></td>
                                    <th>代理邮箱</th>
                                    <td><?= $ib['username'] ?></td>
                                </tr>
                                <tr>
                                    <th>代理手机</th>
                                    <td><?= $ib['mobile'] ?></td>
                                    <th>代理返佣账户</th>
                                    <td><?= $ib['logins'] ?></td>
                                </tr>
                            </table>-->
                        <?php endif; ?>
                    </div>
                    <?php if ($ib): ?>
                       <!-- <div class="hr-line-dashed"></div>
                        <div class="text-center">
                           <button id="setComm" class="btn btn-primary"><i class="fa fa-cogs"></i> 设置返佣规则</button>
                        </div>
                        <div class="hr-line-dashed"></div>-->
                        <?php if ($parents): ?>
                            <!--<table class="table table-bordered">
                                <thead>
                                <tr>
                                    <th>代理商</th>
                                    <th>FOREX</th>
                                    <th>METAL</th>
                                    <th>CFD</th>
                                    <th>黄金</th>
                                    <th>白银</th>
                                    <th>外佣</th>
                                    <th>股指</th>
                                    <th>BTC</th>
                                </tr>
                                </thead>
                                <tbody>
                                <?php $index = count($parents); ?>
                                <?php foreach ((array)$parents as $parent): ?>
                                    <?php if (!empty($parent['comm'])): ?>
                                        <tr>
                                            <td><?= $index-- ?>级-<?= $parent['name'] ?></td>
                                            <td><?= $parent['comm']['forex'] ?>/手</td>
                                            <td><?= $parent['comm']['metal'] ?>/手</td>
                                            <td><?= $parent['comm']['cfd'] ?>/手</td>
                                            <td><?= $parent['comm']['gold'] ?>/手</td>
                                            <td><?= $parent['comm']['silver'] ?>/手</td>
                                            <td><?= $parent['comm']['wy'] ?>/手</td>
                                            <td><?= $parent['comm']['stock'] ?>/手</td>
                                            <td><?= $parent['comm']['btc'] ?>/手</td>
                                        </tr>
                                    <?php else: ?>
                                        <tr>
                                            <td colspan="9">暂未设置</td>
                                        </tr>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                                </tbody>
                            </table>-->
                        <?php endif; ?>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <div class="col-lg-6">
            <div class="ibox float-e-margins">
                <div class="ibox-title clear">
                    <div class="btn btn-link">统计分析</div>
                </div>
                <div class="ibox-content">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="ibox-title">
                                <h5>总收益</h5>
                            </div>
                            <div class="ibox-content">
                                <div class="flot-chart">
                                    <div class="flot-chart-content" id="sumProfitByDay"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <hr>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="ibox-title">
                                <h5>交易手数</h5>
                            </div>
                            <div class="ibox-content">
                                <div class="flot-chart">
                                    <div class="flot-chart-content" id="volumeSumByDay"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <hr>
                    <div class="row">
                        <div class="col-md-4">
                            <div class="ibox-title">
                                <h5>总笔数</h5>
                                <span class="pull-right label label-warning"><?= $historyTotalCount ?></span>
                            </div>
                            <div class="ibox-content">
                                <div class="flot-chart">
                                    <div class="flot-chart-pie-content" id="totalCount"></div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="ibox-title">
                                <h5>交易货币分布</h5>
                            </div>
                            <div class="ibox-content">
                                <div class="flot-chart">
                                    <div class="flot-chart-pie-content" id="symbolCount" style="height: 200px;"></div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="ibox-title">
                                <h5>交易来源分布</h5>
                            </div>
                            <div class="ibox-content">
                                <div class="flot-chart">
                                    <div class="flot-chart-pie-content" id="reasonCount"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $this->beginBlock('footer_script'); ?>
<script src="<?= STATIC_URL ?>/ui/js/plugins/flot/jquery.flot.js"></script>
<script src="<?= STATIC_URL ?>/ui/js/plugins/flot/jquery.flot.tooltip.min.js"></script>
<script src="<?= STATIC_URL ?>/ui/js/plugins/flot/jquery.flot.resize.js"></script>
<script src="<?= STATIC_URL ?>/ui/js/plugins/flot/jquery.flot.pie.js"></script>
<script src="<?= STATIC_URL ?>/ui/js/plugins/flot/jquery.flot.time.js"></script>
<script>
    $(document).ready(function () {
        var sumProfitByDay = <?= $sumProfitByDay ?>;
        $.plot($("#sumProfitByDay"), [{
            data: sumProfitByDay
        }], {
            xaxes: [{
                mode: 'time',
                timeformat: "%y/%m/%d"
            }],
            colors: ["#1ab394"],
            grid: {
                color: "#999999",
                hoverable: true,
                clickable: true,
                tickColor: "#D4D4D4",
                borderWidth: 0,
                hoverable: true //IMPORTANT! this is needed for tooltip to work,
            },
            tooltip: true,
            tooltipOpts: {
                content: "截止 %x 的总收益 %y ",
                xDateFormat: "%y/%m/%d",
                onHover: function (flotItem, $tooltipEl) {
                    // console.log(flotItem, $tooltipEl);
                }
            }
        });
        var volumeSumByDay = <?= $volumeSumByDay ?>;
        $.plot($("#volumeSumByDay"), [{
            data: volumeSumByDay
        }], {
            xaxes: [{
                mode: 'time',
                timeformat: "%y/%m/%d"
            }],
            yaxis: {
                tickDecimals: 2
            },
            colors: ["#1ab394"],
            grid: {
                color: "#999999",
                hoverable: true,
                clickable: true,
                tickColor: "#D4D4D4",
                borderWidth: 0,
                hoverable: true //IMPORTANT! this is needed for tooltip to work,
            },
            tooltip: true,
            tooltipOpts: {
                content: "截止 %x 的总交易手数 %y 手",
                xDateFormat: "%y/%m/%d",
                onHover: function (flotItem, $tooltipEl) {
                    // console.log(flotItem, $tooltipEl);
                }
            }
        });
        var data1 = <?= $symbolCount ?>;
        if (!$.isEmptyObject(data1)) {
            var plotObj1 = $.plot($("#symbolCount"), data1, {
                series: {
                    pie: {
                        show: true
                    }
                },
                grid: {
                    hoverable: true
                },
                tooltip: true,
                tooltipOpts: {
                    content: "%p.0%, %s", // show percentages, rounding to 2 decimal places
                    shifts: {
                        x: 20,
                        y: 0
                    },
                    defaultTheme: false
                }
            });
        }
        var data2 = <?= $reasonCount ?>;
        if (!$.isEmptyObject(data2)) {
            var plotObj2 = $.plot($("#reasonCount"), data2, {
                series: {
                    pie: {
                        show: true
                    }
                },
                grid: {
                    hoverable: true
                },
                tooltip: true,
                tooltipOpts: {
                    content: "%p.0%, %s", // show percentages, rounding to 2 decimal places
                    shifts: {
                        x: 20,
                        y: 0
                    },
                    defaultTheme: false
                }
            });
        }
        var data3 = [
            {
                label: "Win",
                data: <?= $historyWinCount ?>,
            },
            {
                label: "Loss",
                data: <?= $historyLossCount ?>,
            }
        ];
        if (!$.isEmptyObject(data3)) {
            var plotObj3 = $.plot($("#totalCount"), data3, {
                series: {
                    pie: {
                        show: true
                    }
                },
                grid: {
                    hoverable: true
                },
                tooltip: true,
                tooltipOpts: {
                    content: "%p.0%, %s", // show percentages, rounding to 2 decimal places
                    shifts: {
                        x: 20,
                        y: 0
                    },
                    defaultTheme: false
                }
            });
        }
    });
    $('#setIb').click(function () {
        var url = "<?= Url::to(['ibs', 'login' => $user['LOGIN']]) ?>&t=" + new Date().getTime();
        $("#modal").modal({
            remote: url,
            show: true
        });
    });
    $('#delIb').click(function () {
        $.ajax({
            type: "POST",
            url: "<?= Url::to(['del-ib', 'login' => $user['LOGIN']]) ?>",
            dataType: "json",
            beforeSend: function () {
            },
            success: function (data) {
                if (data.isSuccess) {
                    location.reload();
                } else {
                    toastr.error(data.msg, "提示");
                }
            },
            complete: function (XMLHttpRequest, textStatus) {
            },
            error: function () {
            }
        });
    });
    $('#setComm').click(function () {
        var url = "<?= Url::to(['commission', 'login' => $user['LOGIN']]) ?>&" + new Date().getTime();
        $("#modal").modal({
            remote: url,
            show: true
        });
    });
    $('#setHadComm').click(function () {
        swal({
            title: "您确定吗?",
            text: "您确定该账户当前所有的历史交易记录都设置为已返佣状态？",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            closeOnConfirm: true
        }, function (isConfirm) {
            if (isConfirm) {
                $.ajax({
                    type: "POST",
                    url: "<?= Url::to(['set-had-comm']) ?>",
                    data: {
                        login:<?= $user['LOGIN'] ?>
                    },
                    dataType: "json",
                    beforeSend: function () {
                    },
                    success: function (data) {
                        if (data.isSuccess) {
                            toastr.success(data.msg, "提示");
                        } else {
                            toastr.error(data.msg, "提示");
                        }
                    },
                    complete: function (XMLHttpRequest, textStatus) {
                    },
                    error: function () {
                    }
                });
            }
        });
    });
</script>
<?php $this->endBlock(); ?>
