@page
@model SalesAnalyzer
@{
  ViewData["Title"] = "";
}

@section Styles {
  <link href="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)assets/plugins/tablesaw/css/tablesaw.css" rel="stylesheet" type="text/css" />
  <link href="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)assets/plugins/nvd3/nv.d3.min.css" rel="stylesheet" type="text/css" />

  <link href="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)assets/plugins/datatables/dataTables.bootstrap4.min.css" rel="stylesheet" type="text/css" />
  <link href="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)assets/plugins/datatables/buttons.bootstrap4.min.css" rel="stylesheet" type="text/css" />
}

<div class="row">
  <div class="col-md-12">
    <div class="card-box"> 
      <table class="table table-striped table-sm">
          <thead>
            <tr>
              @{
                double currentBalance = Model.PTData.GetCurrentBalance();
                string currentBalanceString = currentBalance.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"));
                if (currentBalance > 100) {
                  currentBalanceString = Math.Round(currentBalance, 2).ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"));
                }
              }
              <th class="m-t-0 header-title text-left">Account Value: &nbsp; <text class="text-autocolor"> @currentBalanceString &nbsp; @Model.Summary.MainMarket </text> <small> <i class="fa fa-info-circle text-muted" data-toggle="tooltip" data-placement="top" title="This is based on your sales history, entries on the Transactions page and any currently held positions."></i></small></th>
              <th class="text-right">Starting Value: &nbsp; <text class="text-autocolor"> @Model.PTMagicConfiguration.GeneralSettings.Application.StartBalance &nbsp; @Model.Summary.MainMarket </text> <small> <i class="fa fa-info-circle text-muted" data-toggle="tooltip" data-placement="top" title="This is the starting value found in your settings file"></i></small></th>
            </tr>
          </thead>
        </table>
    </div>
  </div>
</div>

<div class="row">
  <div class="col-md-12">
    <div class="card-box">       
      <h4 class="m-t-0 header-title text-center">Cumulative Profits <small> <i class="fa fa-info-circle text-muted" data-toggle="tooltip" data-placement="top" title="This value is based on your sales history"></i></small></h4>
      <div class="balance-chart">
          <svg style="height:350px;width:100%"></svg>
      </div>
    </div>
  </div>
</div>

