<template>
    <el-container>
        <el-main>
            <el-card style="background-color: #F5F7FA">
                <!-- 搜素区域 -->
                <div>
                    <el-row>
                        <el-col :span="7">
                            <el-input v-model="routingKey_queryInfo.routingKeyName" placeholder="路由键名称"
                                     size="medium" clearable>
                                <template  slot="prepend" ><i class="el-icon-user"></i>路由键名称</template>
                            </el-input>
                        </el-col>

                        <el-col :span="7">
                            <el-cascader
                                    size="medium" filterable clearable
                                    v-model="routingKey_queryInfo.exchangeId"
                                    :options="routingKey_config.cascade.server_host_exchangeList"
                                    :show-all-levels="false"
                                    :change-on-select="false"
                                    style="width: 70%"
                                    @change="cascadeQueryChange"
                                    :props="routingKey_config.cascade.defaultParams"
                                    placeholder="请选择所属交换机">
                            </el-cascader>
                        </el-col>
                        <el-col :span="8">
                            <el-button type="warning " style="width:150px" size="medium" icon="el-icon-search" @click="routingKey_getList">
                                查询路由键
                            </el-button>
                            <el-button type="primary" style="width:150px" size="medium" icon="el-icon-plus" @click="routingKey_toAddDialog">
                                添加路由键
                            </el-button>
                        </el-col>
                    </el-row>
                </div>
                <!-- 列表区域 -->
                <div style="margin-top: 20px;">
                    <el-table :data="routingKey_page.routingKeyList" border v-loading="routingKey_loading.listLoading"
                              @selection-change="routingKey_selectChange"
                              element-loading-text="获取路由键列表,拼命加载中" size="small"
                              :header-cell-style="{background:'#6F8294',color:'#FFFFFF'}"
                              style="border-radius: 10px 10px 0px 0px;line-height: 25px;">
                        <el-table-column type="selection" width="55" align="center"></el-table-column>
                        <el-table-column type="index" align="center"></el-table-column>
                        <el-table-column label="路由键名称" prop="routingKeyName" align="center"
                                         width="220"></el-table-column>
                        <el-table-column label="所属交换机" prop="busExchange.exchangeName" align="center"
                                         width="220"></el-table-column>
                        <el-table-column label="相关描述" prop="description" align="center" width="280"></el-table-column>
                        <!-- fixed="right" -->
                        <el-table-column label="操作"  align="center">
                            <template slot-scope="scope">
                                <!-- 编辑按钮-->
                                <el-tooltip effect="dark" content="编辑" placement="top-start" :enterable="false">
                                    <el-button type="primary" icon="el-icon-edit" size="mini"
                                               @click="routingKey_toEditDialog(scope.$index,scope.row)">编辑
                                    </el-button>
                                </el-tooltip>

                                <!-- 删除按钮-->
                                <el-tooltip effect="dark" content="删除" placement="top-start" :enterable="false">
                                    <el-button type="danger" icon="el-icon-delete" size="mini"
                                               :loading="routingKey_loading.delLoading"
                                               @click="routingKey_remove(scope.$index,scope.row)">删除
                                    </el-button>
                                </el-tooltip>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>

                <!--分页区域-->
                <div style="margin-top: 10px">
                    <el-row :gutter="24">
                        <el-col :span="5">
                            <el-button type="danger" icon="el-icon-delete"
                                       :loading="routingKey_loading.batchDelLoading"
                                       :disabled="this.routingKey_page.selectList.length===0"
                                       @click="routingKey_batchRemove">批量删除
                            </el-button>
                        </el-col>
                        <el-col :span="10" style="margin-top: 5px">
                            <el-pagination
                                    @size-change="routingKey_handleSizeChange"
                                    @current-change="routingKey_handleCurrentChange"
                                    :current-page="routingKey_queryInfo.pageNum"
                                    :page-sizes="[10,20,30,50]"
                                    :page-size="routingKey_queryInfo.pageSize"
                                    layout="total, sizes, prev, pager, next, jumper"
                                    :total="routingKey_page.total">
                            </el-pagination>
                        </el-col>
                    </el-row>
                </div>
            </el-card>
            <!-- 增加,对话框区域 -->
            <div>
                <el-dialog title="添加路由键"
                           :visible.sync="routingKey_dialog.addDialog"
                           style="margin-top: -50px" text-align="center" width="50%"
                           @close="routingKey_addDialogClosed">

                    <el-form :inline="true" label-width="120px" status-icon style="margin-top: -10px" align="center"
                             :model="routingKey_addForm" :rules="routingKey_addFormRules" ref="routingKey_addFormRef">
                        <el-row>
                            <el-form-item label="" prop="routingKeyName">
                                <el-input v-model="routingKey_addForm.routingKeyName" placeholder="请输入路由键名称"
                                          style="width:300px" size="small" clearable>
                                    <template  slot="prepend" >路由键名称</template>
                                </el-input>
                            </el-form-item>
                        </el-row>
                        <el-row>
                            <el-form-item label="" prop="exchangeId">
                                <el-cascader
                                        style="width:300px" size="small"
                                        filterable clearable
                                        v-model="routingKey_addForm.exchangeId"
                                        :options="routingKey_config.cascade.server_host_exchangeList"
                                        :show-all-levels="false"
                                        :change-on-select="false"
                                        @change="cascadeAddChange"
                                        :props="routingKey_config.cascade.defaultParams"
                                        placeholder="请选择所属交换机"></el-cascader>
                            </el-form-item>
                        </el-row>
                        <el-row>
                            <el-form-item label="" prop="description">
                                <el-input v-model="routingKey_addForm.description" style="width:300px" clearable size="small"
                                          type="textarea" autosize :rows="1"
                                          placeholder="请输入相关描述">
                                </el-input>
                            </el-form-item>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer" style="text-align: center;margin-top: -20px">
                        <el-button type="info" @click="routingKey_dialog.addDialog = false" size="medium"
                                   style="width: 100px;background-color: #6F8294;color: #ffffff">取消
                        </el-button>
                        <el-button type="primary" @click="routingKey_add" :loading="routingKey_loading.addLoading"
                                   size="medium" style="width: 100px">添加
                        </el-button>
                    </div>
                </el-dialog>
            </div>
            <!-- 编辑,对话框区域 -->
            <div>
                <el-dialog title="编辑交换机"
                           :visible.sync="routingKey_dialog.editDialog"
                           style="margin-top: -50px" text-align="center" width="50%"
                           @close="routingKey_editDialogClosed">
                    <el-form :inline="true" label-width="120px" status-icon style="margin-top: -10px" align="center"
                             :model="routingKey_editForm" :rules="routingKey_editFormRules"
                             ref="routingKey_editFormRef">
                        <el-row>
                            <el-form-item label="" prop="routingKeyName">
                                <el-input v-model="routingKey_editForm.routingKeyName" placeholder="请输入路由键名称"
                                          style="width:300px" size="small" disabled clearable>
                                    <template  slot="prepend" >路由键名称</template>
                                </el-input>
                            </el-form-item>
                        </el-row>
                        <el-row>
                            <el-form-item label="" prop="exchangeId">
                                <el-cascader
                                        style="width:300px" size="small"
                                        filterable clearable disabled
                                        v-model="routingKey_editForm.exchangeId"
                                        :options="routingKey_config.cascade.server_host_exchangeList"
                                        :show-all-levels="false"
                                        :change-on-select="false"
                                        @change="cascadeEditChange"
                                        :props="routingKey_config.cascade.defaultParams"
                                        placeholder="请选择所属交换机"></el-cascader>
                            </el-form-item>
                        </el-row>
                        <el-row>
                            <el-form-item label="" prop="description">
                                <el-input v-model="routingKey_editForm.description" style="width:300px" clearable size="small"
                                          type="textarea" autosize :rows="1"
                                          placeholder="相关描述">
                                </el-input>
                            </el-form-item>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer" style="text-align: center">
                        <el-button type="info" @click="routingKey_dialog.editDialog = false" size="medium"
                                   style="width: 100px;background-color: #6F8294;color: #ffffff">取消
                        </el-button>
                        <el-button type="primary" @click="routingKey_edit" :loading="routingKey_loading.addLoading"
                                   size="medium"
                                   style="width: 100px">
                            编辑
                        </el-button>
                    </div>
                </el-dialog>
            </div>

        </el-main>
    </el-container>
