const information = {
  debug: false,
  // baseUrl: '',
  baseUrl: 'http://127.0.0.1:20080',
  title: '可信供给中心',
}

export function useAmis(amisObject) {
  document.title = information.title
  let amis = amisRequire('amis/embed')
  amis.embed(
    '#app',
    amisObject(information),
    {
      data: {
        base: information.baseUrl,
      },
    },
    {
      theme: 'antd',
      enableAMISDebug: information.debug,
    }
  )
}

export function crudCommonOptions() {
    return {
        affixHeader: false,
        stopAutoRefreshWhenModalIsOpen: true,
        resizable: false,
        syncLocation: false,
        silentPolling: true,
    }
}

export function readOnlyDialogOptions() {
    return {
        actions: [],
        showCloseButton: false,
        closeOnEsc: true,
        closeOnOutside: true,
        disabled: true,
    }
}

export function horizontalFormOptions() {
    return {
        mode: 'horizontal',
        canAccessSuperData: false,
        horizontal: {
            left: 3,
        },
    }
}

export function paginationCommonOptions(perPage = true, maxButtons = 5) {
    let option = {
        type: 'pagination',
        layout: [
            'pager'
        ],
        maxButtons: maxButtons,
        showPageInput: false,
        perPageAvailable: [10, 15, 20, 50, 100, 200],
    }
    if (perPage) {
        option.layout.push('perPage')
    }
    return option
}

export function paginationTemplate(perPage = 20, maxButtons = 5) {
    return {
        perPage: perPage,
        headerToolbar: [
            "reload",
            paginationCommonOptions(true, maxButtons),
        ],
        footerToolbar: [
            "statistics",
            paginationCommonOptions(true, maxButtons),
        ],
    }
}

export const size5MB = 5242880
export const size100MB = 104857600
export const size500MB = 524288000
export const size1GB = 1073741824

export function inputFileFormItemCommonOptions(accept = '*', maxSize = size5MB) {
    return {
        useChunk: true,
        accept: accept,
        maxSize: maxSize,
        autoUpload: false,
        startChunkApi: apiPost('${base}/upload/start'),
        chunkApi: apiPost('${base}/upload/slice'),
        finishChunkApi: apiPost('${base}/upload/finish'),
    }
}

export function copyField(field, tips = '复制', ignoreLength = 0, extra = undefined) {
    let tpl = ignoreLength === 0 ? `\${${field}}` : `\${TRUNCATE(${field}, ${ignoreLength})}`
    let content = extra
        ? {
            type: 'action',
            level: 'link',
            label: `\${${field}}`,
            className: 'text-current underline',
            size: 'xs',
            ...extra,
        }
        : {
            type: 'tpl',
            className: 'mr-1',
            tpl: tpl,
        }
    return {
        type: 'wrapper',
        size: 'none',
        body: [
            content,
            {
                type: 'action',
                level: 'link',
                label: '',
                icon: 'fa fa-copy',
                size: 'xs',
                actionType: 'copy',
                content: `\$${field}`,
                tooltip: `${tips}`,
            },
        ]
    }
}

export function mappingField(field, mapping) {
    let mapData = {
        '*': `<span class='label bg-gray-300'>\${${field}}</span>`,
    }
    mapping.forEach(item => {
        mapData[item['value']] = `<span class='label ${item['color']}'>${item['label']}</span>`
    })
    return {
        type: 'mapping',
        value: `\${${field}}`,
        map: mapData,
    }
}

function mappingItem(label, value, color = 'bg-info') {
    return {
        label: label,
        value: value,
        color: color,
    }
}

export const userRoleMapping = [
    mappingItem('数据提供方', 'PROVIDER', 'bg-blue-500'),
    mappingItem('数据使用方', 'CUSTOMER', 'bg-purple-500'),
    mappingItem('审核监管方', 'CHECKER', 'bg-cyan-500'),
    mappingItem('系统管理员', 'ADMINISTRATOR', 'bg-green-500'),
]

export const userStateMapping = [
    mappingItem('审查中', 'CHECKING', 'bg-warning'),
    mappingItem('正常', 'NORMAL', 'bg-success'),
    mappingItem('禁用', 'DISABLED', 'bg-danger'),
]

function api(method, url) {
    return {
        method: method,
        url: url,
        headers: {
            token: '${token|default:undefined}',
        }
    }
}

export function apiGet(url) {
    return api('get', url)
}

export function apiPost(url) {
    return api('post', url)
}
