<template>
    <ap-container>
        <ap-main margin="1111">
            <ap-table ref="tSysTaskEnvRef" :options.sync="tSysTaskEnvOpt"></ap-table>
        </ap-main>
    </ap-container>
</template>

<script>
export default {
    name: 'SysTaskEnv',
    data() {
        let columns = [
            {
                prop: 'fdType',
                label: '类型',
                type: 'select',
                align: "center",
                width: "100px",
                addDefaultValue: 0,
                selectOptions: [
                    {label: "系统", value: 0},
                    {label: "个人", value: 1}
                ]
            }, {
                prop: 'fdMacAddress',
                label: 'MAC地址',
                type: 'input',
                width: "150px",
            }, {
                prop: 'fdExchange',
                label: '交换机',
                type: 'input',
                width: "150px",
            }, {
                prop: 'fdRoutingKey',
                label: '路由',
                type: 'input',
                width: "150px",
            }, {
                prop: 'fdQueue',
                label: '队列',
                type: 'input',
                width: "150px",
            }, {
                prop: 'fdComment',
                label: '备注',
                type: 'input',
                width: "200px",
            },{
                prop: 'createStatus',
                label: '创建状态',
                type: 'select',
                align: "center",
                width: "100px",
                addDefaultValue: 0,
                selectOptions: [
                    {label: "未创建", value: 0},
                    {label: "已创建", value: 1}
                ]
            },{
                prop: 'listenerStatus',
                label: '监听状态',
                type: 'select',
                align: "center",
                width: "100px",
                addDefaultValue: 0,
                selectOptions: [
                    {label: "未监听", value: 0},
                    {label: "已监听", value: 1}
                ]
            }, {
                prop: 'fdDisabled',
                label: '是否有效',
                type: 'select',
                align: "center",
                width: "100px",
                addDefaultValue: 0,
                selectOptions: [
                    {label: "有效", value: 0},
                    {label: "无效", value: 1}
                ],
                html: function (value, row) {
                    if (value == 0) {
                        return '<div class="ap-tag-success">有效</div>'
                    } else if (value == 1) {
                        return '<div class="ap-tag-danger">无效</el-tag>';
                    }
                }
            },
        ];
        let toolbar = [
            {
                btnType: "primary",
                text: "测试",
                icon: "el-icon-bug",
                onClick: () => {
                    this.testMq();
                }
            }, {
                btnType: "primary",
                text: "MAC查看",
                icon: "el-icon-search",
                onClick: () => {
                    this.showMac();
                }
            }, "separator", {
                btnType: "primary",
                text: "初始化队列",
                icon: "el-icon-circle-plus-outline",
                onClick: () => {
                    this.createMq();
                }
            }, {
                btnType: "primary",
                text: "添加监听",
                icon: "el-icon-collection-tag",
                onClick: () => {
                    this.addQueueListener();
                }
            }, {
                btnType: "danger",
                text: "移除队列",
                icon: "el-icon-trash-o",
                onClick: () => {
                    this.deleteMq();
                }
            },
            "separator", "add", "edit", "del", "cancel", "refresh", "save"];
        let tableOpt = {
            title: "调度环境",
            columns: columns,
            editPk: "fdId", //默认fdId
            deletePk: "fdId", // 默认fdId
            dataUrl: "/apt/sys/TSysTaskEnv/getTSysTaskEnvGridList",
            saveUrl: "/apt/sys/TSysTaskEnv/saveTSysTaskEnvGridData",
            deleteUrl: "/apt/sys/TSysTaskEnv/deleteTSysTaskEnvGridData",
            toolbarBtn: toolbar,
            initData: true, // 默认false
            params: {}
        };
        return {
            tSysTaskEnvOpt: tableOpt,
        }
    },
    methods: {
        getOneSelect() {
            let selects = this.$refs.tSysTaskEnvRef.getSelection();
            if (selects == null || selects.length < 1) {
                this.$message.error("请选择需要操作的行")
                return null;
            }
            if (selects.length != 1) {
                this.$message.error("只能选择单行操作！")
                return null;
            }
            return selects[0];
        },
        createMq() {
            let selectRow = this.getOneSelect();
            if (selectRow == null) {
                return
            }
            this.$request({
                url: '/apt/mq/createMq',
                method: 'post',
                data: {
                    queueName: selectRow.fdQueue,
                    exchangeName: selectRow.fdExchange,
                    routingKey: selectRow.fdRoutingKey,
                }
            }).then(response => {
                this.$message.success(response.data);
            })
        },
        deleteMq() {
            let selectRow = this.getOneSelect();
            if (selectRow == null) {
                return
            }
            this.$request({
                url: '/apt/mq/deleteMq',
                method: 'post',
                data: {
                    queueName: selectRow.fdQueue,
                    exchangeName: selectRow.fdExchange
                }
            }).then(response => {
                this.$message.success(response.data);
            })
        },
        addQueueListener() {
            let selectRow = this.getOneSelect();
            if (selectRow == null) {
                return;
            }
            this.$request({
                url: '/apt/mq/addQueueListener',
                method: 'post',
                data: {
                    queueName: selectRow.fdQueue,
                    macAddress: selectRow.fdMacAddress
                }
            }).then(response => {
                this.$message.success(response.data);
            })
        },
        testMq() {
            let selectRow = this.getOneSelect();
            if (selectRow == null) {
                return;
            }
            this.$request({
                url: '/apt/mq/sendMessageTest',
                method: 'post',
                data: {
                    exchangeName: selectRow.fdExchange,
                    routingKey: selectRow.fdRoutingKey,
                    queueName: selectRow.fdQueue
                }
            }).then(response => {
                this.$message.success(response.data);
            })
        },
        showMac(){
            this.$request({
                url: '/apt/mq/showMac',
                method: 'post',
                data: {
                }
            }).then(response => {
                this.$message.success(response.data);
            })
        }
    }
}
</script>

<style scoped>
</style>
