<?php
/**
 * The show import view of trade 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      Yidong Wang <yidong@cnezsoft.com>
 * @package     trade
 * @version     $Id$
 * @link        http://www.zdoo.com
 */
?>
<?php include $app->getModuleRoot() . 'common/view/header.html.php';?>
<?php include '../../../sys/common/view/picker.html.php';?>
<?php js::set('modeType', $mode);?>
<?php if(empty($tradeList)):?>
<div class='panel'>
  <div class='panel-body text-center'>
    <?php echo $lang->trade->noImportDataTip;?> 
  </div>
</div>
<?php else:?>
<?php js::set('showExistTrade', $lang->trade->showExistTrade);?>
<?php js::set('hideExistTrade', $lang->trade->hideExistTrade);?>
<?php $hasTransfer = (!empty($tradeList['transferin']) or !empty($tradeList['transferout']));?>
<?php $canView = commonModel::hasPriv('trade', 'view');?>
<form id='ajaxForm' method='post'>
  <div class='panel'>
    <div class='panel-heading'><strong><?php echo $lang->trade->showImport;?></strong></div>
    <div class='panel-body'>
      <?php if(!empty($ignoreTrades)):?>
      <div class='alert alert-warning'><strong><?php echo $lang->trade->ignoreTip;?></strong></div>
      <?php endif;?>
      <?php $dateWidth = $lang->trade->dateWidth->common;?>
      <?php if($hasExistTrade) $dateWidth = $lang->trade->dateWidth->exist;?>
      <?php if($hasDuplicate)  $dateWidth = $lang->trade->dateWidth->duplicate;?>
      <?php $fixedWidth = 60 + 80 + $dateWidth + 100 + 130;?>
      <table class='table table-hover table-borderless datatable' id='importTradeList' data-fixed-left-width='<?php echo $fixedWidth;?>'>
        <thead>
          <tr class='text-center'>
            <th class='w-80px required'><?php echo $lang->trade->type;?></th>
            <th class='w-<?php echo $dateWidth;?>px required'><?php echo $lang->trade->date;?></th>
            <th class='w-100px required'><?php echo $lang->trade->money;?></th>
            <th class='w-130px required'><?php echo $lang->trade->handlers;?></th>
            <th class='w-200px flex-col'><?php echo $lang->trade->trader;?></th>
            <?php if($hasTransfer):?>
            <th class='w-160px flex-col required'><?php echo $lang->trade->otherDepositor;?></th>
            <?php endif;?>
            <th class='w-200px flex-col'><?php echo $lang->trade->category;?></th>
            <th class='w-160px flex-col'><?php echo $lang->trade->dept;?></th>
            <th class='w-160px flex-col'><?php echo $lang->trade->product;?></th>
            <th class='w-300px flex-col'><?php echo $lang->trade->desc;?></th>
          </tr>
        </thead>
        <tbody>
          <?php $key   = 0;?>
          <?php $index = 0;?>
          <?php $deptList['ditto'] = $lang->ditto;?>
          <?php $users['ditto']    = $lang->ditto;?>
          <?php foreach($tradeList as $type => $trades):?>
          <?php if($type == 'out') $canView &= commonModel::hasPriv('tradebrowse', 'out');?>
          <?php foreach($trades as $trade):?>
          <?php
          $otherDepositor = ($type == 'in' or $type == 'out') ? 0 : zget($trade, 'otherDepositor', 0);
          $existTrades    = zget($trade, 'existTrades', array());
          $existCount     = count($existTrades);  // This means the record exists in th database.
          $duplicate      = zget($trade, 'duplicate', false);   // This means the record is duplicate in the imported file.
          ?>
          <tr data-index='<?php echo $index;?>' data-key='<?php echo $key;?>' <?php if($existCount) echo "class='repeat-record'";?>>
            <td class='w-80px'><?php echo html::select("type[$key]", $lang->trade->typeList, zget($trade, 'type', ''), "id='type{$key}' class='form-control type' data-key='{$key}'");?></td>
            <td class='w-<?php echo $dateWidth;?>px'>
              <div class='input-group'>
                <?php echo html::input("date[$key]", zget($trade, 'date', ''), "id='date{$key}' class='form-control form-date'");?>
                <?php if($existCount):?>
                <div class='input-group-btn'>
                  <a href='javascript:;' class='btn toggleHide' data-key='<?php echo $key;?>' title='<?php echo $lang->trade->showExistTrade;?>'><i class='icon icon-plus'></i></a>
                </div>
                <?php endif;?>
                <?php if($existCount or $duplicate):?>
                <div class='input-group-addon'>
                  <?php $title = '';?>
                  <?php if($existCount && ($type == 'in' or $type == 'out')) $title .= $lang->trade->uniqueTrade;?>
                  <?php if($existCount && ($type == 'transferin' or $type == 'transferout')) $title .= $lang->trade->uniqueTransfer;?>
                  <?php if($duplicate) $title .= "\n" . $lang->trade->uniqueRecord;?>
                  <i class='red icon-warning' title='<?php echo $title;?>'></i>
                </div>
                <?php endif;?>
                <div class='input-group-addon'>
                  <label class="checkbox-inline">
                    <input type='checkbox' <?php if(empty($ignoreTrades) && !$existCount && !$duplicate) echo "checked='checked'";?> name="import[<?php echo $key;?>]" value='1'>
                    <?php echo $lang->import;?>
                  </label>
                </div>
              </div>
              <?php echo html::hidden("time[$key]", zget($trade, 'time', ''), "id='time{$key}'");?>
              <?php echo html::hidden("existTrade[$key]", join(',', array_keys($existTrades)), "id='existTrade{$key}'");?>
              <?php echo html::hidden("duplicate[$key]", $duplicate, "id='duplicate{$key}'");?>
            </td>
            <td class='w-100px'><?php echo html::input("money[$key]", zget($trade, 'money', ''), "id='money{$key}' class='form-control'");?></td>
            <td class='w-130px actions'>
              <?php $defaultHandler = $key > 0 ? 'ditto' : '';?>
              <?php echo html::select("handlers[$key]", $users, !empty($trade->handlers) ? $trade->handlers : $defaultHandler, "id='handlers{$key}' class='form-control select-picker'");?>
            </td>
            <td class='w-200px actions'>
              <?php $hasCustomer = (empty($trade->trader) or is_numeric($trade->trader));?>
              <div class='input-group out' <?php if($type == 'in') echo "style='display:none'"?>>
                <?php $url = $this->createLink('flow', 'ajaxGetPairs', 'module=purchasecontract&field=customer');?>
                <?php echo html::select("trader[$key]", $traderList, ($hasCustomer ? zget($trade, 'trader', 0) : 0), "id='trader{$key}' class='form-control select-picker' data-url='{$url}'");?>
                <?php echo html::input("traderName[$key]", $hasCustomer ? '' : zget($trade, 'trader', ''), "id='traderName{$key}' class='form-control' style='display:none'");?>
                <?php if(!$hasCustomer):?>
                <div class='input-group-addon'>
                  <i class='red icon-help-circle icon-md' title="<?php echo $lang->trade->noTraderMatch;?>"></i>
                </div>
                <?php endif;?>
                <div class='input-group-addon'>
                  <label class="checkbox-inline">
                    <input type="checkbox" name="createTrader[<?php echo $key;?>]" value="1">
                    <?php echo $lang->trade->newTrader;?>
                  </label>
                </div>
              </div>
              <div class='input-group in' <?php if($type == 'out') echo "style='display:none'"?>>
                <?php $url = $this->createLink('flow', 'ajaxGetPairs', 'module=contract&field=customer');?>
                <?php echo html::select("trader[$key]", $customerList, ($hasCustomer ? zget($trade, 'trader', 0) : 0), "id='trader{$key}' class='form-control select-picker' data-url='{$url}'");?>
                <?php echo html::input("customerName[$key]", ($hasCustomer ? '' : zget($trade, 'trader', '')), "id='customerName{$key}' class='form-control' style='display:none'");?>
                <?php if(!$hasCustomer and empty($trader->trader)):?>
                <div class='input-group-addon'>
                  <i class='red icon-help-circle' title="<?php echo $lang->trade->noTraderMatch;?>"></i>
                </div>
                <?php endif;?>
                <div class='input-group-addon'>
                  <label class="checkbox-inline">
                    <input type="checkbox" name="createCustomer[<?php echo $key;?>]" value="1">
                    <?php echo $lang->trade->newTrader;?>
                  </label>
                </div>
              </div>
            </td>
            <?php if($hasTransfer):?>
            <td class='w-160px'><?php echo html::select("otherDepositor[$key]", $depositors, $otherDepositor, "id='otherDepositor{$key}' class='form-control select-picker otherDepositor'");?></td>
            <?php endif;?>
            <td class='w-200px'>
              <?php echo html::select("category[$key]", $incomeTypes, zget($trade, 'category', 0), "id='inCategory{$key}' class='form-control select-picker in' title='" . zget($incomeTypes, zget($trade, 'category', 0)) . "' style='display:none'");?>
              <?php echo html::select("category[$key]", $expenseTypes, zget($trade, 'category', 0), "id='outCategory{$key}' class='form-control select-picker out' title='" . zget($expenseTypes, zget($trade, 'category', 0)) . "'");?>
            </td>
            <td class='w-160px actions'>
              <?php $defaultDept = $key > 0 ? 'ditto' : '';?>
              <?php echo html::select("dept[$key]", $deptList, !empty($trade->dept) ? $trade->dept : $defaultDept, "id='dept{$key}' class='form-control select-picker'");?>
            </td>
            <td class='w-160px actions'><?php echo html::select("product[$key]", $productList, zget($trade, 'product', 0), "id='product{$key}' class='form-control select-picker'");?></td>
            <td class='w-300px'><?php echo html::textarea("desc[$key]", zget($trade, 'desc', ''), "id='desc{$key}' rows='1' class='form-control' title='" . zget($trade, 'desc', '') . "'");?></td>
          </tr>
          <?php $index++;?>
          <?php foreach($existTrades as $trade):?>
          <tr data-index='<?php echo $index;?>' data-key='<?php echo $key;?>' class='text-middle repeat-record existTrades<?php echo $key;?> <?php if($canView) echo 'hasViewLink';?> hide'>
            <?php $trader   = zget($customerList, $trade->trader, '');?>
            <?php $product  = zget($productList, $trade->product, '');?>
            <?php $handlers = '';?>
            <?php foreach(explode(',', trim($trade->handlers, ',')) as $handler) $handlers .= zget($users, $handler, '') . ' ';?>
            <td class='w-80px text-center'>
              <?php echo zget($lang->trade->typeList, $trade->type);?>
              <?php if($canView) echo html::hidden("viewLink[$key]", inlink('view', "tradeID=$trade->id"));?>
            </td>
            <td class='w-<?php echo $dateWidth;?>px'><?php echo formatTime($trade->date . ' ' . $trade->time, DT_DATETIME1);?></td>
            <td class='w-100px text-right'><?php echo $trade->money;?></td>
            <td class='w-130px' title='<?php echo $handlers;?>'><?php $handlers;?></td>
            <td class='w-200px' title='<?php echo $trader;?>'><?php echo $trader;?></td>
            <?php if($hasTransfer):?>
            <?php $otherDepositor = zget($trade, 'otherDepositor', '');?>
            <td class='w-160px' title='<?php echo $otherDepositor;?>'><?php echo $otherDepositor;?></td>
            <?php endif;?>
            <td class='w-200px'><?php echo $trade->type == 'in' ? zget($incomeTypes, $trade->category, '') : zget($expenseTypes, $trade->category, '');?></td>
            <td class='w-160px'><?php echo zget($deptList, $trade->dept, '');?></td>
            <td class='w-160px' title='<?php echo $product;?>'><?php echo $product;?></td>
            <td class='w-300px' title='<?php echo $trade->desc;?>'><?php echo $trade->desc;?></td>
          </tr>
          <?php $index++;?>
          <?php endforeach;?>
          <?php $key++;?>
          <?php endforeach;?>
          <?php endforeach;?>
        </tbody>
      </table>
      <div class='table-footer form-actions text-center'><?php echo html::submitButton() . html::backButton();?></div>
    </div>
  </div>
</form>
<?php endif;?>
<?php include $app->getModuleRoot() . 'common/view/footer.html.php';?>
