<?php

/* ========================================================================
 * $Id: add.php 3676 2020-07-08 08:50:32Z 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');
    $options=array();
    $options['number']='纯数字';
    $options['letter']='纯字母';
    $options['letternumber']='混合';
    $form->add(array('label'=>'卡号格式','key'=>'format1','type'=>'select','options'=>$options,));
    $form->add(array('label'=>'卡号长度','key'=>'len1','type'=>'text','value'=>10,));
    $form->add(array('label'=>'卡密格式','key'=>'format2','type'=>'select','options'=>$options,));
    $form->add(array('label'=>'卡密长度','key'=>'len2','type'=>'text','value'=>6,));
    $form->add(array('label'=>'生成数量','key'=>'num','value'=>'1',));
    $form->add(array('label'=>'面额','key'=>'price','value'=>'',));
    
    $options=array();
    $options['0']='永不过期';
    $options['date']='指定时间';
    $form->add(array('label'=>'有效期','key'=>'exptimeType','ptoken'=>'radio','options'=>$options,));
    $form->add(array('label'=>'过期时间','key'=>'exptime','ptoken'=>'form.time','format'=>'Y-m-d','group'=>'exptimeType=date',));
    if($onez=$form->submit()){
      $num=$onez['num'];
      if($num<1){
        return $this->error('生成数量不能小于1');
      }
      if(onez('bs.app.shipments')->licence('branch')=='demo'){
        if($num>10){
          return $this->error('演示版限制：单次最多可以生成10个');
        }
        $n=$G['this']->data()->open('cards')->rows("upid='$upid'");
        if($n>100){
          return $this->error('演示版限制：库存充足，无需生成');
        }
      }
      $upid=onez()->gp('upid');
      $Format['number']='0123456789';
      $Format['letter']='abcdefghjkmnpqrstuvwxyz';
      $Format['letternumber']=$Format['number'].$Format['letter'];
      $format1=$Format[$onez['format1']];
      $format2=$Format[$onez['format2']];
      $fLen1=strlen($format1);
      $fLen2=strlen($format2);
      for($i=1;$i<=$num;$i++){
        while(1){
          $item=array();
          $item['key1']='';
          for($j=1;$j<=$onez['len1'];$j++){
            $w=$format1[rand(0,$fLen1)];
            $item['key1'].=$w;
          }
          if(strlen($item['key1'])<$onez['len1']){
            continue;
          }
          $item['key2']='';
          for($j=1;$j<=$onez['len2'];$j++){
            $w=$format2[rand(0,$fLen2)];
            $item['key2'].=$w;
          }
          if(strlen($item['key2'])<$onez['len2']){
            continue;
          }
          $item['name']=$item['key1']=strtoupper($item['key1']);
          $item['pwd']=$item['key2']=strtoupper($item['key2']);
          $n=$G['this']->data()->open('cards')->rows("key1='$item[key1]'");
          if($n>0){
            continue;
          }
          $item['money1']=$item['price']=$onez['price'];
          $item['exptimeType']=$onez['exptimeType'];
          if($onez['exptimeType']=='0'){
            $item['key5']=0;
            $item['index2']=$item['exptime']=0;
            $item['exptimeName']='永不过期';
          }else{
            $item['key5']='date';
            $item['index2']=$item['exptime']=strtotime($onez['exptime']);
            $item['exptimeName']=date('Y-m-d',$item['exptime']);
          }
          $item['userid']=$G['userid'];
          $item['upid']=$upid;
          $G['this']->data()->open('cards')->insert($item);
          break;
        }
      }
      return $this->ok('生成成功','reload2');
    }
    $form->output($A);
  }
  return;
}
$this->add(array(
  'type'=>'pageform',
  'name'=>'表单',
  'data'=>'form',
));
