<?php if(!defined('PARENT')) { exit; }
include(PATH . 'control/classes/class.graph.php');
$curYearEnd = date('Y', $DT->ts());
$curYear = $curYearEnd;
$lastYear = $DT->totime('last year');
// Check for variable tampering
if (isset($_GET['y1'], $_GET['y2']) && strlen($_GET['y1']) == 4 && strlen($_GET['y2']) == 4) {
  $lYear = preg_replace('/[^0-9]/', '', $_GET['y1']);
  $cYear = preg_replace('/[^0-9]/', '', $_GET['y2']);
  if (checkdate(01,01,$cYear) && checkdate(01,01,$lYear)) {
    $lastYear = strtotime('01-01-' . $lYear);
    $curYear = $cYear;
  }
}
$G = new graph();
$G->years = array(date('Y', $lastYear), $curYear);
$G->dt = $DT;
$gDisplay = $G->display();
$firstYear = $G->firstYear();
define('DASHBOARD', 1);
?>
<div class="row">
  <div class="col-lg-8 col-md-7">
    <div class="panel panel-default">
      <div class="panel-heading">
        <span class="pull-right"><a href="#" onclick="mswTogHomeGraph('open')"><i class="fa fa-calendar fa-fw"></i></a></span>
        <i class="fa fa-bar-chart fa-fw"></i> <?php echo str_replace(array('{lastyear}','{year}'), array(date('Y', $lastYear), $curYear), $msw_adm_dashboard2); ?></div>
      <div class="panel-body msw-graph">
        <div class="ct-chart"></div>
      </div>
      <div class="panel-body msw-graph-years" style="display:none">
        <form method="get" action="index.php">
        <div class="form-group">
          <label><?php echo $msw_11_admin[6]; ?></label>
          <select name="y1" class="form-control">
            <?php
            for($i=(isset($firstYear->dY) ? ($firstYear->dY == $curYearEnd ? ($curYearEnd - 1) : $firstYear->dY) : ($curYearEnd - 1)); $i<($curYearEnd+1); $i++) {
            ?>
            <option value="<?php echo $i; ?>"<?php echo (isset($lYear) && $i==$lYear ? ' selected="selected"' : (!isset($lYear) && $i==($curYearEnd-1) ? ' selected="selected"' : '')); ?>><?php echo $i; ?></option>
            <?php
            }
            ?>
          </select>
        </div>
        <div class="form-group">
          <label><?php echo $msw_11_admin[7]; ?></label>
          <select name="y2" class="form-control">
            <?php
            for($i=(isset($firstYear->dY) ? $firstYear->dY : $curYearEnd); $i<($curYearEnd+1); $i++) {
            ?>
            <option value="<?php echo $i; ?>"<?php echo (isset($cYear) && $i==$cYear ? ' selected="selected"' : (!isset($cYear) && $i==$curYearEnd ? ' selected="selected"' : '')); ?>><?php echo $i; ?></option>
            <?php
            }
            ?>
          </select>
        </div>
        <div class="form-group">
          <button class="btn btn-primary" type="submit"><i class="fa fa-check fa-fw"></i> <?php echo $msw_common5; ?></button> <button type="button" onclick="mswTogHomeGraph('close')" class="btn btn-default"><i class="fa fa-times fa-fw"></i></button>
        </div>
        </form>
      </div>
    </div>
  </div>
  <div class="col-lg-4 col-md-5">
    <div class="form-group">
      <form method="get" action="index.php" id="sform">
      <input type="hidden" name="p" value="aff-search">
      <input type="hidden" name="dash" value="yes">
      <div class="form-group">
        <div class="form-group input-group">
          <input type="text" class="form-control" name="fname" placeholder="<?php echo mswSH($msw_adm_dashboard4); ?>">
          <span class="input-group-addon"><i class="fa fa-search fa-fw cursorp" onclick="mswSubmit('sform','fname');return false"></i></span>
        </div>
      </div>
      </form>
    </div>
    <div class="panel panel-default dashtable">
      <div class="panel-body">
        <?php
        // Totals..
        $tot_aff = $DB->db_rowcount('affiliates','WHERE `en` = \'yes\' AND `approved` = \'yes\'');
        $tot_app_aff = $DB->db_rowcount('affiliates','WHERE `en` = \'yes\' AND `approved` = \'no\'');
        $com_app = $DB->db_rowcount('affiliates_commissions','WHERE `en` = \'yes\' AND `approved` = \'no\'');
        ?>
        <div class="table-responsive">
          <table class="table table-striped table-hover">
          <tbody>
            <tr>
              <td><i class="fa fa-users fa-fw"></i> <?php echo $msw_adm_dashboard; ?></td>
              <td class="text-right"><a href="?p=aff-manage"><?php echo $tot_aff; ?></a></td>
            </tr>
            <tr>
              <td><i class="fa fa-user-o fa-fw"></i> <?php echo $msw_adm_dashboard13; ?></td>
              <td class="text-right"><a href="?p=aff-approve"><?php echo $tot_app_aff; ?></a></td>
            </tr>
            <tr>
              <td><i class="fa fa-line-chart fa-fw"></i> <?php echo $msw_adm_dashboard3; ?></td>
              <td class="text-right"><a href="?p=commissions-manage"><?php echo mswCRFMT(mswFPCE($DB->db_sum('affiliates_commissions', 'saletotal', 'WHERE `en` = \'yes\' AND `approved` = \'yes\''), true), $SETTINGS); ?></a></td>
            </tr>
            <tr>
              <td><i class="fa fa-money fa-fw"></i> <?php echo $msw_adm_dashboard15; ?></td>
              <td class="text-right"><a href="?p=commissions-manage"><?php echo mswCRFMT(mswFPCE($DB->db_sum('affiliates_commissions', 'commtotal', 'WHERE `en` = \'yes\' AND `approved` = \'yes\''), true), $SETTINGS); ?></a></td>
            </tr>
            <tr>
              <td><i class="fa fa-check fa-fw"></i> <?php echo $msw_adm_dashboard12; ?></td>
              <td class="text-right"><a href="?p=payments-manage"><?php echo mswCRFMT(mswFPCE($DB->db_sum('affiliates_payments', 'payamount'), true), $SETTINGS); ?></a></td>
            </tr>
            <tr>
              <td><i class="fa fa-search fa-fw"></i> <?php echo $msw_adm_dashboard14; ?></td>
              <td class="text-right"><a href="?p=commissions-approve"><?php echo $com_app; ?></a></td>
            </tr>
          </tbody>
          </table>
        </div>
      </div>
    </div>
    <div class="row dashbuttons">
      <div class="col-lg-6 col-md-6 addbutton">
        <button class="btn btn-primary btn-lg btn-block" type="button" onclick="mswWin('aff-add')"><i class="fa fa-plus-circle fa-fw"></i> <?php echo $msw_adm_dashboard7; ?></button>
      </div>
      <div class="col-lg-6 col-md-6 salebutton">
        <button class="btn btn-info btn-lg btn-block" type="button" onclick="mswWin('payments-add')"><i class="fa fa-plus-circle fa-fw"></i> <?php echo $msw_adm_dashboard8; ?></button>
      </div>
    </div>
  </div>
