<?php

/* ========================================================================
 * $Id: edit.php 4483 2017-07-26 15:51:31Z 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');

$list=$this->get('form');
$fields=array();
if($list){
  foreach($list as $v){
    $o=explode('|',$v);
    list($name,$var)=$o;
    $field=array('label'=>$name,'key'=>$var);
    for($i=2;$i<count($o);$i++){
      list($key,$value)=explode('=',$o[$i]);
      $key=trim($key);
      $value=trim($value);
      $field[$key]=$value;
    }
    $fields[]=$field;
  }
}

if($id){#编辑
  $item=$G['this']->data()->open($tablename)->one("id='$id'");
  $G['title']='编辑'.$label;
  $btnname='保存修改';
  foreach($fields as $f){
    if($f['type']=='password'){
      unset($item[$f['key']]);
    }
  }
  #有分类
  if($this->get('category')=='cat1'){
    $cid=$item['upid'];
  }
}else{#添加
  $item=array();
  $G['title']='添加'.$label;
  $btnname='立即添加';
  #有分类
  if($this->get('category')=='cat1'){
    $cid=(int)onez()->gp('cid');
    $item['upid']=$cid;
  }
  foreach($fields as $f){
    if($f['type']=='none'){
      continue;
    }
    if($f['value']){
      $item[$f['key']]=$f['value'];
    }
    
  }
}
$form_init=$this->get('form.init');
if($form_init){
  $form_init($item,$id);
}

#初始化表单
$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')->record("1 order by step,id");
  foreach($T as $r){
    $categorys[$r['id']]=$r['name'];
  }
  $form->add(array('label'=>$label.'分类','key'=>'upid','type'=>'select','options'=>$categorys));
}
foreach($fields as $f){
  $f['key']=str_replace('$rs.','',$f['key']);
  if($f['type']=='select'){
    $options=array();
    list($_type,$_key)=explode('.',$f['options']);
    if($_type=='function'){
      $funcName=$this->get($f['options']);
      if($funcName){
        $options=$funcName($item);
      }
    }elseif($_type=='options'){
      $options=$this->get($f['options']);
      !$options && $options=array();
    }elseif($_type=='category'){
      $T=$G['this']->data()->open($tablename.'.category')->record("1 order by step,id");
      foreach($T as $r){
        $options[$r['id']]=$r['name'];
      }
    }
    $f['options']=$options;
  }
  $form->add($f);
}


#处理提交
if($onez=$form->submit()){
  ob_clean();
  foreach($fields as $f){
    if($f['type']=='none'){
      if($f['value']){#默认值
        $onez[$f['key']]=$f['value'];
      }
      continue;
    }elseif($f['type']=='password'){
      if($id){#默认值
        if($onez[$f['key']]){
          $onez[$f['key']]=md5($onez[$f['key']]);
        }else{
          unset($onez[$f['key']]);
        }
      }else{
        $onez[$f['key']]=md5($onez[$f['key']]);
      }
      continue;
    }
  }
  $form_save=$this->get('form.save');
  if($form_save){
    $form_save($onez);
  }
  if($id){
    $form_update=$this->get('form.update');
    if($form_update){
      $form_update($onez,$id);
    }
    $G['this']->data()->open($tablename)->update($onez,"id='$id'");
  }else{
    $onez['userid']=$G['userid'];
    $form_insert=$this->get('form.insert');
    if($form_insert){
      $form_insert($onez);
    }
    $id=$G['this']->data()->open($tablename)->insert($onez);
  }
  $form_ok=$this->get('form.ok');
  if($form_ok){
    $onez['id']=$id;
    $form_ok($onez);
  }
  onez()->ok('操作成功','reload2');
}
?>
<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 class="box-footer clearfix">
        <button type="submit" class="btn btn-primary">
          <?php echo $btnname;?>
        </button>
      </div>
    </div>
    
    
    
    
    
    
    <input type="hidden" name="action" value="save" />
  </form>
</section>
<?php
echo $form->js();
?>