<?php

use App\Admin\Extensions\Form\WangEditor;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Form;
use Dcat\Admin\Grid\Filter;
use Dcat\Admin\Show;
/**
 * Dcat-admin - admin builder based on Laravel.
 * @author jqh <https://github.com/jqhph>
 *
 * Bootstraper for Admin.
 *
 * Here you can remove builtin form field:
 *
 * extend custom field:
 * Dcat\Admin\Form::extend('php', PHPEditor::class);
 * Dcat\Admin\Grid\Column::extend('php', PHPEditor::class);
 * Dcat\Admin\Grid\Filter::extend('php', PHPEditor::class);
 *
 * Or require js and css assets:
 * Admin::css('/packages/prettydocs/css/styles.css');
 * Admin::js('/packages/prettydocs/js/main.js');
 *
 */

// 注册前端组件别名
// 注册前端组件别名
//Admin::asset()->alias('@ckeditor', [
//    'js' => [
////        '/vendor/ckeditor_4.20.0/ckeditor.js',
////        '/vendor/ckeditor_4.20.0/adapters/jquery.js',
//        '/vendor/ckeditor5/ckeditor.js',
//        '/vendor/ckeditor5/translations/zh-cn.js',
//    ],
//]);

//Form::extend('ckeditor', \App\Admin\Extensions\Form\CKEditor::class);


Form\Field\Editor::resolving(function (Form\Field\Editor $editor) {
    $editor->options([
        'file_picker_types' => 'media',
        'file_picker_callback' => \Dcat\Admin\Support\JavaScript::make(<<<JS
            function file_picker_callback (callback, value, meta) {
                // 设置上传地址为原富文本框图片文件上传地址
                var upurl = opts.images_upload_url;
                var filetype = '';
                // 处理媒体类型文件能选择的文件类型
                if (meta.filetype == 'media') {
                    filetype = '.mp4,.webm,.ogg'
                }
                //模拟出一个input用于添加本地文件
                var input = document.createElement('input');
                input.setAttribute('type', 'file');
                input.setAttribute('accept', filetype);
                // 模拟点击file input
                input.click();
                input.onchange = function() {
                    // 文件选择后进行上传
                    var file = this.files[0];
                    var xhr, formData;
                    console.log(file.name);
                    Dcat.loading()
                    xhr = new XMLHttpRequest();
                    xhr.withCredentials = false;
                    xhr.open('POST', upurl);
                    xhr.onload = function() {
                        var json;
                        if (xhr.status != 200) {
                            failure('HTTP Error: ' + xhr.status);
                            return;
                        }
                        json = JSON.parse(xhr.responseText);
                        if (!json || typeof json.location != 'string') {
                            failure('Invalid JSON: ' + xhr.responseText);
                            return;
                        }
                        json.location = json.location.replace('http://','https://')
                        json.location = json.location.replace('-internal','')
                        Dcat.loading(false)
                        callback(json.location);
                    };
                    formData = new FormData();
                    formData.append('file', file, file.name );
                    xhr.send(formData);
                }
            }
        JS)
    ]);
});
