<?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 if(empty($trades)):?>
<div class='panel'>
  <div class='panel-body text-center'>
    <?php echo $lang->trade->noImportDataTip;?> 
  </div>
</div>
<?php else:?>
<?php include '../../../sys/common/view/datepicker.html.php';?>
<?php js::set('showExistTrade', $lang->trade->showExistTrade);?>
<?php js::set('hideExistTrade', $lang->trade->hideExistTrade);?>
<form id='ajaxForm' method='post'>
  <div class='panel'>
    <div class='panel-heading'><strong><?php echo $lang->trade->showImport;?></strong></div>
    <div class='panel-body'>
      <table class='table table-hover table-fixed table-borderless' id='importTradeList'>
        <thead>
          <tr class='text-center'>
            <th class='w-100px'><?php echo $lang->trade->depositor;?></th>
            <th class='w-80px'><?php echo $lang->trade->type;?></th>
            <th class='w-150px'><?php echo $lang->trade->category;?></th> 
            <th class='w-200px'><?php echo $lang->trade->trader;?></th>
            <th class='w-100px'><?php echo $lang->trade->money;?></th>
            <th class='w-120px'><?php echo $lang->trade->dept;?></th>
            <th class='w-130px'><?php echo $lang->trade->handlers;?></th>
            <th class='w-120px'><?php echo $lang->trade->product;?></th>
            <?php if(!empty($existTrades)):?>
            <th class='w-200px'><?php echo $lang->trade->date;?></th>
            <?php else:?>
            <th class='w-100px'><?php echo $lang->trade->date;?></th>
            <?php endif;?>
            <th class='w-300px'><?php echo $lang->trade->desc;?></th>
          </tr>
        </thead>
        <tbody>
          <?php $deptList['ditto'] = $lang->ditto;?>
          <?php $users['ditto']    = $lang->ditto;?>
          <?php foreach($trades as $i => $trade):?>
          <?php
          if($i == 0)
          {
              $trade['dept']     = !empty($trade['dept'])     ? $trade['dept']     : '';
              $trade['handlers'] = !empty($trade['handlers']) ? $trade['handlers'] : '';
          }
          else
          {
              $trade['dept']     = !empty($trade['dept'])     ? $trade['dept']     : 'ditto';
              $trade['handlers'] = !empty($trade['handlers']) ? $trade['handlers'] : 'ditto';
          }
          ?>
          <tr <?php echo !empty($existTrades[$i]) ? "class='repeat-record' title={$lang->trade->unique} data-toggle='tooltip'" : '';?>>
            <td class='text-middle'><?php echo $depositor->abbr;?></td>
            <td>
              <?php echo html::select("type[$i]", $lang->trade->typeList, zget($trade, 'type', ''), "class='form-control type' id='type{$i}'");?>
            </td>
            <td>
              <?php echo html::select("category[$i]", $incomeTypes, zget($trade, 'category', 0), "class='form-control in' title='" . zget($incomeTypes, zget($trade, 'category', 0)) . "' style='display:none'");?>
              <?php echo html::select("category[$i]", $expenseTypes, zget($trade, 'category', 0), "class='form-control out' title='" . zget($expenseTypes, zget($trade, 'category', 0)) . "'");?>
            </td>
            <td class='actions'>
              <?php $hasCustomer = (empty($trade['trader']) or is_numeric($trade['trader']));?>
              <div class='input-group out' <?php if(zget($trade, 'type', '') == 'in') echo "style='display:none'"?>>
                <?php echo html::select("trader[$i]", $traderList, ($hasCustomer ? zget($trade, 'trader', 0) : 0), "class='form-control chosen' id='trader{$i}' data-no_results_text='" . $lang->searchMore . "'");?>
                <?php echo html::input("traderName[$i]", $hasCustomer ? '' : zget($trade, 'trader', ''), "class='form-control' id='traderName{$i}' style='display:none'");?>
                <div class='input-group-addon'>
                  <label class="checkbox-inline">
                    <input type="checkbox" name="createTrader[<?php echo $i;?>]" value="1">
                    <?php echo $lang->trade->newTrader;?>
                    <?php if(!$hasCustomer):?><i class='red icon-question' title="<?php echo $lang->trade->noTraderMatch;?>"></i><?php endif;?>
                  </label>
                </div>
              </div>
              <div class='input-group in' <?php if(zget($trade, 'type', '') == 'out') echo "style='display:none'"?>>
                <?php echo html::select("trader[$i]", $customerList, ($hasCustomer ? zget($trade, 'trader', 0) : 0), "class='form-control chosen' id='trader{$i}' data-no_results_text='" . $lang->searchMore . "'");?>
                <?php echo html::input("customerName[$i]", ($hasCustomer ? '' : zget($trade, 'trader', '')), "class='form-control' id='customerName{$i}' style='display:none'");?>
                <div class='input-group-addon'>
                  <label class="checkbox-inline">
                    <input type="checkbox" name="createCustomer[<?php echo $i;?>]" value="1">
                    <?php echo $lang->trade->newTrader;?>
                    <?php if(!$hasCustomer and empty($trader['trader'])):?><i class='red icon-question' title="<?php echo $lang->trade->noTraderMatch;?>"></i><?php endif;?>
                  </label>
                </div>
              </div>
            </td>
            <td><?php echo html::input("money[$i]", zget($trade, 'money', ''), "class='form-control'");?></td>
            <td class='actions'><?php echo html::select("dept[$i]", $deptList, zget($trade, 'dept', 0), "class='form-control chosen'");?></td>
            <td class='actions'><?php echo html::select("handlers[$i][]", $users, zget($trade, 'handlers', ''), "class='form-control chosen' id='handlers{$i}' multiple");?></td>
            <td class='actions'><?php echo html::select("product[$i]", $productList, zget($trade, 'product', 0), "class='form-control chosen' id='product{$i}'");?></td>
            <td>
              <?php if(!empty($existTrades[$i])):?>
              <div class='input-group'>
                <?php echo html::input("date[$i]", zget($trade, 'date', ''), "class='form-control form-date' id='date{$i}' title='" . zget($trade, 'date', '') . "'");?>
                <div class='input-group-addon'>
                  <label class="checkbox-inline">
                    <input type='checkbox' checked='checked' name="ignoreUnique[<?php echo $i;?>]" value='1'>
                    <?php echo $lang->trade->ignore;?>
                  </label>
                </div>
                <div class='input-group-btn'>
                  <a href='javascript:;' class='btn toggleHide' data-key='<?php echo $i;?>' title='<?php echo $lang->trade->showExistTrade;?>'><i class='icon icon-plus'></i></a>
                </div>
              </div>
              <?php else:?>
              <?php echo html::input("date[$i]", zget($trade, 'date', ''), "class='form-control form-date' id='date{$i}'");?>
              <?php endif;?>
              <?php echo html::hidden("time[$i]", zget($trade, 'time', ''));?>
            </td>
            <td><?php echo html::textarea("desc[$i]", zget($trade, 'desc', ''), "rows='1' class='form-control' title='" . zget($trade, 'desc', '') . "'");?></td>
          </tr>
          <?php if(!empty($existTrades[$i])):?>
          <?php foreach($existTrades[$i] as $trade):?>
          <tr class='text-middle existTrades<?php echo $i;?> hide'>
            <td class='text-middle'><?php echo $depositor->abbr;?></td>
            <td><?php echo zget($lang->trade->typeList, $trade->type);?></td>
            <td><?php echo $trade->type == 'in' ? zget($incomeTypes, $trade->category, '') : zget($expenseTypes, $trade->category, '');?></td>
            <td><?php echo zget($customerList, $trade->trader, '');?></td>
            <td><?php echo $trade->money;?></td>
            <td><?php echo zget($deptList, $trade->dept, '');?></td>
            <td><?php foreach(explode(',', trim($trade->handlers, ',')) as $handler) echo zget($users, $handler, '') . ' ';?></td>
            <td><?php echo zget($productList, $trade->product, '');?></td>
            <td><?php echo formatTime($trade->date . ' ' . $trade->time, DT_DATETIME1);?></td>
            <td><?php echo $trade->desc;?></td>
          </tr>
          <?php endforeach;?>
          <?php endif;?>
          <?php endforeach;?>
        </tbody>
        <tfoot><tr><td colspan='10' class='form-actions text-center'><?php echo html::submitButton() . html::backButton();?></td></tr></tfoot>
      </table>
    </div>
  </div>
</form>
<?php endif;?>
<?php include $app->getModuleRoot() . 'common/view/footer.html.php';?>
