<template>
    <ed-table :columns="columns" :loading="loading" :data="tableData" border
        @current-change="handleCurrentChange">
    </ed-table>
</template>
<script>
import { getVarType } from "../../utils/utils";
import { controlInfoViewList } from '../../api/ds'

export default {
    name: 'DataSourceViewIdChoose',
    data() {
        return {
            loading: false,
            dataSourceName: '',
            url: "/easydata/controlInfo/viewList",
            tableData: [],
            currentData: {}, //选中的数据
            columns: [{
                label: '视图名称',
                prop: 'viewName'
            },
            {
                label: '视图ID',
                prop: 'id'
            },
            {
                label: '分组名称',
                prop: 'viewGroup'
            },]
        }
    },
    methods: {
        getData() {
            return { label: this.currentData.viewName, value: this.currentData.id }
        },
        loadData(params) {
            if (params) {
                if (getVarType(params.dataSourceName) === 'object') {
                    this.dataSourceName = params.dataSourceName.value
                } else {
                    this.dataSourceName = params.dataSourceName
                }
                if (getVarType(this.dataSourceName) != 'string') {
                    this.$message.error('数据源名称不允许为空')
                    return;
                }
                controlInfoViewList({ data_source_name: this.dataSourceName }).then(res => {
                    if (res.success) {
                        this.tableData = res.data ? res.data : []
                        this.tableData.unshift({
                            id: 'default',
                            viewName: '默认视图',
                            viewGroup: 'default'
                        })
                    } else {
                        this.$message.warning(res.msg)
                    }
                })
            }
        },
        handleCurrentChange(row) {
            this.currentData = row
        }
    }
}
</script>
