<template>
    <ap-container>
        <ap-header margin="1111">
            <el-form ref="tSysConfigServerSearchForm" :model="tSysConfigServerSearchForm"
                     :rules="tSysConfigServerSearchFormRules"
                     :inline="true" class="layout-header-form" @submit.native.prevent="searchEvent">
                <el-form-item label="搜索:" class="layout-header-form-item" prop="likeStr">
                    <el-input v-model="tSysConfigServerSearchForm.likeStr" placeholder="操作名称/容器名"></el-input>
                </el-form-item>
            </el-form>
            <el-button type="primary" @click="searchEvent">查询</el-button>
        </ap-header>
        <ap-main margin="0111">
            <ap-table ref="tSysConfigServerRef" :options.sync="tSysConfigServerOpt"></ap-table>
        </ap-main>
    </ap-container>
</template>

<script>
export default {
    name: 'SysConfigServer',
    data() {
        let columns = [
            {
                prop: 'fdType',
                label: '类型',
                type: 'select',
                width: 80,
                selectOptions: [{label: "正式", value: 1}, {label: "测试", value: 0}],
                selectHtml: function (value, text, row) {
                    if (value == 0) {
                        return '<div class="ap-tag-info">测试</div>';
                    } else if (value == 1) {
                        return '<div class="ap-tag-warning" >正式</div>';
                    }
                }
            }, {
                prop: 'fdName',
                label: '操作名称',
                type: 'input',
                width: 120
            }, {
                prop: 'fdServiceName',
                label: '容器名',
                type: 'input',
                width: 100
            }, {
                prop: 'fdIp',
                label: '服务器IP',
                type: 'input',
                width: 100
            }, {
                prop: 'fdServerPath',
                label: '服务器配置文件路径',
                type: 'input',
                minWidth: 150
            }, {
                prop: 'fdTime',
                label: '服务器文件日期',
                type: 'datePicker',
                datePickerFormat: 'MM-dd HH:mm:ss',
                width: 110,
                add: false,
                edit: false
            }, {
                prop: 'fdLocalPath',
                label: '本地文件路径',
                type: 'input',
                minWidth: 200
            }, {
                prop: 'fdErrorKeyword',
                label: '异常检查关键字',
                type: 'input',
                minWidth: 120
            }, {
                prop: 'fdSort',
                label: '排序',
                type: 'input',
                width: 40
            }
        ];
        let tableOpt = {
            title: "系统配置-服务器",
            columns: columns,
            editPk: "fdId", //默认fdId
            deletePk: "fdId", // 默认fdId
            dataUrl: "/apm/sys/SysConfigServer/getTSysConfigServerGridList",
            saveUrl: "/apm/sys/SysConfigServer/saveTSysConfigServerGridData",
            deleteUrl: "/apm/sys/SysConfigServer/deleteTSysConfigServerGridData",
            toolbarBtn: [
                {btnType: "primary", text: "上传配置文件", icon: "el-icon-upload2", onClick: this.uploadConfigFile},
                {btnType: "warning", text: "重启服务", icon: "el-icon-s-promotion", onClick: this.restartServer},
                "separator",
                {btnType: "warning", text: "更新日期", icon: "el-icon-circle-check", onClick: this.initServerFileTime},
                "separator",
                "add", "edit", "del", "refresh", "save"],
            initData: true, // 默认false
            params: () => {
                return {
                    likeStr: this.tSysConfigServerSearchForm.likeStr
                }
            }
        };
        return {
            tSysConfigServerOpt: tableOpt,
            tSysConfigServerSearchForm: {
                likeStr: ""
            },
            tSysConfigServerSearchFormRules: {
                likeStr: [{min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}]
            }
        }
    },
    methods: {
        // 查询
        searchEvent() {
            // 表单rules认证
            this.$refs.tSysConfigServerSearchForm.validate((valid) => {
                if (!valid) {
                    this.$message.error('搜索条件格式不正确！');
                    return false;
                }
                this.$refs.tSysConfigServerRef.refresh();
            });
        },
        getSelectRow() {
            let selectRows = this.$refs.tSysConfigServerRef.getSelection();
            if (selectRows == null || selectRows.length == 0) {
                this.$message.error("请选择行")
                return null;
            }
            if (selectRows.length > 1) {
                this.$message.error("不允许多行操作，请选择唯一行！")
                return null;
            }
            return selectRows[0];
        },
        uploadConfigFile() {
            let row = this.getSelectRow();
            if (row == null) {
                return;
            }
            this.$request({
                url: '/apm/sys/SysConfigServer/uploadConfigFile',
                method: 'post',
                data: {
                    id: row.fdId
                }
            }).then(response => {
                this.$message.success("上传成功")
            })
        },
        restartServer() {
            let row = this.getSelectRow();
            if (row == null) {
                return;
            }

            this.$confirm(`确定要重启【${row.fdServiceName}】服务?`, '提示', {
                confirmButtonText: '重启',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$request({
                    url: '/apm/sys/SysConfigServer/restartServer',
                    method: 'post',
                    data: {
                        id: row.fdId
                    }
                }).then(response => {
                    this.$message.success(response.data);
                })
            });
        },
        initServerFileTime() {
            this.$confirm(`确定要更新服务器配置文件日期?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$request({
                    url: '/apm/sys/SysConfigServer/initServerFileTime',
                    method: 'post',
                }).then(response => {
                    this.$message.success("日期更新成功");
                    this.$refs.tSysConfigServerRef.refresh();
                })
            });
        },
    }
}
</script>

<style scoped>
</style>
