<?php
/**
 * The browse view file of depositor module of ZDOO.
 *
 * @copyright   Copyright 2009-2018 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
 * @license     ZPL (http://zpl.pub/page/zplv12.html)
 * @author      Tingting Dai <daitingting@xirangit.com>
 * @package     depositor
 * @version     $Id$
 * @link        http://www.zdoo.com
 */
?>
<?php include '../../common/view/header.html.php';?>
<div id='menuActions'>
  <div class='btn-group'>
    <?php echo html::a("javascript:;", "<i class='icon icon-grid'></i>", "data-mode='card' class='mode-toggle btn'");?>
    <?php echo html::a("javascript:;", "<i class='icon icon-list'></i>", "data-mode='list' class='mode-toggle btn'");?>
  </div>
  <?php commonModel::printLink('depositor', 'export', '', $lang->exportIcon . $lang->export, "class='iframe btn btn-secondary' data-width='700'");?></li>
  <?php commonModel::printLink('depositor', 'create', '', "<i class='icon-plus'></i> {$lang->depositor->create}", "class='btn btn-primary' data-toggle='modal'")?>
</div>
<div class='panel'>
  <div class='panel-body hide' id='cardMode'>
    <div class='cards no-padding'>
      <?php foreach($depositors as $depositor):?>
      <div class='col-md-4 col-sm-6'>
        <div class='card card-depositor'>
          <div class='card-heading <?php echo $depositor->type;?>'>
            <div class='info'><span class='label' title='<?php echo $lang->depositor->type?>'><i class='icon'></i> <?php echo $lang->depositor->typeList[$depositor->type]?></span></div>
            <h4 class='title'><?php echo $depositor->abbr;?></h4>
            <div class='subtitle'>
              <?php if($depositor->type != 'cash' && !empty($depositor->title) && $depositor->title != $depositor->abbr):?>
              <span class='text-muted' title='<?php echo $lang->depositor->title;?>'><?php echo $depositor->title;?></span>
              <?php endif;?>
            </div>
          </div>
          <div class='card-caption row' data-id="<?php echo $depositor->id;?>">
            <?php if($depositor->type != 'cash'):?>
            <?php if($depositor->type == 'bank') echo "<dl class='dl-horizontal'><dt>{$lang->depositor->bankProvider} {$lang->colon} </dt><dd>$depositor->provider </dd></dl>";?>
            <?php if($depositor->type == 'online') echo "<dl class='dl-horizontal'><dt>{$lang->depositor->serviceProvider} {$lang->colon} </dt><dd>{$lang->depositor->providerList[$depositor->provider]} </dd></dl>";?>
            <?php echo "<dl class='dl-horizontal'><dt>{$lang->depositor->account} {$lang->colon} </dt><dd>$depositor->account</dd></dl>";?>
            <?php if($depositor->type == 'bank') echo "<dl class='dl-horizontal'><dt>{$lang->depositor->bankcode} {$lang->colon} </dt><dd>$depositor->bankcode</dd></dl>";?>
            <?php endif;?>
            <?php if(($app->user->admin == 'super' or isset($app->user->rights['balance']['browse'])) and isset($balances[$depositor->currency][$depositor->id])):?>
            <?php if($balances[$depositor->currency][$depositor->id]->money == 0):?>
            <?php $savedBalance = $balances[$depositor->currency][$depositor->id]->money;?>
            <?php else:?>
            <?php $savedBalance = formatMoney($balances[$depositor->currency][$depositor->id]->money);?>
            <?php endif;?>
            <?php if($depositor->computed == 0):?>
            <?php $computedBalance = $depositor->computed;?>
            <?php else:?>
            <?php $computedBalance = formatMoney($depositor->computed);?>
            <?php endif;?>
            <?php $currencySymbol = zget($lang->currencySymbols, $depositor->currency);?>
            <?php $lenA  = strlen(strval($savedBalance))    - substr_count(strval($savedBalance), ',')    + (strpos(strval($savedBalance), '.')    === false ? 1 : 0);?>
            <?php $lenB  = strlen(strval($computedBalance)) - substr_count(strval($computedBalance), ',') + (strpos(strval($computedBalance), '.') === false ? 1 : 0);?>
            <?php $lenC  = iconv_strlen(strval($currencySymbol), $config->charset);?>
            <?php $width = ($lenA > $lenB ? 10 * $lenA : 10 * $lenB) + 10 * $lenC + 10;?>
            <dl class='dl-horizontal'>
              <dt class='balance-label'><?php echo $lang->depositor->saveBalance . $lang->colon;?></dt>
              <dd class='balance-value text-success text-right' style='width: <?php echo $width;?>px'>
                <?php echo $currencySymbol;?>
                <?php echo $savedBalance;?>
              </dd>
            </dl>
            <dl class='dl-horizontal'>
              <dt class='balance-label'><?php echo $lang->depositor->computedValue . $lang->colon;?></dt>
              <dd class='balance-value text-danger text-right' style='width: <?php echo $width;?>px'>
                <?php echo zget($lang->currencySymbols, $depositor->currency);?>
                <?php echo $computedBalance;?>
              </dd>
            </dl>
            <?php endif;?>
          </div>
          <div class='card-actions'>
            <div class='pull-right'>
              <?php commonModel::printLink('depositor', 'edit', "depositorID=$depositor->id", $lang->edit, "data-toggle='modal'");?>
              <?php commonModel::printLink('depositor', 'check', "depositorID=$depositor->id", $lang->depositor->check);?>
              <?php if($depositor->status == 'normal') commonModel::printLink('depositor', 'forbid', "depositorID=$depositor->id", $lang->depositor->forbid, "data-toggle=modal");?>
              <?php if($depositor->status == 'disable') commonModel::printLink('depositor', 'activate', "depositorID=$depositor->id", $lang->depositor->activate, "data-toggle=modal");?>
              <?php commonModel::printLink('balance', 'browse', "depositorID=$depositor->id", $lang->depositor->balance, "data-toggle='modal'");?>
              <?php if(empty($usedDepositors[$depositor->id])) commonModel::printLink('depositor', 'delete', "depositorID=$depositor->id", $lang->delete, "class='deleter'");?>
            </div>
            <?php echo "<span class='text-" . ($depositor->status == 'normal' ? 'success': 'danger') . "'>{$lang->depositor->statusList[$depositor->status]}</span>";?>
          </div>
        </div>
      </div>
      <?php endforeach;?>
    </div>
  </div>
  <div class='main-table hide' id='listMode'>
    <table class='table has-sort-head'>
      <thead>
        <tr class='text-center'>
          <?php $vars = "tag={$currentTag}&status={$status}&orderBy=%s&recTotal={$pager->recTotal}&recPerPage={$pager->recPerPage}&pageID={$pager->pageID}";?>
          <th class='w-50px'> <?php commonModel::printOrderLink('id',     $orderBy, $vars, $lang->depositor->id);?></th>
          <th class='w-160px'><?php commonModel::printOrderLink('abbr',   $orderBy, $vars, $lang->depositor->abbr);?></th>
          <th><?php commonModel::printOrderLink('provider', $orderBy, $vars, $lang->depositor->bankProvider . $lang->slash . $lang->depositor->serviceProvider);?></th>
          <th class='w-150px'><?php commonModel::printOrderLink('title', $orderBy, $vars, $lang->depositor->title);?></th>
          <th class='w-140px'><?php commonModel::printOrderLink('account', $orderBy, $vars, $lang->depositor->account);?></th>
          <th class='w-100px'><?php commonModel::printOrderLink('bankcode', $orderBy, $vars, $lang->depositor->bankcode);?></th>
          <?php if($app->user->admin == 'super' or isset($app->user->rights['balance']['browse'])):?>
          <th class='text-right w-80px'><?php echo $lang->depositor->saveBalance;?></th>
          <th class='text-right w-80px'><?php echo $lang->depositor->computedValue;?></th>
          <?php endif;?>
          <th class='w-70px'> <?php commonModel::printOrderLink('type',   $orderBy, $vars, $lang->depositor->type);?></th>
          <th class='w-70px'> <?php commonModel::printOrderLink('status', $orderBy, $vars, $lang->depositor->status);?></th>
          <th class='w-<?php echo $lang->depositor->actionWidth;?>px'><?php echo $lang->actions;?></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach($depositors as $depositor):?>
        <?php
        $savedBalance = $computedBalance = '';
        if(isset($balances[$depositor->currency][$depositor->id]))
        {
            if($balances[$depositor->currency][$depositor->id]->money == 0)
            {
                $savedBalance = $balances[$depositor->currency][$depositor->id]->money;
            }
            else
            {
                $savedBalance = formatMoney($balances[$depositor->currency][$depositor->id]->money);
            }

            if($depositor->computed == 0)
            {
                $computedBalance = $depositor->computed;
            }
            else
            {
                $computedBalance = formatMoney($depositor->computed);
            }
        }
        ?>
        <tr class='text-center' data-id="<?php echo $depositor->id;?>">
          <td><?php echo $depositor->id;?> </td>
          <td title='<?php echo $depositor->abbr;?>'><?php echo $depositor->abbr;?> </td>
          <td title='<?php echo zget($lang->depositor->providerList, $depositor->provider);?>'><?php echo zget($lang->depositor->providerList, $depositor->provider);?> </td>
          <td title='<?php echo $depositor->title;?>'><?php echo $depositor->title;?> </td>
          <td title='<?php echo $depositor->account;?>'><?php echo $depositor->account;?> </td>
          <td title='<?php echo $depositor->bankcode;?>'><?php echo $depositor->bankcode;?> </td>
          <?php if($app->user->admin == 'super' or isset($app->user->rights['balance']['browse'])):?>
          <td class='text-right'><?php echo $savedBalance ? zget($lang->currencySymbols, $depositor->currency) . $savedBalance : '';?></td>
          <td class='text-right'><?php echo $computedBalance ? zget($lang->currencySymbols, $depositor->currency) . $computedBalance : '';?></td>
          <?php endif;?>
          <td><?php echo zget($lang->depositor->typeList, $depositor->type);?> </td>
          <td><?php echo zget($lang->depositor->statusList, $depositor->status);?> </td>
          <td>
            <?php commonModel::printLink('depositor', 'view', "depositorID=$depositor->id", $lang->view);?>
            <?php commonModel::printLink('depositor', 'edit', "depositorID=$depositor->id", $lang->edit, "data-toggle='modal'");?>
            <?php commonModel::printLink('depositor', 'check', "depositorID=$depositor->id", $lang->depositor->check);?>
            <?php if($depositor->status == 'normal') commonModel::printLink('depositor', 'forbid', "depositorID=$depositor->id", $lang->depositor->forbid, "data-toggle=modal");?>
            <?php if($depositor->status == 'disable') commonModel::printLink('depositor', 'activate', "depositorID=$depositor->id", $lang->depositor->activate, "data-toggle=modal");?>
            <?php commonModel::printLink('balance', 'browse', "depositorID=$depositor->id", $lang->depositor->balance, "data-toggle='modal'");?>
            <?php
            if(empty($usedDepositors[$depositor->id]))
            {
                commonModel::printLink('depositor', 'delete', "depositorID=$depositor->id", $lang->delete, "class='deleter'");
            }
            else
            {
                echo html::a('javascript:;', $lang->delete, "class='disabled'");
            }
            ?>
          </td>
        </tr>
        <?php endforeach;?>
      </tbody>
    </table>
  </div>

  <?php
  if($app->user->admin == 'super' or isset($app->user->rights['balance']['browse']))
  {
      $savedHtml = '';
      foreach($balances as $currency => $balanceList)
      {
          $sum = 0;
          foreach($balanceList as $balance)
          {
              if(!isset($depositors[$balance->depositor])) continue;
              $depositor = $depositors[$balance->depositor];
              if($depositor->status != 'normal') continue;
              $sum += $balance->money;
          }
          if($sum) $savedHtml .= " <span title='$sum'>" . $currencyList[$currency] . $lang->colon . commonModel::tidyMoney($sum) . '</span>';
      }

      $totalMoney = array();
      foreach($depositors as $depositor)
      {
          if(empty($depositor->computed)) continue;

          if(isset($totalMoney[$depositor->currency]))
          {
              $totalMoney[$depositor->currency] += $depositor->computed;
          }
          else
          {
              $totalMoney[$depositor->currency] = $depositor->computed;
          }
      }
      $computedHtml = '';
      foreach($totalMoney as $currency => $total)
      {
          if($total) $computedHtml .= " <span title='$total'>" . $currencyList[$currency] . $lang->colon . commonModel::tidyMoney($total) . '</span>';
      }
  }
  ?>
  <div class='table-footer'>
    <?php if($savedHtml or $computedHtml):?>
    <div class='table-statistic'>
      <?php if($savedHtml):?>
      <span><?php echo $lang->depositor->saveBalance . $savedHtml;?></span>
      <?php endif;?>

      <?php if($computedHtml):?>
      <span><?php echo $lang->depositor->computedValue . $computedHtml;?></span>
      <?php endif;?>
    </div>
    <?php endif;?>
    <?php $pager->show();?>
  </div>
</div>
<?php include '../../common/view/footer.html.php';?>
