<?php if(!defined('PARENT')) { exit; } ?>

    <div id="sparams"<?php echo (isset($_GET['fname']) ? ' style="display:none"' : ''); ?>>
      <div class="row">
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
          <ul class="nav nav-tabs">
            <li class="active"><a href="#one" data-toggle="tab"><i class="fa fa-user fa-fw"></i> <span class="hidden-xs"><?php echo $msw_adm_aff_search2; ?></span></a></li>
            <li><a href="#two" data-toggle="tab"><i class="fa fa-file-text-o fa-fw"></i> <span class="hidden-xs"><?php echo $msw_adm_aff_search3; ?></span></a></li>
            <li><a href="#three" data-toggle="tab"><i class="fa fa-cog fa-fw"></i> <span class="hidden-xs"><?php echo $msw_adm_aff_search4; ?></span></a></li>
          </ul>
        </div>
      </div>

      <form method="get" action="index.php">
      <div class="row formarea">
        <input type="hidden" name="p" value="<?php echo $cmd; ?>">
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
          <div class="tab-content">
            <div class="tab-pane active in" id="one">
              <div class="panel panel-default">
                <div class="panel-body">
                  <div class="form-group">
                    <label><?php echo $msw_adm_aff_search5; ?></label>
                    <input type="text" class="form-control" name="fname" value="<?php echo (isset($_GET['fname']) ? mswSH($_GET['fname']) : ''); ?>">
                  </div>
                  <div class="form-group">
                    <label><?php echo $msw_adm_aff_search6; ?></label>
                    <input type="text" class="form-control" name="lname" value="<?php echo (isset($_GET['lname']) ? mswSH($_GET['lname']) : ''); ?>">
                  </div>
                  <div class="form-group">
                    <label><?php echo $msw_adm_aff_search7; ?></label>
                    <input type="text" class="form-control" name="email" value="<?php echo (isset($_GET['email']) ? mswSH($_GET['email']) : ''); ?>">
                  </div>
                </div>
              </div>
            </div>
            <div class="tab-pane fade" id="two">
              <div class="panel panel-default">
                <div class="panel-body">
                  <div class="form-group">
                    <label><?php echo $msw_adm_aff_search8; ?></label>
                    <div class="form-group input-group" style="margin-bottom:5px">
                      <span class="input-group-addon"><i class="fa fa-calendar fa-fw"></i></span>
                      <input type="text" class="form-control mswdatepicker" name="tsf" value="<?php echo (isset($_GET['tsf']) ? mswSH($_GET['tsf']) : ''); ?>">
                    </div>
                    <div class="form-group input-group">
                      <span class="input-group-addon"><i class="fa fa-calendar fa-fw"></i></span>
                      <input type="text" class="form-control mswdatepicker" name="tst" value="<?php echo (isset($_GET['tst']) ? mswSH($_GET['tst']) : ''); ?>">
                    </div>
                  </div>
                  <div class="form-group">
                    <label><?php echo $msw_adm_aff_search9; ?></label>
                    <input type="text" class="form-control" name="payinfo" value="<?php echo (isset($_GET['payinfo']) ? mswSH($_GET['payinfo']) : ''); ?>">
                  </div>
                  <div class="form-group">
                    <label><?php echo $msw_adm_aff_search10; ?></label>
                    <input type="text" class="form-control" name="notes" value="<?php echo (isset($_GET['notes']) ? mswSH($_GET['notes']) : ''); ?>">
                  </div>
                  <div class="form-group">
                    <label><?php echo $msw_adm_aff_search11; ?></label>
                    <input type="text" class="form-control" name="ip" value="<?php echo (isset($_GET['ip']) ? mswSH($_GET['ip']) : ''); ?>">
                  </div>
                  <div class="form-group">
                    <label><?php echo $msw_adm_aff_search17; ?></label>
                    <input type="text" class="form-control" name="code" value="<?php echo (isset($_GET['code']) ? mswSH($_GET['code']) : ''); ?>">
                  </div>
                  <div class="form-group">
                    <label><?php echo $msw_adm_aff_search12; ?></label>
                    <input type="text" class="form-control" name="commtier" value="<?php echo (isset($_GET['commtier']) ? mswSH($_GET['commtier']) : ''); ?>">
                  </div>
                </div>
              </div>
            </div>
            <div class="tab-pane fade" id="three">
              <div class="panel panel-default">
                <div class="panel-body">
                  <div class="form-group">
                    <label><?php echo $msw_adm_aff_search13; ?></label>
                    <select name="sort" class="form-control">
                      <?php
                      foreach ($msw_adm_aff_search_sort AS $k => $v) {
                      ?>
                      <option value="<?php echo $k; ?>"<?php echo (isset($_GET['sort']) && $_GET['sort'] == $k ? ' selected="selected"' : ''); ?>><?php echo $v; ?></option>
                      <?php
                      }
                      ?>
                    </select>
                  </div>
                  <div class="form-group">
                    <label><?php echo $msw_adm_aff_search18; ?></label>
                    <input type="text" class="form-control" name="show" value="<?php echo (isset($_GET['show']) ? (int) $_GET['show'] : PER_PAGE); ?>">
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="act-button">
        <button type="submit" class="btn btn-success"><i class="fa fa-search fa-fw"></i> <span class="hidden-xs"><?php echo $msw_adm_aff_search; ?></span></button>
      </div>
      </form>
    </div>

    <?php
    // Search results..
    ?>
    <div id="sresults"<?php echo (!isset($_GET['fname']) ? ' style="display:none"' : ''); ?>>
      <?php
      if (!empty($search_sql) && isset($_GET['fname'])) {
        $Q  = $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`
              " . implode(mswNL(), $search_sql) . "
              ORDER BY $orderBy
              LIMIT $limit," . (defined('PER_PAGE_SET') ? PER_PAGE_SET : PER_PAGE));
        $NR = $DB->db_foundrows($Q);
        ?>
        <div class="row">
          <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 text-right">
            <div class="btn-group">
              <button type="button" class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="fa fa-chevron-down fa-fw"></i>
              </button>
              <ul class="dropdown-menu dropdown-menu-right">
                <li><a href="#" onclick="mswNewSearch();return false"><i class="fa fa-search fa-fw"></i> <?php echo $msw_adm_aff_search14; ?></a></li>
                <?php
                if ($NR > 0) {
                ?>
                <li><a href="#" onclick="mswExp('<?php echo (isset($_SERVER['QUERY_STRING']) ? '&amp;' . mswSH($_SERVER['QUERY_STRING']) : ''); ?>','aff-export');return false"><i class="fa fa-save fa-fw"></i> <?php echo $msw_adm_aff_search16; ?></a></li>
                <?php
                }
                ?>
              </ul>
            </div>
          </div>
        </div>

        <div class="managepanel" style="margin-top:10px">
          <form method="post" action="?p=<?php echo $cmd; ?>">
          <div class="panel panel-default managearea">
            <div class="panel-heading">
              <?php echo $msw_adm_aff_search15; ?> (<?php echo mswNFM($NR); ?>)
            </div>
            <div class="panel-body">
              <div class="table-responsive">
               <table class="table table-striped table-hover" id="sortTable">
                 <thead>
                   <tr>
                     <th class="text-center"><input type="checkbox" name="c" value="yes" onclick="mswSC('.managearea .table td','delb')"></th>
                     <th data-sort="string"><?php echo $msw_adm_aff_manage15; ?></th>
                     <th data-sort="float"><?php echo $msw_adm_aff_manage12; ?></th>
                     <th data-sort="float"><?php echo $msw_adm_aff_manage13; ?></th>
                     <th data-sort="float"><?php echo $msw_adm_aff_manage21; ?></th>
                     <th data-sort="int"><?php echo $msw_adm_aff_manage14; ?></th>
                     <th data-sort="string"><?php echo $msw_common18; ?></th>
                     <th>&nbsp;</th>
                   </tr>
                 </thead>
                 <tbody>
                   <?php
                   if ($NR > 0) {
                     while ($AF = $DB->db_object($Q)) {
                       $due = mswFPCE($AF->affCommissions - $AF->affPayments);
                       if ($due < 1) {
                        $due = '0.00';
                       }
                       include(PATH . 'content/list-screens/aff-manage.php');
                     }
                   } else {
                     $colspan = 8;
                     include(PATH . 'content/no-data.php');
                   }
                   ?>
                 </tbody>
               </table>
             </div>
             <?php
             // Pagination..
             if ($NR > (defined('PER_PAGE_SET') ? PER_PAGE_SET : PER_PAGE)) {
               $PGS = new pages(array(
                 'count' => $NR,
                 'text' => $msw_pages,
                 'page' => $page,
                 'admin' => 'yes',
                 'flag' => '',
                 'limit' => (defined('PER_PAGE_SET') ? PER_PAGE_SET : PER_PAGE),
                 's' => $SETTINGS
               ));
               echo $PGS->display();
             }
             ?>
            </div>
          </div>
          <?php
          if ($NR > 0) {
          ?>
          <div class="act-button">
            <div class="text-center delselected" id="delb_span" style="display:none"><span>0</span> <?php echo $msw_public_11_global[0]; ?></div>
            <button type="button" class="btn btn-danger disabled" id="delb" onclick="if(jQuery(this).attr('class')!='btn btn-danger disabled'){mswAction('aff-manage-del','.managepanel','no','yes')}"><i class="fa fa-times fa-fw"></i> <span class="hidden-xs"><?php echo $msw_common3; ?></span></button>
          </div>
          <?php
          }
          ?>
          </form>
        </div>
        <?php
      } else {
      ?>
      <div class="panel panel-default managearea">
        <div class="panel-heading">
          <?php echo $msw_adm_aff_search15; ?> (0)
        </div>
        <div class="panel-body" style="padding-bottom:20px">
          <?php echo $msw_adm_aff_search19; ?>
          <hr>
          <button type="button" class="btn btn-default" onclick="window.location='?p=aff-search'"><i class="fa fa-search fa-fw"></i> <?php echo $msw_adm_aff_search14; ?></button>
        </div>
      </div>
      <?php
      }
      ?>

    </div>