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

$action=onez()->gp('action');
if($action=='rebuild'){
  $id=(int)onez()->gp('id');
  $T=$G['this']->data()->open('task')->one("id='$id'");
  $G['this']->build($T);
  onez()->ok('请求成功','reload');

}elseif($action=='status'){
  $ids=onez()->gp('ids');
  !$ids && onez()->error('no ids');
  $status=onez()->gp('status');
  
  $data=onez()->post('http://build.onez.cn/?_m=agent_task_status',http_build_query(array(
    'ids'=>$ids,
  )));
  $json=json_decode($data,1);
  if($json['tasks']){
    foreach($json['tasks'] as $id=>$result){
      $result && $G['this']->data()->open('task')->update($result,"id='$id'");
    }
  }
  $tasks=array();
  $T=$G['this']->data()->open('task')->record("id in ($ids)");
  foreach($T as $rs){
    $tasks[$rs['id']]=array(
      'id'=>$rs['id'],
      'status'=>$rs['key1'],
      'html'=>$G['this']->showstatus($rs),
    );
  }
  $A['tasks']=$tasks;
  onez()->output($A);
}
$record=$G['this']->data()->open('task')->page("1 order by id desc");
?>
<style>
.pictext2{
  padding-left: 40px;
  position: relative;
}
.pictext2 img{
  width:32px;height:32px;
  position: absolute;
  left: 0;
  top: 2px;
}
</style>
<div class="panel panel-primary">
	<div class="panel-heading">生成记录</div>
  <table class="table table-striped">
    <thead>
      <tr>
        <th>编号</th>
        <th>用户</th>
        <th>客户端</th>
        <th>提交时间</th>
        <th>状态</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody class="todo-list">
      <?php foreach($record[0] as $rs){
        $u=$G['this']->data()->open('member')->one("id='$rs[userid]'"); ?>
      <tr>
        <td style="vertical-align: middle"><?=$rs['id']?></td>
        <td style="vertical-align: middle"><img src="<?=$G['this']->avatar($rs['userid'])?>" style="width:16px;height:16px;position: relative;top: -2px;" /> <a href="<?=onez()->href('/index.php?menu=users&kw='.$rs['userid'])?>"> <?=$u['nickname']?> (<?=$u['key2']?>)
          <span style="color:#999;display: block;"><?=$rs['userid']?></span></a>
          <!--<?=var_export($rs,1)?>-->
        </td>
        <td style="vertical-align: middle">
          <div class="pictext2">
            <img src="<?=$rs['icon']?>" />
            <div><?=$rs['softname']?></div>
            <div><a href="<?=$rs['softurl']?>" target="_blank"><?=onez()->substr($rs['softurl'],0,60)?></a></div>
          </div>
          
        </td>
        <td style="vertical-align: middle"><?=date('Y-m-d H:i:s',$rs['addtime'])?></td>
        <td style="vertical-align: middle" data-id="<?=$rs['id']?>" data-status="<?=$rs['key1']?>"><?
        echo $G['this']->showstatus($rs);
        if($rs['index12']){
          echo ' <span class="label label-xs label-danger">DEL</span>';
        }
        ?></td>
        <td style="vertical-align: middle"><a href="javascript:void(0)" onclick="onez.rebuild(<?=$rs['id']?>)" class="btn btn-xs btn-danger">
            重新生成
          </a></td>
      </tr>
      <?php }?>
      <?php if(!$record[0]){?>
      <tr>
        <td colspan="10" class="text-center">
          <p style="margin-top: 10px">没有用户生成过任何客户端</p>
        </td>
      </tr>
      <?php }?>
    </tbody>
  </table>
</div>
<?=$record[1]?>
<script type="text/javascript">
$(function(){
  onez.rebuild=function(id){
    onez.confirm('您确定要重新生成这个客户端吗？',function(){
      $.post(window.location.href,{action:'rebuild',id:id,ajax:1},function(data){
        onez.doit(data);
      },'json');
    });
  };
});
function _status(){
  var ids=[];
  $('[data-status]').each(function(){
    var status=$(this).attr('data-status');
    var id=$(this).attr('data-id');
    if(status!='setup' && status!='zip' && status!='new' && status!='pay' && status!='apk'){
      ids.push(id);
    }
  });
  if(ids.length>0){
    $.post(window.location.href,{action:'status',ids:ids.join(',')},function(data){
      if(data.tasks){
        for(var id in data.tasks){
          $('[data-id="'+id+'"]').attr('data-status',data.tasks[id].status).html(data.tasks[id].html);
        }
      }
    },'json');
    setTimeout(_status,2000);
  }
}
_status();
</script>