<?php

/* ========================================================================
 * $Id: store.php 7945 2017-04-01 08:01:02Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onezphp.com
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');
define('CUR_URL',onez('store')->href('/store/store.php',0));
$type=onez()->gp('type');
$market=onez()->gp('market');
$input=onez()->gp('input');
$urlstr="market=$market&input=$input";
$action=onez()->gp('action');

$types=array();
$types['all']='全部';
$types['site']='综合';
$types['app_event']='APP';
$types['weixin_event']='微信';
$types['auto_event']='移动';
if($market && $types[$market]){
  $types=array($market=>$types[$market]);
}
!$type && $type=key($types);
if($action=='enabled'){
  $id=(int)onez()->gp('id');
  $s=(int)onez()->gp('s');
  $T=$G['this']->data()->open('addons')->update(array('index1'=>$s),"id='$id'");
  onez('store')->update_addons();
  onez()->ok('操作成功');
}elseif($action=='install'){//安装应用
  $ptype=onez()->gp('ptype');
  $ptoken=onez()->gp('ptoken');
  $subject=onez()->gp('subject');
  $summary=onez()->gp('summary');
  $version=onez()->gp('version');
  $__THIS__=$G['this'];
  onez($ptoken)->init();
  $G['this']=$__THIS__;
  $T=$G['this']->data()->open('addons')->one("token='$ptoken' and key12='$ptype'");
  if(!$T){
    $onez=array();
    $onez['userid']=$G['userid'];
    $onez['token']=$ptoken;
    $onez['key11']=$ptoken;
    $onez['key12']=$ptype;
    $onez['index1']=1;
    $onez['index2']=(int)onez()->gp('cloud_search');
    $onez['index3']=(int)onez()->gp('cloud_bind');
    $onez['index4']=(int)onez()->gp('cloud_task');
    $onez['subject']=$subject;
    $onez['summary']=$summary;
    $onez['version']=$version;
    $G['this']->data()->open('addons')->insert($onez);
  }
  onez('store')->update_addons();
  onez('showmessage')->success('安装应用成功',onez('store')->href('/store/store.php?type='.$type));
}elseif($action=='uninstall'){
  $id=(int)onez()->gp('id');
  $ptoken=onez()->gp('ptoken');
  $subject=onez()->gp('subject');
  $T=$G['this']->data()->open('addons')->one("id='$id'");
  if($T){
    $G['this']->data()->open('addons')->delete("id='$id'");
  }
  onez('store')->update_addons();
  onez()->ok('卸载应用成功','reload');
}elseif($action=='steps'){
  ob_clean();
  $ids=explode(',',onez()->gp('ids'));
  $n=-1000;
  foreach($ids as $k=>$id){
    $rs=array();
    $rs['step']=$n;
    $G['this']->data()->open('addons')->update($rs,"id='$id'");
    $n++;
  }
  onez('store')->update_addons();
  onez()->ok('重新排序成功','reload');
}
$xxx='';
$record=$G['this']->data()->open('addons')->page("1$xxx order by step,id");
onez('onezjs')->init();
$G['title']='应用中心';
onez('admin')->header();
onez('animate.css')->head();

$shopurl=onez('store')->shopurl($type);
?>
<style>
.desc p{
  padding-bottom: 0;
  margin-bottom: 0;
  line-height: 1.8;
}
</style>
<section class="content-header">
  <h1>
    <?php echo $G['title']?><small></small>
  </h1>
  <ol class="breadcrumb">
    <li>
      <a href="<?php echo onez()->href('/')?>">
        <i class="fa fa-dashboard">
        </i>
        管理首页
      </a>
    </li>
    <li class="active">
      <?php echo $G['title']?>
    </li>
  </ol>
</section>
<section class="content">
  <div class="btns" style="padding-bottom: 10px">
    <div class="pull-left">
      <a href="http://www.onezphp.com" target="_blank" class="btn btn-success">
        佳蓝应用市场
      </a>
      <?php /*foreach($types as $k=>$v){
        $style=$type==$k?'info':'default';
        ?>
        <a href="<?php echo onez('store')->href('/store/store.php?type='.$k.'&'.$urlstr)?>" class="btn btn-<?php echo $style?>"><?php echo $v?></a>
      <?php }
      */?>
    </div>
    <div class="pull-right">
      <a href="<?php echo onez('store')->href('/store/store.setting.php')?>" data-width="1280" class="onez-miniwin btn btn-info">
        全局设置
      </a>
    </div>
    <div class="clearfix"></div>
  </div>
  
  <div class="box box-info">
    <div class="box-header with-border">
      <h3 class="box-title">
        <?php echo $G['title']?>
      </h3>
      <div class="box-tools pull-right">
      </div>
    </div>
    <div class="box-body  table-responsive no-padding">
      <table class="table table-striped">
        <thead>
          <tr>
                      <th width="48">应用图标</th>
                      <th>应用名称</th>
            <th>
              当前版本
            </th>
                      <th>操作</th>
          </tr>
        </thead>
        <tbody class="todo-list">
          <?php foreach($record[0] as $rs){
             ?>
          <tr>
                      <td>
                <img  class="handle" src="http://shop.onezphp.com/?method=icon&ptoken=<?php echo $rs['token'];?>" width="48" style="border-radius: 6px" data-id="<?php echo $rs['id'];?>" />
                      </td>
            <td>
              <div class="desc">
                <p style="font-size:14px"><a href="http://www.onezphp.com/?mod=/view.php&ptoken=<?php echo $rs['token']?>" target="_blank"><?php echo $rs['subject'];?></a> <code><?php echo $rs['token']?></code></p>
                <p style="font-size:12px;color:#999"><?php echo $rs['summary'];?></p>
                <p class="links"><?php echo onez('store')->urls_admin($rs['token'])?></p>
              </div>
                      </td>
            <td style="font-size:12px">
              v<?php echo $rs['version']?$rs['version']:'1.0'?>
            </td>
                      <td>
              <?php if($rs['index1']){?>
              <a href="javascript:void(0)" onclick="_enabled('<?php echo $rs['id'];?>',0)" class="btn btn-xs btn-info" title="点击停用">
                已启用
              </a>
              <?php }else{?>
              <a href="javascript:void(0)" onclick="_enabled('<?php echo $rs['id'];?>',1)" class="btn btn-xs btn-default" title="点击启用">
                已停用
              </a>
              <?php }?>
              <a href="<?php echo onez('store')->href('/store/store.edit.php?id='.$rs['id'])?>" class="onez-miniwin btn btn-xs btn-success">
                编辑
              </a>
              <a href="javascript:void(0)" onclick="_uninstall('<?php echo $rs['id'];?>')" class="btn btn-xs btn-danger">
                卸载
              </a>
                      </td>
                    
          </tr>
          <?php }?>
        </tbody>
      </table>
    </div>
