<?php 
/**
 * The create view of order module of ZDOO.
 *
 * @copyright   Copyright 2009-2016 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
 * @license     商业软件，非开源软件
 * @author      Gang Liu <liugang@cnezsoft.com>
 * @package     order 
 * @version     $Id$
 * @link        http://www.zdoo.com
 */
?>
<?php include '../../common/view/header.html.php';?>
<?php include '../../../sys/common/view/datepicker.html.php';?>
<?php js::set('companiesCount', count($companies));?>
<div class='panel'>
  <div class='panel-heading'>
    <strong><i class='icon-plus'></i> <?php echo $lang->order->create;?></strong>
  </div>
  <div class='panel-body'>
    <form method='post' id='orderForm'>
      <table class='table table-form' id='createTable'>
        <tr>
          <td class='w-60px'></td>
          <td class='w-200px'></td>
          <td></td>
          <td class='w-100px'></td>
          <td class='w-120px'></td>
          <td class='w-120px'></td>
          <td class='w-140px'></td>
          <td class='w-60px'></td>
        </tr>
        <tr>
          <th><?php echo $lang->order->traderList[$type];?></th>
          <td colspan='2'>
            <div class='required required-wrapper'></div>
            <div class='input-group'>
              <?php echo html::select('trader', $companies, isset($trader) ? $trader : '', "class='form-control chosen' data-no_results_text='" . $lang->searchMore . "'");?>
              <?php echo html::input('newTrader', '', "class='form-control newTrader' style='display:none' placeholder='{$lang->order->traderList[$type]}'");?>
              <span class='input-group-addon'>
                <?php echo html::checkbox('createTrader', $lang->create, '', "class='createTrader'");?>
              </span>
            </div>
          </td>
          <th><?php echo $lang->order->contract;?></th>
          <td colspan='3'><?php echo html::select('contract', $contracts, $contract, "class='form-control chosen'");?></td>
          <td></td>
        </tr>
        <tr class='newTrader hide'>
          <th><?php echo $lang->customer->contact;?></th>
          <td colspan='2'>
            <div class='required required-wrapper'></div>
            <?php echo html::input('contact', '', "class='form-control'");?>
          </td>
          <td colspan='5'></td>
        </tr>
        <tr class='newTrader hide'>
          <th><?php echo $lang->customer->phone;?></th>
          <td colspan='2'><?php echo html::input('phone', '', "class='form-control'");?></td>
          <td colspan='5'></td>
        </tr>
        <tr class='newTrader hide'>
          <th><?php echo $lang->customer->email;?></th>
          <td colspan='2'><?php echo html::input('email', '', "class='form-control'");?></td>
          <td colspan='5'></td>
        </tr>
        <tr class='newTrader hide'>
          <th><?php echo $lang->customer->qq;?></th>
          <td colspan='2'><?php echo html::input('qq', '', "class='form-control'");?></td>
          <td colspan='5'></td>
        </tr>
        <tr class='newTrader hide'>
          <th><?php echo $lang->customer->weixin;?></th>
          <td colspan='2'><?php echo html::input('weixin', '', "class='form-control'");?></td>
          <td colspan='5'></td>
        </tr>
        <?php 
          $i = 1; 
          $productsCount = count($orderProducts); 
          $newItemsCount = $config->order->newItemsCount;
          $money = 0;
          foreach($orderProducts as $productID => $orderProduct):
          $money += isset($orderProduct->money) ? $orderProduct->money : 0;
        ?>
        <tr>
          <?php if($i == 1):?>
          <th id='product_title' rowspan='<?php echo $productsCount < $newItemsCount ? $newItemsCount : $productsCount + 1;?>'><?php echo $lang->order->product;?></th>
          <?php endif;?>
          <td colspan='2'>
            <div class='input-group'>
              <select id='product<?php echo $i;?>' name='product[<?php echo $i;?>]' class='product-batchEdit select-product form-control chosen' data-placeholder='<?php echo $lang->orderProduct->product;?>'>
                <option></option>
                <?php foreach($products as $product):?>
                <option value='<?php echo $product->id;?>' data-keys='<?php echo $product->pinyin;?>' data-unit='<?php echo $product->unit;?>' <?php if($product->id == $productID) echo 'selected';?>><?php echo $product->name;?></option>
                <?php endforeach;?>
              </select>
              <?php if(commonModel::hasPriv('product', 'createFromOrder')):?>
              <span class='input-group-addon'>
                <?php echo html::a($this->createLink('product','createFromOrder', "id=product{$i}"), $lang->create, "data-toggle='modal'");?>
              </span>
              <?php endif;?>
            </div>
          </td>
          <td>
            <?php $product = zget($products, $productID);?>
            <?php echo html::input("unit[$i]", isset($product->unit) ? $product->unit : '', "id='unit{$i}' class='product-batchEdit form-control' placeholder='{$lang->orderProduct->unit}'");?>
          </td>
          <td>
            <?php echo html::input("price[$i]", isset($orderProduct->price) ? $orderProduct->price : '', "id='price{$i}' class='product-batchEdit form-control product-price' placeholder='{$lang->orderProduct->price}'");?>
          </td>
          <td>
            <?php echo html::input("amount[$i]", isset($orderProduct->amount) ? $orderProduct->amount : '', "id='amount{$i}' class='product-batchEdit form-control product-amount' placeholder='{$lang->orderProduct->amount}'");?>
          </td>
          <td>
            <?php echo html::input("productMoney[$i]", isset($orderProduct->money) ? $orderProduct->money : '', "id='productMoney{$i}' class='product-batchEdit form-control product-money' placeholder='{$lang->order->money}'");?>
          </td>
          <td>
            <?php echo html::a('javascript:;', "<i class='icon-plus'></i>", "class='btn-plus'");?>
            <?php echo html::a('javascript:;', "<i class='icon-remove'></i>", "class='btn-remove'");?>
            <?php echo html::hidden("mode[$i]", 'purchase', "class='product-batchEdit'");?>
          </td>
        </tr>
        <?php $i++;?>
        <?php endforeach;?>
        <?php $count = $productsCount < $newItemsCount ? $newItemsCount - $productsCount : 1;?>
        <?php for($j = 0; $j < $count; $j++, $i++):?>
        <tr>
          <?php if($i == 1):?>
          <th id='product_title' rowspan='<?php echo $newItemsCount;?>'><?php echo $lang->order->product;?></th>
          <?php endif;?>
          <td colspan='2'>
            <div class='input-group'>
              <select id='product<?php echo $i;?>' name='product[<?php echo $i;?>]' class='product-batchEdit select-product form-control chosen' data-placeholder='<?php echo $lang->orderProduct->product;?>'>
                <option></option>
                <?php foreach($products as $product):?>
                <option value='<?php echo $product->id;?>' data-keys='<?php echo $product->pinyin;?>' data-unit='<?php echo $product->unit;?>'><?php echo $product->name;?></option>
                <?php endforeach;?>
              </select>
              <?php if(commonModel::hasPriv('product', 'createFromOrder')):?>
              <span class='input-group-addon'>
                <?php echo html::a($this->createLink('product','createFromOrder', "id=product{$i}"), $lang->create, "data-toggle='modal'");?>
              </span>
              <?php endif;?>
            </div>
          </td>
          <td>
            <?php echo html::input("unit[$i]", '', "id='unit{$i}' class='product-batchEdit form-control' placeholder='{$lang->orderProduct->unit}'");?>
          </td>
          <td>
            <?php echo html::input("price[$i]", '', "id='price{$i}' class='product-batchEdit form-control product-price' placeholder='{$lang->orderProduct->price}'");?>
          </td>
          <td>
            <?php echo html::input("amount[$i]", '', "id='amount{$i}' class='product-batchEdit form-control product-amount' placeholder='{$lang->orderProduct->amount}'");?>
          </td>
          <td>
            <?php echo html::input("productMoney[$i]", '', "id='productMoney{$i}' class='product-batchEdit form-control product-money' placeholder='{$lang->order->money}'");?>
          </td>
          <td>
            <?php echo html::a('javascript:;', "<i class='icon-plus'></i>", "class='btn-plus'");?>
            <?php echo html::a('javascript:;', "<i class='icon-remove'></i>", "class='btn-remove'");?>
            <?php echo html::hidden("mode[$i]", 'create', "class='product-batchEdit'");?>
          </td>
        </tr>
        <?php endfor;?>
        <tr>
          <th><?php echo $lang->order->finishedDate;?></th>
          <td>
            <div class='required required-wrapper'></div>
            <?php echo html::input('finishedDate', '', "class='form-control form-date'");?>
          </td>
          <td></td>
          <td></td>
          <th><?php echo $lang->order->money;?></th>
          <td colspan='2'><?php echo html::input('money', $money ? $money : '', "class='form-control'");?></td>
          <td></td>
        </tr>
        <tr>
          <th><?php echo $lang->order->taxed;?></th>
          <td>
            <div class='required required-wrapper'></div>
            <?php echo html::select('taxed', $lang->order->taxedList, 1, "class='form-control'");?>
          </td>
          <td></td>
          <td></td>
          <th><?php echo $lang->order->settlement;?></th>
          <td colspan='2'><?php echo html::select('settlement', $lang->order->settlementList, '', "class='form-control'");?></td>
          <td></td>
        </tr>
        <tr>
          <th><?php echo $lang->order->desc;?></th>
          <td colspan='6'><?php echo html::textarea('desc', '', "rows='3' class='form-control'");?></td>
          <td></td>
        </tr>
        <tr>
          <th></th>        
          <td colspan='7' class='form-actions'>
            <?php echo html::hidden('type', $type);?>
            <?php echo html::submitButton();?>
            <?php echo html::backButton();?>
            <div id='duplicateError' class='hide'></div>
          </td>   
        </tr>
      </table>
    </form>
  </div>
</div>
<div class='errorMessage hide'>
  <div class='alert alert-danger alert-dismissable'>
    <button aria-hidden='true' data-dismiss='alert' class='close' type='button'>×</button>
    <button type='submit' class='btn btn-default' id='continueSubmit'><?php echo $lang->continueSave;?></button>
  </div>
</div>
<?php js::set('key', $i);?>
<script>
<?php helper::import('../js/searchcustomer.js');?>
</script>
<?php include '../../common/view/footer.html.php';?>
