<?php
!defined('IN_ONEZ') && exit('Access Denied');

$id=(int)onez()->gp('id');
if($id){#编辑
  $item=$G['this']->data()->open('android_keystore')->one("id='$id'");
  $G['title']='编辑分类';
  $btnname='保存修改';
}else{#添加
  $item=array();
  $copyid=(int)onez()->gp('copyid');
  $type=onez()->gp('type');
  if($type=='auto'){
    $item=array();
    $item['name']='系统随机生成';
    $item['alias']='ks'.$G['userid'];
    $item['password']=md5(uniqid());
    $item['index1']=1;
  }
  $G['title']='添加分类';
  $btnname='立即添加';
}

#初始化表单
$form=onez('admin')->widget('form')
  ->set('title',$G['title'])
  ->set('values',$item)
;
#创建表单项
$form->add(array('type'=>'hidden','key'=>'action','value'=>'save'));
$form->add(array('label'=>'证书名称','type'=>'text','key'=>'name','hint'=>'','notempty'=>'证书名称不能为空','group'=>''));

if($id && $item['index1']){
  $form->add(array('label'=>'证书别名','type'=>'html','key'=>'alias','html'=>'<pre>'.$item['alias'].'</pre>'));
  $form->add(array('label'=>'证书密码','type'=>'html','key'=>'password','html'=>'<pre>'.$item['password'].'</pre>'));
  $form->add(array('label'=>'证书文件','type'=>'html','key'=>'fileurl','html'=>'<pre>'.($item['fileurl']?$item['fileurl']:'第一次使用时自动创建').'</pre>'));
}else{
  $form->add(array('label'=>'证书别名','type'=>'text','key'=>'alias','hint'=>'仅支持英文或数字','notempty'=>'','group'=>''));
  $form->add(array('label'=>'证书密码','type'=>'text','key'=>'password','hint'=>'仅支持英文或数字','notempty'=>'','group'=>''));
  $form->add(array('label'=>'证书文件','type'=>'upyun','key'=>'fileurl','hint'=>'','notempty'=>'','group'=>''));
}


#处理提交
if($onez=$form->submit()){
  if(!$onez['alias'] || preg_match('/[^a-zA-Z0-9]+/i',$onez['alias'],$mat)){
    onez()->error('证书别名只允许英文字母或数字');
  }
  if(!$onez['password'] || preg_match('/[^a-zA-Z0-9]+/i',$onez['password'],$mat)){
    onez()->error('证书密码只允许英文字母或数字');
  }
  ob_clean();
  if($id){
    $G['this']->data()->open('android_keystore')->update($onez,"id='$id'");
  }else{
    $onez['userid']=$G['userid'];
    if($type=='auto'){
      $onez['index1']=1;
    }
    $id=$G['this']->data()->open('android_keystore')->insert($onez);
  }
  onez()->ok('操作成功','reload2');
}
onez('admin')->header();
?>
<section class="content-header">
  <h1>
    <?=$G['title']?>
  </h1>
  <ol class="breadcrumb">
    <li>
      <a href="<?php echo onez()->href('/')?>">
        <i class="fa fa-dashboard">
        </i>
        管理首页
      </a>
    </li>
    <li class="active">
      <?=$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">
          <?=$G['title']?>
        </h3>
        <div class="box-tools pull-right">
        </div>
      </div>
      <div class="box-body">
        <?=$form->code()?>
      </div>
      <div class="box-footer clearfix">
        <button type="submit" class="btn btn-primary">
          <?=$btnname?>
        </button>
      </div>
    </div>
    <input type="hidden" name="action" value="save" />
  </form>
</section>
<?php
echo $form->js();
onez('admin')->footer();
?>