<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2023/8/25
 * Time: 16:12
 */
namespace app\api\controller;
use app\api\model\Book;
use app\api\model\Column;
use app\api\model\Genealogyarticle;
use app\common\controller\Api;
use app\lib\model\FamilyModel;

class Genealogy extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 获取谱文类目
     */
    public function getColumnList()
    {
        $family_id = $this->request->post('family_id', 0);//族谱id
        $where = ['status' => 0, 'family_id' => 0];
        $list = Column::where(['status' => 0, 'family_id' => 0])->select();
        if ($family_id) {
            $where['family_id'] = $family_id;
        }
        $extend_list = Column::where($where)->select();
        if ($extend_list) {
            $list = array_merge($list, $extend_list);
        }
        $this->success('获取成功', ['list' => $list]);
    }

    /**
     * 新增谱文类目
     */
    public function addColumn()
    {
        $family_id = $this->request->post('family_id', 0);//族谱id
        $title = xss_clean($this->request->post('title', 0));//类目标题
        $info = Column::where(['title' => $title])->find();
        if ($info) {
            $this->error('此类目已存在');
        }
        $data = [
            'title' => $title,
            'family_id' => $family_id,
            'createtime' => time(),
        ];
        Column::create($data);
        $this->success('创建成功');
    }

    /**
     * 删除谱文类目
     */
    public function delColumn()
    {
        $column_id = $this->request->post('column_id', 0);//类目id
        $column = Column::where(['id' => $column_id])->find();
        $family = FamilyModel::where(['id' =>$column['family_id'],'user_id' =>$this->auth->id])->find();
        if(!$family){
            $this->error('此类目不可删除');
        }
        $article = Genealogyarticle::where(['column_id' => $column_id])->find();
        if ($article) {
            $this->error('此类目下有谱文，暂不可删除');
        }
        Column::where(['id' => $column_id])->delete();
        $this->success('删除成功');
    }

    /**
     * 编辑谱文类目
     */
    public function editColumn()
    {
        $column_id = $this->request->post('column_id', 0);//类目id
        $title = xss_clean($this->request->post('title', 0));//类目标题
        $article = Column::where(['column_id' => $column_id, 'title' => $title])->find();
        if ($article) {
            $this->error('有重复，请重新编辑');
        }
        Column::update(['title' => $title], ['column_id' => $column_id]);
        $this->success('编辑成功');
    }

    /**
     * 提交谱文
     */
    public function submitArticle()
    {
        $title = xss_clean($this->request->post('title', 0));//标题
        $column_id = $this->request->post('column_id', 0);//类目id
        $image_url = $this->request->post('image_url', '');//缩略图
        $content = xss_clean($this->request->post('content', 0));//内容
        $family_id = $this->request->post('family_id', 0);//族谱id
        if (!$title || !$content || !$column_id) {
            $this->error('缩略图、标题、栏目、内容不能为空');
        }
        $family = FamilyModel::where(['id' =>$family_id,'create_user_id' =>$this->auth->id])->find();
        if(!$family){
            $this->error('此谱不是您创建');
        }
        $data = [
            'title' => $title,
            'column_id' => $column_id,
            'image_url' => $image_url,
            'content' => $content,
            'user_id' => $this->auth->id,
            'createtime' => time(),
            'family_id' => $family_id,
            'title' => $title,
        ];
        Genealogyarticle::create($data);
        $this->success('提交成功');
    }

    /**
     * 获取谱文列表
     */
    public function getArticleList()
    {
        $family_id = $this->request->post('family_id', 0);//族谱id
        $page = $this->request->post('page', 1);//族谱id
        $column_list = Genealogyarticle::alias('ga')
            ->join('genealogy_column gc', 'ga.column_id = gc.id', 'left')
            ->where(['ga.status' => 0, 'ga.family_id' => $family_id])
            ->field('gc.*')
            ->group('ga.column_id')
            ->select();
        if (!$column_list) {
            $this->error('暂无谱文');
        }
//        dump($column_list);die;
        foreach ($column_list as $key => $val) {
            $article_list = Genealogyarticle::where(['column_id' => $val['id']])->select();
            $column_list[$key]['article_list'] = $article_list;
        }

        $this->success('获取成功', ['list' => $column_list]);
    }


    /**
     * 文章详情
     */
    public function detail()
    {
        $article_id = $this->request->post('article_id', 0);
        if (!$article_id) {
            $this->error('查无此文章');
        }
        $info = Genealogyarticle::alias('ga')
            ->join('family_people p', 'p.user_id = ga.user_id', 'left')
            ->where(['ga.id' => $article_id])
            ->field("ga.*,p.name,p.photo,p.address,FROM_UNIXTIME(ga.createtime,'%Y-%m-%d %T') as createtime")
            ->find();

        $this->success('获取成功', ['info' => $info]);
    }

    /**
     * 编辑文章
     */
    public function editArticle()
    {
        $article_id = $this->request->get('article_id', 0);
        $info = Genealogyarticle::get($article_id);
        if (!$info) {
            $this->error(__('No Results were found'));
        }
        if (false === $this->request->isPost()) {
            $this->success('获取成功', ['info' => $info]);
        }
        $params = $this->request->post();
        extract($params);
        $params['title'] = xss_clean($params['title']);
        $params['content'] = xss_clean($params['content']);
        $info->save($params);
        $this->success('编辑成功');
    }

    /**
     * 删除谱文
     */
    public function delArticle()
    {
        $article_id = $this->request->post('article_id', 0);//类目id
        $article = Genealogyarticle::where(['id' => $article_id])->find();
        if (!$article) {
            $this->error('文章不存在');
        }
        Genealogyarticle::where(['id' => $article_id])->delete();
        $this->success('删除成功');
    }

    /**
     * 自动生成谱书
     */
    public function autoCreateBook()
    {
        $params = $this->request->post();
        extract($params);

        foreach ($params as $value){
            xss_clean($value);
        }

        $arr = [
            'title' =>$params['title'],
            'volume_number' =>$params['volume_number'],
            'hall_number' =>$params['hall_number'],
            'article_ids' =>$params['article_ids'],
            'user_id' =>$this->auth->id,
            'upload_name' =>$params['upload_name'],
            'createtime' =>time(),
            'format_id' =>$params['format_id'],
            'family_id' =>$params['family_id'],
        ];

        Book::create($arr);
        $this->success('自动生成谱书成功');
    }

    /**
     * 预览谱书
     */
    public function previewBook()
    {
        $book_id = $this->request->post('book_id',0);
        $info = Book::where(['id' =>$book_id,'status' =>0])->find();
        if(!$info){
            $this->error('此书已不存在');
        }
        $article_arr = explode(",", $info['article_ids']);
        $list = [];
        foreach ($article_arr as $key => $val){
            $article_info = Genealogyarticle::alias('ga')
                ->join('family_people p', 'p.user_id = ga.user_id', 'left')
                ->where(['ga.id' => $val])
                ->field("ga.*,p.name,p.photo,p.address,FROM_UNIXTIME(ga.createtime,'%Y-%m-%d %T') as createtime")
                ->find();
            if($article_info){
                $list[] = $article_info;
            }
        }
        $info['article_arr'] = $list;
        $this->success('获取成功', ['info' => $info]);
    }

    /**
     * 获取族谱谱书列表
     */
    public function getBookList()
    {
        $family_id = $this->request->post('family_id',0);
        $list = Book::where(['family_id' =>$family_id,'status' =>0])->select();
        if(!$list){
            $this->error('此书已不存在');
        }

        $this->success('预览谱书成功',['list' =>$list]);
    }

    /**
     * 编辑修改谱书
     */
    public function editBook()
    {
        $book_id = $this->request->get('book_id',0);
        $info = Book::get($book_id);
        if (!$info) {
            $this->error(__('No Results were found'));
        }
        if (false === $this->request->isPost()) {
            $this->success('获取成功', ['info' => $info]);
        }
        $params = $this->request->post();
        extract($params);

        foreach ($params as $value){
            xss_clean($value);
        }
        $info->save($params);
        $this->success('编辑成功');

    }

}