<?php

session_start();

if (!isset($_SESSION["id"])) {
    echo json_encode(["ok" => false, "error_code" => 100]);
    exit();
}

if (!isset($_POST["id"])) {
    echo json_encode(["ok" => false, "error_code" => 101]);
    exit();
}

include("../../autoload.php");
$users_function = new \Functions\Users();
$admins_function = new \Functions\Admins();
$trading_transaction_function = new \Functions\Trading_Transactions();
$persian_date = new \Classes\PersianDate();
$admin_info = $admins_function->get_by_id($_SESSION["id"])[0];
$output = "<input type=\"hidden\" id=\"update-tr-id\" value=\"" . $_POST["id"] . "\">";
$root = "api/update_trading_transaction.php";
if (in_array($root, json_decode($admin_info->permissions))) {
    $trading_transaction_info = $trading_transaction_function->get_by_id($_POST["id"])[0];


    $output .= '
<input type="hidden"  id="update-tr-id" value="'.$_POST['id'].'">
<div class="form-group row">
        <div class=" col-12 mb-2 buyer-div">
        <label>خریدار</label>
            <select id="update-tr-buyer" 
                   class="form-control js-select1">

                <option disabled>یک کاربر را انتخاب کنید</option>';

    $users = $users_function->get_users(["use_bot", "open"]);

    foreach ($users as $user) {
        $selected = (($trading_transaction_info->buyer == $user->userid) ? "selected" : "");
        $output .= "<option value=\"{$user->userid}\" $selected >{$user->id} - {$user->name} - {$user->nick_name} - {$user->userid}</option>";
    }
    $output .= '
            </select>
        </div>
        <div class=" col-12 mb-2 seller-div">
        <label>فروشنده</label>
            <select id="update-tr-seller" 
                   class="form-control js-select2">

                <option disabled>یک کاربر را انتخاب کنید</option>';

    $users = $users_function->get_users(["use_bot", "open"]);

    foreach ($users as $user) {
        $selected = (($trading_transaction_info->seller == $user->userid) ? "selected" : "");
        $output .= "<option value=\"{$user->userid}\" $selected >{$user->id} - {$user->name} - {$user->nick_name} - {$user->userid}</option>";
    }
    $output .= '
            </select>
        </div>
    
        <div class=" col-12 mb-2">
        <label>تعداد</label>
            <input id="update-tr-count" class="form-control" type="text" style="direction: ltr" 
                   placeholder="تعداد" value="' . $trading_transaction_info->count . '">
        </div>
    
        <div class=" col-12 mb-2">
        <label>قیمت</label>
            <input id="update-tr-price" class="form-control" type="text" style="direction: ltr"
                   placeholder="قیمت" value="' . $trading_transaction_info->price . '">
        </div>

       
        <div class=" col-12 mb-2">
        <label>نوع تراکنش</label>
            <select id="update-tr-method" class="form-control">
            <option value="0" ' . (($trading_transaction_info->method == 0) ? "selected" : "") . '>عادی</option>
            <option value="1" ' . (($trading_transaction_info->method == 1) ? "selected" : "") . '>شنا</option>
            <option value="2" ' . (($trading_transaction_info->method == 2) ? "selected" : "") . '>معکوس</option>
            </select>
        </div>
        
        
        <div class="col-12 mb-2">
        <label>ضریب شدت</label>
            <input id="update-tr-leverage" class="form-control" type="text" style="direction: ltr"
                   placeholder="ضریب شدت" value="' . $trading_transaction_info->leverage . '">
        </div>
        
        <div class="col-12 mb-2">
        <label>تاریخ تسویه</label>      
            <input type="text" class="form-control"
                   id="update-tr-settlement-date" autocomplete="off"
                   data-jdp
                   value="'.$persian_date->jdate("Y/m/d",$trading_transaction_info->settling_timestamp).'">
        </div>
        
        <div class="col-12 mb-2">
        <label>زمان تسویه</label>      
            <select id="update-tr-settlement-time" class="form-control">
                <option value="00:29" ' . (((int)$persian_date->jdate("Hi",$trading_transaction_info->settling_timestamp) == 29) ? "selected" : "") . '>00:29</option>
                <option value="01:30" ' . (((int)$persian_date->jdate("Hi",$trading_transaction_info->settling_timestamp) == 130) ? "selected" : "") . '>01:30</option>
                <option value="21:01" ' . (((int)$persian_date->jdate("Hi",$trading_transaction_info->settling_timestamp) == 2101) ? "selected" : "") . '>21:01</option>
            </select>
        </div>

        <div class="col-12 my-2">
            <button onclick="update_transaction();"
                    class="btn btn-primary btn-block waves-effect waves-light" type="submit">
                ثبت تغییرات
            </button>
    </div>';
    $output .= '<!-- Magnific popup -->
<script src="assets/plugins/magnific-popup/jquery.magnific-popup.min.js"></script>
<script src="assets/js/lightbox.js"></script>
<script type="text/javascript" src="assets/plugins/persian-datepicker/jalalidatepicker.js"></script>

<script>
$(document).ready(function() {
let elm = document.getElementById("update-tr-settlement-date")[0];
     jalaliDatepicker.startWatch({
        minDate: "attr",
        maxDate: "attr",
        zIndex : 1500,
        autoShow : false
    });

    
    $(".js-select2").select2({
    dropdownParent: $(".seller-div")
  });
    $(".js-select1").select2({
    dropdownParent: $(".buyer-div")
  });
});
</script>';
    echo json_encode(["ok" => true, "output" => $output]);
    exit();
} else {
    echo json_encode(["ok" => false, "error_code" => 102]);
    exit();
}
?>