</template>

<script>
    import {
        selectRoutingKeyList,
        insertRoutingKey,
        updateRoutingKey,
        deleteRoutingKey,
        batchRemoveRoutingKey,
        getServerAndHostAndExchangeList,
    } from "../../api/message_bus";

    export default {
        name: "RoutingKey",

        data() {
            return {
                /**
                 * routingKey,配置与参数
                 */
                routingKey_config: {
                    // 级联显示
                    cascade: {
                        // 服务器_虚拟主机_交换机列表
                        server_host_exchangeList: [],
                        defaultParams: {
                            label: 'aliasName',
                            value: 'id',
                            children: 'aliasList'
                        }
                    },
                },
                /**
                 *  routingKey,搜索参数
                 */
                routingKey_queryInfo: {
                    // 路由键名称
                    routingKeyName: '',
                    // 所属交换机id
                    exchangeId: '',
                    // 当前页数
                    pageNum: 1,
                    // 每页条数
                    pageSize: 10
                },
                /**
                 * routingKey,分页
                 */
                routingKey_page: {
                    // 路由键列表
                    routingKeyList: [],
                    // 总条数
                    total: 0,
                    // 批量删除,选中列表
                    selectList: [],
                },
                /**
                 * routingKey,新增表单
                 */
                routingKey_addForm: {
                    // 路由键名称
                    routingKeyName: '',
                    // 所属交换机id
                    exchangeId: '',
                    // 相关描述
                    description: '',
                },
                /**
                 * routingKey,编辑表单
                 */
                routingKey_editForm: {},
                /**
                 * routingKey,加载
                 */
                routingKey_loading: {
                    // 列表加载
                    listLoading: false,
                    // 添加加载
                    addLoading: false,
                    // 编辑加载
                    editLoading: false,
                    // 删除加载
                    delLoading: false,
                    // 批量删除加载
                    batchDelLoading: false,
                },
                /**
                 *  routingKey,对话框管理
                 */
                routingKey_dialog: {
                    // 添加对话框
                    addDialog: false,
                    // 编辑对话框
                    editDialog: false,
                },

                /**
                 * routingKey,添加表单,验证规则
                 */
                routingKey_addFormRules: {
                    // 路由键名称
                    routingKeyName: [
                        {required: true, message: '请输入路由键名称', trigger: ['blur', 'change']},
                    ],
                    // 所属交换机id
                    exchangeId: [
                        {required: true, message: '请选择所属交换机', trigger: ['blur', 'change']},
                    ],
                    // // 相关描述
                    // description: [
                    //     {required: true, message: '请输入相关描述', trigger: ['blur', 'change']},
                    // ],
                },

                /**
                 * routingKey,编辑表单,验证规则
                 */
                routingKey_editFormRules: {
                    // 路由键名称
                    routingKeyName: [
                        {required: true, message: '请输入路由键名称', trigger: ['blur', 'change']},
                    ],
                    // 所属交换机id
                    exchangeId: [
                        {required: true, message: '请选择所属交换机', trigger: ['blur', 'change']},
                    ],
                    // 相关描述
                    description: [
                        {required: true, message: '请输入相关描述', trigger: ['blur', 'change']},
                    ],
                },

                /**
                 * Boolean属性,选择列表
                 */
                booleanList: [
                    {
                        value: true,
                        label: '是'
                    },
                    {
                        value: false,
                        label: '否'
                    },
                ],
            }
        },
        methods: {
            /**
             * 分页查询,监听 pageSize 改变的事件
             */
            routingKey_handleSizeChange(newSize) {
                this.routingKey_queryInfo.pageSize = newSize;
                //刷新列表
                this.routingKey_getList();
            },
            /**
             * 分页查询,监听 pageNum 改变的事件
             */
            routingKey_handleCurrentChange(newPage) {
                this.routingKey_queryInfo.pageNum = newPage;
                //刷新列表
                this.routingKey_getList();
            },
            /**
             * routingKey,分页查询
             */
            routingKey_getList() {
                // 开启加载
                this.routingKey_loading.listLoading = true;
                selectRoutingKeyList(this.routingKey_queryInfo).then((response) => {
                    let res = response.data;
                    if (res.code !== '200') {
                        // 关闭加载
                        this.routingKey_loading.listLoading = false;
                        return this.$message.error('获取路由键列表,失败!');
                    }
                    // 获取列表数据
                    this.routingKey_page.routingKeyList = res.data.list;
                    // 获取列表的总记录数
                    this.routingKey_page.total = res.data.total;
                    // 关闭加载
                    this.routingKey_loading.listLoading = false;
                    this.$message.success('获取路由键列表,成功!');
                }).catch(error => {
                    // 关闭加载
                    this.routingKey_loading.listLoading = false;
                    this.$message.error(error.toString());
                });
            },

            /**
             * routingKey,添加对话框,打开事件
             */
            routingKey_toAddDialog() {
                this.routingKey_dialog.addDialog = true;
            },
            /**
             * routingKey,添加对话框,关闭事件
             */
            routingKey_addDialogClosed() {
                // 重置对话框
                this.$refs.routingKey_addFormRef.resetFields();
            },
            /**
             * routingKey,添加功能
             */
            routingKey_add() {
                // 进行表单的预验证
                this.$refs.routingKey_addFormRef.validate(valid => {
                    // 未通过,表单预校验
                    if (!valid) return;
                    // 通过,表单预检验
                    this.routingKey_loading.addLoading = true;
                    insertRoutingKey(this.routingKey_addForm).then((response) => {
                        let res = response.data;
                        // 添加失败
                        if (res.code !== '200') {
                            if (res.code === '400') {
                                // 关闭加载
                                this.routingKey_loading.addLoading = false;
                                return this.$message.error('该路由键,已存在');
                            }
                            if (res.code === '999') {
                                // 关闭加载
                                this.routingKey_loading.addLoading = false;
                                return this.$message.error('未知异常,添加路由键,失败');
                            }
                            // 关闭加载
                            this.routingKey_loading.addLoading = false;
                            return this.$message.error('添加路由键,失败');
                        }
                        // 添加,成功
                        this.$message.success('添加路由键,成功');
                        // 关闭加载
                        this.routingKey_loading.addLoading = false;
                        // 隐藏对话框
                        this.routingKey_dialog.addDialog = false;
                        // 刷新列表
                        this.routingKey_getList();
                    }).catch(error => {
                        // 关闭加载
                        this.routingKey_loading.addLoading = false;
                        this.$message.error(error.toString());
                    });
                })
            },

            /**
             * routingKey,编辑对话框,打开事件
             */
            routingKey_toEditDialog(index, row) {
                this.routingKey_editForm = Object.assign({}, row);
                this.routingKey_dialog.editDialog = true;
            },
            /**
             * routingKey,编辑对话框,关闭事件
             */
            routingKey_editDialogClosed() {
                //重置对话框
                this.$refs.routingKey_editFormRef.resetFields();
            },
            /**
             * routingKey,编辑功能
             */
            routingKey_edit() {
                //进行表单的预验证
                this.$refs.routingKey_editFormRef.validate(valid => {
                    // 未通过,表单预校验
                    if (!valid) return;
                    // 通过,表单预检验
                    this.routingKey_loading.editLoading = true;
                    updateRoutingKey(this.routingKey_editForm).then((response) => {
                        let res = response.data;
                        if (res.code !== '200') {
                            if (res.code === '400') {
                                // 关闭加载
                                this.routingKey_loading.editLoading = false;
                                return this.$message.error('该路由键,已存在');
                            }
                            if (res.code === '999') {
                                // 关闭加载
                                this.routingKey_loading.editLoading = false;
                                return this.$message.error('未知异常,编辑路由键信息,失败');
                            }
                            //关闭加载
                            this.routingKey_loading.editLoading = false;
                            return this.$message.error('编辑路由键信息,失败');
                        }
                        // 关闭加载
                        this.routingKey_loading.editLoading = false;
                        this.$message.success('编辑路由键信息,成功!');
                        // 隐藏对话框
                        this.routingKey_dialog.editDialog = false;
                        // 刷新列表
                        this.routingKey_getList();
                    }).catch(error => {
                        // 关闭加载
                        this.routingKey_loading.editLoading = false;
                        this.$message.error(error.toString());
                    });
                })
            },

            /**
             * routingKey,删除功能
             */
            routingKey_remove(index, row) {
                // 弹框询问是否删除?
                this.$confirm('此操作永久删除该路由键信息, 是否继续?', '警告', {
                        confirmButtonText: '确定删除',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }
                ).then(() => {
                    // 开启加载
                    this.routingKey_loading.delLoading = true;
                    deleteRoutingKey(row).then((response) => {
                        let res = response.data;
                        if (res.code !== '200') {
                            // 关闭加载
                            this.routingKey_loading.delLoading = false;
                            return this.$message.error('删除路由键信息,失败');
                        }
                        // 关闭加载
                        this.routingKey_loading.delLoading = false;
                        this.$message.success('删除路由键信息,成功!');
                        // 刷新列表
                        this.routingKey_getList();
                    }).catch(error => {
                        // 关闭加载
                        this.routingKey_loading.delLoading = false;
                        this.$message.error(error.toString());
                    });
                }).catch(() => {
                });
            },

            /**
             * 批量删除---选中
             */
            routingKey_selectChange: function (selectList) {
                this.routingKey_page.selectList = selectList;
            },
            /**
             * routingKey,批量删除功能
             */
            routingKey_batchRemove() {
                const ids = this.routingKey_page.selectList.map(item => item.id).toString();
                this.$confirm('此操作将永久删除选中的路由键信息, 是否继续?', '警告', {
                        confirmButtonText: '确定删除',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }
                ).then(() => {
                    // 开启加载
                    this.routingKey_loading.batchDelLoading = true;
                    let params = {ids: ids};
                    batchRemoveRoutingKey(params).then(response => {
                        let res = response.data;
                        if (res.code !== '200') {
                            // 关闭加载
                            this.routingKey_loading.batchDelLoading = false;
                            return this.$message.error('批量删除路由键信息,失败');
                        }
                        this.$message.success('批量删除路由键信息,成功!');
                        // 关闭加载
                        this.routingKey_loading.batchDelLoading = false;
                        // 刷新列表
                        this.routingKey_getList();
                    }).catch(error => {
                        // 关闭加载
                        this.routingKey_loading.batchDelLoading = false;
                        this.$message.error(error.toString());
                    });
                }).catch(() => {
                });
            },
            /**
             * 服务器与虚拟主机 1:n
             * 虚拟主机与交换机 1: n
             * 获取列表
             */
            selectServerAndHostAndExchangeList() {
                getServerAndHostAndExchangeList().then((response) => {
                    let res = response.data;
                    if (res.code !== '200') {
                        return this.$message.error('获取服务器与虚拟主机与交换机信息,失败!');
                    }
                    // 获取列表数据
                    this.routingKey_config.cascade.server_host_exchangeList = res.data;
                    // this.$message.success('获取虚拟主机列表,成功!');
                }).catch(error => {
                    this.$message.error(error.toString());
                });
            },

            /**
             * 查询,级联。值改变激发的方法
             * @param value 改变后的值
             */
            cascadeQueryChange(value) {
                let ids = value.join().split(",");
                this.routingKey_queryInfo.exchangeId = ids[ids.length - 1];
            },
            /**
             * 添加,级联。值改变激发的方法
             * @param value 改变后的值
             */
            cascadeAddChange(value) {
                let ids = value.join().split(",");
                this.routingKey_addForm.exchangeId = ids[ids.length - 1];
            },
            cascadeEditChange(value) {
                let ids = value.join().split(",");
                this.routingKey_editForm.exchangeId = ids[ids.length - 1];
            },
        },
        created() {
            if (this.$route.params.exchange_scopeRow !== undefined) {
                // 参数,交换机id
                this.routingKey_queryInfo.exchangeId = this.$route.params.exchange_scopeRow.id;
                // 刷新列表
                this.routingKey_getList();
            } else {
                // 刷新列表
                // this.routingKey_getList();
            }
        },
        mounted() {
            // 获取,服务器、虚拟主机、交换机的级联列表
            this.selectServerAndHostAndExchangeList();
        },
        computed: {},
    }
</script>

<style scoped>

</style>