<?php
/**
 * The create view file of commission 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     trade
 * @version     $Id$
 * @link        http://www.zdoo.com
 */
?>
<?php include '../../common/view/header.html.php';?>
<?php js::set('type', $type);?>
<?php js::set('confirmReset', $salaryList ? $lang->commission->salaryExist : $lang->commission->confirmReset);?>
<form method='post' id='ajaxForm' action='<?php echo inlink('create', "type={$type}&tradeID={$trade->id}");?>'>
  <div class='panel'>
    <table class='table table-borderless text-center text-middle'>
      <thead>
        <tr>
          <th class='w-100px'><?php echo $lang->trade->date;?></th>
          <th><?php echo $lang->trade->trader;?></th>
          <th class='w-160px'><?php echo $lang->trade->product;?></th>
          <th class='w-120px'><?php echo $lang->commission->money;?></th>
          <th class='w-160px'><?php echo $lang->commission->base;?></td>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><?php echo formatTime($trade->date, DT_DATE1);?></td>
          <td title="<?php echo zget($customerList, $trade->trader, '');?>"><?php if($trade->trader) echo zget($customerList, $trade->trader, '');?></td>
          <td title="<?php echo zget($productList, $trade->product, '');?>"><?php echo zget($productList, $trade->product, '');?></td>
          <td><?php echo zget($lang->currencySymbols, $config->setting->mainCurrency, '') . formatMoney($trade->money * $trade->exchangeRate);?></td>
          <td><?php echo html::input('commission', $trade->commission != 0.00 ? $trade->commission : ($trade->money * $trade->exchangeRate), "class='form-control'")?></td>
        </tr>
      </tbody>
    </table>
  </div>
  <?php $lineCommissionList = $commissions->lineCommissionList;?>
  <?php $saleCommissionList = $commissions->saleCommissionList;?>
  <?php $saleRules          = $commissions->saleRules;?>
  <?php $i = 0;?>
  <div class='row'>
    <div class='col-md-3 lineCommission'>
      <div class='panel panel-block panel-success'>
        <div class='panel-heading'>
          <strong>
            <?php echo $lang->commission->typeList['line'];?>
            <span class='rate'></span>
            <span class='amount'></span>
          </strong>
        </div>
        <div class='panel-body'>
          <?php $rateTotal = 0;?>
          <?php $lineCommissionTotal= 0;?>
          <?php foreach($lineCommissionList as $lineCommission):?>
          <?php if(!isset($userList[$lineCommission->account])) continue;?>
          <div class='itemDiv input-group'>
            <span class='input-group-addon'><?php echo zget($userList, $lineCommission->account);?></span>
            <?php echo html::hidden("account[$i]", $lineCommission->account);?>
            <?php echo html::input("rate[$i]", $lineCommission->rate, "class='form-control' readonly");?>
            <span class='input-group-addon'></span>
            <?php echo html::input("amount[$i]", $lineCommission->amount, "class='form-control amount' readonly" )?>
            <?php echo html::hidden("contribution[$i]", '');?>
            <?php echo html::hidden("type[$i]", 'line');?>
          </div>
          <?php $i++;?>
          <?php endforeach;?>
        </div>
      </div>
    </div>
    <div class='col-md-3 saleCommission'>
      <div class='panel panel-block panel-primary'>
        <div class='panel-heading'>
          <strong>
            <?php echo $lang->commission->saleCommission->ruleList['fixed'];?>
            <span class='rate'></span>
            <span class='amount'></span>
          </strong>
        </div>
        <div class='panel-body'>
          <?php foreach($handlers as $account => $handler):?>
          <?php if(!$account) continue;?>
          <?php if(!isset($fixedUsers[$account])) continue;?>
          <?php $saleCommissionRule = zget($saleRules, $account);?>
          <?php if($saleCommissionRule->rule != 'fixed') continue;?>
          <?php $saleCommission = isset($saleCommissionList[$account]) ? $saleCommissionList[$account] : null;?>
          <div class='input-group itemDiv'>
            <?php if($type == 'commissioned' and !empty($saleCommission->account)):?>
            <span class='input-group-addon'><?php echo zget($userList, $saleCommission->account);?></span>
            <?php echo html::hidden("account[$i]", $saleCommission->account);?>
            <?php else:?>
            <?php echo html::select("account[$i]", $fixedUsers, $account, "class='form-control chosen' data-placeholder='{$lang->commission->handlers}'");?>
            <span class='input-group-addon fix-border fix-padding'></span>
            <?php endif;?>

            <?php if($type == 'commissioned' and !empty($saleCommission->rate)):?>
            <?php echo html::input("rate[$i]", (float)$saleCommission->rate, "class='form-control saleCommission' readonly='readonly'");?>
            <?php else:?>
            <?php echo html::input("rate[$i]", zget($handler, 'rate', ''), "class='form-control' placeholder='{$lang->commission->rate}'")?>
            <?php endif;?>

            <span class='input-group-addon fix-border fix-padding'></span>

            <?php if($type == 'commissioned' and !empty($saleCommission->amount)):?>
            <?php echo html::input("amount[$i]", (float)$saleCommission->amount, "class='form-control saleCommission' readonly='readonly'");?>
            <?php else:?>
            <?php echo html::input("amount[$i]", '', "class='form-control' placeholder='{$lang->commission->amount}'");?>
            <?php endif;?>

            <?php echo html::hidden("contribution[$i]", '');?>
            <?php echo html::hidden("type[$i]", 'sale');?>
            <?php if(!empty($saleCommissionList)):?>
            <span class='input-group-btn'><a href='javascript:;' class='btn btn-default editItem'><i class='icon-pencil'></i></a></span>
            <span class='input-group-btn'><a href='javascript:;' class='btn btn-default delItem'><i class='icon-remove'></i></a></span>
            <?php else:?>
            <span class='input-group-btn'><a href='javascript:;' class='btn btn-default addItem'><i class='icon-plus'></i></a></span>
            <span class='input-group-btn'><a href='javascript:;' class='btn btn-default delItem'><i class='icon-remove'></i></a></span>
            <?php endif;?>
          </div>
          <?php $i++;?>
          <?php endforeach;?>
          <div class='input-group itemDiv'>
            <?php echo html::select("account[$i]", $fixedUsers, '', "class='form-control chosen' data-placeholder='{$lang->commission->handlers}'");?>
            <span class='input-group-addon fix-border fix-padding'></span>
            <?php echo html::input("rate[$i]", '', "class='form-control' placeholder='{$lang->commission->rate}'")?>
            <span class='input-group-addon fix-border fix-padding'></span>
            <?php echo html::input("amount[$i]", '', "class='form-control' placeholder='{$lang->commission->amount}'");?>
            <?php echo html::hidden("contribution[$i]", '', "class='form-control'");?>
            <?php echo html::hidden("type[$i]", 'sale');?>
            <span class='input-group-btn'><a href='javascript:;' class='btn btn-default addItem'><i class='icon-plus'></i></a></span>
            <span class='input-group-btn'><a href='javascript:;' class='btn btn-default delItem'><i class='icon-remove'></i></a></span>
          </div>
          <?php $i++;?>
        </div>
      </div>
    </div>
    <div class='col-md-3 multistepCommission'>
      <div class='panel panel-block'>
        <div class='panel-heading'>
          <strong>
            <?php echo $lang->commission->saleCommission->ruleList['multistep'];?>
            <span class='contribution'></span>
          </strong>
        </div>
        <div class='panel-body'>
          <?php foreach($handlers as $account => $handler):?>
          <?php if(!$account) continue;?>
          <?php if(!isset($multistepUsers[$account])) continue;?>
          <?php $saleCommissionRule = zget($saleRules, $account, '');?>
          <?php if($saleCommissionRule && $saleCommissionRule->rule != 'multistep') continue;?>
          <?php $saleCommission = isset($saleCommissionList[$account]) ? $saleCommissionList[$account] : null;?>
          <div class='itemDiv input-group'>
            <?php if($type == 'commissioned' and !empty($saleCommission->account)):?>
            <span class='input-group-addon'><?php echo zget($multistepUsers, $saleCommission->account);?></span>
            <?php echo html::hidden("account[$i]", $saleCommission->account);?>
            <?php else:?>
            <?php echo html::select("account[$i]", $multistepUsers, $account, "class='form-control chosen' data-placeholder='{$lang->commission->handlers}'");?>
            <span class='input-group-addon fix-border fix-padding'></span>
            <?php endif;?>

            <?php if($type == 'commissioned' and !empty($saleCommission->contribution)):?>
            <?php echo html::input("contribution[$i]", (float)$saleCommission->contribution, "class='form-control saleCommission' readonly='readonly'");?>
            <?php else:?>
            <?php echo html::input("contribution[$i]", isset($handler->contribution) ? $handler->contribution : '', "class='form-control' placeholder='{$lang->commission->contribution}'");?>
            <?php endif;?>
            <?php echo html::hidden("rate[$i]", '');?>
            <?php echo html::hidden("amount[$i]", '');?>
            <?php echo html::hidden("type[$i]", 'sale');?>
            <?php if(!empty($saleCommissionList)):?>
            <span class='input-group-btn'><a href='javascript:;' class='btn btn-default editItem'><i class='icon-pencil'></i></a></span>
            <span class='input-group-btn'><a href='javascript:;' class='btn btn-default delItem'><i class='icon-remove'></i></a></span>
            <?php else:?>
            <span class='input-group-btn'><a href='javascript:;' class='btn btn-default addItem'><i class='icon-plus'></i></a></span>
            <span class='input-group-btn'><a href='javascript:;' class='btn btn-default delItem'><i class='icon-remove'></i></a></span>
            <?php endif;?>
          </div>
          <?php $i++;?>
          <?php endforeach;?>
          <div class='itemDiv input-group'>
            <?php echo html::select("account[$i]", $multistepUsers, '', "class='form-control chosen' data-placeholder={$lang->commission->handlers}");?>
            <span class='input-group-addon fix-border fix-padding'></span>
            <?php echo html::input("contribution[$i]", '', "class='form-control' placeholder='{$lang->commission->contribution}'")?>
            <?php echo html::hidden("rate[$i]", '');?>
            <?php echo html::hidden("amount[$i]", '');?>
            <?php echo html::hidden("type[$i]", 'sale');?>
            <span class='input-group-btn'><a href='javascript:;' class='btn btn-default addItem'><i class='icon-plus'></i></a></span>
            <span class='input-group-btn'><a href='javascript:;' class='btn btn-default delItem'><i class='icon-remove'></i></a></span>
          </div>
          <?php $i++;?>
        </div>
      </div>
    </div>
    <div class='col-md-3 fixedMultistepCommission'>
      <div class='panel panel-block panel-warning'>
        <div class='panel-heading'>
          <strong>
            <?php echo $lang->commission->saleCommission->ruleList['fixedMultistep'];?>
            <span class='contribution'></span>
          </strong>
        </div>
        <div class='panel-body'>
          <?php foreach($handlers as $account => $handler):?>
          <?php if(!$account) continue;?>
          <?php if(!isset($fixedMultistepUsers[$account])) continue;?>
          <?php $saleCommissionRule = zget($saleRules, $account, '');?>
          <?php if($saleCommissionRule && $saleCommissionRule->rule != 'fixedMultistep') continue;?>
          <?php $saleCommission = isset($saleCommissionList[$account]) ? $saleCommissionList[$account] : null;?>
          <div class='itemDiv input-group'>
            <?php if($type == 'commissioned' and !empty($saleCommission->account)):?>
            <span class='input-group-addon'><?php echo zget($fixedMultistepUsers, $saleCommission->account);?></span>
            <?php echo html::hidden("account[$i]", $saleCommission->account);?>
            <?php else:?>
            <?php echo html::select("account[$i]", $fixedMultistepUsers, $account, "class='form-control chosen'");?>
            <span class='input-group-addon'></span>
            <?php endif;?>

            <?php if($type == 'commissioned' and !empty($saleCommission->contribution)):?>
            <?php echo html::input("contribution[$i]", (float)$saleCommission->contribution, "class='form-control saleCommission' readonly='readonly' data-placeholder='{$lang->commission->contribution}'");?></span>
            <?php else:?>
            <?php echo html::input("contribution[$i]", isset($handler->contribution) ? $handler->contribution : '', "class='form-control' placeholder='{$lang->commission->contribution}'");?>
            <?php endif;?>
            <?php echo html::hidden("rate[$i]", '');?>
            <?php echo html::hidden("amount[$i]", '');?>
            <?php echo html::hidden("type[$i]", 'sale');?>

            <?php if(!empty($saleCommissionList)):?>
            <span class='input-group-btn'><a href='javascript:;' class='btn btn-default editItem'><i class='icon-pencil'></i></a></span>
            <span class='input-group-btn'><a href='javascript:;' class='btn btn-default delItem'><i class='icon-remove'></i></a></span>
            <?php else:?>
            <span class='input-group-btn'><a href='javascript:;' class='btn btn-default addItem'><i class='icon-plus'></i></a></span>
            <span class='input-group-btn'><a href='javascript:;' class='btn btn-default delItem'><i class='icon-remove'></i></a></span>
            <?php endif;?>
          </div>
          <?php $i++;?>
          <?php endforeach;?>
          <div class='itemDiv input-group'>
            <?php echo html::select("account[$i]", $fixedMultistepUsers, '', "class='form-control chosen' data-placeholder={$lang->commission->handlers}");?>
            <span class='input-group-addon fix-border fix-padding'></span>
            <?php echo html::input("contribution[$i]", '', "class='form-control' placeholder='{$lang->commission->contribution}'")?>
            <?php echo html::hidden("rate[$i]", '');?>
            <?php echo html::hidden("amount[$i]", '');?>
            <?php echo html::hidden("type[$i]", 'sale');?>
            <span class='input-group-btn'><a href='javascript:;' class='btn btn-default addItem'><i class='icon-plus'></i></a></span>
            <span class='input-group-btn'><a href='javascript:;' class='btn btn-default delItem'><i class='icon-remove'></i></a></span>
          </div>
          <?php $i++;?>
        </div>
      </div>
    </div>
  </div>
  <div class='text-center form-actions'>
    <?php echo html::submitButton();?>
    <?php if($type == 'commissioned') echo html::a(inlink('create', "type=$type&tradeID=$trade->id&reset=1"), $lang->commission->reset, "class='btn btn-primary reset'");?>
    <?php echo html::backButton();?>
  </div>
