<!--
 * @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" @sort-change="sortChange">

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

    </div>
</template>

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

    },
    data() {
        return {
            id: "",
            form: {},
            selectionList: [],
            query: {},
            sort: {},
            loading: true,
            page: {
                pageSize: 10,
                currentPage: 1,
                total: 0,
            },
            treeCode: '',
            treeParentCode: '',
            treeData: [],
            treeOption: {
                nodeKey: "id",
                defaultExpandedKeys: [],
                lazy: false,
                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: "rtuCode",
                        search: true,
                        span: 24,
                        width: 120,
                    },
                    {
                        label: "站点名称",
                        prop: "rtuName",
                        search: true,
                        span: 24,
                        width: 160,
                    },
                    {
                        label: "行政区",
                        prop: "adCity",
                        span: 12,
                        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: 8,
                        sortable: 'custom',
                    },
                    {
                        label: "含水量",
                        children: [
                            // {
                            //     label: "垂线平均",
                            //     prop: "vtavslm",
                            //     span: 8,
                            //     formatter: (val) => {
                            //         if (val.vtavslm != null) {
                            //             return val.vtavslm;
                            //         } else {
                            //             return "";
                            //         }
                            //     }
                            // },
                            // {
                            //     label: "表层",
                            //     prop: "srlslm",
                            //     span: 8,
                            //     formatter: (val) => {
                            //         if (val.srlslm != null) {
                            //             return val.srlslm;
                            //         } else {
                            //             return "";
                            //         }
                            //     }
                            // },
                            {
                                label: "10CM",
                                prop: "slm10",
                                span: 8,
                                formatter: (val) => {
                                    if (val.slm10 != null) {
                                        return val.slm10;
                                    } else {
                                        return "";
                                    }
                                }
                            },
                            {
                                label: "20CM",
                                prop: "slm20",
                                span: 8,
                                formatter: (val) => {
                                    if (val.slm20 != null) {
                                        return val.slm20;
                                    } else {
                                        return "";
                                    }
                                }
                            },
                            {
                                label: "30CM",
                                prop: "slm30",
                                span: 8,
                                formatter: (val) => {
                                    if (val.slm30 != null) {
                                        return val.slm30;
                                    } else {
                                        return "";
                                    }
                                }
                            },
                            {
                                label: "40CM",
                                prop: "slm40",
                                span: 8,
                                formatter: (val) => {
                                    if (val.slm40 != null) {
                                        return val.slm40;
                                    } else {
                                        return "";
                                    }
                                }
                            },
                            {
                                label: "50CM",
                                prop: "slm50",
                                span: 8,
                                formatter: (val) => {
                                    if (val.slm50 != null) {
                                        return val.slm50;
                                    } else {
                                        return "";
                                    }
                                }
                            },
                            {
                                label: "60CM",
                                prop: "slm60",
                                span: 8,
                                formatter: (val) => {
                                    if (val.slm60 != null) {
                                        return val.slm60;
                                    } else {
                                        return "";
                                    }
                                }
                            },
                            {
                                label: "80CM",
                                prop: "slm80",
                                span: 8,
                                formatter: (val) => {
                                    if (val.slm80 != null) {
                                        return val.slm80;
                                    } else {
                                        return "";
                                    }
                                }
                            },
                            {
                                label: "100CM",
                                prop: "slm100",
                                span: 8,
                                formatter: (val) => {
                                    if (val.slm100 != null) {
                                        return val.slm100;
                                    } 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);
        },
        sortChange(val) {
            console.log(JSON.stringify(val))
            //prop":"tm","order":"ascending"  descending
            let prop = val.prop;
            if (prop === 'tm') {
                this.page.currentPage = 1;
                var params = {};
                let order = val.order;
                if (order) {
                    params['order'] = order;
                    params['prop'] = prop;
                    this.sort = {};
                    Object.assign(this.sort, params)
                } else {
                    this.sort = {};
                }
                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.sort, this.query)
            ).then((res) => {
                const data = res.data.data;
                this.page.total = data.total;
                this.data = data.records;
                this.loading = false;
                this.selectionClear();
            });
        },
    },
};
</script>
