<template>
    <Modal v-model="opened"
        scrollable
        :width="800">
        <div slot="header">
            <span class="title">字段选择</span>
            <span class="description">Column Select</span>
        </div>
        <div class="ivu-ml-8 ivu-mr-8">
            <Tree :data="data"
                show-checkbox></Tree>
        </div>
        <div slot="footer">
            <Button type="primary"
                custom-icon="fa fa-save"
                @click="finish">
                确定
            </Button>
            <Button type="default"
                custom-icon="fa fa-times"
                @click="close">
                关闭
            </Button>
        </div>
    </Modal>
</template>
<script>
    import mixinPage from '../../mixins/page';
    import dataViewApi from '../../api/dataView';

    // 转换为tree列表对象
    var convertList = (nodes, columns) => {
        if (!nodes) {
            return null;
        }

        return nodes.map(item => {
            return {
                column: {
                    code: item.code,
                    name: item.name,
                    dataType: item.dataType,
                    columns: item.columns
                },
                title: item.name + ' (' + item.code + ')',
                checked: columns.some(column => {
                    return column.code === item.code;
                }),
                children: convertList(item.columns, columns)
            };
        });
    };

    // 获取选中项目
    var getChecked = data => {
        let checked = [];

        data.forEach(item => {
            if (item.checked) {
                // 外键字段
                if (item.column.dataType === 'BigInteger') {
                    let obj = data.find(x => {
                        return x.column.code + 'ID' === item.column.code;
                    });
                    if (obj) {
                        item.column.objColumn = obj.column;
                    }
                }

                checked.push({
                    ...item.column
                });
            }

            if (item.children) {
                checked = checked.concat(getChecked(item.children));
            }
        });

        return checked;
    };

    export default {
        mixins: [mixinPage],
        components: {},
        data() {
            return {
                data: [],
                columns: null,
                opened: false,
                callback: null
            };
        },
        props: {},
        computed: {},
        methods: {
            open(dataView, columns, callback) {
                if (!dataView.isDataSource) {
                    // 对象
                    dataViewApi.queryColumnByModel(dataView.model).then(data => {
                        this.data = convertList(data, columns);
                        this.opened = true;
                    });
                } else {
                    // 数据源
                    dataViewApi.queryColumnByDataSource(dataView.dataSource).then(data => {
                        this.data = convertList(data, columns);
                        this.opened = true;
                    });
                }

                this.columns = columns;
                this.callback = callback;
            },
            finish() {
                let checked = getChecked(this.data);

                this.callback(checked);
                this.opened = false;
            },
            close() {
                this.opened = false;
            }
        }
    };
</script>
