<?php

if ((int)$admin_info->role !== 1) {
    echo '<script> location.replace("dashboard"); </script>';
    exit();
}
$users_function = new \Functions\Users();
$trading_transactions_function = new \Functions\Trading_Transactions();
$persian_date = new \Classes\PersianDate();
$banks_function = new \Functions\Banks();

$transactions = [];
$transactions_count = 0;
$page = 1;
$limit = 100;
$avg_count_cal = [];
$searched = false;
if (isset($_GET['page'])) {
    $page = $_GET['page'];
}
if (isset($_GET['submit'])) {
    extract($query);

    if ($search_date !== "") {
        $date_explode = explode("/", $search_date);
        $search_date = $persian_date->jalali_to_gregorian($date_explode[0], $date_explode[1], $date_explode[2]);
        $search_date = implode("-", $search_date) . " " . $search_time;
        $search_date = strtotime($search_date);
        $transactions = $trading_transactions_function->search(
            ((isset($search_orderi) && (int)$search_orderi < 2) ? $search_orderi : null),
            ((isset($search_userid) && $search_userid !== "") ? $search_userid : null),
            NULL,
            NULL,
            ((isset($search_date) && $search_date !== "") ? $search_date : null),
            NULL,
            NULL,
            "id DESC",
            ($page - 1) * $limit,
            $limit
        );
        $transactions_count = $trading_transactions_function->search_row_count(
            ((isset($search_orderi) && (int)$search_orderi < 2) ? $search_orderi : null),
            ((isset($search_userid) && $search_userid !== "") ? $search_userid : null),
            NULL,
            NULL,
            ((isset($search_date) && $search_date !== "") ? $search_date : null),
            NULL,
            NULL
        );
        $avg_count_cal = $trading_transactions_function->calculator_avg_count_orders(
            ((isset($search_orderi) && (int)$search_orderi < 2) ? $search_orderi : null),
            ((isset($search_userid) && $search_userid !== "") ? $search_userid : null),
            NULL,
            NULL,
            ((isset($search_date) && $search_date !== "") ? $search_date : null),
            NULL,
            NULL
        );
        $searched = true;
    }

} else {
    $transactions = [];
}
$pages_count = round($transactions_count / $limit, 0, PHP_ROUND_HALF_UP);
if ($pages_count < 1) {
    $pages_count = 1;
}
?>
<div class="container-fluid">
    <div class="row">
        <div class="col-lg-12">
            <div class="page-title-box">
                <div class="align-items-center mx-1 mb-5 rounded rounded-5">
                    <div class="card m-b-30 w-100">

                        <div class="card-header">
                            <div class="card-title">
                                <h5>لیست معاملات </h5>
                            </div>
                            <hr>
                            <form autocomplete="off">
                                <div class="row">
                                    <div class=" col-md-6 mb-2">
                                        <label for="search_userid">کاربر</label>
                                        <select id="search_userid" name="search_userid"
                                                class="form-control js-select2">
                                            <option value="">یک کاربر را انتخاب کنید</option>
                                            <?php
                                            $users = $users_function->get_users(["use_bot", "open"]);

                                            foreach ($users as $user) {
                                                echo "
                                            <option value=\"{$user->userid}\" " . ((isset($_GET['search_userid']) && $_GET['search_userid'] == $user->userid) ? "selected" : "") . ">{$user->id} - {$user->name} - {$user->nick_name} -
                                                {$user->userid}
                                            </option>
                                            ";
                                            } ?>
                                        </select>
                                    </div>

                                    <div class=" col-md-6 mb-2">
                                        <label for="search_orderi">نوع تسویه</label>
                                        <select id="search_orderi" name="search_orderi" class="form-control">
                                            <option value="2" <?php echo((isset($_GET['search_orderi']) && $_GET['search_orderi'] == 2) ? "selected" : ""); ?>>
                                                همه
                                            </option>
                                            <option value="0" <?php echo((isset($_GET['search_orderi']) && $_GET['search_orderi'] == 0) ? "selected" : ""); ?>>
                                                عادی
                                            </option>
                                            <option value="1" <?php echo((isset($_GET['search_orderi']) && $_GET['search_orderi'] == 1) ? "selected" : ""); ?>>
                                                اردری
                                            </option>
                                        </select>
                                    </div>


                                </div>
                                <div class="row">
                                    <div class="col-md-5 mb-2">
                                        <label for="search_date">تاریخ</label>
                                        <input type="text" class="form-control" id="search_date"
                                               name="search_date" autocomplete="off"
                                               data-jdp
                                               value="<?php echo((isset($_GET['search_date'])) ? $_GET['search_date'] : ""); ?>">
                                    </div>

                                    <div class=" col-md-5 mb-2">
                                        <label for="search_time">زمان تسویه</label>
                                        <select id="search_time" name="search_time" class="form-control">
                                            <option value="00:29:00" <?php echo((isset($_GET['search_time']) && $_GET['search_time'] == "00:29:00") ? "selected" : ""); ?>>
                                                00:29
                                            </option>
                                            <option value="01:30:00" <?php echo((isset($_GET['search_time']) && $_GET['search_time'] == "01:30:00") ? "selected" : ""); ?>>
                                                01:30
                                            </option>
                                            <option value="21:01:00" <?php echo((isset($_GET['search_time']) && $_GET['search_time'] == "21:01:00") ? "selected" : ""); ?>>
                                                21:01
                                            </option>
                                        </select>
                                    </div>

                                    <div class="col-md-2">
                                        <label></label>
                                        <button type="submit" class="btn btn-primary w-100 mt-2">
                                            جستجو<i class="mdi mdi-database-search"></i>
                                        </button>
                                    </div>
                                    <input type="hidden" name="submit">

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

                        <div class="card-body">
                            <div id="loading" class="loading">
                                <div class="spinner-grow text-primary" style="margin-top: auto;margin-bottom: auto"
                                     role="status">
                                    <span class="sr-only">بارگذاری...</span>
                                </div>
                                <div class="spinner-grow text-primary" style="margin-top: auto;margin-bottom: auto"
                                     role="status">
                                    <span class="sr-only">بارگذاری...</span>
                                </div>
                                <div class="spinner-grow text-primary" style="margin-top: auto;margin-bottom: auto"
                                     role="status">
                                    <span class="sr-only">بارگذاری...</span>
                                </div>
                            </div>
                            <div class="table-responsive">
                                <table id="datatable"
                                       class="mt-2 table table-bordered"
                                       style="border-collapse: collapse; border-spacing: 0; width: 100%;">
                                    <thead>
                                    <tr>
                                        <th class="text-center">شناسه</th>
                                        <th class="text-center">طرف مقابل</th>
                                        <th class="text-center">نوع</th>
                                        <th class="text-center">نوع سند</th>
                                        <th class="text-center">زمان ثبت</th>
                                        <th class="text-center">حجم</th>
                                        <th class="text-center">ضریب شدت</th>
                                        <th class="text-center">مظنه</th>
                                        <th class="text-center">زمان تسویه</th>
                                        <th class="text-center">نوع تسویه</th>
                                        <th class="text-center">تسویه شده</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php
                                    if ($searched) {
                                        foreach ($transactions as $transaction) {
                                            ?>
                                            <tr class="text-center" id="trading_<?php echo $transaction->id; ?>">
                                                <td class="text-center">
                                                    <?php echo $transaction->id; ?>
                                                </td>
                                                <td id="oposit_<?php echo $transaction->id; ?>">
                                                    <?php echo $users_function->get_by_userid((($transaction->buyer == $search_userid) ? $transaction->seller : $transaction->buyer))[0]->nick_name ?>
                                                </td>
                                                <td id="type_<?php echo $transaction->id; ?>" class="<?php echo(($transaction->buyer == $search_userid) ? "text-success" : "text-danger") ?>">
                                                    <?php echo(($transaction->buyer == $search_userid) ? "خرید" : "فروش") ?>
                                                </td>

                                                <td class="text-center" id="method_<?php echo $transaction->id; ?>">
                                                    <?php echo(($transaction->method == 0) ? "عادی" : (($transaction->method == 1) ? "شنا" : "معکوس")); ?>
                                                </td>
                                                <td class="text-center"
                                                    id="createdtime_<?php echo $transaction->id; ?>">
                                                    <?php echo $persian_date->jdate("H:i:s Y/m/d", strtotime($transaction->createdtime)); ?>
                                                </td>
                                                <td class="text-center" id="count_<?php echo $transaction->id; ?>">
                                                    <?php echo number_format($transaction->count, 0); ?>
                                                </td>
                                                <td class="text-center" id="leverage_<?php echo $transaction->id; ?>">
                                                    <?php echo $transaction->leverage; ?>
                                                </td>
                                                <td class="text-center" id="price_<?php echo $transaction->id; ?>">
                                                    <?php echo number_format($transaction->price, 2, "/"); ?>
                                                </td>
                                                <td class="text-center"
                                                    id="settlement_time_<?php echo $transaction->id; ?>">
                                                    <?php echo $persian_date->jdate("H:i Y/m/d", $transaction->settling_timestamp); ?>
                                                </td>
                                                <td class="text-center"
                                                    id="settlement_type_<?php echo $transaction->id; ?>">
                                                    <?php echo(($transaction->settling_order == 0) ? "عادی" : "اردری🎯"); ?>
                                                </td>
                                                <td class="text-center"
                                                    id="settlement_type_<?php echo $transaction->id; ?>">
                                                    <?php echo(($transaction->settling_info == '[]') ? "❌" : "✅"); ?>
                                                </td>
                                            </tr>
                                            <?php
                                        }
                                    }
                                    ?>
                                    </tbody>
                                </table>
                            </div>
                            <div class="col-12 text-center">
                                <?php
                                $parms = "trader_trading_transactions?";
                                foreach ($_GET as $parm => $value) {
                                    if ($parm == "page" || $parm == "uri") {
                                        continue;
                                    }
                                    $parms .= "$parm=$value&";
                                }
                                ?>
                                <ul class="pagination justify-content-center">
                                    <li class="page-item no-print <?php echo(($page == 1) ? "disabled" : "") ?>">
                                        <a class="page-link" <?php echo(($page == 1) ? "" : "href=\"$parms" . "page=" . ($page - 1) . "\"") ?>
                                           tabindex="-1">قبلی</a>
                                    </li>
                                    <?php
                                    for ($x = 1; $x <= $pages_count; $x++) {
                                        echo '<li class="page-item' . (($page == $x) ? " active" : "") . '"><a class="page-link" href="' . $parms . 'page=' . $x . '">' . $x . '</a></li>';
                                    }


                                    ?>
                                    <li class="page-item  no-print <?php echo(($page == $pages_count) ? "disabled" : "") ?>">
                                        <a class="page-link" <?php echo(($page == $pages_count) ? "" : "href=\"$parms" . "page=" . ($page + 1) . "\"") ?>>بعدی</a>
                                    </li>
                                </ul>
                            </div>
                            <?php
                            if ($searched) {

                                ?>
                                <div class="row align-items-center mx-auto text-center">
                                    <div class="col-3">
                                    <span class="font-10">
                                    تعداد فروش
                                    </span>
                                    </div>
                                    <div class="col-3">
                                    <span class="font-10">
                                    <?php echo (int)$avg_count_cal["sell"][0]->c2; ?>&nbsp;تا
                                    </span>
                                    </div>
                                    <div class="col-3">
                                    <span class="font-10">
                                    میانگین فروش
                                    </span>
                                    </div>
                                    <div class="col-3">
                                    <span class="font-10">
                                    <?php echo number_format($avg_count_cal["sell"][0]->p, 2, "/", ""); ?>&nbsp;$
                                    </span>
                                    </div>
                                </div>
                                <div class="row align-items-center mx-auto text-center">
                                    <div class="col-3">
                                    <span class="font-10">
                                    تعداد خرید
                                    </span>
                                    </div>
                                    <div class="col-3">
                                    <span class="font-10">
                                    <?php echo (int)$avg_count_cal["buys"][0]->c2; ?>&nbsp;تا
                                    </span>
                                    </div>
                                    <div class="col-3">
                                    <span class="font-10">
                                    میانگین خرید
                                    </span>
                                    </div>
                                    <div class="col-3">
                                    <span class="font-10">
                                    <?php echo number_format($avg_count_cal["buys"][0]->p, 2, "/", ""); ?>&nbsp;$
                                    </span>
                                    </div>
                                </div>
                            <?php } ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="modal" class="modal mt-md-5" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h6 class="modal-title"></h6>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">

            </div>
        </div>
    </div>
</div>
