import './dialog-ware.css'
import {apiGet, apiPost, formInputClearable, horizontalFormOptions, information,} from "../constants.js";
import {resourceList} from '../../pages/index/tab-data.js'
import {resourceDetailDialog} from '../resource/dialog-resource.js'

function detailForm() {
    return {
        id: 'ware-form',
        debug: information.debug,
        type: 'form',
        ...horizontalFormOptions(),
        horizontal: {
            left: 2,
        },
        body: [
            {
                type: 'hidden',
                name: 'id',
            },
            {
                visibleOn: '${!detail}',
                type: 'input-image',
                name: 'icon',
                label: '图标',
                required: true,
                receiver: apiPost("${base}/upload"),
                onEvent: {
                    success: {
                        actions: [
                            {
                                actionType: 'setValue',
                                componentId: 'ware-form',
                                args: {
                                    value: {
                                        iconId: '${event.data.result.id}'
                                    }
                                }
                            }
                        ]
                    }
                }
            },
            {
                visibleOn: '${detail}',
                type: 'control',
                name: 'icon',
                label: '图标',
                required: true,
                body: {
                    type: 'image',
                    src: '${icon}',
                    innerClassName: 'no-border',
                }
            },
            {
                type: 'picker',
                name: 'resourceId',
                label: '数据资源',
                required: true,
                multiple: false,
                size: 'md',
                valueField: 'id',
                labelField: 'name',
                source: apiGet('${base}/data_resource/list_no_ware'),
                pickerSchema: {
                    ...resourceList(),
                },
                staticSchema: {
                    type: 'tpl',
                    tpl: "<span class='text-primary' style='cursor: pointer'>${resourceName}</span>",
                    onEvent: {
                        click: {
                            actions: [
                                {
                                    actionType: 'dialog',
                                    ...resourceDetailDialog('resourceId'),
                                }
                            ]
                        }
                    }
                }
            },
            {
                type: 'input-text',
                name: 'name',
                label: '商品名称',
                maxLength: 10,
                showCounter: true,
                required: true,
                ...formInputClearable,
            },
            {
                type: 'textarea',
                name: 'description',
                label: '商品简介',
                required: true,
                ...formInputClearable,
                showCounter: true,
                trimContents: true,
                minRows: 2,
                maxRows: 2,
            },
            {
                type: 'input-rich-text',
                name: 'content',
                label: '商品详情',
                required: true,
                receiver: apiPost("${base}/upload"),
                options: {
                    min_height: 300,
                }
            }
        ]
    }
}

export function wareAddDialog() {
    return {
        actionType: 'dialog',
        dialog: {
            title: '上架数据产品',
            size: 'md',
            actions: [
                {
                    type: 'reset',
                    label: '重置',
                },
                {
                    type: 'submit',
                    label: '确定',
                    level: 'primary',
                }
            ],
            body: {
                ...detailForm(),
                api: apiPost(`\${base}/ware/save`),
                data: {
                    add: true,
                }
            }
        }
    }
}

export function wareDetailDialog(field = 'id', actions = []) {
    return {
        actionType: 'dialog',
        dialog: {
            title: '数据产品详情',
            size: 'md',
            actions: actions,
            body: {
                ...detailForm(),
                initApi: apiGet(`\${base}/ware/detail/\${${field}}`),
                static: true,
                data: {
                    detail: true,
                }
            }
        }
    }
}

export function wareEditeDialog() {
    return {
        actionType: 'dialog',
        dialog: {
            title: '数据产品详情',
            size: 'md',
            actions: [
                {
                    type: 'reset',
                    label: '重置',
                },
                {
                    type: 'submit',
                    label: '确定',
                    level: 'primary',
                }
            ],
            body: {
                ...detailForm(),
                api: apiPost(`\${base}/ware/save`),
                initApi: apiGet(`\${base}/ware/detail/\${id}`),
                data: {
                    edit: true
                }
            },
        }
    }
}
