<?php

/* ========================================================================
 * $Id: edit.php 3694 2020-05-16 05:05:04Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onez.cn
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');
# edit
#请求数据
if($MODE=='$post'){
  $id=onez()->gp('id');
  $action=onez()->gp('action');
  if($action=='data'){
    $form=$this->load('form');
    if($id){
      $T=$G['this']->data()->open('sites')->one("userid='$G[userid]' and id='$id'");
      $form->init($T);
    }
    $form->add(array('label'=>'网站状态','type'=>'select','key'=>'status','options'=>$G['this']->siteStatus(),'notempty'=>'','default'=>'','group'=>''));
    #$form->add(array('label'=>'核心功能','type'=>'select','key'=>'type','options'=>$G['this']->siteTypes(),'notempty'=>'','default'=>'','group'=>'','tip'=>'其他功能可以在创建后通过插件管理添加'));
    if(!$id){
      $form->add(array('label'=>'预选插件','type'=>'form.checkbox','key'=>'plugins','options'=>$G['this']->onezapi('data','/sites.add.default'),'notempty'=>'','default'=>'','group'=>'','tip'=>''));
    }
    $form->add(array('label'=>'网站名称','type'=>'text','key'=>'name','hint'=>'','notempty'=>'网站不能为空','default'=>'','group'=>''));
    $form->add(array('label'=>'网站域名','type'=>'textarea','key'=>'domains','hint'=>'每行填写一个，需要解析后才可以访问','notempty'=>'','default'=>'','group'=>''));
    
    $datas=array();
    $datas['create']='-创建新的数据体系-';
    $T=$G['this']->data()->open('datas')->record("userid='$G[userid]' order by id desc");
    foreach($T as $rs){
      $datas[$rs['id']]=$rs['name'];
    }
    $form->add(array('label'=>'数据体系','type'=>'select','key'=>'data','options'=>$datas,'notempty'=>'','default'=>'','group'=>''));
    $users=array();
    $users['create']='-创建新的用户体系-';
    $T=$G['this']->data()->open('users')->record("userid='$G[userid]' order by id desc");
    foreach($T as $rs){
      $users[$rs['id']]=$rs['name'];
    }
    $form->add(array('label'=>'用户体系','type'=>'select','key'=>'user','options'=>$users,'notempty'=>'','default'=>'','group'=>''));
    
    
    $form->add(array('label'=>'备注','type'=>'textarea','key'=>'summary','hint'=>'','notempty'=>'','default'=>'','group'=>''));
    if($onez=$form->submit()){
      $plugins=onez()->gp('plugins[]');
      unset($onez['plugins']);
      if($onez['data']=='create'){
        $item=array();
        $item['name']=$onez['name'];
        $item['summary']='';
        $item['userid']=$G['userid'];
        $onez['data']=$G['this']->data()->open('datas')->insert($item);
      }
      if($onez['user']=='create'){
        $item=array();
        $item['name']=$onez['name'];
        $item['summary']='';
        $item['userid']=$G['userid'];
        $item['grades']=array(
          'admin'=>'超级管理员',
          'user'=>'普通用户',
        );
        $item['gradesTxt']="admin=超级管理员\nuser=普通用户";
        $onez['user']=$G['this']->data()->open('users')->insert($item);
      }
      if($id){
        $G['this']->data()->open('sites')->update($onez,"userid='$G[userid]' and id='$id'");
      }else{
        $onez['userid']=$G['userid'];
        $_siteid=$G['this']->data()->open('sites')->insert($onez);
        if($plugins){
          $installs=$G['this']->onezapi('api',array(
            'key'=>'/plugin/install',
            'tokens'=>$plugins,
            '_siteid'=>$_siteid,
          ));
          if($installs){
            foreach($installs as $v){
              $G['this']->install($v,$_siteid);
            }
          }
        }
      }
      $G['this']->siteUpdate();
      return $this->ok('提交成功','reload2');
    }
    $form->output($A);
  }
  return;
}
$this->add(array(
  'type'=>'pageform',
  'name'=>'表单',
  'data'=>'form',
));
