<?php

namespace App\Controllers\User;
use App\Views;
use App\Models\CommonModel;
use Binance;

class TradingOrders extends BaseController
{
	public function __construct(){
		// $CommonModel = new CommonModel();
		$CommonModel = model('CommonModel');
		$this->db = db_connect();

	}
	public function index($id='') {
		$session = \Config\Services::session();
		if(!$session->get('user_login')){
			return redirect()->to(site_url('user/login'));
		}
		
		$this->indexpage();
	}
	
	public function indexpage() {
		$this->db = db_connect();
		$session = \Config\Services::session();
		if(!$session->get('user_login')){
			return redirect()->to(site_url('user/login'));
		}
		$CommonModel = model('CommonModel');
		//$this->db->query("SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''))");


		// $languageid = ($this->session->get('user_langid')) ? $this->session->get('user_langid'): '1';

		$condition = "payment_key in('logo','status')";
		$payment_data_list = $CommonModel->GetPaymentsName($condition);
		$site_data = $CommonModel->GetSettingall('infobox');
		if($site_data) {
			
			foreach ($site_data as $row) {
				$this->data[$row->site_key] = $row->site_value;
			}

		} else {
			$site_data_default = $CommonModel->defaultlangSettingall('infobox');
			foreach ($site_data_default as $row) {
				$this->data[$row->site_key] = $row->site_value;
			}
		}

		$site_lang_data = $CommonModel->GetSiteSettings('site');
		
		if($site_lang_data){	
			$site_data = array_merge($site_data, $site_lang_data);
		}

		foreach ($site_data as $row) {
			$this->data[$row->site_key] = $row->site_value;
		}

		$uri = service('uri');
		$segments = $uri->getSegments();

		$uusersid =$session->get('uusersid');

		if ($segments[1]) {
			$this->data['sell_records1'] =  $this->db->table('trade_process')->where('uusersid',$uusersid)->where('pair',$segments[1])->get()->getResult();

			$this->data['filter'] = $segments[1];

			if($segments[1] == "all"){
            $this->data['sell_records'] =  $this->db->table('api_trade_history')->where('uuserid',$uusersid)->orderBy('time','desc')->get()->getResult();
			$this->data['sell'] =  $this->db->table('api_trade_history')->where('uuserid',$uusersid)->where('type','2')->orderBy('time','desc')->get()->getResult();
			$this->data['buy'] =  $this->db->table('api_trade_history')->where('uuserid',$uusersid)->where('type','1')->orderBy('time','desc')->get()->getResult();
			}else{
				$this->data['sell_records'] =  $this->db->table('api_trade_history')->where('uuserid',$uusersid)->where('pair',$segments[1])->orderBy('time','desc')->get()->getResult();
				$this->data['sell'] =  $this->db->table('api_trade_history')->where('uuserid',$uusersid)->where('pair',$segments[1])->where('type','2')->orderBy('time','desc')->get()->getResult();
			$this->data['buy'] =  $this->db->table('api_trade_history')->where('uuserid',$uusersid)->where('pair',$segments[1])->where('type','1')->orderBy('time','desc')->get()->getResult();
			}
			
			

		}else{

			
            $this->data['sell_records'] =  $this->db->table('api_trade_history')->where('uuserid',$uusersid)->orderBy('time','desc')->get()->getResult();
			$this->data['sell'] =  $this->db->table('api_trade_history')->where('uuserid',$uusersid)->where('type','2')->orderBy('time','desc')->get()->getResult();
			$this->data['buy'] =  $this->db->table('api_trade_history')->where('uuserid',$uusersid)->where('type','1')->orderBy('time','desc')->get()->getResult();
		}

		echo view('user/tradingorders',$this->data);
	}

}
