<?php

namespace Content\Lib;

use Dever;

class Feature
{
    # 根据文章id 获取文章信息
    public function get($data, $uid = false)
    {
        if (!is_array($data)) {
            $data = Dever::db('content/feature')->find($data);
        }
        
        if (!$data) {
            Dever::alert('错误的专题信息');
        }
    
        $data = $this->getContent($data, $uid);

        return $data;
    }

    public function getContent($data, $uid = false)
    {
        //embed
        $data['content_array'] = array();

        $data['content'] = Dever::filter($data['content']);

        $content = $data['content'];
        $replace = array();

        if (strstr($data['content'], 'data-id')) {
            # 商品
            preg_match_all('/<img(.*?)data-id="(.*?)" data-key="(.*?)" \/>/', $content, $matches);
            
            
            if (!isset($matches[2][0])) {
                preg_match_all('/<img(.*?)data-key="(.*?)" data-id="(.*?)" \/>/', $content, $matches);
                $temp = array();
                if (isset($matches[2][0]) && isset($matches[3][0])) {
                    $temp = $matches;
                    $matches[2] = $temp[3];
                    $matches[3] = $temp[2];
                    unset($temp);
                }
            }
            
            if (isset($matches[2][0]) && isset($matches[3][0])) {

                foreach ($matches[2] as $k => $v) {
                    $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
                    if ($matches[3][$k] == 'goods/lib/manage.editor') {
                        $method = 'goods/lib/info';
                        $type = 3;

                        $info = Dever::load($method)->getInfo($v);
                        if (isset($info['content'])) {
                            unset($info['content']);
                        }

                        $replace[] = array('id' => $v, 'type' => $type, 'content' => $info);
                    }
                }
            }
        } 
        /*
        elseif (strstr($data['content'], 'jpg')) {
            # gif
            preg_match_all('/<img(.*?)src="(.*?)\.jpg" (.*?)\/>/', $content, $matches);

            if (isset($matches[2])) {
                foreach ($matches[2] as $k => $v) {
                    $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
                    $pic = $v;

                    $replace[] = array('type' => 2, 'pic' => $pic . '.jpg');
                }
            }
        }

        elseif (strstr($data['content'], 'gif')) {
            # gif
            preg_match_all('/<img(.*?)src="(.*?)\.gif" (.*?)\/>/', $content, $matches);

            if (isset($matches[2])) {
                foreach ($matches[2] as $k => $v) {
                    $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
                    $pic = $v;

                    $replace[] = array('type' => 2, 'pic' => $pic . '.gif');
                }
            }
        }

        elseif (strstr($data['content'], 'png')) {
            # gif
            preg_match_all('/<img(.*?)src="(.*?)\.png" (.*?)\/>/', $content, $matches);

            if (isset($matches[2])) {
                foreach ($matches[2] as $k => $v) {
                    $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
                    $pic = $v;

                    $replace[] = array('type' => 2, 'pic' => $pic . '.png');
                }
            }
        }
        */

        //$data['content'] = preg_replace('/<div class="dever-drop">([\s\S]*?)<\/div>/i', '', $data['content']);

        $content = explode('{replace}', $content);
        $data['content_array'] = array();
        //print_r($content);die;
        foreach ($content as $k => $v) {
            $v = trim($v);
            if (is_numeric($v) && $v >= 0 && isset($replace[$v])) {
                $data['content_array'][] = $replace[$v];
            } elseif ($v) {
                $data['content_array'][] = array
                (
                    'type' => 1,
                    'content' => $v,
                );
            }
        }

        if (!$data['content_array']) {
            $data['content_array'][] = array
            (
                'type' => 1,
                'content' => $data['content'],
            );
        }

        unset($data['content']);

        return $data;
    }
}