<?php 
include('view/new_header_file.php');
include_once("classes/database.class.php");
include_once("classes/oanda.php");
include_once('classes/phpToPDF.php');
?>

<link rel="stylesheet" type="text/css" href="node_modules/css-toggle-switch/dist/toggle-switch.css">




  <style type="text/css">

    .tabs-left, .tabs-right {

      border-bottom: none;

      padding-top: 2px;

  }

  .tabs-left {

    border-right: 1px solid #ddd;

  }

  .tabs-right {

    border-left: 1px solid #ddd;

  }

  .tabs-left>li, .tabs-right>li {

    float: none;

    margin-bottom: 2px;

  }

  .tabs-left>li {

    margin-right: -1px;

  }

  .tabs-right>li {

    margin-left: -1px;

  }

  .tabs-left>li.active>a,

  .tabs-left>li.active>a:hover,

  .tabs-left>li.active>a:focus {

    border-bottom-color: #ddd;

    border-right-color: transparent;

  }



  .tabs-right>li.active>a,

  .tabs-right>li.active>a:hover,

  .tabs-right>li.active>a:focus {

    border-bottom: 1px solid #ddd;

    border-left-color: transparent;

  }

  .tabs-left>li>a {

    border-radius: 4px 0 0 4px;

    margin-right: 0;

    display:block;

  }

  .tabs-right>li>a {

    border-radius: 0 4px 4px 0;

    margin-right: 0;

  }

  .vertical-text {

    margin-top:50px;

    border: none;

    position: relative;

  }

  .vertical-text>li {

    height: 20px;

    width: 120px !!important;

    margin-bottom: 100px;

  }

  .vertical-text>li>a {

    border-bottom: 1px solid #ddd;

    border-right-color: transparent;

    text-align: center;

    border-radius: 4px 4px 0px 0px;

  }

  .vertical-text>li.active>a,

  .vertical-text>li.active>a:hover,

  .vertical-text>li.active>a:focus {

    border-bottom-color: transparent;

    border-right-color: #ddd;

    border-left-color: #ddd;

  }

  .vertical-text.tabs-left {

    left: -50px;

  }

  .vertical-text.tabs-right {

    right: -50px;

  }

  .vertical-text.tabs-right>li {

    -webkit-transform: rotate(90deg);

    -moz-transform: rotate(90deg);

    -ms-transform: rotate(90deg);

    -o-transform: rotate(90deg);

    transform: rotate(90deg);

  }

  .vertical-text.tabs-left>li {

    -webkit-transform: rotate(-90deg);

    -moz-transform: rotate(-90deg);

    -ms-transform: rotate(-90deg);

    -o-transform: rotate(-90deg);

    transform: rotate(-90deg);

  }

  .tabs-left > li 

  {

    padding-top: 5px !important;

    padding-bottom: 5px !important;

  }

  .panel-heading

  {

    background-color: #F7614E !important;

    text-align: center;

    padding: 7px !important;

    font-size: 18px !important;

    font-weight: 500;

  }

  .panel-body

  {

    background-color: #0E162D !important;

    text-align: center;

    padding-top: 40px;

    padding-bottom: 40px;

    font-size: 48px !important;

  }

  .swal-modal

  {

    background-color: #202943;

  }

  .swal-title

  {

    color: #F7614E;

  }

  .swal-text

  {

    color: #fff;

  }

  .swal-icon--success:before, .swal-icon--success:after

  {

    background-color: #202943;  

  }

  .swal-icon--success__hide-corners {



    background-color: #202943;  

  }

  .lightTabs

  {

    background: #fff !important;

  }

  .lightTabs > li > a

  {

    color: #F7614E !important; 

  }

  #home

  {

    color: white;

    background: #1F2638 !important;

  }

  #rightTab, #rightTab > .crypt-tab-content

  {

    background-color: #1F2638;

  }

  .lightRightTab, .lightRightTab > div, .lightRightTab > div > #home, .lightRightTab >  .crypt-tab-content 

  {

    background-color: #fff !important;

  }

  .light-panel-body

  {

    background: #eee !important;

    color: #1F2638 !important;

  }

  .lightTableVersion, .lightTableVersion > thead, .lightTableVersion > thead > tr, .lightTableVersion > thead > tr > th, .lightTableVersion > tbody, .lightTableVersion > tbody > tr, .lightTableVersion > tbody > tr > td

  {

    background-color: #fff;

    color: #1F2638 !important;

    border: 1px solid #1F2638 !important;

  }

  .lightRightTab > div > #profile > form > div > label, .lightRightTab > div > #messages > form > div > div > label, .lightRightTab > div > #targets > form > div > div > label, .lightRightTab > div > #userSettings > #acc_type_row > form > div > label, .lightRightTab > div > #userSettings > #logRules > form > div > label

  {

    color: #1F2638 !important;

  }

  .lightRightTab > div > #profile > form > div > input, .lightRightTab > div > #profile > form > div > select, .lightRightTab > div > #messages > form > div > div > input, .lightRightTab > div > #targets > form > div > div > input, .lightRightTab > div > #userSettings > #acc_type_row > form > div > input, .lightRightTab > div > #userSettings > #logRules > form > div > input, .lightRightTab > div > #userSettings > #logRules > form > div > select

  {

    color: #1F2638 !important;

    background-color: #eee !important;  

  }

  .round {

  position: relative;

}



