<?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;
if (isset($_GET['page'])) {
    $page = $_GET['page'];
}
if (isset($_GET['submit'])) {
    extract($query);

    $bank = null;
    if (isset($search_bank) && $search_bank !== "") {
        $bank = $search_bank;
    }
    if (isset($search_userid_destination) && $search_userid_destination !== "") {
        $bank = $search_userid_destination;
    }
    if ($search_from_date !== "") {
        $from_date_explode = explode("/", $search_from_date);
        $search_from_date = $persian_date->jalali_to_gregorian($from_date_explode[0], $from_date_explode[1], $from_date_explode[2]);
        $search_from_date = implode("-", $search_from_date) . " 00:00:00";
    }
    if ($search_to_date !== "") {
        $to_date_explode = explode("/", $search_to_date);
        $search_to_date = $persian_date->jalali_to_gregorian($to_date_explode[0], $to_date_explode[1], $to_date_explode[2]);
        $search_to_date = implode("-", $search_to_date) . " 23:59:59";
    }
    $search_price = ((isset($search_price) && $search_price !== "") ? str_replace("/","",str_replace(",","",$search_price)):null);

    $transactions = $trading_transactions_function->search(
        1,
        ((isset($search_userid) && $search_userid !== "") ? $search_userid : null),
        ((isset($search_price) && $search_price !== "") ? $search_price : null),
        ((isset($search_method) && $search_method !== "") ? $search_method : null),
        NULL,
        ((isset($search_from_date) && $search_from_date !== "") ? $search_from_date : null),
        ((isset($search_to_date) && $search_to_date !== "") ? $search_to_date : null),
        "id DESC",
        ($page - 1) * $limit,
        $limit
    );
    $transactions_count = $trading_transactions_function->search_row_count(
        1,
        ((isset($search_userid) && $search_userid !== "") ? $search_userid : null),
        ((isset($search_price) && $search_price !== "") ? $search_price : null),
        ((isset($search_method) && $search_method !== "") ? $search_method : null),
        NULL,
        ((isset($search_from_date) && $search_from_date !== "") ? $search_from_date : null),
        ((isset($search_to_date) && $search_to_date !== "") ? $search_to_date : null)
    );
} else {
    $transactions = $trading_transactions_function->search(
        1,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        "id DESC",
        ($page - 1) * $limit,
        $limit
    );
    $transactions_count = $trading_transactions_function->search_row_count(
        1
    );
}
$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-4 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-4 mb-2">
                                        <label for="search_method">نوع تراکنش</label>
                                        <select id="search_method" name="search_method" class="form-control">
                                            <option value="">همه</option>
                                            <option value="0" <?php echo((isset($_GET['search_method']) && $_GET['search_method'] == 0) ? "selected" : ""); ?>>
                                                عادی
                                            </option>
                                            <option value="1" <?php echo((isset($_GET['search_method']) && $_GET['search_method'] == 1) ? "selected" : ""); ?>>
                                                شنا
                                            </option>
                                            <option value="2" <?php echo((isset($_GET['search_method']) && $_GET['search_method'] == 2) ? "selected" : ""); ?>>
                                                معکوس
                                            </option>
                                        </select>
                                    </div>

                                    <div class=" col-md-4 mb-2">

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

                                    <div class="col-md-5 mb-2">
                                        <label for="search_to_date">تا تاریخ</label>
                                        <input type="text" class="form-control" id="search_to_date"
                                               name="search_to_date" autocomplete="off"
                                               data-jdp
                                               value="<?php echo((isset($_GET['search_to_date'])) ? $_GET['search_to_date'] : ""); ?>">
                                    </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>
                                        <th class="text-center no-print">عملیات</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php

                                    foreach ($transactions as $transaction) {
                                        ?>
                                        <tr id="trading_<?php echo $transaction->id; ?>">
                                            <td class="text-center">
                                                <?php echo $transaction->id; ?>
                                            </td>
                                            <td class="text-center" id="buyer_<?php echo $transaction->id; ?>">
                                                <?php
                                                echo $users_function->get_by_userid($transaction->buyer)[0]->nick_name . "<br>" . $transaction->buyer;
                                                ?>
                                            </td>
                                            <td class="text-center" id="seller_<?php echo $transaction->id; ?>">
                                                <?php
                                                echo $users_function->get_by_userid($transaction->seller)[0]->nick_name . "<br>" . $transaction->seller;
                                                ?>
                                            </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_time_<?php echo $transaction->id; ?>">
                                                نزده
                                            </td>

                                            <td class="text-center"
                                                id="settlement_type_<?php echo $transaction->id; ?>">
                                                <?php echo (($transaction->settling_info == '[]')?"❌":"✅"); ?>
                                            </td>
                                            <td class="text-center no-print">
                                                <i class="mdi mdi-pencil text-primary font-20" data-toggle="tooltip" data-placement="top" title="ویرایش معامله" data-original-title=""
                                                   id="edit_transaction" data-id="<?php echo $transaction->id; ?>"></i>
                                                <i class="mdi mdi-delete text-danger font-20" data-toggle="tooltip" data-placement="top" title="حذف معامله" data-original-title=""
                                                   id="delete_transaction" data-id="<?php echo $transaction->id; ?>"></i>
                                            </td>
                                        </tr>
                                        <?php
                                    }
                                    ?>
                                    </tbody>
                                </table>
                            </div>
                            <div class="col-12 text-center">
                                <?php
                                $parms = "order_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>
                        </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>
