<?php 
require_once ('../../../../wp-load.php');
wp_head();
echo '<link rel="stylesheet" href="'.WP_PLUGIN_URL.'/ctspider/file/layui.css">';
echo '<link rel="stylesheet" href="'.WP_PLUGIN_URL.'/ctspider/file/ctspider.css">';
$data = get_option('ctspider_data');
$task = get_option('ctspider_task');
$type = get_option('ctspider_type');

/**
*  任务前台显示类
*/
class TaskShows
{
	// 任务数据库设置信息
	private $task;

	// 显示数据
	private $data; // array('htmldata','custom'); htmldata = 固定规则数据, custom = 文章自定义规则数据

	// 显示类别设置
	private $type;

	// 初始化
	function __construct($task, $data, $type)
	{
		// 初始化任务设置信息
		$this->task = $task;

		// 初始化显示数据信息
		$this->data = $data;

		// 初始化显示类别
		$this->type = $type;

	}

	// 弹出层接口调用
	public function ShowHtml() 
	{
		//获取显示数据
		$data = $this->data['htmldata'];
		// print_r($data);
	    echo '<div class="layui-container-fuill"><div class="layui-row layui-form-pane cthtml-pane" style="padding:20px;">';
	    if ($this->type == 'listtest') $this->Listtest($data);
	    if ($this->type == 'contenttest') $this->Contenttest($data);
	    echo '</div></div>';
	}


	// 列表显示
	private function Listtest($data)
	{
		if (empty($data)) {
			echo '<div class="layui-container">  
					<div class="layui-row">
						<div class="layui-col-md12">
							<div class="layadmin-tips" style="text-align:center; padding:50px;"><i class="layui-icon" face="" style="font-size:100px"></i><h3>没有获取任何数据...</h3>
							</div>
						</div>
					</div>
				</div>';
			return false;
		}
	    foreach ($data as $key => $value) {

	    	// 标题显示
	    	if (!empty($value['listtitle'])) {
		        echo '<div class="layui-form-item">
		                    <label class="layui-form-label layui-color-red layui-bg-black"><span class="layui-badge layui-bg-red">'.($key + 1).'</span> 标题：</label>
		                    <div class="layui-input-block">
		                    <input type="text" value="'.ctesc_html(trim($value['listtitle'])).'" class="layui-input" disabled>
		                    </div>
		                </div>';
	    	}else {
		        echo '<div class="layui-form-item">
		                    <label class="layui-form-label layui-color-red layui-bg-black"><span class="layui-badge layui-bg-red">'.($key + 1).'</span> 标题：</label>
		                    <div class="layui-input-block">
		                    <input type="text" style="color:red" value="没有获取 [标题] 请查看采集规则" class="layui-input" disabled>
		                    </div>
		                </div>';
	    	}
  
	        // 链接显示
		    if (!empty($value['url'])) {
			    echo $this->HtmlDiv($value['url'], '链接', false);
		    } else {
		    	echo $this->HtmlDiv('没有获取 [链接] 请查看采集规则', '链接', true);
		    }	                          
 
		    // 缩略图显示
	        if ($this->task['list']['list_thumb'] == 1) {   
			    if (!empty($value['thumb'])) {
				    echo $this->HtmlDiv($value['thumb'], '缩略图', false);
			    } else {
			    	echo $this->HtmlDiv('没有获取 [缩略图] 请查看采集规则', '缩略图', true);
			    }	               
	        }       

	        //echo '<hr class="layui-bg-cyan1">';
	        echo '<br>';
	    }
	}


	//html 展示代码
	private function HtmlDiv($data, $title, $style = false, $class = '')
	{
		$style == true ? $style = 'style="color:red"' : $style = '';
		return '<div class="layui-form-item">
		                <label class="layui-form-label layui-color-red layui-bg-blue '.$class.'">'.$title.'：</label>
		                <div class="layui-input-block">
		                <input type="text" '.$style.' value="'.ctesc_html($data).'" class="layui-input" disabled>
		                </div>
		            </div>';
	}


	//html 展示自定义属性代码
	private function HtmlDivCustom($data, $title, $style = false) 
	{
		$style == true ? $style = 'style="color:red"' : $style = '';
        return '<div class="layui-form-item layui-form-text">
                    <label class="layui-form-label ctspider-custom layui-color-red layui-bg-green">'.$title.'：</label>
                    <div class="layui-input-block">
                    <input type="text" '.$style.' value="'.$data.'" class="layui-input" disabled>
                    </div>
                </div>';
	}

	//文章内容显示
	private function Contenttest($data)
	{
	    // tag标签格式化
	    if (!empty($data)) {
		    foreach ($data as $key => $value) {
		        if (!empty($value['tag'])) {
		                @$tag .= $value['tag'].' 	';
		            }                     
		    }
	    }


	    if (!empty($data[0]['url'])) {
		    echo $this->HtmlDiv($data[0]['url'], '原文');
	    } else {
	    	echo $this->HtmlDiv('没有获取 [URL] 请查看采集规则', '原文', true);
	    }

	    if (!empty($data[0]['title'])) {
	    	echo $this->HtmlDiv(trim($data[0]['title']), '标题');
	    } else {
	    	echo $this->HtmlDiv('没有获取 [标题] 请查看采集规则', '标题', true);
	    }

	    if (!empty($data[0]['time'])) {
	    	echo $this->HtmlDiv($data[0]['time'], '时间');               
	    } else {
	    	//echo $this->HtmlDiv('没有获取 [时间] 请查看采集规则', '时间', true);
	    }               

	    if (!empty($data[0]['tag'])) {
	    	echo $this->HtmlDiv(TrimArray($tag), 'Tag');                
	    } else {
	    	//echo $this->HtmlDiv('没有获取 [Tag] 请查看采集规则', 'Tag');
	    }

	    if (!empty($data[0]['excerpt'])) {
	    	echo $this->HtmlDiv(trim($data[0]['excerpt']), '摘要');
	    } else {
	    	//echo $this->HtmlDiv('没有获取 [摘要] 请查看采集规则', '摘要');
	    }         

	    if (!empty($data[0]['thumb'])) {
	    	echo $this->HtmlDiv($data[0]['thumb'], '特色图片');               
	    } else {
	    	//echo $this->HtmlDiv('没有获取 [特色图片] 请查看采集规则', '特色图片');
	    }

	    // 自定义字段属性 显示
	    if (!empty($this->data['custom'])) {
	        foreach ($this->data['custom'] as $key => $value) {
	        	if (!empty($data[0][$value])) {
	        		echo $this->HtmlDivCustom($data[0][$value], '自定义属性 ( '.$value.' )');
	            } else {
	            	echo $this->HtmlDivCustom('没有获取 ['.$value.'] 请查看采集规则', '自定义属性 ( '.$value.' )', true);
	            }
	        }
	    }         

	    if (!empty($data[0]['content'])) {
	    	echo '<div class="layui-form-item">';
	        wp_editor( $data[0]['content'], 'ctspider_content', $settings = array(
	                                    'quicktags'=>1,
	                                    'tinymce'=>1,
	                                    'media_buttons'=>0,
	                                    'textarea_rows'=>28,
	                                   	'editor_class'=>"textareastyle"
	                                ) );
	    	echo '</div>';		
    	} else {
    		echo $this->HtmlDivCustom('没有获取 [文章内容] 请查看采集规则', '文章内容', true);
    	}
	}


}

$html = new TaskShows($task,$data,$type);
$html->ShowHtml();
wp_footer();