<!--
 * @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" :page.sync="page" :data="data" ref="crud"
                        v-model="form" :permission="permissionList" @search-change="searchChange"
                        @search-reset="searchReset" @selection-change="selectionChange" @current-change="currentChange"
                        @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad"
                        @before-open="beforeOpen" @sort-change="sortChange" @cell-click="cellClick">
                        <template slot="runStatusId" slot-scope="row">
                            <div v-if="row.row.runStatusId == 0" style="color: blue;">
                                正常
                            </div>
                            <div v-else-if="row.row.runStatusId == 1"
                                style="color: red;cursor:pointer;text-decoration-line: underline;"
                                @click="openWarningInfo(row.row)">
                                异常
                            </div>
                            <div v-else style="color: red;cursor:pointer;text-decoration-line: underline;">
                                异常
                            </div>
                        </template>
                        <template slot-scope="{ row, index }" slot="menu">
                            <el-button type="text" size="small" @click="toHistroyData(row)" icon="el-icon-s-data">历史数据
                            </el-button>
                        </template>
                    </avue-crud>
                </basic-container>
            </el-col>
        </el-row>

        <el-drawer :visible.sync="historyDataDialog" v-if="historyDataDialog" title="历史信息" direction="rtl" size="60%"
            append-to-body="true">
            <div slot="title" class="header-title">
                <span class="name">{{ dialogTitle }}</span>
            </div>
            <waterflowratehistorylistvue :mpcd="mpcd" ref="waterflowratehistorylist"></waterflowratehistorylistvue>
        </el-drawer>
    </div>
</template>

<script>
import { getPage, getDetail } from "@/api/business/monitor/waterflowrate.js";
import { getTree } from "@/api/baseinfo/project.js";
import { mapGetters } from "vuex";
import waterflowratehistorylistvue from "./waterflowratehistorylist.vue"
export default {
    components: {
        waterflowratehistorylistvue
    },
    data() {
        return {
            id: "",
            historyDataDialog: false,
            dialogTitle: "",
            mpcd: '',
            form: {},
            selectionList: [],
            query: {},
            sort: {},
            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) ? "" : 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: 160,
                viewLabelWidth: 160,
                searchLabelWidth: 160,
                height: "auto",
                calcHeight: 80,
                align: "center",
                headerAlign: "center",
                tip: false,
                simplePage: true,
                searchShow: true,
                searchMenuSpan: 6,
                tree: true,
                border: true,
                index: true,
                selection: false,
                viewBtn: true,
                editBtn: false,
                delBtn: false,
                addBtn: false,
                menuWidth: 160,
                menu: true,
                dialogType: "drawer",
                dialogClickModal: false,
                columnBtn: false,
                column: [
                    {
                        label: "测站状态",
                        prop: "runStatusId",
                        span: 24,
                        width: 100,
                        viewDisplay: false,
                    },
                    {
                        label: "测站编码",
                        prop: "mpcd",
                        span: 24,
                        search: true,
                        width: 130,
                    },
                    {
                        label: "测站名称",
                        prop: "mpnm",
                        search: true,
                        span: 24,
                    },
                    {
                        label: "所属项目",
                        prop: "projectName",
                        span: 24,
                    },
                    {
                        label: "上次报文时间",
                        prop: "tm",
                        span: 24,
                        width: 150,
                        sortable: 'custom',
                    },
                    {
                        label: "瞬时流量",
                        prop: "mpq",
                        span: 24,
                        width: 110,
                    },
                    {
                        label: "累计流量",
                        prop: "accw",
                        span: 24,
                        width: 110,
                    },
                    {
                        label: "水位",
                        prop: "waterLevel",
                        span: 24,
                        width: 120,
                    },
                    {
                        label: "流速",
                        prop: "flowSpeed",
                        span: 24,
                        width: 120,
                    },
                ],
            },
            data: [],
        };
    },
    computed: {
        ...mapGetters(["userInfo", "permission"]),
        permissionList() {
            return {
                addBtn: false,
                viewBtn: true,
                delBtn: false,
                editBtn: 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;
                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['projectId'] = 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);
            }
        },
        cellClick(row, column, cell, event) {
            if (event.isTrusted) {
                // this.rtuCode = row.rtuCode;
                // if (column.label === 'RTU更换时间') {
                //     if (row.rtuReplaceDate) {
                //         this.dialogTitle = row.rtuName + 'RTU更换信息';
                //         this.rtureplacelistDialog = true;
                //         this.$nextTick(() => {
                //             this.$refs["rtureplacelist"].init();
                //         });
                //     }
                // } 
            }
        },
        toHistroyData(row) {
            this.dialogTitle = "历史数据[" + row.mpnm + row.mpcd + "]";
            this.mpcd = row.mpcd;
            this.historyDataDialog = true;
            this.$nextTick(() => {
                this.$refs["waterflowratehistorylist"].init();
            });
        },
        searchReset() {
            this.query = {};
            this.query['projectId'] = this.treeCode;
            this.page.currentPage = 1;
            this.onLoad(this.page);
        },
        searchChange(params, done) {
            this.query = {};
            this.query['projectId'] = this.treeCode;
            Object.assign(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();
        },
        beforeOpen(done, type) {
            if (["view"].includes(type)) {
                getDetail(this.form.id).then((res) => {
                    this.form = res.data.data;
                });
            }
            done();
        },
        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;
                //console.log(JSON.stringify(data))
                this.page.total = data.total;
                this.data = data.records;
                this.loading = false;
                this.selectionClear();
            });
        },
    },
};
</script>

<style  lang="scss" scoped></style>