.round label {

  background-color: #fff;

  border: 1px solid #ccc;

  border-radius: 50%;

  cursor: pointer;

  height: 28px;

  left: 0;

  position: absolute;

  top: 0;

  width: 28px;

}



.round label:after {

  border: 2px solid #fff;

  border-top: none;

  border-right: none;

  content: "";

  height: 6px;

  left: 7px;

  opacity: 0;

  position: absolute;

  top: 8px;

  transform: rotate(-45deg);

  width: 12px;

}



.round input[type="checkbox"] {

  visibility: hidden;

}



.round input[type="checkbox"]:checked + label {

  background-color: #66bb6a;

  border-color: #66bb6a;

}



.round input[type="checkbox"]:checked + label:after {

  opacity: 1;

}




  </style>

  <?php 

    if(isset($_COOKIE['id']) && isset($_COOKIE['uid'])){

    $userId = filter_input(INPUT_COOKIE, 'id');

    $userUId = filter_input(INPUT_COOKIE, 'uid');


    $db = new database();

    $stmt = $db->db->prepare("SELECT * FROM users WHERE id = :id and uid = :uid");

    $stmt->execute(array(':id' => $userId, ':uid' => $userUId));

    $userDetails = $stmt->fetch(PDO::FETCH_OBJ);

    $fullN = $userDetails->name;

    $splitname = explode(" ", $fullN);

    $loggedIn = true;

    $firstname = $splitname[0];

    $lastname = $splitname[1];

    $accType = $userDetails->acctype;

    $starting_money = $userDetails->starting_money;

    $account = new oandaAPI();

    $account->retrieveAccount($userUId);



    $gettrades = $account->retrieveAccount($userUId);

    $allreport = $account->completeReport($userUId);

    $allreport = array_reverse($allreport);

   

    $todayProfit = $account->todaysProfit($userUId);

    $weeklyProfit = $account->weekProfit($userUId);

    $monthProfit = $account->month_profit($userUId);

    $halfyearpro = $account->halfyear_profit($userUId);

    $yearpro = $account->year_profit($userUId);

    $totalpro = $account->total_profit($userUId);



  }

  ?>


      <div class="container">
         <div class="row sm-gutters">


                    <div class="col-xs-3 col-sm-3 col-lg-3">

                         
                          <?php 

                          include_once('menu.php');

                          ?>

                      </div>

                      <div class="col-xs-9 col-sm-9 col-lg-9" id="rightTab">


                          <div class="tab-content crypt-tab-content" style="width: 100%;">

                            <div class="tab-pane active" id="profile" style="color: white;padding-top: 20px !important;padding-bottom: 20px !important;">
                                    



                                  



