<template>
    <div>
        <el-row>
            <el-col :span="4">
                <div class="box">
                    <el-scrollbar>
                        <basic-container>
                            <avue-tree :option="treeOption" :data="treeData" @node-click="nodeClick">
                                <span class="el-tree-node__label" slot-scope="{ node, data }">
                                    <span>
                                        <i class="el-icon-office-building"></i>
                                        {{ (node || {}).label }}
                                    </span>
                                </span>
                            </avue-tree>
                        </basic-container>
                    </el-scrollbar>
                </div>
            </el-col>
            <el-col :span="20">
                <basic-container>
                    <avue-crud :option="option" :table-loading="loading" :data="data" ref="crud" v-model="form"
                        :permission="permissionList" @search-change="searchChange" @search-reset="searchReset"
                        @refresh-change="refreshChange" @on-load="onLoad">
                    </avue-crud>
                </basic-container>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import { getWaterUseConsumerFlowCountPage } from "@/api/business/count/count";
import { getTree } from "@/api/baseinfo/org";
import { mapGetters } from "vuex";

export default {
    data() {
        return {
            form: {},
            selectionList: [],
            query: {},
            loading: true,
            treeCode: '',
            treeParentCode: '',
            treeData: [],
            treeOption: {
                nodeKey: "id",
                defaultExpandedKeys: [],
                lazy: false,
                // treeLoad: function (node, resolve) {
                //     const parentCode = (node.level === 0) ? "" : node.data.id;
                //     getLazyDeptTree(parentCode, {}).then(res => {
                //         resolve(res.data.data.map(item => {
                //             return {
                //                 ...item,
                //                 leaf: !item.hasChildren
                //             }
                //         }))
                //     });
                // },
                addBtn: false,
                menu: true,
                size: "small",
                props: {
                    labelText: "标题",
                    label: "title",
                    value: "value",
                    children: "children",
                },
            },
            option: {
                height: 'auto',
                calcHeight: 30,
                searchLabelWidth: 140,
                tip: false,
                searchShow: true,
                searchMenuSpan: 6,
                border: true,
                index: true,
                selection: true,
                viewBtn: false,
                dialogClickModal: false,
                menu: false,
                column: [
                    {
                        label: "取用水户代码",
                        prop: "wiuCd",
                        search: true,
                    },
                    {
                        label: "取用水户名称",
                        prop: "wiuNm",
                        search: true,
                    },
                    {
                        label: "当月累计用水量",
                        prop: "waterFlowSum",
                    }
                ]
            },
            data: []
        };
    },
    created() {
        this.initTree();
    },
    computed: {
        ...mapGetters(["userInfo", "permission"]),
        permissionList() {
            return {
                addBtn: false,
                viewBtn: false,
                delBtn: false,
                editBtn: false
            };
        },
        ids() {
            let ids = [];
            this.selectionList.forEach(ele => {
                ids.push(ele.id);
            });
            return ids.join(",");
        }
    },
    methods: {
        initTree() {
            this.treeData = [];
            getTree().then(res => {
                this.treeData = res.data.data.map(item => {
                    return {
                        ...item,
                        leaf: !item.hasChildren
                    }
                })
                this.treeOption.defaultExpandedKeys.push(this.treeData[0].id);
                this.treeCode = this.treeData[0].id;
                this.query = {};
                this.query['orgId'] = this.treeCode;
            });
        },
        nodeClick(data) {
            this.treeCode = data.id;
            this.excludeCode = data.exclude;
            this.treeParentCode = data.parentId;
            this.query = {};
            this.query['orgId'] = this.treeCode;
            var params = {};
            this.onLoad(params);
        },
        searchReset() {
            this.query = {};
            this.onLoad();
        },
        searchChange(params, done) {
            this.query = params;
            this.onLoad(params);
            done();
        },
        selectionChange(list) {
            this.selectionList = list;
        },
        selectionClear() {
            this.selectionList = [];
            this.$refs.crud.toggleSelection();
        },
        refreshChange() {
            this.onLoad(this.query);
        },
        onLoad(params = {}) {
            this.loading = true;
            getWaterUseConsumerFlowCountPage(Object.assign(params, this.query)).then(res => {
                this.data = res.data.data;
                this.loading = false;
                this.selectionClear();
            });
        }
    }
};
</script>

<style></style>
