var symbols = ['BTC','ETH','USDT','USDC','BNB','SOL'];
var symbols2 = ['USDT','USDT','BTC','BTC','USDT','USDT'];
var OrderSymbols = ['BTCUSDT','ETHUSDT','BTCUSDT','BTCUSDC','BNBUSDT','SOLUSDT'];
var OrderDecimal = [2,2,8,8,2,2];
// Loop through each symbol
for (var i = 0; i < symbols.length; i++) {
    // Create a new paragraph element and set its text
    var ele= '';
    var ele2= '';
    var symbol = symbols[i];
    var symbol2 = symbols2[i];
    var bookPair = OrderSymbols[i];
    ele = '<li class="nav-item">'+
                    '<a class="nav-link lnk_ctr" data-toggle="pill" data-index="' +i+ '" data-value="' +symbol+ '" data-value2="' +symbol2+ '" data-pair="' +bookPair+ '" href="#' +symbol+ '" role="tab" aria-selected="false">' +symbol+ '</a>'+
                '</li>';
    ele2 = '<div class="tab-pane fade show" id="' +symbol+ '" role="tabpanel">'+
                '<table class="table">'+
                    '<thead>'+
                        '<tr>'+
                            '<th>Pairs</th><th>Last Price</th><th>Change</th>'+
                        '</tr>'+
                    '</thead>'+
                    '<tbody id="' +symbol+ '_body">'+
                        '<tr > <td colspan="3">'+
                            '<div class="spinner-border spinner-border-sm text-secondary" role="status"></div>'+
                        '</td></tr>'+
                    '</tbody>'+
                '</table>'+
            '</div>';
    if(i == 0){
        
        ele = '<li class="nav-item">'+
                    '<a class="nav-link active lnk_ctr" data-toggle="pill" data-index="' +i+ '" data-value="' +symbol+ '" data-value2="' +symbol2+ '" data-pair="' +bookPair+ '" href="#' +symbol+ '" role="tab" aria-selected="true">' +symbol+ '</a>'+
                '</li>';
        ele2 = '<div class="tab-pane fade show active" id="' +symbol+ '" role="tabpanel">'+
                '<table class="table">'+
                    '<thead>'+
                        '<tr>'+
                            '<th>Pairs</th><th>Last Price</th><th>Change</th>'+
                        '</tr>'+
                    '</thead>'+
                    '<tbody id="' +symbol+ '_body">'+
                        '<tr > <td  colspan="3">'+
                            '<div class="spinner-border spinner-border-sm text-secondary" role="status"></div>'+
                        '</td></tr>'+
                    '</tbody>'+
                '</table>'+
            '</div>';
    }
    $('.symbolsTab').append(ele);
    $('.symbolsContainer').append(ele2);
}



$(document).ready(function() {
    symbols.forEach(function(symbol) {
        if(symbol == 'SOL'){
            getTradingPairs2(symbol)
            .then(pairs => {
                // Call getPairInfo and append the result
                return getPairInfo(pairs); // Get the HTML content for the pairs
                
            })
            .then(htmlContent => {
                $('#'+symbol+'_body').html('');
                $('#'+symbol+'_body').append(htmlContent); // Append the HTML to the corresponding element
            })
            .catch(error => {
                console.error('Error:', error);
            });
        }else{
            getTradingPairs(symbol)
            .then(pairs => {
                // Call getPairInfo and append the result
                return getPairInfo(pairs); // Get the HTML content for the pairs
                
            })
            .then(htmlContent => {
                $('#'+symbol+'_body').html('');
                $('#'+symbol+'_body').append(htmlContent); // Append the HTML to the corresponding element
            })
            .catch(error => {
                console.error('Error:', error);
            });
        }
        
    });
    getPairOrderBook(symbols[0],OrderSymbols[0],OrderDecimal[0],8);
    $(".lnk_ctr").click(function(){    
        var data = $(this).attr('data-index');	
        if(data.length > 0){
            getPairOrderBook(symbols[data],OrderSymbols[data],OrderDecimal[data],8);
        }
    });
    setTimeout(function() {
        call_get_trades();
        get_fund_action();
    },5000);
    setTimeout(function() {
        startTrade();
    },2000);
});
function updateDisplay(message) {
    $("#cmd1").append(message);
    $("#cmd").scrollTop($("#cmd")[0].scrollHeight); // Scroll to the bottom
}

function startTrade() {
    $("#cmd1").html(""); // Clear previous output
    updateDisplay("Loading...<br/>");
    const interval = setInterval(function() {
        get_trade_info(function(tradeMessage) {
            if (tradeMessage !== '') {
                updateDisplay(tradeMessage);
                $(".cmd1").removeClass('d-none');
            } else {
                // clearInterval(interval);
            }
        });
    }, 5000); // Adjust the interval as needed
}
function call_get_trades(){
    setInterval(function() {
        get_trades();// run after 11 seconds
    }, 11000); // Adjust the interval as needed
}
// Set the interval to run the function every 30 minutes (1800000 milliseconds)
setInterval(function() {
    get_fund_action();
}, 1800000); // 30 minutes
function get_trades(){
    $(".loader_tr").removeClass('d-none');
    var container = $(".trades_table");
    var data = {'trade_id':'7'};
	$.ajax({
		type:'POST',
		url:'php/scripts.php?check_trade',
		data:data,
		success: function(data){
            $(".loader_tr").addClass('d-none');
            var check = data.split("|")[0];
            if(check == 2){
                var newData = data.split("|")[1];
                // Skip the first <tr> and append the new data
                var firstRow = container.find("tr").first(); // Get all <tr> elements except the first
                container.html(firstRow); // Update container with remaining rows
                container.append(newData); // Append the new data
            }else{
                alert(data);
            }
		}
	});
}
function get_fund_action(){
    $(".loader_tr2").removeClass('d-none');
    var container = $(".funds_table");
    var data = {'trade_id':'6'};
	$.ajax({
		type:'POST',
		url:'php/scripts.php?get_fund_action',
		data:data,
		success: function(data){
            $(".loader_tr2").addClass('d-none');
            var check = data.split("|")[0];
            if(check == 2){
                var newData = data.split("|")[1];
                // Skip the first <tr> and append the new data
                var firstRow = container.find("tr").first(); // Get all <tr> elements except the first
                container.html(firstRow); // Update container with remaining rows
                container.append(newData); // Append the new data
            }else{
                alert(data);
            }
		}
	});
}
function get_trade_info(callback) {
    var data = { 'trade_id': '1' };
    $.ajax({
        type: 'POST',
        url: 'php/scripts.php?get_trade_info',
        data: data,
        success: function(data) {
            var check = data.split("|")[0];
            var newData = '';
            if (check == 2) {
                newData = data.split("|")[1];
            }
            callback(newData); // Call the callback with the newData
        },
        error: function() {
            callback('Error fetching trade info'); // Handle error
        }
    });
}