@if (Model.PTData.SellLog.Count == 0) {
  <div class="row">
    <div class="col-md-12">
      <div class="card-box">
        <h4 class="m-t-0 header-title">No Sales!</h4>

        <p>Sorry, but your Profit Trailer did not sell anything so far. Please wait for the bot to have at least one sale and you will start seeing data in here.</p>
      </div>
    </div>
  </div>
} else {
  <div class="row">
    <div class="col-md-6">
      <div class="card-box">

        <h4 class="m-t-0 header-title">Sales Analysis</h4>
        
        @{
          double totalProfit = Model.PTData.SellLog.Sum(s => s.Profit);
          double totalProfitFiat = Math.Round(totalProfit * Model.Summary.MainMarketPrice, 2);
          double percentGain = Math.Round(totalProfit / Model.PTMagicConfiguration.GeneralSettings.Application.StartBalance * 100, 2);
          double avgDailyGain = Model.DailyGains.Values.Average(dg => dg);
          double avgMonthlyGain = Model.MonthlyGains.Values.Average(dg => dg);

          string percentGainText = percentGain.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")) + "%";
          if (Model.PTData.TransactionData.Transactions.Count > 0) {
            percentGainText = "<i class=\"fa fa-info-circle text-muted\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"You have added at least one manual transaction, so the total gain percentage cannot be calculated.\"></i>";
          }
        }

        <table class="table table-striped table-sm">
          <thead>
            <tr>
              <th></th>
              <th class="text-right">Total</th>
              <th class="text-right">AVG/Day</th>
              <th class="text-right">AVG/Month</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <th>Sales</th>
              <td class="text-right">@Model.PTData.SellLog.Count</td>
              <td class="text-right">@Math.Round((double)Model.PTData.SellLog.Count / (double)Model.PTData.SellLog.GroupBy(d => d.SoldDate.Date).ToList().Count, 2).ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))</td>
              <td class="text-right">@Math.Round((double)Model.PTData.SellLog.Count / (double)Model.PTData.SellLog.GroupBy(d => d.SoldDate.Date.ToString("yyyy-MM")).ToList().Count, 2).ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))</td>
            </tr>
            <tr>
              <th>Profit</th>
              <td class="text-right text-autocolor">@totalProfit.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))</td>
              <td class="text-right text-autocolor">@Math.Round(totalProfit / (double)Model.PTData.SellLog.GroupBy(d => d.SoldDate.Date).ToList().Count, 8).ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))</td>
              <td class="text-right text-autocolor">@Math.Round(totalProfit / (double)Model.PTData.SellLog.GroupBy(d => d.SoldDate.Date.ToString("yyyy-MM")).ToList().Count, 8).ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))</td>
            </tr>
            <tr>
              <th>Profit USD</th>
              <td class="text-right text-autocolor">@Html.Raw(Model.MainFiatCurrencySymbol + totalProfitFiat.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))</td>
              <td class="text-right text-autocolor">@Html.Raw(Model.MainFiatCurrencySymbol + Math.Round(totalProfitFiat / (double)Model.PTData.SellLog.GroupBy(d => d.SoldDate.Date).ToList().Count, 8).ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))</td>
              <td class="text-right text-autocolor">@Html.Raw(Model.MainFiatCurrencySymbol + Math.Round(totalProfitFiat / (double)Model.PTData.SellLog.GroupBy(d => d.SoldDate.Date.ToString("yyyy-MM")).ToList().Count, 8).ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))</td>
            </tr>
            <tr>
              <th>% Gain</th>
              <td class="text-right text-autocolor">@Html.Raw(percentGainText)</td>
              <td class="text-right text-autocolor">@avgDailyGain.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%</td>
              <td class="text-right text-autocolor">@avgMonthlyGain.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>

    <div class="col-md-6">
      <div class="card-box">
        @{
          double currentTotalBalance = Model.PTData.GetCurrentBalance();
          double estimatedBalance1Month = Math.Round(currentTotalBalance * Math.Pow((1 + (avgDailyGain / 100)), 30.0), 8);
          double estimatedBalance3Months = Math.Round(currentTotalBalance * Math.Pow((1 + (avgDailyGain / 100)), 90.0), 8);
          double estimatedBalance6Months = Math.Round(currentTotalBalance * Math.Pow((1 + (avgDailyGain / 100)), 180.0), 8);
          double estimatedBalance1Year = Math.Round(currentTotalBalance * Math.Pow((1 + (avgDailyGain / 100)), 365.0), 8);
        }

        <h4 class="m-t-0 header-title">Balance Prediction <small><i class="fa fa-info-circle text-muted" data-toggle="tooltip" data-placement="top" title="The balance prediction is based on your daily average gain of @avgDailyGain.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))% and your current approximate balance of @currentTotalBalance.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))"></i></small></h4>

        <table class="table table-striped table-sm">
          <thead>
            <tr>
              <th class="text-right"></th>
              <th class="text-right">Est. Balance</th>
              <th class="text-right">Est. @Model.Summary.MainFiatCurrency Value</th>
              <th class="text-right">Est. Gain</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <th>1 month</th>
              <td class="text-right text-autocolor">@estimatedBalance1Month.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))</td>
              <td class="text-right text-autocolor">@Html.Raw(Model.MainFiatCurrencySymbol + Math.Round(estimatedBalance1Month * Model.Summary.MainMarketPrice, 2).ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))</td>
              <td class="text-right text-autocolor">@Math.Round((estimatedBalance1Month - currentTotalBalance) / currentTotalBalance * 100, 2).ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%</td>
            </tr>
            <tr>
              <th>3 months</th>
              <td class="text-right text-autocolor">@estimatedBalance3Months.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))</td>
              <td class="text-right text-autocolor">@Html.Raw(Model.MainFiatCurrencySymbol + Math.Round(estimatedBalance3Months * Model.Summary.MainMarketPrice, 2).ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))</td>
              <td class="text-right text-autocolor">@Math.Round((estimatedBalance3Months - currentTotalBalance) / currentTotalBalance * 100, 2).ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%</td>
            </tr>
            <tr>
              <th>6 months</th>
              <td class="text-right text-autocolor">@estimatedBalance6Months.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))</td>
              <td class="text-right text-autocolor">@Html.Raw(Model.MainFiatCurrencySymbol + Math.Round(estimatedBalance6Months * Model.Summary.MainMarketPrice, 2).ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))</td>
              <td class="text-right text-autocolor">@Math.Round((estimatedBalance6Months - currentTotalBalance) / currentTotalBalance * 100, 2).ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%</td>
            </tr>
            <tr>
              <th>1 year</th>
              <td class="text-right text-autocolor">@estimatedBalance1Year.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))</td>
              <td class="text-right text-autocolor">@Html.Raw(Model.MainFiatCurrencySymbol + Math.Round(estimatedBalance1Year * Model.Summary.MainMarketPrice, 2).ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))</td>
              <td class="text-right text-autocolor">@Math.Round((estimatedBalance1Year - currentTotalBalance) / currentTotalBalance * 100, 2).ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>

  <div class="row">
    <div class="col-md-6">
      <div class="card-box">
        <div class="trades-chart">
          <svg style="height:220px;width:100%"></svg>
        </div>
      </div>
    </div>

    <div class="col-md-6">
      <div class="card-box">
        <div class="profit-chart">
          <svg style="height:220px;width:100%"></svg>
        </div>
      </div>
    </div>
  </div>

  <div class="row">
    <div class="col-md-6">
      <div class="card-box">
        <h4 class="m-t-0 header-title">Last @Model.PTMagicConfiguration.GeneralSettings.Monitor.MaxDailySummaries days</h4>

        <table class="table table-sm">
          <thead>
            <tr>
              <th>Day</th>
              <th class="text-right">Sales</th>
              <th class="text-right">Profit @Model.Summary.MainMarket</th>
              <th class="text-right">Profit @Model.Summary.MainFiatCurrency</th>
              <th class="text-right">% Gain</th>
            </tr>
          </thead>
          <tbody>
            @for (DateTime salesDate = Model.DateTimeNow.DateTime.Date; salesDate >= Model.DateTimeNow.DateTime.AddDays(-Model.PTMagicConfiguration.GeneralSettings.Monitor.MaxDailySummaries) && salesDate >= Model.MinSellLogDate; salesDate = salesDate.AddDays(-1)) {
              List<Core.Main.DataObjects.PTMagicData.SellLogData> salesDateSales = Model.PTData.SellLog.FindAll(sl => sl.SoldDate.Date == salesDate);
              double salesDateProfit = salesDateSales.Sum(sl => sl.Profit);
              double salesDateProfitFiat = Math.Round(salesDateProfit * Model.Summary.MainMarketPrice, 2);
              double salesDateStartBalance = Model.PTData.GetSnapshotBalance(salesDate);
              double salesDateGain = Math.Round(salesDateProfit / salesDateStartBalance * 100, 2);
              <tr>
                <td><a href="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)_get/SalesList/?d=@salesDate.ToString("yyyy-MM-dd")" data-remote="false" data-toggle="modal" data-target="#salesList">@salesDate.ToShortDateString()</a></td>
                <td class="text-right">@salesDateSales.Count</td>
                <td class="text-right text-autocolor">@salesDateProfit.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))</td>
                <td class="text-right text-autocolor">@Html.Raw(Model.MainFiatCurrencySymbol + salesDateProfitFiat.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))</td>
                <td class="text-right text-autocolor">@salesDateGain.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%</td>
              </tr>
            }
          </tbody>
        </table>
      </div>
    </div>

    <div class="col-md-6">
      <div class="card-box">
        <h4 class="m-t-0 header-title">Last @Model.PTMagicConfiguration.GeneralSettings.Monitor.MaxMonthlySummaries months</h4>

        <table class="table table-sm">
          <thead>
            <tr>
              <th>Month</th>
              <th class="text-right">Sales</th>
              <th class="text-right">Profit @Model.Summary.MainMarket</th>
              <th class="text-right">Profit @Model.Summary.MainFiatCurrency</th>
              <th class="text-right">% Gain</th>
              <th class="text-right">AVG %/Day</th>
            </tr>
          </thead>
          <tbody>
            @{
              DateTime minSellLogMonthDate = new DateTime(Model.MinSellLogDate.Year, Model.MinSellLogDate.Month, 1).Date;
              DateTime salesMonthStartDate = new DateTime(Model.DateTimeNow.DateTime.Year, Model.DateTimeNow.DateTime.Month, 1).Date;
            }
            @for (DateTime salesMonthDate = salesMonthStartDate.Date; salesMonthDate >= Model.DateTimeNow.DateTime.AddMonths(-Model.PTMagicConfiguration.GeneralSettings.Monitor.MaxMonthlySummaries) && salesMonthDate >= minSellLogMonthDate; salesMonthDate = salesMonthDate.AddMonths(-1)) {
              List<Core.Main.DataObjects.PTMagicData.SellLogData> salesMonthSales = Model.PTData.SellLog.FindAll(sl => sl.SoldDate.Date.Month == salesMonthDate.Month && sl.SoldDate.Date.Year == salesMonthDate.Year);
              double salesDateProfit = salesMonthSales.Sum(sl => sl.Profit);
              double salesDateProfitFiat = Math.Round(salesDateProfit * Model.Summary.MainMarketPrice, 2);
              double salesDateStartBalance = Model.PTData.GetSnapshotBalance(salesMonthDate);
              double salesDateGain = Math.Round(salesDateProfit / salesDateStartBalance * 100, 2);

              double salesDateAVGDailyGain = 0;
              double monthDailyProfit = 0;
              int days = 0;
              for (int d = 1; d <= DateTime.DaysInMonth(salesMonthDate.Year, salesMonthDate.Month); d++) {
                DateTime monthDay = salesMonthDate.AddDays(-salesMonthDate.Day + d);
                if (monthDay <= Model.DateTimeNow) {
                  days++;
                  List<Core.Main.DataObjects.PTMagicData.SellLogData> monthDaySales = Model.PTData.SellLog.FindAll(sl => sl.SoldDate.Date == monthDay.Date);
                  double monthDayProfit = monthDaySales.Sum(sl => sl.Profit);
                  double monthDayStartBalance = Model.PTData.GetSnapshotBalance(monthDay);
                  monthDailyProfit += Math.Round(monthDayProfit / monthDayStartBalance * 100, 2);
                }
              }
              salesDateAVGDailyGain = Math.Round(monthDailyProfit / days, 2);
            <tr>
              <td><a href="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)_get/SalesList/?m=@salesMonthDate.ToString("yyyy-MM")" data-remote="false" data-toggle="modal" data-target="#salesList">@salesMonthDate.ToString("MMMM", new System.Globalization.CultureInfo("en-US"))</a></td>
              <td class="text-right text-autocolor">@salesMonthSales.Count</td>
              <td class="text-right text-autocolor">@salesDateProfit.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))</td>
              <td class="text-right text-autocolor">@Html.Raw(Model.MainFiatCurrencySymbol + salesDateProfitFiat.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")))</td>
              <td class="text-right text-autocolor">@salesDateGain.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%</td>
              <td class="text-right text-autocolor">@salesDateAVGDailyGain.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%</td>
            </tr>
            }
          </tbody>
        </table>
      </div>
    </div>
  </div>

  <div class="row">
    <div class="col-sm-12">
      <div class="card-box">

        <h4 class="m-t-0 header-title"><b>Top @Model.PTMagicConfiguration.GeneralSettings.Monitor.MaxTopMarkets Sales Market Analysis</b></h4>

        <table class="tablesaw table m-b-0" data-tablesaw-sortable data-tablesaw-sortable-switch>
          <thead>
            <tr>
              <th scope="col" data-tablesaw-priority="persist" data-tablesaw-sortable-col data-tablesaw-sortable-default-col>Rank</th>
              <th scope="col" data-tablesaw-sortable-col>Market</th>
              <th scope="col" class="text-right" data-tablesaw-sortable-col>Sales</th>
              <th scope="col" class="text-right" data-tablesaw-sortable-col>Profit @Model.Summary.MainMarket</th>
              <th scope="col" class="text-right" data-tablesaw-sortable-col>Profit @Model.Summary.MainFiatCurrency</th>
              <th scope="col" class="text-right" data-tablesaw-sortable-col>Profit @Model.Summary.MainFiatCurrency/Trade</th>
              <th scope="col" class="text-right" data-tablesaw-sortable-col>Profit %/Trade</th>
            </tr>
          </thead>
          <tbody>
            @{
              var topMarkets = Model.PTData.SellLog.GroupBy(m => m.Market).Select(mg => mg.Sum(m => m.Profit));
              int marketRank = 0;
            }
            @foreach (KeyValuePair<string, double> marketData in Model.TopMarkets) {
              marketRank++;
              int trades = Model.PTData.SellLog.FindAll(m => m.Market == marketData.Key).Count;
              double profitFiat = Math.Round(marketData.Value * Model.Summary.MainMarketPrice, 2);
              double profitFiatPerTrade = Math.Round(profitFiat / trades, 2);
              <tr>
                <td>@marketRank</td>
                <td><a href="@Core.Helper.SystemHelper.GetMarketLink(Model.PTMagicConfiguration.GeneralSettings.Monitor.LinkPlatform, Model.PTMagicConfiguration.GeneralSettings.Application.Exchange, marketData.Key, Model.Summary.MainMarket)" target="_blank">@marketData.Key</a></td>
                <td class="text-right">@trades</td>
                <td class="text-right text-autocolor-saw">@marketData.Value.ToString("#,#0.00000000", new System.Globalization.CultureInfo("en-US"))</td>
                <td class="text-right text-autocolor-saw">@profitFiat.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")) @Model.Summary.MainFiatCurrency</td>
                <td class="text-right text-autocolor-saw">@profitFiatPerTrade.ToString("#,#0.00", new System.Globalization.CultureInfo("en-US")) @Model.Summary.MainFiatCurrency</td>
                <td class="text-right text-autocolor-saw">@Model.PTData.SellLog.FindAll(m => m.Market == marketData.Key).Average(p => p.ProfitPercent).ToString("#,#0.00", new System.Globalization.CultureInfo("en-US"))%</td>
              </tr>
            }
          </tbody>
        </table>
      </div>
    </div>
  </div>

  <div id="salesList" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true" style="display: none;">
    <div class="modal-dialog modal-full">
      <div class="modal-content">
        <i class="fa fa-circle-o-notch fa-spin fa-3x fa-fw"></i>
      </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
  </div><!-- /.modal -->
}

