<?php

namespace app\widget\manager;

use Exception;
use think\facade\View;

class Upload
{
    //视频
    public function video($src = '', $append = '')
    {
        $data = [
            'src'    => $src,
            'append' => $append
        ];
        return View::assign($data)->fetch('manager/widget/video');
    }

    //图片（layui自带上传控件）,若同一页面内徐亚加载多层本上传控件则需要传不同的$append来区分控件ID
    public function image($src = '', $append = '', $imgSize = 0, $thumb = 0, $fullName = '')
    {
        $data = [
            'src'       => $src,
            'append'    => !empty($append) ? $fullName : '',//预防只传了fullName
            'imgSize'   => $imgSize,
            'fieldName' => $fullName ?: 'img'.$append,//最终后端接收数据的name
            'thumb'     => $thumb,
        ];
        return View::assign($data)->fetch('manager/widget/image');
    }

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

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

    /**
     * layui组图上传
     * milo
     */
    public function multi_bak($imgs = [], $num = 10, $append = '', $imgSize = '')
    {
        if (!empty($imgs) && $imgs == 'null') {
            $imgs = [];
        }
        $data = [
            'imgs'    => $imgs,
            'append'  => $append,
            'imgSize' => $imgSize,
            'num'     => $num
        ];
        return View::assign($data)->fetch('manager/widget/multi_bak');
    }

    /**
     * @param  array  $imgs  需要展示的图片列表
     * @param  int  $num  最大数量
     * @param  string  $append  追加名称，用于多组图片时区别名称 当$fullName存在时不生效
     * @param  string  $imgSize  图片建议尺寸
     * @param  string  $fullName  组图name 传递到后台程序的名字 如image  后台则收到image的数组
     * @param  array|bool  $fields  图片额外树形字段，默认有alt link desc time 若此处设置后将替换默认值。为false则不填写 格式如下
     *                          ['alt' => 'alt', 'desc' => '描述，选填', 'link' => '链接，选填']
     * @return string
     * @throws Exception
     */
    public function multi(array $imgs = [], int $num = 10, string $append = 'img', string $imgSize = '', string $fullName = '', $fields = []): string
    {
        if (!empty($imgs) && $imgs == 'null') {
            $imgs = [];
        }

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

        $data = [
            'imgs'      => $imgs,
            'append'    => $append ?: $fullName,//预防只传了fullName
            'imgSize'   => $imgSize,
            'fields'    => $fields,
            'fieldName' => $fullName ?: 'img'.$append,//最终后端接收数据的name
            'num'       => $num
        ];
        return View::assign($data)->fetch('manager/widget/multi');
    }
}
