<?php

/* ========================================================================
 * $Id: form.class.php 2617 2017-07-19 18:36:36Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onezphp.com
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');
onez('page.item');
class onezphp_page_item_form extends onezphp_page_item{
  var $items=array();
  function __construct(){
  }
  function header(){
    global $G;
    $form=onez('admin')->widget('form')
      ->set('title',$G['title'])
      ->set('values',$this->get('values'))
    ;
    $form->add(array('type'=>'hidden','key'=>'action','value'=>'save'));
    foreach($this->items as $item){
      $form->add($item);
    }
    $this->form=$form;
    #处理提交
    if($onez=$form->submit()){
      ob_clean();
      $submit=$this->get('submit');
      if($submit){
        $submit($onez);
      }
      $submit=$this->get('form.save');
      if($submit){
        $submit($onez);
      }
      onez()->ok('操作成功','reload');
    }
    return $this;
  }
  function add($arr){
    $this->items[]=$arr;
    return $this;
  }
  function is_setting($bool){
    global $G;
    if(strpos($bool,'|')!==false){
      list($ptoken,$table,$xxx)=explode('|',$bool);
      $this->set('btnname','保存修改');
      $this->set('values',onez('appdata')->one($ptoken,$table,$xxx));
      $this->set('submit',function($onez)use($ptoken,$table,$xxx){
        global $G;
        onez('appdata')->update($ptoken,$table,$onez,"$xxx");
      });
    }elseif($bool){
      $this->set('btnname','保存修改');
      $this->set('values',$G['this']->option());
      $this->set('submit',function($onez){
        global $G;
        $G['this']->option_set($onez);
      });
    }else{
      
    }
    return $this;
  }
  function body(){
    global $G;
    $btnname=$this->get('btnname');
    if(!$btnname){
      $id=$this->get('id');
      if($id){
        $btnname='保存修改';
      }else{
        $btnname='立即添加';
      }
    }
    $btns_left=$this->get('btns.left');
    $btns_right=$this->get('btns.right');
    if($btns_left || $btns_right){
      echo'
  <div class="btns" style="padding-bottom: 10px">
    <div class="pull-left">
      '.$btns_left.'
    </div>
    <div class="pull-right">
      '.$btns_right.'
    </div>
    <div class="clearfix"></div>
  </div>';
    }
    echo '
    <form id="form-common" method="post">
    <div class="box box-info">
      <div class="box-header with-border">
        <h3 class="box-title">
          '.$G['title'].'
        </h3>
        <div class="box-tools pull-right">
        </div>
      </div>
      <div class="box-body">
        '.$this->form->code().'
      </div>
    </div>
    <button type="submit" class="btn btn-primary">
      '.$btnname.'
    </button>
  </form>';
    echo $this->form->js();
  }
}