function time(field) {
    return {
        type: 'tpl',
        tpl: `\${IF(${field}, DATETOSTR(${field}, 'YYYY-MM-DD HH:mm:ss'), undefined)}`
    }
}

function cloudCrud(title, path) {
    return {
        type: 'crud',
        title: title,
        api: `\${base}${path}`,
        ...crudCommonOptions(),
        interval: 2000,
        headerToolbar: ['reload'],
        loadDataOnce: true,
        perPage: 100,
        columns: [
            {name: 'name', label: '名称'},
            {
                name: 'status',
                label: '状态',
                align: 'center',
                width: 60,
            },
            {
                name: 'serviceUpTime',
                label: '启动时间',
                ...time('serviceUpTime'),
                align: 'center',
                width: 160,
            },
            {name: 'url', label: '地址'},
            {
                type: 'operation',
                label: '操作',
                width: 100,
                fixed: 'right',
                className: 'nowrap',
                buttons: [
                    {
                        label: "日志",
                        type: "action",
                        level: "link",
                        tooltip: '打开Grafana日志',
                        onEvent: {
                            click: {
                                actions: [
                                    {
                                        actionType: 'custom',
                                        script: (context, doAction, event) => {
                                            let data = context.props.data
                                            let url = ''
                                            if (data['metadata']) {
                                                url = serviceLogByAppNameAndHost(data.serviceId, data.metadata.hostname)
                                            } else if (data['name']) {
                                                url = serviceLogByAppName(data.name)
                                            }
                                            window.open(url, '_blank')
                                        },
                                    }
                                ]
                            }
                        }
                    },
                ]
            },
        ]
    }
}

function cloudTab() {
    return {
        title: 'Cloud',
        icon: 'fa fa-cloud',
        tab: [
            /*{
                type: 'service',
                silentPolling: true,
                schemaApi: 'get:${base}/cloud/deploy_plan',
            },*/
            cloudCrud('服务列表', '/cloud/list'),
            cloudCrud('服务列表 (IP)', '/cloud/list_ip'),
        ],
    }
}