<div class="row">

                          <div class="col-md-12">

                            <h3 style="color: #F7614E;">Profit History</h3>

                          </div>

                          <div class="col-md-4 col-sm-6 col-xs-12" style="margin-top: 10px;margin-bottom: 30px;">

                            <button id="completeSummarybtn" onclick="exportpdfSummary('<?=$account->balance; ?>', '<?=$account->marginUsed; ?>')" class="btn btn-sm btn-success">Export Complete Summary</button>

                          </div>                                  

                                </div>

                        <div class="row" id="weeklyhistory" style="margin-top: 20px;">



                                  <div class="col-md-12 col-lg-12 col-sm-12" style="margin-bottom: 20px !important;">

                                    <canvas id="weeklyhistorygraphshow" style="width: 100%;height: 300px;background-color: #0E162D !important;padding: 10px !important;"></canvas>

                                  </div>

                                  

                                </div>



                                <div class="row">



                                  <div class="col-md-4 col-sm-4 col-xs-12">

                                    

                                      <div class="panel panel-default">

                                <div class="panel-heading" style="color: #fff;">Today Profit</div>

                                <div class="panel-body" id="tdPro" <?php if ($todayProfit <= 0): ?>

                                  style="color: red;"

                                  <?php else: ?>

                                    style="color: lime;"

                                <?php endif ?>><?php 



                                   $todayProfit = number_format((float)$todayProfit, 2, '.', '');



                                   echo $todayProfit;



                                 ?></div>

                              </div>



                                  </div>

                                  <div class="col-md-4 col-sm-4 col-xs-12">

                                    

                                      <div class="panel panel-default">

                                <div class="panel-heading" style="color: #fff;">Weekly Profit</div>

                                <div class="panel-body" id="wkProfit" <?php if ($weeklyProfit <= 0): ?>

                                  style="color: red;"

                                  <?php else: ?>

                                    style="color: lime;"

                                <?php endif ?>><?php 



                                   $wkpro = number_format((float)$weeklyProfit, 2, '.', '');



                                   echo $wkpro;



                                 ?></div>

                              </div>



                                  </div>

                                  <div class="col-md-4 col-sm-4 col-xs-12">

                                    

                                      <div class="panel panel-default">

                                <div class="panel-heading" style="color: #fff;">Monthly Profit</div>

                                <div class="panel-body" id="monPro" <?php if ($monthProfit <= 0): ?>

                                  style="color: red;"

                                  <?php else: ?>

                                    style="color: lime;"

                                <?php endif ?>><?php 



                                   $monthProfit = number_format((float)$monthProfit, 2, '.', '');



                                   echo $monthProfit;



                                 ?></div>

                              </div>



                                  </div>

                                  <div class="col-md-4 col-sm-4 col-xs-12">

                                    

                                      <div class="panel panel-default">

                                <div class="panel-heading"  style="color: #fff;">Half Year Profit</div>

                                <div class="panel-body" id="hyPro" <?php if ($halfyearpro <= 0): ?>

                                  style="color: red;"

                                  <?php else: ?>

                                    style="color: lime;"

                                <?php endif ?>><?php 



                                   $halfyearpro = number_format((float)$halfyearpro, 2, '.', '');



                                   echo $halfyearpro;



                                 ?></div>

                              </div>



                                  </div>

                                  <div class="col-md-4 col-sm-4 col-xs-12">

                                    

                                      <div class="panel panel-default">

                                <div class="panel-heading" style="color: #fff;">Yearly Profit</div>

                                <div class="panel-body" id="ypro" <?php if ($yearpro <= 0): ?>

                                  style="color: red;"

                                  <?php else: ?>

                                    style="color: lime;"

                                <?php endif ?>><?php 



                                   $yearpro = number_format((float)$yearpro, 2, '.', '');



                                   echo $yearpro;



                                 ?></div>

                              </div>



                                  </div>

                                    <div class="col-md-4 col-sm-4 col-xs-12">

                                    

                                      <div class="panel panel-default">

                                <div class="panel-heading" style="color: #fff;">Total Profit</div>

                                <div class="panel-body" id="tpPRO" <?php if ($totalpro <= 0): ?>

                                  style="color: red;"

                                  <?php else: ?>

                                    style="color: lime;"

                                <?php endif ?>><?php 



                                   $totalpro = number_format((float)$totalpro, 2, '.', '');



                                   echo $totalpro;



                                 ?></div>

                              </div>



                                  </div>

                                  

                                </div>







                                      



                              </div>

                            </div>



                      </div>                      



        </div>
    </div>


