<?php
!defined('IN_ONEZ') && exit('Access Denied');
$item=$G['this']->data()->open('task')->one("userid='$G[userid]' and token='desktopnw' 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'){
  #初始化表单
  !$item['xieyi'] && $item['xieyi']='欢迎安装'.$item['softname'].'！';
  $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.desktopnw&ac=pay'));
    }
    $btnname='';
    $btns='';
    $btns.='<a class="btn btn-default" href="'.onez()->href('/index.php?menu=build.desktopnw').'">上一步</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.desktopnw&ac=pay'));
  }
}elseif($ac=='pay'){
  #$G['this']->build($item);
  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[]='exe文件名: '."\t".'<span class="value">'.$item['exename'].'</span>';
  $info[]='版本名: '."\t".'<span class="value">'.$item['version'].'</span>';
  
  !$item['width'] && $item['width']=960;
  !$item['height'] && $item['height']=650;
  $info[]='窗口宽度尺寸: '."\t".'<span class="value">'.$item['width'].' * '.$item['height'].'</span>';
  if($item['url_print_api']){
    $info[]='高级打印接口: '."\t".'<span class="value">'.$item['url_print_api'].'</span>';
  }
  $info[]='';
  
  if($item['is_tray']){
    $info[]='<span class="value">右下角托盘显示</span>';
  }
  if($item['is_close2min']){
    $info[]='<span class="value">点击关闭按钮后转为最小化，窗口不会关闭，可点击托盘右键退出</span>';
  }
  if($item['is_setup']){
    $info[]='<span class="value">生成一个setup安装包</span> <a href="javascript:;" data-toggle="modal" data-target="#win_xieyi">点击查看安装协议</a>';
    $G['footer'].=<<<ONEZ
<!-- Modal -->
<div class="modal fade" id="win_xieyi" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">安装包协议</h4>
      </div>
      <div class="modal-body">
        <pre style="max-height:400px;overflow:hidden;overflow-y:auto;">$item[xieyi]</pre>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal">确定</button>
      </div>
    </div>
  </div>
</div>
ONEZ;
  }
  if($item['is_max']){
    $info[]='<span class="value">打开默认最大化</span>';
  }
  $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.desktopnw').'">修改资料</a> ';
  }
}else{
  $btnname='下一步';
  $copyid=(int)onez()->gp('copyid');
  if($copyid){
    $item=$G['this']->data()->open('task')->one("id='$copyid'");
  }
  !$item && $item=array(
    'softname'=>'客户端',
    'softurl'=>'http://build.onez.cn/',
    'token'=>'main',
    'version'=>'1.0',
    'company'=>'佳蓝',
    'homepage'=>'http://build.onez.cn/',
    'icon'=>'http://xl.onez.cn/cache/plugins/form.file/2016/11/15/582a77a0dbb0c.jpg',
    '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'=>'exename','hint'=>'用于生成文件名，只允许英文字母或数字','notempty'=>'唯一标识不能为空','group'=>''));
  $form->add(array('label'=>'版本号','type'=>'text','key'=>'version','hint'=>'','notempty'=>'','group'=>''));
  #$form->add(array('label'=>'公司名称','type'=>'text','key'=>'company','hint'=>'','notempty'=>'','group'=>''));
  #$form->add(array('label'=>'官网地址','type'=>'text','key'=>'homepage','hint'=>'','notempty'=>'','group'=>''));
  $form->add(array('label'=>'窗口宽度','type'=>'text','key'=>'width','hint'=>'默认960像素','notempty'=>'','group'=>''));
  $form->add(array('label'=>'窗口高度','type'=>'text','key'=>'height','hint'=>'默认650像素','notempty'=>'','group'=>''));
  #$form->add(array('label'=>'打印接口(高级接口，如没有请勿填写)','type'=>'text','key'=>'url_print_api','hint'=>'','notempty'=>'','group'=>''));
  #$form->add(array('label'=>'','type'=>'space',));
  #$form->add(array('label'=>'默认最大化','type'=>'checkbox','key'=>'is_max','hint'=>'','notempty'=>'','group'=>''));
  #$form->add(array('label'=>'右下角托盘显示','type'=>'checkbox','key'=>'is_tray','hint'=>'','notempty'=>'','group'=>''));
  #$form->add(array('label'=>'点关闭最小化(可使用托盘右键退出)','type'=>'checkbox','key'=>'is_close2min','hint'=>'','notempty'=>'','group'=>''));
  #$form->add(array('label'=>'只显示关闭按钮','type'=>'checkbox','key'=>'is_close_only','hint'=>'','notempty'=>'','group'=>''));
  #$form->add(array('label'=>'禁止调整大小','type'=>'checkbox','key'=>'is_nosize','hint'=>'','notempty'=>'','group'=>''));
  $form->add(array('label'=>'自定义package.json','type'=>'textarea','key'=>'package_json','hint'=>'','notempty'=>'','group'=>''));
  $form->add(array('label'=>'生成setup安装包','type'=>'checkbox','key'=>'is_setup','hint'=>'','notempty'=>'','group'=>''));
  $form->add(array('label'=>'桌面图标','type'=>'form.file','key'=>'icon','hint'=>'','notempty'=>'桌面图标不能为空','group'=>'','cloudname'=>'云图标','cloud'=>'http://icon.onezphp.com/?key=icon&kw='.urlencode($item['softname'])));

  #处理提交
  if($onez=$form->submit()){
    if(!$onez['exename'] || preg_match('/[^a-zA-Z0-9]+/i',$onez['exename'],$mat)){
      onez()->error('唯一标识只允许英文字母或数字');
    }
    ob_clean();
    if($id){
      $G['this']->data()->open('task')->update($onez,"id='$id'");
    }else{
      $onez['userid']=$G['userid'];
      $onez['token']='desktopnw';
      $onez['key1']='new';
      $id=$G['this']->data()->open('task')->insert($onez);
    }
    onez()->ok(1,onez()->href('/index.php?menu=build.desktopnw&ac=extra'));
  }
}
?>
<div class="panel panel-danger">
	<div class="panel-heading">
    高级定制版说明
  </div>
  <div class="panel-body" style="font-size:18px;line-height:2">
    <p>1、本服务仅面向普通封装无法满足需要的用户，如果您只需要把网页打包成本地客户端(exe)，使用<a href="index.php">标准封装</a>即可，无需定制。</p>
    <p>2、定制范围包括：内部网页与客户端交互、网页功能扩展、开机自动运行、消息通知、右下角托盘、多开、兼容Linux系统、兼容苹果系统等，详情请联系客服。</p>
    <p>3、开发周期为1-3个工作日，定制费用500元起，详情请联系客服。</p>
    <div style="text-align: center;font-size:28px;padding:20px 0;font-weight:bold;">
      咨询QQ：6200103，微信：onez-team
      <a href="<?=onez()->href('')?>" class="btn btn-lg btn-default url-build">
        返回
      </a>
    </div>
  </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.desktopnw&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();?>