<!--
 * @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="24">
                <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 v-if="permissionList.excludeRegion" type="text" size="small"
                                @click="excludeRegion(row)" icon="el-icon-view">排除行政区划
                            </el-button>

                            <el-button v-if="permissionList.resetRegion" type="text" size="small" @click="resetRegion(row)"
                                icon="el-icon-view">重置行政区划
                            </el-button>
                        </template>
                    </avue-crud>
                </basic-container>
            </el-col>
        </el-row>
        <el-dialog title="排除行政机构" :visible.sync="excludeRegionDialog" width="60%" height="60%" :close-on-click-modal="false"
            append-to-body v-if="excludeRegionDialog">
            <avue-form ref="excludeRegionRef" :option="excludeRegionOption" v-model="excludeRegionForm"
                @submit="addCheckOrder" />
            <span slot="footer" class="dialog-footer">
                <el-button @click="excludeRegionDialog = false" size="small">取 消</el-button>
                <el-button type="primary" @click="excludeRegionSubmit" size="small">提 交</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { getPage, getDetail, add, update, remove } from "@/api/baseinfo/org.js";
import { excludeRegion, resetRegion } from "@/api/baseinfo/region.js";
import { getStore } from '@/util/store.js';
import { mapGetters } from "vuex";
export default {
    components: {

    },
    data() {
        return {
            id: "",
            post_code: '0',
            editForm: {},
            editDialog: false,
            excludeRegionDialog: false,
            excludeRegionTreeData: [],
            excludeRegionForm: {
                deptId: 0,
                adcd: '',
            },
            excludeRegionOption: {
                labelWidth: 180,
                emptyBtn: false,
                submitBtn: false,
                column: [
                    {
                        label: "运维行政区划",
                        prop: "adcd",
                        span: 24,
                        editDisplay: false,
                        accordion: true,
                        type: "tree",
                        //dictData: [],
                        //dicUrl: `./api/galaxy-business/baseinfo/region/original/tree/` + this.adCode,
                        defaultExpandAll: true,
                        props: {
                            label: "title",
                            value: "value",
                        },
                        rules: [
                            {
                                required: true,
                                message: "请选择运维的行政区划",
                                trigger: "click",
                            },
                        ],

                    },
                ]
            },
            form: {},
            selectionList: [],
            query: {},
            loading: true,
            page: {
                pageSize: 10,
                currentPage: 1,
                total: 0,
            },
            option: {
                labelWidth: 140,
                viewLabelWidth: 140,
                searchlabelWidth: 140,
                height: "auto",
                calcHeight: 80,
                align: "center",
                headerAlign: "center",
                tip: false,
                simplePage: true,
                searchShow: true,
                searchMenuSpan: 6,
                border: true,
                index: true,
                selection: false,
                viewBtn: false,
                viewTitle: "运维人员信息",
                menuWidth: 300,
                dialogType: "drawer",
                dialogClickModal: false,
                columnBtn: false,
                addBtnText: "新增盟市",
                column: [
                    {
                        label: "盟市全称",
                        prop: "deptName",
                        span: 24,
                        search: true,
                        rules: [
                            {
                                required: true,
                                message: "请录入盟市全称",
                                trigger: "click",
                            },
                        ],
                    },
                    {
                        label: "运维行政区划",
                        prop: "adcd",
                        span: 24,
                        hide: true,
                        editDisplay: false,
                        accordion: true,
                        type: "tree",
                        dicUrl: `./api/galaxy-business/baseinfo/region/original/tree`,
                        props: {
                            label: "title",
                            value: "value",
                        },
                        rules: [
                            {
                                required: true,
                                message: "请选择运维的行政区划",
                                trigger: "click",
                            },
                        ],
                    },
                    {
                        label: "运维行政区划",
                        prop: "adnm",
                        span: 24,
                        editDisabled: true,
                        addDisplay: false,
                    },
                    {
                        label: "备注说明",
                        prop: "remark",
                        span: 24,
                        type: "textarea",
                    },
                ],
            },
            data: [],
        };
    },
    computed: {
        ...mapGetters(["userInfo", "permission"]),
        permissionList() {
            if (this.post_code == '0') {
                return {
                    addBtn: true,
                    viewBtn: false,
                    delBtn: true,
                    editBtn: true,
                    excludeRegion: true,
                    resetRegion: true,
                };
            } else {
                return {
                    addBtn: false,
                    viewBtn: false,
                    delBtn: false,
                    editBtn: false,
                    excludeRegion: false,
                    resetRegion: false,
                };
            }
        },
        ids() {
            let ids = [];
            this.selectionList.forEach((ele) => {
                ids.push(ele.id);
            });
            return ids.join(",");
        },
    },
    mounted() { },
    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;
        }
    },
    methods: {
        excludeRegion(row) {
            this.excludeRegionForm['deptId'] = row.id;
            let dicUrl = `./api/galaxy-business/baseinfo/region/original/tree/` + row.adcd;
            let column = this.findObject(this.excludeRegionOption.column, "adcd")
            column.value = ''
            column.dicUrl = dicUrl
            column.display = true;
            //this.excludeRegionTreeData = [];
            // getOriginalSubTree(row.adcd).then(res => {
            //     console.log(JSON.stringify(res.data))
            //     this.excludeRegionTreeData = res.data.data.map(item => {
            //         return {
            //             ...item,
            //             leaf: !item.hasChildren
            //         }
            //     })
            //     let column = this.findObject(this.excludeRegionOption.column, "adcd")
            //     column.dictData = this.excludeRegionTreeData
            //     column.display = true;

            // });
            this.excludeRegionDialog = true;
        },
        resetRegion(row) {
            resetRegion({ 'adcd': row.adcd, 'deptId': row.id }).then(res => {
                console.log(JSON.stringify(res))
                if (res.data.success) {
                    this.$message({
                        type: "success",
                        message: "操作成功!",
                    });
                }
            });
        },
        excludeRegionSubmit() {
            excludeRegion(this.excludeRegionForm).then(res => {
                console.log(JSON.stringify(res))
                this.excludeRegionDialog = false;
                this.$message({
                    type: "success",
                    message: "操作成功!",
                });

            });
        },
        rowSave(row, done, loading) {
            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: "操作成功!",
                    });
                });
        },
        searchReset() {
            this.query = {};
            this.onLoad(this.page);
        },
        searchChange(params, done) {
            this.query = params;
            this.page.currentPage = 1;
            this.onLoad(this.page, params);
            done();
        },
        selectionChange(list) {
            this.selectionList = list;
        },
        selectChange(item) {
            console.log(item)
        },
        selectionClear() {
            this.selectionList = [];
            this.$refs.crud.toggleSelection();
        },
        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();
        },
        currentChange(currentPage) {
            this.page.currentPage = currentPage;
        },
        sizeChange(pageSize) {
            this.page.pageSize = pageSize;
        },
        refreshChange() {
            this.onLoad(this.page, this.query);
        },
        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>
