<?php

namespace app\index\controller;

use app\common\controller\Frontend;

/**
 * 我的海报
 */
class Poster extends Frontend
{

    protected $layout = 'default';
    protected $noNeedRight = ["*"];
    // Child模型对象
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Poster;
        $posterLog = new \app\admin\model\PosterLog;
    }

    /**
     * 我的海报列表
     *
     * @param string  $token Token
     * @param integer $page  页码
     */
    public function index()
    {
        $where = array(
            'status' => 'normal'
        );
        $list = $this->model->where($where)->order('weigh DESC')->paginate(1);
        foreach ($list as $row) {
            $row->visible(['id', 'title', 'bg_image', 'data']);
        }
        $this->view->assign('list', $list);
        $this->view->assign('title', "我的海报");
        return $this->view->fetch();
    }

    /**
     * 生成海报
     * @author Created by Xing <464401240@qq.com>
     */
    public function getposter($poster_id = null)
    {
        $poster = $this->model->get($poster_id);
        if (!$poster) {
            $this->error(__('未找到海报' . $poster_id));
        }

        $image = new \addons\poster\library\Image();
        $imgurl = $image->createPosterImage($poster, $this->auth->getUser());

        if (!$imgurl) {
            $this->error('生成海报出错');
        }

        $this->success('', '', '/' . $imgurl);
    }
}