<?php

namespace app\widget\manager;

use think\facade\View;

class Upload
{
    protected $uploadMaxSize = '2M';
    protected $data = [];

    public function __construct()
    {
        $uploadMaxSize = ini_get('post_max_size');
        if (!empty($uploadMaxSize)) {
            $this->uploadMaxSize = $uploadMaxSize;
        }
        $this->data['post_max_size'] = $this->uploadMaxSize;
    }

    protected function showView(string $template = '')
    {
        return View::assign($this->data)->fetch($template);
    }

    //视频
    public function file($src = '', $append = '', $fileSize = '')
    {
        if (empty($fileSize)) {
            $fileSize = '文件大小限制 '.ini_get('upload_max_filesize');
        }

        $this->data['src']      = $src;
        $this->data['append']   = $append;
        $this->data['fileSize'] = $fileSize;
        return $this->showView('manager/widget/file');
    }

    //视频
    public function video($src = '', $append = '', $videoSize = '')
    {
        if (empty($videoSize)) {
            $videoSize = '文件大小限制 '.ini_get('upload_max_filesize');
        }

        $this->data['src']       = $src;
        $this->data['append']    = $append;
        $this->data['videoSize'] = $videoSize;
        return $this->showView('manager/widget/video');
    }

    //图片（layui自带上传控件）,若同一页面内徐亚加载多层本上传控件则需要传不同的$append来区分控件ID
    public function image($src = '', $append = '', $imgSize = 0, $thumb = 0, $className = '', $midStr = '')
    {
        $this->data['src']       = $src;
        $this->data['append']    = $append;
        $this->data['imgSize']   = $imgSize;
        $this->data['thumb']     = $thumb;
        $this->data['className'] = $className;
        $this->data['midStr']    = $midStr;
        return $this->showView('manager/widget/image');
    }

    //上传文件，目前在文章中添加附件
    public function files($files = [], $num = 10, $append = '')
    {
        if (!empty($files) && $files == 'null') {
            $files = [];
        }
        $this->data['files']  = $files;
        $this->data['append'] = $append;
        $this->data['num']    = $num;
        return $this->showView('manager/widget/files');
    }

    /**
     * 水印图片上传
     * milo
     * 2018-01-13
     */
    public function mark($src = '')
    {
        $this->data['src'] = $src;
        return $this->showView('manager/widget/mark');
    }

    /**
     * layui组图上传
     * milo
     * @param  array|bool  $fields  图片额外树形字段，默认有alt link desc time 若此处设置后将替换默认值。为false则不填写 格式如下
     *                          ['alt' => 'alt', 'desc' => '描述，选填', 'link' => '链接，选填']
     */
    public function multi($imgs = [], $num = 10, $append = '', $imgSize = '', $fields = [])
    {
        if (!empty($imgs) && $imgs == 'null') {
            $imgs = [];
        }

        if (empty($fields)) {
            $fields = $fields === false ? [] : [
                'title' => '标题',
                'desc'  => '描述',
                'alt'   => 'alt',
                'link'  => '链接，选填',
                'time'  => '日期，选填',
            ];
        }

        $this->data['imgs']    = $imgs;
        $this->data['append']  = $append;
        $this->data['imgSize'] = $imgSize;
        $this->data['num']     = $num;
        $this->data['fields']  = $fields;
        return $this->showView('manager/widget/multi');
    }

    /**
     * 音频组上传
     */
    public function multiAudio($audios = [], $num = 10, $append = '')
    {
        if (!empty($audios) && $audios == 'null') {
            $audios = [];
        }
        $this->data['audios'] = $audios;
        $this->data['append'] = $append;
        $this->data['num']    = $num;
        return $this->showView('manager/widget/multi_audio');
    }

    /**
     * 视频组上传
     */
    public function multiVideo($videos = [], $num = 10, $append = '')
    {
        if (!empty($videos) && $videos == 'null') {
            $videos = [];
        }
        $this->data['videos'] = $videos;
        $this->data['append'] = $append;
        $this->data['num']    = $num;
        return $this->showView('manager/widget/multi_video');
    }
}