@section Scripts {
  <script src="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)assets/plugins/tablesaw/js/tablesaw.js"></script>
  <script src="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)assets/plugins/d3/d3.min.js"></script>
  <script src="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)assets/plugins/nvd3/nv.d3.min.js"></script>

  <script src="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)assets/plugins/tablesaw/js/tablesaw-init.js"></script>

  <!-- Required datatable js -->
  <script src="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)assets/plugins/datatables/jquery.dataTables.min.js"></script>
  <script src="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)assets/plugins/datatables/dataTables.bootstrap4.min.js"></script>
  <!-- Buttons examples -->
  <script src="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)assets/plugins/datatables/dataTables.buttons.min.js"></script>
  <script src="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)assets/plugins/datatables/buttons.bootstrap4.min.js"></script>
  <script src="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)assets/plugins/datatables/jszip.min.js"></script>
  <script src="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)assets/plugins/datatables/pdfmake.min.js"></script>
  <script src="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)assets/plugins/datatables/vfs_fonts.js"></script>
  <script src="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)assets/plugins/datatables/buttons.html5.min.js"></script>
  <script src="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)assets/plugins/datatables/buttons.print.min.js"></script>
  <script src="@Html.Raw(Model.PTMagicConfiguration.GeneralSettings.Monitor.RootUrl)assets/plugins/datatables/buttons.colVis.min.js"></script>

  <script type="text/javascript">
  (function ($) {
    'use strict';
    nv.addGraph(function () {
      var lineChart = nv.models.lineChart();
      var height = 300;
      /**/
      var chartData = @Html.Raw(Model.TradesChartDataJSON);
      /**/
      lineChart.useInteractiveGuideline(true);
      lineChart.xAxis.tickFormat(function (d) { return d3.time.format('%Y/%m/%d')(new Date(d)); });
      lineChart.yAxis.axisLabel('Daily Sales').tickFormat(d3.format(','));
      d3.select('.trades-chart svg').attr('perserveAspectRatio', 'xMinYMid').datum(chartData).transition().duration(500).call(lineChart);
      nv.utils.windowResize(lineChart.update);

      return lineChart;
    });

    nv.addGraph(function () {
      var lineChart = nv.models.lineChart();
      var height = 300;
      /**/
      var chartData = @Html.Raw(Model.ProfitChartDataJSON);
      /**/
      lineChart.useInteractiveGuideline(true);
      lineChart.xAxis.tickFormat(function (d) { return d3.time.format('%Y/%m/%d')(new Date(d)); });
      lineChart.yAxis.axisLabel('Daily Profit').tickFormat(d3.format(',.2f'));
      d3.select('.profit-chart svg').attr('perserveAspectRatio', 'xMinYMid').datum(chartData).transition().duration(500).call(lineChart);
      nv.utils.windowResize(lineChart.update);

      return lineChart;
    });

    nv.addGraph(function () {
      var lineChart = nv.models.lineChart();
      var height = 400;
      /**/
      var chartData = @Html.Raw(Model.BalanceChartDataJSON);
      /**/
      lineChart.useInteractiveGuideline(true);
      lineChart.xAxis.tickFormat(function (d) { return d3.time.format('%Y/%m/%d')(new Date(d)); });
      lineChart.yAxis.axisLabel('Profit').tickFormat(d3.format(',.2f'));
      d3.select('.balance-chart svg').attr('perserveAspectRatio', 'xMinYMid').datum(chartData).transition().duration(500).call(lineChart);
      nv.utils.windowResize(lineChart.update);

      return lineChart;
    });

    $("#salesList").on("show.bs.modal", function (e) {
      $(this).find(".modal-content").html('<i class="fa fa-circle-o-notch fa-spin fa-3x fa-fw"></i>');
      var link = $(e.relatedTarget);
      $(this).find(".modal-content").load(link.attr("href"), function () {
        $('.text-autocolor').autocolor(false);
      });
    });
  })(jQuery);


  </script>
}