<?php
!defined('IN_ONEZ') && exit('Access Denied');
$item=$G['this']->data()->open('task')->one("userid='$G[userid]' and token='android' and key1<>'zip' and key1<>'setuping' and key1<>'setup'");
$id=(int)$item['id'];

$action=onez()->gp('action');
if($action=='buy'){
  list($total)=$G['this']->totalprice($item);
  $G['user']['amt']<=$total && onez()->error('账户余额不足');
  
}
$ac=onez()->gp('ac');
if($ac=='extra'){
  #初始化表单
  $form=onez('admin')->widget('form')
    ->set('title',$G['title'])
    ->set('values',$item)
  ;
  $form->add(array('type'=>'hidden','key'=>'action','value'=>'save'));
  $btnname='下一步';
  $index=0;
  if($item['is_setup']){
    $index++;
    //$form->add(array('label'=>'安装协议','type'=>'textarea','key'=>'xieyi','hint'=>'','notempty'=>'安装协议不能为空','group'=>''));
  }
  if($index){
    #处理提交
    if($onez=$form->submit()){
      ob_clean();
      $onez['key1']='pay';
      $G['this']->data()->open('task')->update($onez,"id='$id'");
      onez()->ok(1,onez()->href('/index.php?menu=build.android&ac=pay'));
    }
    $btnname='';
    $btns='';
    $btns.='<a class="btn btn-default" href="'.onez()->href('/index.php?menu=build.android').'">上一步</a> ';
    $btns.='<button class="btn btn-success" type="submit">下一步</button> ';
  }else{
    $G['this']->data()->open('task')->update(array('key1'=>'pay'),"id='$id'");
    onez()->location(onez()->href('/index.php?menu=build.android&ac=pay'));
  }
}elseif($ac=='pay'){
  #$G['this']->build($item);
  #print_r($item);exit();
  if($item['key1']!='pay'){
    onez()->location(onez()->href('/index.php?menu=log'));
  }
  $paytype=onez()->gp('paytype');
  $action=onez()->gp('action');
  #初始化表单
  $form=onez('admin')->widget('form')
    ->set('title',$G['title'])
    ->set('values',$item)
  ;
  $form->add(array('type'=>'hidden','key'=>'action','value'=>'save'));
  $btnname='立即生成';
  $info=array();
  $info[]='应用图标: '."\t".'<img src="'.$item['icon'].'" style="width:48px;height:48px;position: relative;top: -10px;" />';
  $info[]='应用名称: '."\t".'<span class="value">'.$item['softname'].'</span>';
  $info[]='应用网址: '."\t".'<span class="value">'.$item['softurl'].'</span>';
  $info[]='应用包名: '."\t".'<span class="value">'.$item['packagename'].'</span>';
  $info[]='版本号: '."\t".'<span class="value">'.$item['version'].'</span>';
  $info[]='安卓证书: '."\t".'<span class="value">'.$item['keystore_name'].'</span>';
  
  $info[]='';
  
  $info=implode("\n",$info);
  $form->add(array('label'=>'应用信息','type'=>'html','html'=>'<pre>'.$info.'</pre>'));
  list($total,$html,$btns)=$G['this']->totalprice($item);
  if($paytype=='alipay'){
    ob_clean();
    onez('alipay')->pay($total,'客户端生成费用“'.$item['softname'].'”',array(
      'plugintoken'=>$G['this']->token,
      'paytype'=>'alipay',
      'payname'=>'支付宝支付',
      'itemid'=>$item['id'],
      'userid'=>$G['userid'],
      'type'=>'buy',
      'price'=>$total,
      'goto'=>'http://build.onez.cn/?mod=%2Findex.php&menu=log',
    ));
  }
  if($action=='save'){
    if($total==0){
      $use=$G['this']->once($G['userid']);
      if($use){
        $G['this']->build($item,'使用套餐次数');
        onez()->ok(1,onez()->href('/index.php?menu=log'));
      }else{
        onez()->error('套餐次数已用完');
      }
    }else{
      onez()->error('账户余额不足');
    }
  }
  $form->add(array('label'=>'费用清单','type'=>'html','html'=>'<pre>'.$html.'</pre>'));
  if($btns){
    $btnname='';
  }else{
    $btnname='';
    $btns.='<button class="btn btn-success" type="submit">立即生成</button> ';
    $btns.='<a class="btn btn-default" href="'.onez()->href('/index.php?menu=build.android').'">修改资料</a> ';
  }
}else{
  $btnname='下一步';
  $copyid=(int)onez()->gp('copyid');
  if($copyid){
    $item=$G['this']->data()->open('task')->one("id='$copyid'");
    unset($id);
  }
  !$item && $item=array(
    'softname'=>'我的应用',
    'softurl'=>'http://build.onez.cn/',
    'packagename'=>'cn.onez.build',
    'version'=>'1.0',
    'company'=>'佳蓝',
    'homepage'=>'http://build.onez.cn/',
    'icon'=>'http://xl.onez.cn/cache/plugins/form.file/2016/11/15/582a77a0dbb0c.jpg',
    'splash'=>'http://cdn.onez.cn/2017/0706/2017070613440875170001.png',
    'is_tray'=>'1',
  );
  #初始化表单
  $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'=>'softname','hint'=>'','notempty'=>'应用名称不能为空','group'=>''));
  $form->add(array('label'=>'应用网址','type'=>'text','key'=>'softurl','hint'=>'','notempty'=>'应用网址不能为空','group'=>''));
  $form->add(array('label'=>'版本号','type'=>'text','key'=>'version','hint'=>'','notempty'=>'','group'=>''));
  $form->add(array('label'=>'应用图标(512x512)','type'=>'form.file','key'=>'icon','hint'=>'','notempty'=>'桌面图标不能为空','group'=>'','cloudname'=>'云图标','cloud'=>'http://icon.onezphp.com/?key=icon&kw='.urlencode($item['softname'])));
  $form->add(array('label'=>'启动图片(640x1136)','type'=>'form.file','key'=>'splash','hint'=>'','notempty'=>''));
  $form->add(array('label'=>'应用包名','type'=>'text','key'=>'packagename','hint'=>'例：cn.onez.build','notempty'=>'应用包用不能为空','group'=>''));
  $form->add(array('label'=>'安卓证书 <a href="'.onez()->href('/pages/android/keystore.php').'" data-title="选择我的安卓证书" class="onez-miniwin btn btn-xs btn-info">选择</a>','type'=>'text','key'=>'keystore_name','hint'=>'','notempty'=>'安卓证书不能为空','group'=>''));
  $form->add(array('type'=>'hidden','key'=>'keystore_id','save'=>'1'));
  
  #处理提交
  if($onez=$form->submit()){
    if(!$onez['packagename'] || preg_match('/[^a-zA-Z0-9\.]+/i',$onez['packagename'],$mat)){
      onez()->error('应用包名格式有误');
    }
    ob_clean();
    if($id){
      $G['this']->data()->open('task')->update($onez,"id='$id'");
    }else{
      $onez['userid']=$G['userid'];
      $onez['token']='android';
      $onez['key1']='new';
      $id=$G['this']->data()->open('task')->insert($onez);
    }
    onez()->ok(1,onez()->href('/index.php?menu=build.android&ac=extra'));
  }
}
?>
<div class="panel panel-primary">
	<div class="panel-heading">在线生成安卓客户端</div>
	<form id="form-common" method="post" role="form" style="width:600px;margin:60px auto;display: block;">
        <?=$form->code()?>
        <input type="hidden" id="paytype" name="paytype" value="">
		<p class="text-center">
      <?if($btnname){?>
      <button class="btn btn-success" type="submit"><?=$btnname?></button>
      <?}?>
      <?if($btns){echo $btns;}?>
    </p>
	</form>
</div>
</div>
<script type="text/javascript">
$(function(){
  $('#input-softname').bind('input keyup',function(){
    $('label[for="input-icon"] a').attr('data-href','http://icon.onezphp.com/?key=icon&input=icon&kw='+encodeURIComponent($(this).val()));
  });
});
function _pay_alipay(){
  location.href='<?=onez()->href('/index.php?menu=build.android&ac=pay&paytype=alipay')?>';
}
function _pay_account(){
  onez.confirm('您确定要使用余额支付<code class="totalprice"><?=$total?></code>元生成客户端吗？',function(){
    $.post(window.location.href,{action:'buy',ajax:1},function(data){
      onez.doit(data);
    },'json');
  });
}
</script>
<?echo $form->js();?>