<?php

namespace App\Admin\Traits;

use Encore\Admin\Form;
use Encore\Admin\Form\Field\Image;
use Encore\Admin\Form\Field\MultipleFile;
use Encore\Admin\Form\Field\MultipleImage;
use Illuminate\Contracts\Support\Renderable;

trait WithUploads
{
    /**
     * Notes   : 单张封面图上传
     *
     * @Date   : 2021/4/25 2:06 下午
     * @Author : <Jason.C>
     * @param  Form  $form
     * @param  string  $filed
     * @param  string  $label
     * @return Image
     */
    public function cover(Renderable $form, string $filed = 'cover', string $label = '封面图片'): Image
    {
        $cover = $form->image($filed, $label)
            ->move('images/'.date('Y/m/d'))
            ->uniqueName()
            ->removable()
            ->retainable();

        $waterConfig = config('admin.image_water');

        if (! empty($waterConfig)) {
            $cover->insert(...$waterConfig);
        }

        $coverThumb = config('admin.cover_thumb');

        if (! empty($coverThumb)) {
            $cover->thumbnail($coverThumb);
        }
        return $cover;
    }

    /**
     * Notes   : 统一的多图上传
     *
     * @Date   : 2021/4/25 2:06 下午
     * @Author : <Jason.C>
     * @param  Form  $form
     * @param  string  $filed
     * @param  string  $label
     * @return MultipleImage
     */
    public function pictures(Renderable $form, string $filed = 'pictures', string $label = '多图轮播'): MultipleImage
    {
        $pictures = $form->multipleImage($filed, $label)
            ->move('images/'.date('Y/m/d'))
            ->uniqueName()
            ->removable()
            ->retainable();

        // 多图如果开启排序的话，会报错，暂时没由解决办法 ->sortable()
        $waterConfig = config('admin.image_water');

        if (! empty($waterConfig)) {
            $pictures->insert(...$waterConfig);
        }
        return $pictures;
    }

    /**
     * Notes   : 统一的附件上传
     *
     * @Date   : 2021/4/25 3:03 下午
     * @Author : <Jason.C>
     * @param  Renderable  $form
     * @param  string  $filed
     * @param  string  $label
     * @return MultipleFile
     */
    public function attachments(
        Renderable $form,
        string $filed = 'attachments',
        string $label = '内容附件'
    ): MultipleFile {
        return $form->multipleFile($filed, $label)
            ->move('attachments/'.date('Y/m/d'))
            ->uniqueName()
            ->removable()
            ->retainable()
            ->sortable();
    }
}