</div>

<?php
if (DASHBOARD_LATEST_COMMISSIONS > 0) {
?>
<div class="row">
  <div class="col-lg-12">
    <div class="panel panel-default">
      <div class="panel-heading">
        <span class="pull-right">
          <a href="?p=commissions-search"><i class="fa fa-search fa-fw"></i></a>
          <a href="?p=commissions-add"><i class="fa fa-plus fa-fw"></i></a>
        </span>
        <i class="fa fa-line-chart fa-fw"></i> <?php echo str_replace('{count}', DASHBOARD_LATEST_COMMISSIONS, $msw_adm_dashboard9); ?>
      </div>
      <div class="panel-body">
        <div class="table-responsive">
           <table class="table table-striped table-hover">
             <thead>
               <tr>
                 <th><?php echo $msw_adm_commissions_manage7; ?></th>
                 <th><?php echo $msw_adm_commissions_manage8; ?></th>
                 <th><?php echo $msw_adm_commissions_manage9; ?></th>
                 <th><?php echo $msw_adm_commissions_manage10; ?></th>
                 <th>&nbsp;</th>
               </tr>
             </thead>
             <tbody>
             <?php
             $Q  = $DB->db_query("SELECT SQL_CALC_FOUND_ROWS *,
                   (SELECT CONCAT(`fname`,' ',`lname`) FROM `" . DB_PREFIX . "affiliates` WHERE `" . DB_PREFIX . "affiliates`.`id` = `" . DB_PREFIX . "affiliates_commissions`.`affid`) AS `affName`,
                   (SELECT `name` FROM `" . DB_PREFIX . "products` WHERE `" . DB_PREFIX . "products`.`id` = `" . DB_PREFIX . "affiliates_commissions`.`product`) AS `productName`
                   FROM `" . DB_PREFIX . "affiliates_commissions`
                   WHERE (`en` = 'yes' AND `approved` = 'yes')
                   ORDER BY `id` DESC
                   LIMIT " . DASHBOARD_LATEST_COMMISSIONS);
             $NR = $DB->db_foundrows($Q);
             if ($NR > 0) {
               while ($CM = $DB->db_object($Q)) {
                 include(PATH . 'content/list-screens/commissions-manage.php');
               }
             } else {
               $colspan = 5;
               include(PATH . 'content/no-data.php');
             }
             ?>
             </tbody>
           </table>
         </div>
      </div>
    </div>
  </div>
</div>
<?php
}

if (DASHBOARD_LATEST_PAYMENTS > 0) {
?>
<div class="row">
  <div class="col-lg-12">
    <div class="panel panel-default">
      <div class="panel-heading">
        <span class="pull-right">
          <a href="?p=payments-search"><i class="fa fa-search fa-fw"></i></a>
          <a href="?p=payments-add"><i class="fa fa-plus fa-fw"></i></a>
        </span>
        <i class="fa fa-money fa-fw"></i> <?php echo str_replace('{count}', DASHBOARD_LATEST_PAYMENTS, $msw_adm_dashboard10); ?>
      </div>
      <div class="panel-body">
        <div class="table-responsive">
           <table class="table table-striped table-hover">
             <thead>
               <tr>
                 <th><?php echo $msw_adm_payments_manage7; ?></th>
                 <th><?php echo $msw_adm_payments_manage8; ?></th>
                 <th><?php echo $msw_adm_payments_manage9; ?></th>
                 <th><?php echo $msw_adm_payments_manage10; ?></th>
                 <th>&nbsp;</th>
               </tr>
             </thead>
             <tbody>
             <?php
             $Q2  = $DB->db_query("SELECT SQL_CALC_FOUND_ROWS *,
                   (SELECT CONCAT(`fname`,' ',`lname`) FROM `" . DB_PREFIX . "affiliates` WHERE `" . DB_PREFIX . "affiliates`.`id` = `" . DB_PREFIX . "affiliates_payments`.`affid`) AS `affName`
                   FROM `" . DB_PREFIX . "affiliates_payments`
                   ORDER BY `ts` DESC,`id` DESC
                   LIMIT " . DASHBOARD_LATEST_PAYMENTS);
             $NR2 = $DB->db_foundrows($Q2);
             if ($NR2 > 0) {
               while ($PY = $DB->db_object($Q2)) {
                 include(PATH . 'content/list-screens/payments-manage.php');
               }
             } else {
               $colspan = 5;
               include(PATH . 'content/no-data.php');
             }
             ?>
             </tbody>
           </table>
         </div>
      </div>
    </div>
  </div>
</div>
<?php
}

if (DASHBOARD_LATEST_AFFILIATES > 0) {
?>
<div class="row">
  <div class="col-lg-12">
    <div class="panel panel-default">
      <div class="panel-heading">
        <span class="pull-right">
          <a href="?p=aff-search"><i class="fa fa-search fa-fw"></i></a>
          <a href="?p=aff-add"><i class="fa fa-plus fa-fw"></i></a>
        </span>
        <i class="fa fa-users fa-fw"></i> <?php echo str_replace('{count}', DASHBOARD_LATEST_AFFILIATES, $msw_adm_dashboard11); ?>
      </div>
      <div class="panel-body">
        <div class="table-responsive">
           <table class="table table-striped table-hover">
             <thead>
               <tr>
                 <th><?php echo $msw_adm_aff_manage15; ?></th>
                 <th><?php echo $msw_adm_aff_manage12; ?></th>
                 <th><?php echo $msw_adm_aff_manage13; ?></th>
                 <th><?php echo $msw_adm_aff_manage21; ?></th>
                 <th><?php echo $msw_adm_aff_manage14; ?></th>
                 <th><?php echo $msw_common18; ?></th>
                 <th>&nbsp;</th>
               </tr>
             </thead>
             <tbody>
             <?php
             $Q3  = $DB->db_query("SELECT SQL_CALC_FOUND_ROWS *,
                    (SELECT ROUND(SUM(`commtotal`),2) FROM `" . DB_PREFIX . "affiliates_commissions` WHERE `" . DB_PREFIX . "affiliates_commissions`.`affid` = `" . DB_PREFIX . "affiliates`.`id` AND `" . DB_PREFIX . "affiliates_commissions`.`en` = 'yes' AND `" . DB_PREFIX . "affiliates_commissions`.`approved` = 'yes') AS `affCommissions`,
                    (SELECT ROUND(SUM(`payamount`),2) FROM `" . DB_PREFIX . "affiliates_payments` WHERE `" . DB_PREFIX . "affiliates_payments`.`affid` = `" . DB_PREFIX . "affiliates`.`id`) AS `affPayments`,
                    (SELECT SUM(`count`) FROM `" . DB_PREFIX . "affiliates_tracking` WHERE `" . DB_PREFIX . "affiliates_tracking`.`affid` = `" . DB_PREFIX . "affiliates`.`id`) AS `affReferrals`
                    FROM `" . DB_PREFIX . "affiliates`
                    WHERE `approved` = 'yes'
                    ORDER BY `fname`,`lname`
                    LIMIT " . DASHBOARD_LATEST_AFFILIATES);
             $NR3 = $DB->db_foundrows($Q3);
             if ($NR3 > 0) {
               while ($AF = $DB->db_object($Q3)) {
                 $due = mswFPCE($AF->affCommissions - $AF->affPayments);
                 if ($due < 1) {
                   $due = '0.00';
                 }
                 include(PATH . 'content/list-screens/aff-manage.php');
               }
             } else {
               $colspan = 7;
               include(PATH . 'content/no-data.php');
             }
             ?>
             </tbody>
           </table>
         </div>
      </div>
    </div>
  </div>
</div>
<?php
}
?>