<?php

/* ========================================================================
 * $Id: view.php 2987 2017-07-19 08:01:41Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onezphp.com
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');
$id=(int)onez()->gp('id');

$view=$this->get('view');
$fields=array();
if($view){
  foreach($view 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;
  }
}

$item=$rs=$G['this']->data()->open($tablename)->one("id='$id'");
$myitem=$this->get('function.item');
if($myitem){
  $myitem($item,$id);
}
$G['title']='查看'.$label;
#有分类
if($this->get('category')=='cat1'){
  $cid=$item['upid'];
}
#初始化表单
$form=onez('admin')->widget('form')
  ->set('title',$G['title'])
  ->set('values',$item)
;
#创建字段
$form->add(array('type'=>'hidden','key'=>'action','value'=>'save'));
#有分类
if($this->get('category')=='cat1'){
  $categorys=array();
  $T=$G['this']->data()->open($tablename.'.category')->one("id='$item[upid]'");
  $form->add(array('label'=>$label.'分类','type'=>'html','html'=>'<pre>'.$T['name'].'</pre>'));
}
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>';
  }
  $form->add(array('label'=>$f['name'],'type'=>'html','html'=>'<pre>'.$value.'</pre>'));
}
$formAdd=$this->get('view.add');
if($formAdd){
  $formAdd($form,$item);
}
?>
<style>
.box-body pre p{
  margin:0;
}
</style>
<section class="content-header">
  <h1>
    <?php echo $G['title']?>
  </h1>
  <ol class="breadcrumb">
    <li>
      <a href="<?php echo onez()->href('/')?>">
        <i class="fa fa-dashboard">
        </i>
        管理首页
      </a>
    </li>
    <li class="active">
      <?php echo $G['title'];?>
    </li>
  </ol>
</section>
<section class="content">
  <form id="form-common" method="post">
    <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">
        <?php echo $form->code();?>
      </div>
    </div>
</section>