<?php
include('view/new_footer_file.php');
?>


	  <script type="text/javascript">
	  	var getCar = '<?=$userDetails->pcarrier; ?>';
	  	$("#pcar").val(getCar).change();
	</script>
  <script type="text/javascript">
      
      $(function() {

            var firstcharsale = '<?=$firstSaleChar; ?>';
            var firstcharbuy = '<?=$firstBuyChar; ?>';
            var firstcharclose = '<?=$firstCloseChar ?>';

            $("#saleskey1").val(firstcharsale).change();
            $("#buykey1").val(firstcharbuy).change();
            $("#closekey1").val(firstcharclose).change();

      });

  </script>


    <script type="text/javascript">

    

    var ctx = document.getElementById('weeklyhistorygraphshow');



    var myChart = new Chart(ctx, {

    type: 'line',

    data: {

        labels: [ <?php 

              $count = 0;



              foreach ($allreport as $all_report) {

                # code...

                echo $all_report.",";



                $count++;



                if ($count >= 250) {



                  break;

                }



              }





            ?>],

        datasets: [{

            label: 'Account Balance History',

            data: [

            



            <?php 

              $count = 0;



              foreach ($allreport as $all_report) {

                # code...

                echo $all_report.",";



                $count++;



                if ($count > 250) {



                  break;

                }

                  

              }





            ?>



            ],

            backgroundColor: [

                'rgba(255, 99, 132, 0.2)',

            ],

            borderColor: [

                'rgba(255, 99, 132, 1)',

            ],

            borderWidth: 3

        }]

    },

    options: {

        scales: {

            yAxes: [{

                ticks: {

                    beginAtZero: true

                }

            }]

        }

    }

});



</script>


  <script type="text/javascript">
    function exportpdfSummary(acc_balance, margin_used)
    {
        var canvas = document.getElementById("weeklyhistorygraphshow");
        var encodedImage    = canvas.toDataURL("image/png");
        var uid = '<?=$userId; ?>';
        blogImage = makeBlob(encodedImage);
        $("#completeSummarybtn").html('Generating Summary'); 
        var formData = new FormData();
        formData.append('image',blogImage);
        formData.append('uid', uid);
        var avail_margin = $("#availMargin").html();
        var unrealizedEarning = $("#unrealizedEarning").html();
        var openTrades = $("#openTrades").html();
        var openOrders = $("#openOrders").html();
        var ag = $("#agGoal").html();
        var mg = $("#mgGoal").html();
        var dg = $("#dgGoal").html();
        var tdpro = $("#tdPro").html();
        var wkpro = $("#wkProfit").html();
        var monPro = $("#monPro").html();
        var hyPro = $("#hyPro").html();
        var ypro = $("#ypro").html();
        var tppro = $("#tpPRO").html();



         $.ajax({
                url        : "controller/reportimage.controller.php",
                type       : 'POST',
                data       : formData,
                cache      : false,
                contentType: false,
                processData: false,
                success    : function (response) {
                    if (response == 'done') {

                      window.open('https://trading.invata.io/report.php?id='+uid+'&balance='+acc_balance+'&margin='+margin_used+'&avail_margin='+avail_margin+'&unearning='+unrealizedEarning+"&un_earning="+unrealizedEarning+"&openOrders="+openOrders+"&openTrades="+openTrades+"&ag="+ag+"&mg="+mg+"&dg="+dg+"&tdpro="+tdpro+"&wkpro="+wkpro+"&monPro="+monPro+"&hyPro="+hyPro+"&ypro="+ypro+"&tppro="+tppro, '_blank');

                         $("#completeSummarybtn").html('Export Complete Summary'); 
                    }else{

                      alert('Some error occured');
                    }
                },
               error: function (xhr, ajaxOptions, thrownError) {
        console.log(xhr);
      },
            });


    }


      function makeBlob(dataURL)
  {
     var BASE64_MARKER = ';base64,';
            if (dataURL.indexOf(BASE64_MARKER) == -1) {
                var parts = dataURL.split(',');
                var contentType = parts[0].split(':')[1];
                var raw = decodeURIComponent(parts[1]);
                return new Blob([raw], { type: contentType });
            }
            var parts = dataURL.split(BASE64_MARKER);
            var contentType = parts[0].split(':')[1];
            var raw = window.atob(parts[1]);
            var rawLength = raw.length;

            var uInt8Array = new Uint8Array(rawLength);

            for (var i = 0; i < rawLength; ++i) {
                uInt8Array[i] = raw.charCodeAt(i);
            }

            return new Blob([uInt8Array], { type: contentType });
  }
  </script>
