<!--
 * @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"
                        @before-open="beforeOpen" @select-change="selectChange">
                        <template slot-scope="{ row, index }" slot="menu">
                            <el-button type="text" size="small" @click="toAddMonitorPoint(row)" icon="el-icon-plus">添加监测点
                            </el-button>
                        </template>

                    </avue-crud>
                </basic-container>
            </el-col>
        </el-row>
        <el-drawer title="添加监测点" :visible.sync="addMonitorPointDialog" size="60%" append-to-body
            v-if="addMonitorPointDialog">
            <monitorPointAddVue ref="addMonitorPointView" :visible.sync="addMonitorPointDialog" :wiustNm="wiustNm"
                :wiustCd="wiustCd" @fevent="update_table">
            </monitorPointAddVue>
        </el-drawer>
    </div>
</template>

<script>
import { getPage, getDetail, add, update, remove } from "@/api/baseinfo/monitorsite.js";
import { getTree } from "@/api/baseinfo/org";
import { mapGetters } from "vuex";
import monitorPointAddVue from "./addmonitorpoint.vue";
export default {
    components: {
        monitorPointAddVue
    },
    data() {
        return {
            addMonitorPointDialog: false,
            wiustCd: '',
            wiustNm: '',
            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) ? "" : 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: 80,
                align: "center",
                headerAlign: "center",
                lazy: false,
                tip: false,
                simplePage: true,
                searchShow: true,
                searchMenuSpan: 6,
                tree: true,
                border: true,
                index: true,
                selection: false,
                viewBtn: true,
                viewTitle: "监测站信息",
                editBtn: true,
                delBtn: true,
                addBtn: true,
                addBtnText: "添加监测站",
                menuWidth: 220,
                dialogType: "drawer",
                dialogClickModal: false,
                columnBtn: false,
                column: [
                    {
                        label: "监测站代码",
                        prop: "wiustCd",
                        search: true,
                        span: 24,
                        width: 120,
                        editDisabled: true,
                        rules: [
                            {
                                required: true,
                                message: "请录入取用水测站代码",
                                trigger: "click",
                            },
                        ],
                    },
                    {
                        label: "监测站名称",
                        prop: "wiustNm",
                        search: true,
                        span: 24,
                        rules: [
                            {
                                required: true,
                                message: "请录入取用水测站名称",
                                trigger: "click",
                            },
                        ],

                    },
                    {
                        label: "用水户名称",
                        prop: "wiuNm",
                        span: 24,
                        search: true,
                        editDisabled: true,
                        addDisplay: false,
                    },
                    {
                        label: "行政机构",
                        prop: "orgName",
                        span: 24,
                        editDisabled: true,
                        addDisplay: false,
                    },
                    {
                        label: '所属用水户',
                        prop: 'wiuCd',
                        type: 'select',
                        hide: true,
                        span: 24,
                        viewDisplay: false,
                        editDisplay: false,
                        remote: true,
                        dicUrl: `api/galaxy-business/base/wateruse/list?wiuNm={{key}}`,
                        props: {
                            label: 'wiuNm',
                            value: 'wiuCd'
                        },
                        rules: [
                            {
                                required: true,
                                message: "请选择用水户",
                                trigger: "click",
                            },
                        ],
                    },
                    {
                        label: "证照编号",
                        prop: "siteCertId",
                        span: 24,
                        hide: true,
                        rules: [
                            {
                                required: true,
                                message: "请录入证照编号",
                                trigger: "click",
                            },
                        ],
                    },
                    {
                        label: '站点建设来源类型',
                        prop: 'siteOwner',
                        hide: true,
                        type: 'select',
                        props: {
                            label: 'name',
                            value: 'code'
                        },
                        span: 24,
                        dicData: [{
                            name: '国控转计量',
                            code: '1'
                        }, {
                            name: '用户安装的计量设施',
                            code: '2'
                        }, {
                            name: '中央专项资金',
                            code: '3'
                        }],
                        rules: [
                            {
                                required: true,
                                message: "请选择数据来源类型",
                                trigger: "click",
                            },
                        ],
                    }, {
                        label: '是否同步到金水',
                        prop: 'dataShareActive',
                        hide: true,
                        type: 'radio',
                        props: {
                            label: 'name',
                            value: 'code'
                        },
                        span: 24,
                        dicData: [{
                            name: '否',
                            code: 1
                        }, {
                            name: '同步',
                            code: 2
                        }],
                        rules: [
                            {
                                required: true,
                                message: "请选择是否同步",
                                trigger: "click",
                            },
                        ],
                    },
                    {
                        label: '监测站类别',
                        prop: 'wiustTp',
                        type: 'select',
                        props: {
                            label: 'name',
                            value: 'code'
                        },
                        span: 24,
                        dicData: [{
                            name: '地表水取水站',
                            code: '1'
                        }, {
                            name: '地下水取水站',
                            code: '2'
                        }, {
                            name: '退（排）水站',
                            code: '3'
                        }],
                        rules: [
                            {
                                required: true,
                                message: "请选择取用水测站类别",
                                trigger: "click",
                            },
                        ],
                    },
                    {
                        label: "所在地",
                        prop: "loc",
                        span: 24,
                        overHidden: true,
                    },
                    {
                        label: '基面类型',
                        prop: 'datTp',
                        type: 'select',
                        hide: true,
                        props: {
                            label: 'name',
                            value: 'code'
                        },
                        span: 24,
                        dicData: [{
                            name: '地表水取水站',
                            code: '01'
                        }, {
                            name: '1954年黄海高程系',
                            code: '02'
                        }
                            , {
                            name: '其他',
                            code: '99'
                        }],
                    },
                    {
                        label: "基面高程",
                        prop: "datElev",
                        span: 24,
                        hide: true
                    },
                    {
                        label: "修正基值",
                        prop: "modBasVal",
                        span: 24,
                        hide: true
                    },
                    {
                        label: "修正参数",
                        prop: "modPara",
                        span: 24,
                        hide: true
                    },
                    {
                        label: '测井类型',
                        prop: 'wellTp',
                        type: 'select',
                        props: {
                            label: 'name',
                            value: 'code'
                        },
                        span: 24,
                        dicData: [{
                            name: '专用井',
                            code: '1'
                        }, {
                            name: '结合井',
                            code: '2'
                        }],
                    },
                    {
                        label: "测井地面高程",
                        prop: "wellLandElev",
                        span: 24,
                        hide: true
                    },
                    {
                        label: "设站年月",
                        prop: "estStYm",
                        type: "date",
                        span: 24,
                        format: 'yyyy年MM月',
                        valueFormat: 'yyyy-MM-dd HH:mm:ss',
                    }
                ],
            },
            data: [],
        };
    },
    computed: {
        ...mapGetters(["userInfo", "permission"]),
        permissionList() {
            return {};
            // if (this.post_code == '0') {
            //     return {
            //         addBtn: this.vaildData(this.permission.rtu_add, false),
            //         viewBtn: this.vaildData(this.permission.rtu_view, false),
            //         delBtn: this.vaildData(this.permission.rtu_del, false),
            //         editBtn: this.vaildData(this.permission.rtu_edit, false),
            //         importBtn: this.vaildData(this.permission.rtu_import, false),
            //         videoSiteAdd: this.vaildData(this.permission.video_site_add, false),
            //     };
            // } else {
            //     return {
            //         addBtn: false,
            //         viewBtn: true,
            //         delBtn: false,
            //         editBtn: false,
            //         importBtn: false,
            //         videoSiteAdd: false
            //     };
            // }
        },
    },
    created() {
        // let u = getStore({ name: 'userInfo' });
        // console.log(JSON.stringify(u));
        // if (u.post_id == this.website.nm_post_code_sysadmin_id) {
        //     this.post_code = this.website.nm_post_code_sysadmin_code;
        // } else if (u.post_id == this.website.nm_post_code_office_personnel_id) {
        //     this.post_code = this.website.nm_post_code_office_personnel_code;
        // }
        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;
            });
        },
        update_table() {
            this.onLoad(this.page, this.query);
        },
        nodeClick(data) {
            this.treeCode = data.id;
            this.treeParentCode = data.parentId;
            this.query = {};
            this.query['orgId'] = this.treeCode;
            this.page.currentPage = 1;
            var params = {};
            this.onLoad(this.page, params);
        },
        toAddMonitorPoint(row) {
            this.addMonitorPointDialog = true;
            this.wiustCd = row.wiustCd;
            this.wiustNm = row.wiustNm;
            this.$nextTick(() => {
                this.$refs["addMonitorPointView"].init();
            });
        },
        // toRtuImportHistoryHandler() {
        //     this.rtuImportHistoryDialog = true;
        // },
        // toAddVideoSite() {
        //     this.videoSiteAddDialog = true;
        //     this.$nextTick(() => {
        //         this.$refs["videositeadd"].init();
        //     });
        // },
        // downloadTemplate() {
        //     window.open('/file/download/template/站点信息记录表模板.xlsx')
        // },
        // 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.query = {};
            // this.query['adCode'] = this.treeCode;
            this.page.currentPage = 1;
            this.onLoad(this.page);
        },
        searchChange(params, done) {
            this.query = params;
            //this.query['adCode'] = this.treeCode;
            this.page.currentPage = 1;
            this.onLoad(this.page, params);
            done();
        },
        currentChange(currentPage) {
            console.log("currentPage " + currentPage);
            this.page.currentPage = currentPage;
        },
        sizeChange(pageSize) {
            this.page.pageSize = pageSize;
        },
        refreshChange() {
            var params = {};
            this.onLoad(this.page, params);
        },
        selectionChange(list) {
            this.selectionList = list;
        },
        selectChange(item) {
            console.log(JSON.stringify(item));
        },
        selectionClear() {
            this.selectionList = [];
            this.$refs.crud.toggleSelection();
        },
        rowSave(row, done, loading) {
            console.log(JSON.stringify(row))
            add(row).then(
                () => {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: "操作成功!",
                    });
                    done();
                },
                (error) => {
                    window.console.log(error);
                    loading();
                }
            );
        },
        rowUpdate(row, index, done, loading) {
            update(row).then(
                () => {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: "操作成功!",
                    });
                    done();
                },
                (error) => {
                    window.console.log(error);
                    loading();
                }
            );
        },
        rowDel(row) {
            this.$confirm("确定将选择数据删除?", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    return remove(row.id);
                })
                .then(() => {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: "操作成功!",
                    });
                });
        },
        handleDelete() {
            if (this.selectionList.length === 0) {
                this.$message.warning("请选择至少一条数据");
                return;
            }
            this.$confirm("确定将选择数据删除?", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    return remove(this.ids);
                })
                .then(() => {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: "操作成功!",
                    });
                    this.$refs.crud.toggleSelection();
                });
        },
        beforeOpen(done, type) {
            if (["edit", "view"].includes(type)) {
                getDetail(this.form.id).then((res) => {
                    this.form = res.data.data;
                });
            }
            done();
        },
        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>
.box {
    height: 800px;
}

.el-scrollbar {
    height: 100%;
}

.box .el-scrollbar__wrap {
    overflow: scroll;
}
</style>
