<?php

// app/Libraries/CommonHelper.php
namespace App\Libraries;

use Dcat\Admin\Admin;

class CommonHelper
{

    /*
     * $images 格式:['image.jpg','image2.jpg']
     * 返回显示的HTML显示图片
     */
    public static function displayImage($images,$boxSize=60,$imgSize=1024)
    {
        if (empty($images) || empty($images[0])) {
            $largeUrl = $thumbnailUrl = '/static/images/no-image.jpg';
            $html = "<a href='$largeUrl' target='_blank'><img src='$thumbnailUrl' style='height:{$boxSize}px; margin-right:5px; border: 1px solid #ececf1;'></a>";
        } else {


            // 默认显示 100x100 的图片
            $thumbnailImages = array_map(function ($imageUrl) use ($boxSize) {
                $imageUrl= CommonHelper::ossUrl($imageUrl);
                $extension = pathinfo($imageUrl, PATHINFO_EXTENSION);
                if ($extension == 'svg') {
                    return $imageUrl;
                } else {
                    return $imageUrl . "?x-oss-process=image/resize,m_pad,h_{$boxSize},w_{$boxSize},color_ffffff";
                }

            }, $images);

            // 生成点击查看大图的链接
            $largeImages = array_map(function ($imageUrl) use ($imgSize) {
                $imageUrl= CommonHelper::ossUrl($imageUrl);
                $extension = pathinfo($imageUrl, PATHINFO_EXTENSION);
                if ($extension == 'svg') {
                    return $imageUrl;
                } else {
                    return $imageUrl . "?x-oss-process=image/resize,m_lfit,w_{$imgSize},h_{$imgSize}";
                }
            }, $images);

            // 显示缩略图,并添加点击查看大图的功能
            $html = '';
            foreach ($thumbnailImages as $index => $thumbnailUrl) {
                $largeUrl = $largeImages[$index];
                $html .= "<a href='$largeUrl' target='_blank'><img src='$thumbnailUrl' style='height:{$boxSize}px; margin-right:5px; border: 1px solid #ececf1;'></a>";
            }

            return $html;

//
//            //默认用等比例缩放
//            $process = "?x-oss-process=image/resize,h_{$imgSize},m_lfit";
//            $html = '<div style="display: flex; flex-wrap: wrap; gap: 5px;">';
//            foreach ($images as $image) {
//                $html .= "<div style='width: {$boxSize}px; height: {$boxSize}px; padding: 3px; border: 1px solid #ddd; border-radius: 3px; background-color: #f9f9f9; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); display: flex; align-items: center; justify-content: center;'>
//                    <img  data-action='preview-img'   src='" . self::ossUrl($image).$process . "' style='max-width: 100%; max-height: 100%; object-fit: contain;'>
//                  </div>";
//            }
//            $html .= '</div>';
        }
        return $html;
    }

    /*
     * 返回oss的url
     */
    public static function ossUrl($imageUrl)
    {
        if (strpos($imageUrl, 'http:') === 0 || strpos($imageUrl, 'https:') === 0) {
            return  $imageUrl;
        }

        if (env('OSS_DOMAIN')) {
            return "http://".env('OSS_DOMAIN').'/'.$imageUrl;
        }
        return "http://".env('OSS_BUCKET').'.'.env('OSS_ENDPOINT').'/'.$imageUrl;
    }

    /*
     * 替换新增与编辑的url,在后边加上指定的参数 (适用于弹出框的新增与编辑)
     * $addButton = '.tree-quick-create';
     * $editButton = '.tree-quick-edit';
     * $paramsUrl = 'location=0';
     */
    public static function replaceAddEditerUrl($addButton,$editButton,$paramsUrl) {

        Admin::script(
            <<<JS
var button = $('{$addButton}');
var currentUrl = button.attr('data-url');
if (currentUrl.indexOf('?') === -1) {
    button.attr('data-url', currentUrl + '?{$paramsUrl}');
} else {
    button.attr('data-url', currentUrl + '&{$paramsUrl}');
}

$('{$editButton}').each(function() {
    var currentUrl = $(this).attr('data-url');
    if (currentUrl.indexOf('?') === -1) {
        $(this).attr('data-url', currentUrl + '?{$paramsUrl}');
    } else {
        // 如果已经有查询参数,添加 &id=123
        $(this).attr('data-url', currentUrl + '&{$paramsUrl}');
    }
});
JS
        );
    }


    public static function seoReplace($titleName = 'title',$modelName = 'pages')
    {
        if ($titleName) {
            Admin::script(
<<<JS
    $('input[name="{$titleName}"]').on('input', function() {
        // 将 title 的值赋给 seo_title
        $('input[name="seo_title"]').val($(this).val());
    });
JS
            );
        }
        //ajax 生成slug
        if ($modelName) {
            Admin::script(
<<<JS
    $('input[name="{$titleName}"]').on('blur', function() {
        //通过ajax请求修改slug
        title = $(this).val();
        $.ajax({
            url: '/dist/api/generate-slug',
            type: 'GET',
            data: {
                model:'{$modelName}',
                title: title,
            },
            success: function(response) {
                $('input[name="slug"]').val(response.slug);
            }
        });
    });
JS
            );
        }

    }

}