<?php
namespace app\controller\manager;

use app\model\{Category, Article, Block, Log, History};

class Content extends Base
{
    //文章
    public function article()
    {
        $categoryId = input('param.category_id');
        $keyword = input('param.keyword');
        $param = input('param.param/a', []);
        $category = Category::getById($categoryId);
        if(empty($category)){
            return $this->error('无此栏目');
        }
        $list = Article::getList($categoryId, 20, $keyword, $param);
        $this->data['list'] = $list;
        $this->data['category'] = $category;
        $this->data['keyword'] = $keyword;
        $this->data['param'] = $param;
        return $this->view();
    }

    //做页面跳转
    public function index()
    {
        $items = Category::getList();
        if(!empty($items)){
            $items = Category::getCates($items);
        }

        if(!empty($items)){
            $first = array_shift($items);
            if(isset($first['children'])){
                $childrenFirst = array_shift($first['children']);
                $url = url('manager.content/'.$childrenFirst['manager'],['category_id' => $childrenFirst['id']]);
            }else{
                $url = url('manager.content/'.$first['manager'],['category_id' => $first['id']]);
            }
            if(!empty($url)){
                return $this->redirect($url);
            }
        }else{
            return $this->redirect(url('manager.category/add'));
        }
    }
    //单页
    public function page()
    {
        if($this->request->isAjax()){
            $blocks = input('post.block/a');    //所有文本信息
            $texts = input('post.text/a');      //所有富文本信息
            $codes = input('post.code/a');      //所有代码信息
            $categoryId = input('post.category_id/d');
            $category = Category::getById($categoryId);
            unset($_POST['block']);
            unset($_POST['text']);
            unset($_POST['file']);
            unset($_POST['code']);
            $imgs = [];     //图片信息
            $videos = [];   //视频信息
            $groups = [];   //组图信息
            $groupIds = input('post.groupIds/a', []);
            foreach($_POST as $key => $val){
                if(strpos($key, '_') !== FALSE){
                    $keys = explode('_',$key);
                    if($keys[1] == 'img'){  //图片
                        $imgs[$keys[2]] = $val;
                    }elseif($keys[1] == 'video'){   //视频
                        $videos[$keys[2]][$keys[0]] = $val;
                    }elseif($keys[1] == 'group'){    //组图
                        $groups[$keys[2]] = $val;
                    }
                }
            }
            $data = [];
            if(!empty($blocks)){
                foreach($blocks as $key => $block){
                    $data[] = [
                        'id' => $key,
                        'value' => $block
                    ];
                }
            }
            if(!empty($texts)){
                foreach($texts as $key => $text){
                    $data[] = [
                        'id' => $key,
                        'value' => $text
                    ];
                }
            }
            if(!empty($codes)){
                foreach($codes as $key => $code){
                    $data[] = [
                        'id' => $key,
                        'value' => $code
                    ];
                }
            }
            if(!empty($imgs)){
                foreach($imgs as $key => $img){
                    $data[] = [
                        'id' => $key,
                        'value' => $img
                    ];
                }
            }
            if(!empty($videos)){
                foreach($videos as $key => $video){
                    $data[] = [
                        'id' => $key,
                        'img' => $video['img'],
                        'value' => $video['video']
                    ];
                }
            }
            if(!empty($groupIds)){
                foreach($groupIds as $key => $groupId){
                    $group = $groups[$groupId] ?? [];
                    $data[] = [
                        'id' => $groupId,
                        'value' => json_encode($group)
                    ];
                }
            }
            $block = new Block;
            $block->saveAll($data);
            Log::write('content', 'page', "单页编辑,栏目ID:{$category['id']} ,标题:{$category['title']}");
            return $this->json();
        }else{
            $categoryId = input('param.category_id');
            $children = Category::getChildrenByParentId($categoryId);
            if(empty($children)){
                $category = Category::getById($categoryId);
            }else{
                $child = Category::getChildrenByParentId($children[0]['id']);
                if(empty($child)){
                    $category = $children[0];
                }else{
                    $category = $child[0];
                }
            }
            if(empty($category)){
                return $this->redirect(url('manager.content/index'));
            }
            $blocks = Block::getByCategoryId($category['id']);
            $this->data['categoryId'] = $categoryId;
            $this->data['category'] = $category;
            $this->data['blocks'] = $blocks;
            $this->data['groupId'] = session('auth.groupId');
            $this->data['types'] = Block::getTypes();
            return $this->view();
        }
    }

    // 发展历程
    public function history()
    {
        $categoryId = input('param.category_id/d', 0);
        $category = Category::getById($categoryId);
        if(empty($category)){
            return $this->redirect(url('manager.content/index'));
        }

        $this->data['categoryId'] = $categoryId;
        $this->data['category'] = $category;
        $this->data['items'] = History::getPaginateList($categoryId, 20, false);
        return $this->view();
    }
}