<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');?>
<?php $this->load->view(TPL_FOLDER."header");?>
<table width="100%" border="0" align="center" cellpadding="5" cellspacing="0" >
  <tr>
    <td width="3%" align="left" ><img src="{tpl_path}images/forward.jpg" /></td>
    <td width="97%" align="left" >您现在的位置：淘宝店铺数据采集系统</td>
  </tr>
</table>
<fieldset>
<legend>第一步:填写店铺掌柜的淘宝账号</legend>
  <table width="96%"  border="0" cellpadding="5" cellspacing="0"   style="margin:10px; line-height:30px">
    <tr>
      <td style="border-bottom:#CCCCCC 1px solid"><img src="{tpl_path}images/tbnick.jpg" /><br />进入掌柜的淘宝店铺，如上图所示，一般在店铺的顶部可以看到该账号，暂时只支持单个店铺的数据采集，不支持多个店铺同时采集。把你要采集店铺的掌柜账号填入下面的文本框中。</td>
    </tr>
    <tr>
      <td align="center"><input type="text" maxlength="50" name="nick" id="nick" size="20" /> &nbsp;&nbsp;<input type="button" value="确定" id="sbc" onclick="get_summary()" class="button-style2" /><br />
      <div class="blockdiv" style="display:none; line-height:30px"></div>
      </td>
    </tr>
  </table>
</fieldset>
<fieldset>
<legend>第二步:采集数据</legend>
  <table width="96%"  border="0" cellpadding="5" cellspacing="0" style="margin:10px; line-height:30px" id="process">
   
  </table>
</fieldset>
<script language="javascript">
var goods_total = 0;
var cat_total = 0;
var nick = '';
var pId = 0;
var mitem;
function get_data(o)
{
	if( ! nick)
	{
		alert('请先填写店铺掌柜账号，然后再采集');
		return ;
	}
	if(goods_total == 0 && cat_total == 0)
	{
		alert('操作有误，没有任何数据可采集');
		return ;
	}
	if(confirm('确定要开始采集吗'))
	{
		o.disabled = true;
		document.getElementById('sbc').disabled = true;
		mitem = $('.cqueue');
		pId = 0;
		ajax_process();
	}
}

function ajax_process()
{
	if(pId>mitem.length-1) 
	{
		$('<tr><td align="center" style="font-weight:bold;color:#009900" colspan="3">全部数据采集完毕</td></tr>').insertBefore('#process tr:eq(0)');
		document.getElementById('sbc').disabled = false;
		return ;
	}
	var ppar = "md5key=<?php echo md5($this->config->item('md5_encode_key'));?>&mitem="+mitem.eq(pId).attr('id')+'&nick='+escape(nick);
	if(typeof(mitem.eq(pId).attr('page_no')) != 'undefined') ppar += '&page_no='+mitem.eq(pId).attr('page_no');
	mitem.eq(pId).html('<img src="'+pic_loading_path+'" /> 数据采集中...');
	$.ajax({
		url:js_root_path+"<?php echo CTL_FOLDER;?>get_top_data/ajax_make",
		data:ppar,
		type:"POST",
		success:function(msg)
		{
			if(msg=="1")
			{
				mitem.eq(pId).html("<font style=\"color:#009900\">采集完毕</font>");
			}
			else
			{
				mitem.eq(pId).html("<font style=\"color:#FF0000\">采集失败</font>");
			}
			pId++;
			ajax_process();
		}
	});
}

function get_summary()
{
	nick = document.getElementById('nick').value;
	if(nick != '')
	{
		$('div.blockdiv').html('<img src="'+pic_loading_path+'" /> 数据处理中...').show();
		$('#process').html('');
		$.ajax({
			url:js_root_path+"<?php echo CTL_FOLDER;?>get_top_data/get_summary",
			data:"md5key=<?php echo md5($this->config->item('md5_encode_key'));?>&nick="+escape(nick),
			type:"POST",
			dataType:'json',
			success:function(msg)
			{
				var str = '';
				if(msg.cat_total != 'undefined')
				{
					str += '<p>店铺自定义类目数：'+msg.cat_total+'&nbsp;&nbsp;&nbsp;&nbsp;';
					str += '店铺商品总数：'+msg.goods_total+'</p>';
					if(msg.goods_total > 0 || msg.cat_total > 0)
					str += '<p><input type="button" value="点击开始采集" onclick="get_data(this)" class="button-style" style="width:100px"/></p>';
					goods_total = msg.goods_total;
					cat_total = msg.cat_total;
					if(cat_total > 0)
					{
						$('<tr><td width="17%" align="left">店铺自定义类目('+cat_total+')</td><td align="center" valign="middle">----------------------------------------------------------------------------------------------------</td><td width="15%" align="right" class="cqueue" id="get_cat"><font color="#FF0000">等待采集</font></td></tr>').appendTo('#process');
					}
					if(goods_total > 0)
					{
						var page_total = Math.ceil(goods_total / 40);
						for(var i = 1; i <= page_total; i++)
						{
							$('<tr><td width="17%" align="left">店铺商品(第'+i+'页/'+page_total+')</td><td align="center" valign="middle">----------------------------------------------------------------------------------------------------</td><td width="15%" align="right" class="cqueue" id="get_goods" page_no="'+i+'"><font color="#FF0000">等待采集</font></td></tr>').appendTo('#process');
						}
					}
					$('div.blockdiv').html(str);
				}
				else
				{
					str += '<p>远程采集数据出错，请检查输入是否正确</p>';
					$('div.blockdiv').html(str);
					setTimeout(function(){$('div.blockdiv').slideUp();},2000);
				}
			},
			error:function()
			{
				$('div.blockdiv').html('数据处理失败');
				setTimeout(function(){$('div.blockdiv').slideUp();},2000);
			}
		});
	}
	else
	{
		alert('店铺掌柜账号必填');
	}
}
</script>
<?php $this->load->view(TPL_FOLDER."footer");?>