<!--
 * @Title: 
 * @Description: 
 * @Author: swp
 * @Date: 2022-08-24 10:49:21
 * @LastEditors: 
 * @LastEditTime: 2022-08-24 10:49:21
-->
<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" :page.sync="page"
                        :before-open="beforeOpen" ref="crud" v-model="form" :permission="permissionList" @row-del="rowDel"
                        @row-update="rowUpdate" @row-save="rowSave" @search-change="searchChange"
                        @search-reset="searchReset" @selection-change="selectionChange" @current-change="currentChange"
                        @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad"
                        @select-change="selectChange">

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

    </div>
</template>

<script>
import { getPage, } from "@/api/data/dataRain.js";
// import { getTree } from "@/api/business/manage/manage.js";
import { getTree } from "@/api/baseinfo/region";
export default {
    components: {

    },
    data() {
        return {
            id: "",
            form: {},
            selectionList: [],
            query: {},
            loading: true,
            page: {
                pageSize: 10,
                currentPage: 1,
                total: 0,
            },
            treeCode: '',
            treeParentCode: '',
            treeData: [],
            treeOption: {
                nodeKey: "id",
                defaultExpandedKeys: [],
                lazy: false,
                // treeLoad: function (node, resolve) {
                //     const parentCode = (node.level === 0) ? "00" : node.data.value;
                //     getLazyTree(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: 120,
                viewLabelWidth: 100,
                searchlabelWidth: 100,
                height: "auto",
                calcHeight: 80,
                align: "center",
                headerAlign: "center",
                lazy: false,
                tip: false,
                simplePage: true,
                searchShow: true,
                searchMenuSpan: 6,
                tree: true,
                border: true,
                index: true,
                selection: false,
                viewBtn: false,
                viewTitle: "测站信息",
                editBtn: false,
                delBtn: false,
                addBtn: false,
                menu: false,
                dialogType: "drawer",
                dialogClickModal: false,
                columnBtn: false,
                column: [
                    {
                        label: "站点名称",
                        prop: "rtuName",
                        search: true,
                        span: 24,
                        width: 160,
                    },
                    {
                        label: "站点编码",
                        prop: "rtuCode",
                        search: true,
                        span: 24,
                        width: 120,
                    },
                    {
                        label: "行政区",
                        prop: "adCity",
                        span: 24,
                        editDisplay: false,
                        addDisplay: false,
                        html: true,
                        formatter: (val) => {
                            if (val.adCity && val.adDist) {
                                return '<b>' + val.adCity + '</b>/' + '<b>' + val.adDist + '</b>'
                            } else {
                                return '<b>' + val.adCity + '</b>'
                            }
                        }
                    },
                    {
                        label: "上报时间",
                        prop: "tm",
                        span: 24,
                    },
                    {
                        label: "时段长",
                        prop: "intv",
                        span: 24,
                        html: true,
                        formatter: (val) => {
                            if (val.intv === 1.0) {
                                return '<b>1小时</b>'
                            } if (val.intv === 0.05) {
                                return '<b>5分钟</b>'
                            } else {
                                return '';
                            }
                        }
                    },
                    {
                        label: "时段降水量",
                        prop: "drp",
                        span: 24,
                        html: true,
                        formatter: (val) => {
                            if (val.drp != null) {
                                return val.drp;
                            } else {
                                return "";
                            }
                        }
                    },

                ],
            },
            data: [],
        };
    },
    computed: {
        // ...mapGetters(["userInfo", "permission"]),
        permissionList() {
            return {
                // addBtn: this.vaildData(this.permission.docCategory_add, false),
                // viewBtn: this.vaildData(this.permission.docCategory_view, false),
                // delBtn: this.vaildData(this.permission.docCategory_delete, false),
                // editBtn: this.vaildData(this.permission.docCategory_edit, false),
            };
        },
        ids() {
            let ids = [];
            this.selectionList.forEach((ele) => {
                ids.push(ele.id);
            });
            return ids.join(",");
        },
    },
    mounted() { },
    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;
            });
        },
        nodeClick(data) {
            this.treeCode = data.id;
            this.treeParentCode = data.parentId;
            this.query = {};
            this.query['adCode'] = this.treeCode;
            this.page.currentPage = 1;
            var params = {};
            this.onLoad(this.page, params);
        },
        searchReset() {
            this.page.currentPage = 1;
            this.onLoad(this.page);
        },
        searchChange(params, done) {
            this.page.currentPage = 1;
            this.onLoad(this.page, params);
            done();
        },
        selectionChange(list) {
            this.selectionList = list;
        },
        selectChange(item) {
            this.$message.success("" + item)
        },
        selectionClear() {
            this.selectionList = [];
            this.$refs.crud.toggleSelection();
        },

        currentChange(currentPage) {
            this.page.currentPage = currentPage;
        },
        sizeChange(pageSize) {
            this.page.pageSize = pageSize;
        },
        refreshChange() {
            var params = {};
            this.onLoad(this.page, params);
        },
        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>
