<?php use_helper('Javascript', 'ModalBox'); ?>

<h4>Sắp xếp các module</h4>
<?php foreach ($modules as $key => $module):?>
  <div class="span-3" style="text-align:right">
    <?php echo form_tag('admin/updateadvance?act=order&name='.$key, 'method=POST') ?>
      <?php echo select_tag('order', options_for_select($position[$module['pos']], $module['order']),
        array('style'=>'width:45px;', 'onChange'=>'submit();')); ?>
    </form>
  </div>

  <div class="span-5">
    <div id="name<?php echo $key?>" class="modulename-<?php echo $module['pos'] ?>">
      <?php echo @$module['name']? @$module['name']: $key ?>
    </div>
    <div id="edit<?php echo $key?>" style="display:none">
      <?php echo input_tag($key, @$module['name'], array('style'=>'width:200px')) ?>
    </div>
  </div>

  <?php if (strpos($modulepos, '_')): ?>
  <div class="span-2">
    <?php if ($module['pos'] != 'center'): ?>
      <?php echo form_tag('admin/updateadvance?act=column&name='.$key, 'method=POST') ?>
        <?php $col_options = array('left'=>'cột 1', 'right'=>'cột 2') ?>
        <?php echo select_tag('modulepos', options_for_select($col_options, $module['pos']), array('onChange'=>'submit();'))?>
      </form>
    <?php else: ?>
      &nbsp;
    <?php endif ?>
  </div>
  <?php endif ?>

  <div class="span-3" style="width:130px">
    <?php $visible = (@$module['visible'] !== 0) ?>
    <?php echo link_to_if(!$visible, 'Hiển thị','/admin/updateadvance?act=status&name='.$key.'&status=1') ?>&nbsp;&nbsp;
    <?php echo link_to_if($visible, 'Ẩn','/admin/updateadvance?act=status&name='.$key.'&status=0') ?>&nbsp;&nbsp;
    <?php echo link_to_function('Sửa tên', "editmoduletitle('$key')", array("id"=>"btedit$key"))?>
    <?php echo link_to_function('Cập nhật', "updatemoduletitle('$key')", array("id"=>"btadd$key", "style"=>"display:none")) ?>
  </div>

  <div class="span-2">
    <?php if (preg_match('/^(\D+)(\d*)/', $key, $match) && $config = @$config_links[$match[1]]): ?>
      <?php $link = str_replace('$id', $match[2], $config[1]) ?>
      <?php echo $config[0]('Cài đặt', $link, array('title'=>'Cài đặt module "'.$module['name'].'"')) ?>
    <?php endif ?>
    &nbsp;
  </div>

  <div class="span-2">
    <?php echo link_to('Xóa','/admin/deletemodule?modulename='.$key, array('confirm'=>'Bạn có chắc chắn xóa module "'.@$module['name'].'"?')) ?>
  </div>
  <div class="clear"></div>
<?php endforeach ?>

<hr>
<h4>Các module chưa hoạt động:</h4>
<?php foreach ($defModules as $key => $module):?>
  <div class="span-2">&nbsp;</div>
  <?php $activeLink = link_to('(hoạt động)','/admin/updateadvance?act=activate&name='.$key) ?>
  <?php echo content_tag('li', $module['name'].' '.$activeLink, array('class'=>'modulename-'.$module['pos'])) ?>
  <div class="clear"></div>
<?php endforeach ?>

<hr>
<h4>Tự tạo module bằng mã nhúng HTML:</h4>
<div class="span-2">&nbsp;</div>
<div class="span-10">
  <li><?php echo m_link_to('Thêm module trên cột nội dung', 'admin/editmodule?pos=center'); ?></li>
  <li><?php echo m_link_to('Thêm module trên cột chức năng', 'admin/editmodule?pos=left'); ?></li>
</div>
<div class="clear"></div>

<?php $module_script = array() ?>
<?php foreach ($modules as $key => $module) $module_script[] = "'$key':'{$module['name']}'" ?>
<?php $module_script = '{'.join(',', $module_script).'}' ?>
<?php $update_module_url = url_for('/admin/updatemoduletitle') ?>

<?php $sf_context->getResponse()->addJavascriptCommand(<<<EOD
function editmoduletitle(key) {
  keys = $module_script;
  for (akey in keys) { //close other inputext
    if (akey != key) {
      $('edit'+akey).hide();
      $('name'+akey).show();
      $('btedit'+akey).show();
      $('btadd'+akey).hide();
      $(akey).value = keys[akey];
    }
  }
  $('edit'+key).show();
  $('name'+key).hide();
  $('btedit'+key).hide();
  $('btadd'+key).show();
}
function updatemoduletitle(key) {
  var title = $(key).value;
  $('btadd'+key).hide();
  new Ajax.Request('$update_module_url/name/'+key+'/title/'+encodeURI(title), {onSuccess: function(response) {
    alert('Cập nhật thành công');
    $(key).value = response.responseText;
    $('edit'+key).hide();
    $('name'+key).show();
    $('btedit'+key).show();
    $('name'+key).innerHTML = response.responseText;
  }});
}
EOD
); ?>