<?php if($record[1]){ ?>
    <div class="box-footer clearfix">
      <?php echo $record[1]?>
    </div>
<?php }?>
  </div>
</section>
<?php if($input){?>
<script type="text/javascript">
function _select(v){
  parent.postMessage({
    '<?php echo $input?>':v
  },'*');
}
</script>
<?php }?>
<script type="text/javascript">
$(function(){
  $(".todo-list").sortable({
    placeholder: "tr",
    handle: ".handle",
    forcePlaceholderSize: true,
    stop: function(){
      var ids=[];
      $('[data-id]').each(function(){
        ids.push($(this).attr('data-id'));
      });
      $.post(window.location.href,{action:'steps',ids:ids.join(',')},function(){
      });
    },
    zIndex: 999999
  });
});
function _enabled(id,s){
  $.post(window.location.href,{action:'enabled',id:id,s:s},function(data){
    location.reload();
  },'json');
}
function _upgrade(id){
  onez.confirm('强制更新后此应用将会立即更新到官方最新版本，是否继续？',function(){
    $.post(window.location.href,{action:'upgrade',id:id},function(data){
      if(typeof data.message!='undefined'){
        onez.alert(data);
      }
    },'json');
  });
}
function _uninstall(id){
  onez.confirm('您确定要卸载这个应用吗？应用内容数据将被清空，且不可恢复！',function(){
    $.post(window.location.href,{action:'uninstall',id:id},function(data){
      location.reload();
    },'json');
  });
}
$(function(){
  window.addEventListener('message', function(event){
    var data=event.data;
    data.action='install';
    closeWin();
    $.post(window.location.href,data,function(o){
      if(o.error){
        onez.alert(o.error);
      }else{
        location.reload();
      }
    },'json');
  }, false);
});
</script>
<?php
onez('admin')->footer();
?>