</form>
<?php
foreach($lang->commission->saleCommission->ruleList as $key => $rule)
{
    echo html::hidden($key, $rule);
}
$fixedSelect = html::select("account[key]", $fixedUsers, '', "class='form-control' data-placeholder='{$lang->commission->handlers}'");
$fixedItemRow = <<<EOT
  <div class='input-group itemDiv'>
    {$fixedSelect}
    <span class='input-group-addon fix-border fix-padding'></span>
    <input type='text' value='' name='rate[key]' class='form-control' placeholder='{$lang->commission->rate}'>
    <span class='input-group-addon fix-border fix-padding'></span>
    <input type='text' value='' name='amount[key]' class='form-control' placeholder='{$lang->commission->amount}'>
    <input type='hidden' value='' name='contribution[key]'>
    <input type='hidden' value='sale' name='type[key]'>
    <span class='input-group-btn'><a href='javascript:;' class='btn btn-default addItem'><i class='icon-plus'></i></a></span>
    <span class='input-group-btn'><a href='javascript:;' class='btn btn-default delItem'><i class='icon-remove'></i></a></span>
  </div>
EOT;

$multistepSelect = html::select("account[key]", $multistepUsers, '', "class='form-control chosen' data-placeholder='{$lang->commission->handlers}'");
$multistepItemRow = <<<EOT
  <div class='itemDiv input-group'>
    {$multistepSelect}
    <span class='input-group-addon fix-border fix-padding'></span>
    <input type='text' value='' name='contribution[key]' class='form-control' placeholder='{$lang->commission->contribution}'>
    <input type='hidden' value='' name='rate[key]'>
    <input type='hidden' value='' name='amount[key]'>
    <input type='hidden' value='sale' name='type[key]'>
    <span class='input-group-btn'><a href='javascript:;' class='btn btn-default addItem'><i class='icon-plus'></i></a></span>
    <span class='input-group-btn'><a href='javascript:;' class='btn btn-default delItem'><i class='icon-remove'></i></a></span>
  </div>
EOT;

$fixedMultistepSelect = html::select("account[key]", $fixedMultistepUsers, '', "class='form-control' data-placeholder='{$lang->commission->handlers}'");
$fixedMultistepItemRow = <<<EOT
  <div class='itemDiv input-group'>
    {$fixedMultistepSelect}
    <span class='input-group-addon fix-border fix-padding'></span>
    <input type='text' value='' name='contribution[key]' class='form-control' placeholder='{$lang->commission->contribution}'>
    <input type='hidden' value='' name='rate[key]'>
    <input type='hidden' value='' name='amount[key]'>
    <input type='hidden' value='sale' name='type[key]'>
    <span class='input-group-btn'><a href='javascript:;' class='btn btn-default addItem'><i class='icon-plus'></i></a></span>
    <span class='input-group-btn'><a href='javascript:;' class='btn btn-default delItem'><i class='icon-remove'></i></a></span>
  </div>
EOT;

js::set('fixedItemRow', $fixedItemRow);
js::set('multistepItemRow', $multistepItemRow);
js::set('fixedMultistepItemRow', $fixedMultistepItemRow);
js::set('key', ++$i);
?>
<?php include '../../common/view/footer.html.php';?>
