<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"
                        :page.sync="page" :permission="permissionList" @search-change="searchChange"
                        @search-reset="searchReset" @selection-change="selectionChange" @current-change="currentChange"
                        @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad">
                        <template slot-scope="scope" slot="menu">
                            <el-button type="text" icon="el-icon-view" size="small"
                                @click.stop="handlePreview(scope.row)">查看年报
                            </el-button>
                        </template>

                    </avue-crud>
                </basic-container>
            </el-col>
        </el-row>

        <el-drawer :visible.sync="monthCounDialog" v-if="monthCounDialog" title="年报" direction="rtl" size="60%"
            append-to-body="true">
            <consumerwaterflowyearcountdetailVue ref="consumerwaterflowyearcountdetail" :wiuCd="wiuCd">
            </consumerwaterflowyearcountdetailVue>
        </el-drawer>
    </div>
</template>

<script>
import { getPage } from "@/api/baseinfo/wateruseconsumer.js";
import { getTree } from "@/api/baseinfo/org";
import { mapGetters } from "vuex";
import consumerwaterflowyearcountdetailVue from './consumerwaterflowyearcountdetail.vue'
export default {
    components: {
        consumerwaterflowyearcountdetailVue
    },
    data() {
        return {
            form: {},
            selectionList: [],
            query: {},
            loading: true,
            monthCounDialog: false,
            page: {
                pageSize: 10,
                currentPage: 1,
                total: 0
            },
            wiuCd: '',
            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: {
                labelWidth: 140,
                viewLabelWidth: 140,
                searchLabelWidth: 140,
                height: 'auto',
                calcHeight: 30,
                tip: false,
                searchShow: true,
                searchMenuSpan: 6,
                border: true,
                index: true,
                selection: true,
                viewBtn: false,
                menu: true,
                dialogClickModal: false,
                column: [
                    {
                        label: "取用水户代码",
                        prop: "wiuCd",
                        search: true,
                        span: 24,
                    },
                    {
                        label: "取用水户名称",
                        prop: "wiuNm",
                        search: true,
                        span: 24,
                    },
                    {
                        label: "行政区",
                        prop: "orgName",
                        span: 24,
                    },
                    {
                        label: "法人代表",
                        prop: "lrNm",
                        span: 24,
                    },
                ]
            },
            data: []
        };
    },
    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(",");
        }
    },
    created() {
        this.initTree();
    },
    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;
            this.page.currentPage = 1;
            var params = {};
            this.onLoad(this.page, params);
        },
        handlePreview(row) {
            this.wiuCd = row.wiuCd;
            this.monthCounDialog = true;
            this.$nextTick(() => {
                this.$refs["consumerwaterflowyearcountdetail"].init();
            });
        },
        searchReset() {
            this.query = {};
            this.onLoad(this.page);
        },
        searchChange(params, done) {
            this.query = params;
            this.page.currentPage = 1;
            this.onLoad(this.page, params);
            done();
        },
        selectionChange(list) {
            this.selectionList = list;
        },
        selectionClear() {
            this.selectionList = [];
            this.$refs.crud.toggleSelection();
        },
        currentChange(currentPage) {
            this.page.currentPage = currentPage;
        },
        sizeChange(pageSize) {
            this.page.pageSize = pageSize;
        },
        refreshChange() {
            this.onLoad(this.page, this.query);
        },
        onLoad(page, params = {}) {
            this.loading = true;
            getPage(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
                const data = res.data.data;
                this.page.total = data.total;
                this.data = data.records;
                this.loading = false;
                this.selectionClear();
            });
        }
    }
};
</script>

<style></style>
