<?php $clientLang = $this->app->getClientLang();?>
<form id='ajaxForm' method='post' class='main-table'>
  <table class='table table-bordered'>
    <thead>
      <tr class='text-center text-middle'>
        <th class='w-50px'><?php echo $lang->budget->item;?></th>
        <?php $class = ($clientLang == 'en' ? 'w-100px' : 'w-70px');?>
        <th class='<?php echo $class;?>'><?php echo $lang->budget->amebaAccount;?></th>
        <th class='w-100px'><?php echo $lang->budget->category;?></th>
        <th class='w-100px'><?php echo $lang->budget->product;?></th>
        <th class='w-100px'><?php echo $lang->budget->money;?></th>
        <?php foreach($dateList as $date):?>
        <th><?php echo $date;?></th>
        <?php endforeach;?>
        <th class='w-70px'><?php echo html::checkbox('allAverage', $lang->budget->average);?></th>
      </tr>
    </thead>
    <tbody>
      <?php
      $incomeRowCount = $expenseRowCount = 0;
      foreach($srcBudgets as $type => $typeBudgets)
      {
          foreach($typeBudgets as $amebaAccount => $budgets)
          {
              ${$type . 'RowCount'} += count($budgets);
          }
      }
      foreach($srcBudgets as $type => $typeBudgets)
      {
          $typeIndex = 0;
          foreach($typeBudgets as $amebaAccount => $budgets)
          {
              $amebaIndex    = 0;
              $amebaRowCount = count($budgets);
              foreach($budgets as $budget)
              {
                  echo "<tr class='text-center text-middle'>";
                  if($typeIndex == 0)
                  {
                      $rowspan = "rowspan='" . ${$type . 'RowCount'} . "'";
                      echo "<td class='text-middle' $rowspan>{$lang->budget->$type}</td>";
                  }
                  if($amebaIndex == 0)
                  {
                      $label = $lang->budget->amebaAccountList[$amebaAccount];
                      echo "<td class='text-middle' rowspan='$amebaRowCount'>" . $label . '</td>';
                  }
                  if($amebaAccount !== 'total')
                  {
                      if(empty($budget->product))
                      {
                          echo "<td colspan='2'>" . zget($categoryList, $budget->category) . '</td>';
                      }
                      else
                      {
                          echo '<td>' . zget($categoryList, $budget->category) . '</td>';
                          echo '<td>' . zget($products, $budget->product) . '</td>';
                      }
                      echo "<td class='text-right'>";
                      echo $budget->money;
                      echo "<input type='hidden' name='money[$amebaAccount][$budget->product][$budget->category]' value='{$budget->money}' id='money{$amebaAccount}{$budget->product}{$budget->category}'>";
                      echo "<input type='hidden' name='tradeDept[$amebaAccount][$budget->product][$budget->category]' value='{$budget->tradeDept}' id='tradeDept{$amebaAccount}{$budget->product}{$budget->category}'>";
                      echo "<input type='hidden' name='line[$amebaAccount][$budget->product][$budget->category]' value='{$budget->line}' id='line{$amebaAccount}{$budget->product}{$budget->category}'>";
                      echo '</td>';
                  }
                  foreach($dateList as $date => $label)
                  {
                      $money = isset($destBudgets[$type][$amebaAccount][$budget->product][$budget->category][$date]->money) ? $destBudgets[$type][$amebaAccount][$budget->product][$budget->category][$date]->money : '';
                      echo "<td class='budgetTD'>" . html::input("budget[$amebaAccount][$budget->product][$budget->category][$date]", $money, "id='budget{$amebaAccount}{$budget->product}{$budget->category}{$date}' class='form-control'") . '</td>';
                  }
                  echo '<td>' . html::checkbox('average', $lang->budget->average) . '</td>';
                  echo '</tr>';
  
                  $typeIndex++;
                  $amebaIndex++;
              }
          }
      }
      ?>
    </tbody>
  </table>
  <div class='table-footer form-actions text-center'>
    <?php echo html::submitButton();?>
    <?php echo html::backButton();?>
  </div>
</form>
<script>
$('[name^=average]').change(function()
{
    if($(this).prop('checked'))
    {
        var count   = <?php echo count($dateList);?>;
        var money   = $(this).parents('tr').find('[name^=money]').val();
        var average = Math.round(money / count * 100) / 100;
        var last    = Math.round((money - average * (count - 1)) * 100) / 100;
        var index   = 1;
        $(this).parents('tr').find('[name^=budget]').each(function()
        {
            var budget = (index == count ? last : average);
            $(this).val(budget);
            index++;
        });
    }
    else
    {
        $(this).parents('tr').find('[name^=budget]').each(function()
        {
            $(this).val('');
        });
    }
});
$('[name^=allAverage]').change(function()
{
    $('[name^=average]').prop('checked', $(this).prop('checked')).change();
});
</script>
