<?php
namespace app\controller\manager;

use app\model\{Category, Block, Log};
use app\validate\Block as VBlock;
use think\exception\ValidateException;

class Page extends Base
{
    //源码,代码
    public function code()
    {
        if($this->request->isPost()){
            $item = input('post.item/a');
            $categoryId = input('post.category_id/d');
            $id = input('post.id/d');
            try{
                validate(VBlock::class)->check($item);
                if(empty($item['value'])){
                    return $this->json(2, '内容不可为空!');
                }
                $block = Block::getByKeyword($item['keyword'], $categoryId);
                if($id){
                    if(!empty($block) && $block['id'] != $id){
                        return $this->json(3, '键值已存在,请更改键值');
                    }
                    Block::updateById($id, $item);
                    Log::write('page', 'code', "单页代码编辑,ID:{$id}, 键值:{$item['keyword']}");
                }else{
                    if($categoryId <= 0){
                        return $this->json(4, '栏目参数错误!');
                    }
                    if(!empty($block)){
                        return $this->json(3, '键值已存在,请更改键值');
                    }
                    $item['category_id'] = $categoryId;
                    $item['type'] = Block::CODE;
                    $block = Block::create($item);
                    Log::write('page', 'code', "单页代码新增,ID:{$block->id}, 键值:{$item['keyword']}");
                }
                return $this->json();
            } catch (ValidateException $e){
                return $this->json(1, $e->getError());
            }
        }else{
            $id = input('param.id/d');
            if($id <= 0){    //添加
                $categoryId = input('param.category_id/d');
                $category = Category::getById($categoryId);
                if(empty($category)){
                    $url = url('manager.content/index')->__toString();
                    return $this->error('无此栏目', $url);
                }
            }else{    //修改
                $item = Block::getById($id);
                if(empty($item)){
                    return $this->error('无此代码块!');
                }
                $categoryId = $item['category_id'];
                $this->data['item'] = $item;
            }
            $this->data['categoryId'] = $categoryId;
            return $this->view();
        }
    }
    //排序
    public function sort()
    {
        if($this->request->isPost()){
            $id = input('post.id/d');
            $sort = input('post.sort');
            $num = input('post.num/d', 1);
            if($num <= 0){
                $num = 1;
            }
            if(!in_array($sort, ['up', 'down'], true)){
                return $this->json(2, '参数错误');
            }
            $item = Block::getById($id);
            if(empty($item)){
                return $this->json(3, '无此块信息');
            }
            if($sort == 'up'){
                $where = "category_id='{$item['category_id']}' and sort < {$item['sort']}";
                $order = "sort desc";
            }else{
                $where = "category_id='{$item['category_id']}' and sort > {$item['sort']}";
                $order = "sort asc";
            }
            $forSortItems = Block::getListByWhereAndOrder($where, $order, $num);
            if(!empty($forSortItems)){
                $updateData = [];
                $forSortCount = count($forSortItems);
                for($i = 0; $i < $forSortCount; $i++){
                    if($i == 0){
                        $updateData[] = [
                            'id' => $forSortItems[$i]['id'],
                            'sort' => $item['sort']
                        ];
                    }else{
                        $updateData[] = [
                            'id' => $forSortItems[$i]['id'],
                            'sort' => $forSortItems[$i - 1]['sort']
                        ];
                    }
                }
                $updateData[] = [
                    'id' => $item['id'],
                    'sort' => $forSortItems[$i - 1]['sort']
                ];
                if(!empty($updateData)){
                    $model = new Block();
                    $model->saveAll($updateData);
                    $sortStr = $sort == 'up' ? '上移' : '下调';
                    Log::write('page', 'sort', "单页区块排序,ID:{$id} ,键值:{$item['keyword']},{$sortStr}了{$num}位");
                    return $this->json();
                }
            }
            return $this->json(4, '无须调整排序!');
        }
        return $this->error('无此操作');
    }
    
