<?php
!defined('IN_ONEZ') && exit('Access Denied');
$action=onez()->gp('action');
if($action=='delete'){
  $id=(int)onez()->gp('id');
  $G['this']->data()->open('task')->update(array('index12'=>1),"userid='$G[userid]' and id='$id'");
  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){
      $G['this']->data()->open('task')->update($result,"id='$id'");
    }
  }
  $tasks=array();
  $T=$G['this']->data()->open('task')->record("userid='$G[userid]' and 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);

}
$xxx='';
$record=$G['this']->data()->open('task')->page("userid='$G[userid]' and index12=0$xxx 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>
      </tr>
    </thead>
    <tbody class="todo-list">
      <?php foreach($record[0] as $rs){
        /*
        ob_clean();
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); 
header("Cache-Control: no-cache, must-revalidate"); 
header("Pramga: no-cache"); 
    $ico=new Imagick();
    $ico->setFormat("ico");
    $img=new Imagick();
    $img->readImageBlob(file_get_contents($rs['icon']));
    $img->setImageFormat("png");
    
    $clone = $img->clone();
    $clone->scaleImage(256,0);    
    $ico->addImage($clone);
    $clone->clear();
    $clone->destroy();
    
    $clone = $img->clone();
    $clone->scaleImage(48,0);    
    $ico->addImage($clone);
    $clone->clear();
    $clone->destroy();
    
    $clone = $img->clone();
    $clone->scaleImage(32,0);    
    $ico->addImage($clone);
    $clone->clear();
    $clone->destroy();
    
    $clone = $img->clone();
    $clone->scaleImage(16,0);    
    $ico->addImage($clone);
    $clone->clear();
    $clone->destroy();
    
    $ico->writeImages(ONEZ_CACHE_PATH."/favicon.ico", true);
    
    $data=onez()->read(ONEZ_CACHE_PATH."/favicon.ico");
    $ico->clear();
    $ico->destroy();
    $img->clear();
    $img->destroy();
    onez('download')->call($data,'test.ico');
    exit();
        ob_clean();
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); 
header("Cache-Control: no-cache, must-revalidate"); 
header("Pramga: no-cache"); 
        $data=$G['this']->ico(file_get_contents($rs['icon']));
    onez('download')->call($data,'001.ico');
    exit();
        */
         ?>
      <tr>
        <td style="vertical-align: middle"><?=$rs['id']?></td>
        <td style="vertical-align: middle">
          <div class="pictext2">
            <img src="<?=$rs['icon']?>" />
            <div><?=$rs['softname']?></div>
            <div><a href="<?=$rs['softurl']?>" target="_blank"><?=$rs['softurl']?></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);
        ?>
          
          
              <a href="javascript:void(0)" onclick="onez.del('<?=$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">您没有生成过任何客户端，<a href="<?=onez()->href('/index.php?menu=build')?>">点此立即生成</a></p>
        </td>
      </tr>
      <?php }?>
    </tbody>
  </table>
</div>
<?=$record[1]?>
<p>如在生成过程中发现问题，请与QQ：6200103联系</p>
<p style="color:red">注：下载地址生成后服务器保留7天，过期自动删除，请及时下载</p>
<script type="text/javascript">
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>