<?php

/* ========================================================================
 * $Id: page.php 6105 2017-06-28 18:01:07Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onezphp.com
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');
$xxx='';
#有分类
if($this->get('category')=='cat1'){
  $categorys=array();
  $categorys['']='所有分类';
  $T=$G['this']->data()->open($tablename.'.category')->record("1 order by step,id");
  foreach($T as $r){
    $categorys[$r['id']]=$r['name'];
  }
  $cid=(int)onez()->gp('cid');
  if($cid){
    $xxx.=" and upid='$cid'";
  }
}
$function_xxx=$this->get('function.xxx');
if($function_xxx){
  $function_xxx($xxx);
}
if(strpos($xxx,'order by')===false){
  if($this->get('step')){
    $xxx.=" order by step,id";
  }else{
    $xxx.=" order by id desc";
  }
}
$record=$this->get('record');
if(!$record){
  $record=$G['this']->data()->open($tablename)->page("1$xxx");
}

$list=$this->get('list');
$fields=array();
if($list){
  foreach($list as $v){
    $o=explode('|',$v);
    list($name,$var,$format)=$o;
    
    $field=array('name'=>$name,'value'=>$var,'format'=>$format,'extra'=>array_splice($o,3));
    for($i=2;$i<count($o);$i++){
      if(strpos($o[$i],'=')===false){
        continue;
      }
      list($key,$value)=explode('=',$o[$i]);
      $key=trim($key);
      $value=trim($value);
      $field[$key]=$value;
    }
    
    $fields[]=$field;
  }
}
?>
  <div class="btns" style="padding-bottom: 10px">
<?php 
#有分类
if($this->get('category')=='cat1'){
?>
    <div class="pull-left">
      <?php foreach($categorys as $k=>$v){?>
      <a href="<?php echo $this->page->href('cid='.$k)?>" class="btn btn-<?php echo $k==$cid?'info':'default'?>">
        <?php echo $v?>
      </a>
      <?php }?>
    </div>
    <div class="pull-right">

<?php if($this->get('add')!='none'){?>
      <a href="<?php echo $this->page->href('pagemode=form&cid='.$cid)?>" data-width="960" class="onez-miniwin btn btn-success">
        添加<?php echo $label?>
      </a>
<?php }?>
    </div>
<?php }else{?>
    <div class="pull-left">
<?php if($this->get('add')!='none'){?>
      <a href="<?php echo $this->page->href('pagemode=form')?>" data-width="960" class="onez-miniwin btn btn-success">
        添加<?php echo $label?>
      </a>
<?php }?>

    </div>
    <div class="pull-right">
    </div>
<?php }?>
    <div class="clearfix"></div>
  </div><?php 
#排序
if($this->get('step')){
?>
  <form id="form-common" method="post">
<?php }?>
  <div class="box box-info">
    <div class="box-header with-border">
      <h3 class="box-title">
        <?php echo $G['title']?>
      </h3>
      <div class="box-tools pull-right">
      </div>
    </div>
    <div class="box-body  table-responsive no-padding">
      <table class="table table-striped">
        <thead>
          <tr><?php 
#有分类
if($this->get('category')=='cat1'){
?>
            <th>所属分类</th>
<?php }?>
          <?php foreach($fields as $f){?>
            <th><?php echo $f['name']?></th>
          <?php }?>
<?php 
#排序
if($this->get('step')){
?>
            <th>排序</th>
<?php }?>
<?php if($this->get('btns')!='none'){?>
            <th>操作</th>
<?php }?>
          </tr>
        </thead>
        <tbody class="todo-list">
          <?php foreach($record[0] as $rs){ ?>
          <tr><?php 
#有分类
if($this->get('category')=='cat1'){
  $C=$G['this']->data()->open($tablename.'.category')->one("id='$rs[upid]'");
?>
            <td style="vertical-align: middle;"><?php echo $C['name']?></td>
<?php }?>
          <?php foreach($fields as $f){
            list($rtype,$token)=explode('.',$f['value']);
            if($rtype=='$rs'){
              $value=$rs[$token];
              $key=$token;
            }else{
              $value=$rs[$rtype];
              $key=$rtype;
            }
            if($f['format']=='thumb'){
              $value='<img src="'.onez('m.plugin')->thumb($value).'" style="max-width:120px" width="60" />';
            }elseif($f['format']=='time'){
              $value=$value?date('Y-m-d H:i:s',$value):'---';
            }elseif($f['format']=='function'){
              $funcName=$this->get('function.'.$f['extra'][0]);
              if($funcName){
                $value=$funcName($rs,$key);
              }
            }elseif($f['format']=='options'){
              $options=$this->get('options.'.$f['extra'][0]);
              $value=$options[$value];
            }elseif($f['format']=='bool'){
              $value=$value?'<span style="color:green">是</span>':'<span style="color:gray">否</span>';
            }
            if($f['token']=='view'){
              $value='<a href="'.$this->page->href('pagemode=view&id='.$rs['id']).'" data-width="960" class="onez-miniwin">'.$value.'</a>';
            }
            ?>
            <td style="vertical-align: middle;"><?php echo $value?></td>
          <?php }?>
<?php 
#排序
if($this->get('step')){
?>
            <td style="vertical-align: middle;"><input class="form-control" name="step[<?php echo $rs['id']?>]" style="width: 100px" value="<?php echo $rs['step']?>"></td>
<?php }?>
<?php if($this->get('btns')!='none'){?>
            <td style="vertical-align: middle;">

<?php 
$listbtns=$this->get('listbtns');
if($listbtns){
  echo $listbtns($rs);
}?>


<?php if($this->get('view')){?>
              <a href="<?php echo $this->page->href('pagemode=view&id='.$rs['id'])?>" data-width="960" class="onez-miniwin btn btn-xs btn-info">
                详情
              </a>
<?php }?>

<?php if($this->get('edit')!='none'){?>
              <a href="<?php echo $this->page->href('pagemode=form&id='.$rs['id'].'&upid='.$rs['upid'])?>" data-width="960" class="onez-miniwin btn btn-xs btn-success">
                编辑
              </a>
<?php }?>
<?php if($this->get('del')!='none'){?>
              <a href="javascript:void(0)" onclick="onez.del('<?php echo $rs['id']?>')" class="btn btn-xs btn-danger">
                删除
              </a>
<?php }?>
            </td>
<?php }?>
          </tr>
          <?php }?>
        </tbody>
      </table>
    </div>
<?php if($record[1]){ ?>
    <div class="box-footer clearfix">
      <?php echo $record[1]?>
    </div>
<?php  } ?>
  </div>
<?php 
#排序
if($this->get('step')){
?>
        <button type="submit" class="btn btn-primary">
          保存排序
        </button>
    <input type="hidden" name="action" value="savestep" />
  </form>
<script type="text/javascript">
$(function(){
  $('#form-common').bind('submit',function(){
    onez.formpost(this);
    return false;
  });
});
</script>
<?php }?>