    //删除
    public function delblock()
    {
        if ($this->request->isAjax()) {
            $id = input('post.id/d');
            $item = Block::getById($id);
            if(!empty($item)){
                Block::destroy($id);
                Log::write('page', 'delblock', "单页区块删除,ID:{$id} ,键值:{$item['keyword']}");
                return $this->json();
            }
            return $this->json(1, 'fail');
        }
        return $this->error('无此操作');
    }
    //图片
    public function img()
    {
        if($this->request->isPost()){
            $item = input('post.item/a');
            $img = trim(input('post.img'));
            $categoryId = input('post.category_id/d');
            if (!empty($img) && $img == 'null') {
                $img = '';
            }
            $id = input('post.id/d');
            try{
                validate(VBlock::class)->check($item);
                $block = Block::getByKeyword($item['keyword'], $categoryId);
                if($id){
                    if(!empty($block) && $block['id'] != $id){
                        return $this->json(4, '键值已存在,请更改键值');
                    }
                    if(!empty($img)){
                        $item['value'] = $img;
                    }
                    Block::updateById($id, $item);
                    Log::write('page', 'img', "单页图片编辑,ID:{$id} ,键值:{$item['keyword']}");
                }else{
                    if(!empty($block)){
                        return $this->json(4, '键值已存在,请更改键值');
                    }
                    if($categoryId <= 0){
                        return $this->json(2, '栏目参数错误!!');
                    }
                    if(empty($img)){
                        return $this->json(3, '图片不可为空');
                    }
                    $item['value'] = $img;
                    $item['type'] = Block::IMG;
                    $item['category_id'] = $categoryId;
                    $block = Block::create($item);
                    Log::write('page', 'img', "单页图片新增,ID:{$block->id} ,键值:{$item['keyword']}");
                }
                return $this->json();
            } catch (ValidateException $e){
                return $this->json(1, $e->getError());
            }
        }else{
            $id = input('param.id/d');
            if($id <= 0){    //添加
                $categoryId = input('param.category_id/d');
                $category = Category::getById($categoryId);
                if(empty($category)){
                    $url = url('manager.content/index')->__toString();
                    return $this->error('无此栏目', $url);
                }
            }else{    //修改
                $item = Block::getById($id);
                if(empty($item)){
                    return $this->error('无此图片!');
                }
                $categoryId = $item['category_id'];
                $this->data['item'] = $item;
            }
            if(isset($item) && $item['width'] && $item['height']){
                $imgSize = $item['width'].'px X '.$item['height'].'px';
            }else{
                $imgSize = '';
            }
            $this->data['categoryId'] = $categoryId;
            $this->data['imgSize'] = $imgSize;
            $this->data['groupId'] = session('auth.groupId');
            return $this->view();
        }
    }
    //文字块
    public function block()
    {
        if($this->request->isPost()){
            $item = input('post.item/a');
            $categoryId = input('post.category_id/d');
            $id = input('post.id/d');
            try{
                validate(VBlock::class)->check($item);
                if(empty($item['value'])){
                    return $this->json(1, '内容不可为空!');
                }
                $block = Block::getByKeyword($item['keyword'], $categoryId);
                if($id){
                    if(!empty($block) && $block['id'] != $id){
                        return $this->json(4, '键值已存在,请更改键值');
                    }
                    Block::updateById($id, $item);
                    Log::write('page', 'block', "单页文字块编辑,ID:{$id} ,键值:{$item['keyword']}");
                }else{
                    if($categoryId <= 0){
                        return $this->json(2, '栏目参数错误!');
                    }
                    if(!empty($block)){
                        return $this->json(4, '键值已存在,请更改键值');
                    }
                    $item['category_id'] = $categoryId;
                    $block = Block::create($item);
                    Log::write('page', 'block', "单页文字块新增,ID:{$block->id} ,键值:{$item['keyword']}");
                }
                return $this->json();
            } catch (ValidateException $e){
                return $this->json(1, $e->getError());
            }
        }else{
            $id = input('param.id/d');
            if($id <= 0){    //添加
                $categoryId = input('param.category_id/d');
                $category = Category::getById($categoryId);
                if(empty($category)){
                    $url = url('manager.content/index')->__toString();
                    return $this->error('无此栏目', $url);
                }
            }else{    //修改
                $item = Block::getById($id);
                if(empty($item)){
                    return $this->error('无此文字块!');
                }
                $categoryId = $item['category_id'];
                $this->data['item'] = $item;
            }
            $this->data['categoryId'] = $categoryId;
            return $this->view();
        }
    }
    //富文本内容
    public function text()
    {
        if($this->request->isPost()){
            $item = input('post.item/a');
            $categoryId = input('post.category_id/d');
            try{
                validate(VBlock::class)->check($item);
                if(empty($item['value'])){
                    return $this->json(1, '内容不可为空!');
                }
                $block = Block::getByKeyword($item['keyword'], $categoryId);
                $id = input('post.id/d');
                if($id){
                    if(!empty($block) && $block['id'] != $id){
                        return $this->json(4, '键值已存在,请更改');
                    }
                    Block::updateById($id, $item);
                    Log::write('page', 'text', "单页富文本编辑,ID:{$id} ,键值:{$item['keyword']}");
                }else{
                    if($categoryId <= 0){
                        return $this->json(2, '栏目参数错误!');
                    }
                    if(!empty($block)){
                        return $this->json(4, '键值已存在,请更改键值');
                    }
                    $item['category_id'] = $categoryId;
                    $item['type'] = Block::TEXT;
                    $block = Block::create($item);
                    Log::write('page', 'text', "单页富文本新增,ID:{$block->id} ,键值:{$item['keyword']}");
                }
                return $this->json();
            } catch (ValidateException $e){
                return $this->json(1, $e->getError());
            }
        }else{
            $id = input('param.id/d');
            if($id <= 0){    //添加
                $categoryId = input('param.category_id/d');
                $category = Category::getById($categoryId);
                if(empty($category)){
                    $url = url('manager.content/index')->__toString();
                    return $this->error('无此栏目', $url);
                }
            }else{    //修改
                $item = Block::getById($id);
                if(empty($item)){
                    return $this->error('无此富文本!');
                }
                $categoryId = $item['category_id'];
                $this->data['item'] = $item;
            }
            $this->data['categoryId'] = $categoryId;
            return $this->view();
        }
    }
    //组图
    public function group()
    {
        if($this->request->isPost()){
            $item = input('post.item/a');
            $imgs = input('post.img/a');
            $categoryId = input('post.category_id/d');
            if (!empty($imgs) && is_array($imgs)) {
                $item['value'] = json_encode($imgs);
            } else {
                $item['value'] = '';
            }
            try{
                validate(VBlock::class)->check($item);
                $block = Block::getByKeyword($item['keyword'], $categoryId);
                $id = input('post.id/d');
                if($id){
                    if(!empty($block) && $block['id'] != $id){
                        return $this->json(4, '键值已存在,请更改');
                    }
                    Block::updateById($id, $item);
                    Log::write('page', 'group', "单页组图编辑,ID:{$id} ,键值:{$item['keyword']}");
                }else{
                    if($categoryId <= 0){
                        return $this->json(2, '栏目参数错误!');
                    }
                    if(!empty($block)){
                        return $this->json(4, '键值已存在,请更改键值');
                    }
                    $item['category_id'] = $categoryId;
                    $item['type'] = Block::GROUP;
                    $block = Block::create($item);
                    Log::write('page', 'group', "单页组图新增,ID:{$block->id} ,键值:{$item['keyword']}");
                }
                return $this->json();
            } catch (ValidateException $e){
                return $this->json(1, $e->getError());
            }
        }else{
            $id = input('param.id/d');
            if($id <= 0){    //添加
                $categoryId = input('param.category_id/d');
                $category = Category::getById($categoryId);
                if(empty($category)){
                    $url = url('manager.content/index')->__toString();
                    return $this->error('无此栏目', $url);
                }
            }else{    //修改
                $item = Block::getById($id);
                if(empty($item)){
                    return $this->error('无此组图!');
                }
                $categoryId = $item['category_id'];
                $this->data['item'] = $item;
            }
            $this->data['categoryId'] = $categoryId;
            if(isset($item) && $item['width'] && $item['height']){
                $imgSize = $item['width'].'px X '.$item['height'].'px';
            }else{
                $imgSize = '';
            }
            $this->data['imgSize'] = $imgSize;
            $this->data['groupId'] = session('auth.groupId');
            return $this->view();
        }
    }
    //视频
    public function video()
    {
        if($this->request->isPost()){
            $item = input('post.item/a');
            $img = trim(input('post.img'));
            $video = trim(input('post.video'));
            $categoryId = input('post.category_id/d');

            try{
                validate(VBlock::class)->check($item);
                $block = Block::getByKeyword($item['keyword'], $categoryId);
                $id = input('post.id/d');
                if($id){
                    if(!empty($block) && $block['id'] != $id){
                        return $this->json(4, '键值已存在,请更改');
                    }
                    if(!empty($img)){
                        $item['img'] = $img;
                    }
                    if(!empty($video)){
                        $item['value'] = $video;
                    }
                    Block::updateById($id, $item);
                    Log::write('page', 'video', "单页视频编辑,ID:{$id} ,键值:{$item['keyword']}");
                }else{
                    if($categoryId <= 0){
                        return $this->json(2, '栏目参数错误!');
                    }
                    if(!empty($block)){
                        return $this->json(3, '键值已存在,请更改键值');
                    }
                    if(empty($video)){
                        return $this->json(3, '视频不可为空');
                    }
                    $item['category_id'] = $categoryId;
                    $item['type'] = Block::VIDEO;
                    $item['value'] = $video;
                    $item['img'] = $img;
                    $block = Block::create($item);
                    Log::write('page', 'video', "单页视频新增,ID:{$block->id} ,键值:{$item['keyword']}");
                }
                return $this->json();
            } catch (ValidateException $e){
                return $this->json(1, $e->getError());
            }
        }else{
            $id = input('param.id/d');
            if($id <= 0){    //添加
                $categoryId = input('param.category_id/d');
                $category = Category::getById($categoryId);
                if(empty($category)){
                    $url = url('manager.content/index')->__toString();
                    return $this->error('无此栏目', $url);
                }
            }else{    //修改
                $item = Block::getById($id);
                if(empty($item)){
                    return $this->error('无此视频!');
                }
                $categoryId = $item['category_id'];
                $this->data['item'] = $item;
            }
            $this->data['categoryId'] = $categoryId;
            if(isset($item) && $item['width'] && $item['height']){
                $imgSize = $item['width'].'px X '.$item['height'].'px';
            }else{
                $imgSize = '';
            }
            $this->data['imgSize'] = $imgSize;
            $this->data['groupId'] = session('auth.groupId');
            return $this->view();
        }
    }
}