<!--
 * @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">
                <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>
            </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="toAddMonitorSite(row)" icon="el-icon-plus">添加监测站
                            </el-button>
                        </template>
                    </avue-crud>
                </basic-container>
            </el-col>
        </el-row>
        <el-drawer title="添加监测站" :visible.sync="addMonitorSiteDialog" size="60%" append-to-body v-if="addMonitorSiteDialog">
            <monitorSiteAddVue ref="addMonitorSiteView" :visible.sync="addMonitorSiteDialog" :orgName="orgName"
                :wiuNm="wiuNm" :wiuCd="wiuCd" @fevent="update_table">
            </monitorSiteAddVue>
        </el-drawer>
    </div>
</template>

<script>
import { getPage, getDetail, add, update, remove } from "@/api/baseinfo/wateruseconsumer.js";
import { getTree } from "@/api/baseinfo/org";
import { mapGetters } from "vuex";
import monitorSiteAddVue from './addmonitorsite.vue'
export default {
    components: {
        monitorSiteAddVue
    },
    data() {
        return {
            addMonitorSiteDialog: false,
            wiuNm: '',
            wiuCd: '',
            orgName: '',
            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: "wiuCd",
                        search: true,
                        span: 24,
                        width: 120,
                        editDisabled: true,
                        rules: [
                            {
                                required: true,
                                message: "请录入取用水户代码",
                                trigger: "click",
                            },
                        ],
                    },
                    {
                        label: "取用水户名称",
                        prop: "wiuNm",
                        search: true,
                        span: 24,
                        rules: [
                            {
                                required: true,
                                message: "请录入取用水户名称",
                                trigger: "click",
                            },
                        ],

                    },
                    {
                        label: "行政机构",
                        prop: "orgName",
                        span: 24,
                        editDisplay: false,
                        addDisplay: false,
                    },
                    {
                        label: "行政机构",
                        prop: "orgId",
                        span: 24,
                        hide: true,
                        viewDisplay: false,
                        dicUrl: '/api/galaxy-business/base/org/tree',
                        type: "tree",
                        props: {
                            labelText: "标题",
                            label: "title",
                            value: "value",
                            children: "children",
                        },
                        rules: [
                            {
                                required: true,
                                message: "请选择行政区",
                                trigger: "click",
                            },
                        ],
                    },
                    {
                        label: "行政区划编码",
                        prop: "adlCd",
                        span: 24,
                        hide: true,
                        tip: "请输入县级行政区划编码",
                    },
                    {
                        label: "法人代表",
                        prop: "lrNm",
                        span: 24,
                    },
                    {
                        label: '行业类别',
                        prop: 'tradTp',
                        type: 'select',
                        props: {
                            label: 'name',
                            value: 'code'
                        },
                        span: 24,
                        dicData: [{
                            name: '火力发电',
                            code: '4411'
                        }, {
                            name: '水力发电',
                            code: '4412'
                        }, {
                            name: '核力发电',
                            code: '4413'
                        }, {
                            name: '其他能源发电',
                            code: '4419'
                        }, {
                            name: '自来水的生产和供应',
                            code: '4610'
                        }, {
                            name: '污水处理',
                            code: '4620'
                        }, {
                            name: '其他水的处理、利用与分配',
                            code: '4620'
                        }],
                    },
                    {
                        label: '经济类型',
                        prop: 'ecoTp',
                        type: 'select',
                        props: {
                            label: 'name',
                            value: 'code'
                        },
                        span: 24,
                        dicData: [{
                            name: '国有全资',
                            code: "1"
                        }, {
                            name: '集体全资',
                            code: "2"
                        }, {
                            name: '私营经济',
                            code: "3"
                        }, {
                            name: '个体经济',
                            code: "4"
                        }, {
                            name: '联营经济',
                            code: "5"
                        }, {
                            name: '股份制经济',
                            code: "6"
                        }, {
                            name: '外商投资经济',
                            code: "7"
                        }, {
                            name: '港澳台投资经济',
                            code: "8"
                        }, {
                            name: '其他',
                            code: "9"
                        }],
                    },
                    {
                        label: '监控级别',
                        prop: 'monLevel',
                        type: 'select',
                        props: {
                            label: 'name',
                            value: 'code'
                        },
                        span: 24,
                        dicData: [{
                            name: '国控级',
                            code: "1"
                        }, {
                            name: '省控级',
                            code: "2"
                        }, {
                            name: '地市级',
                            code: "3"
                        }, {
                            name: '区县级',
                            code: "4"
                        }, {
                            name: '其他',
                            code: "9"
                        }],
                    },
                    {
                        label: '取用级别',
                        prop: 'wiuTp',
                        type: 'select',
                        props: {
                            label: 'name',
                            value: 'code'
                        },
                        span: 24,
                        dicData: [{
                            name: '既取又用',
                            code: "1"
                        }, {
                            name: '只取不用',
                            code: "2"
                        }, {
                            name: '只用不取',
                            code: "3"
                        }],
                    },
                ],
            },
            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() {
        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);
        },
        toAddMonitorSite(row) {
            this.addMonitorSiteDialog = true;
            this.wiuCd = row.wiuCd;
            this.wiuNm = row.wiuNm;
            this.orgName = row.orgName;
            this.$nextTick(() => {
                this.$refs["addMonitorSiteView"].init();
            });
        },
        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);
        },
        searchReset() {
            this.query = {};
            this.query['orgId'] = this.treeCode;
            this.page.currentPage = 1;
            this.onLoad(this.page);
        },
        searchChange(params, done) {
            this.query = params;
            this.query['orgId'] = 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></style>
