<template>
    <div class="app-container">
        <el-card v-show="showSearch">
            <el-form ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
                <el-form-item label="设备名称" prop="equipmentName">
                    <el-input v-model="queryParams.equipmentName" clearable placeholder="请输入设备名称"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="设备地址" prop="ipAddress">
                    <el-input v-model="queryParams.ipAddress" clearable placeholder="请输入设备地址"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="团体名" prop="community">
                    <el-input v-model="queryParams.community" clearable placeholder="请输入团体名"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item>
                    <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">{{ $t('search')
                        }}</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('reset')
                    }}</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="main-card" shadow="never">
            <div class="card-toolbar mb8">
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button v-hasPermi="['snmp:snmp:add']" icon="el-icon-plus" size="mini" type="primary"
                            @click="handleAdd">{{ $t('add') }}</el-button>
                    </el-col>
                    <!--            <el-col :span="1.5">
                      <el-button
                          type="success"
                          plain
                          icon="el-icon-edit"
                          size="mini"
                          :disabled="single"
                          @click="handleUpdate"
                          v-hasPermi="['snmp:snmp:edit']"
                      >修改</el-button>
                  </el-col>-->
                    <el-col :span="1.5">
                        <el-button v-hasPermi="['snmp:snmp:remove']" :disabled="multiple" icon="el-icon-delete"
                            size="mini" type="danger" @click="handleDelete">删除</el-button>
                    </el-col>
                </el-row>
                <div class="display:flex">
                    <el-tooltip class="item" effect="dark" content="导出" placement="top">
                        <el-button size="mini" style="margin-right: 10px" circle icon="el-icon-download"
                            v-hasPermi="['snmp:snmp:export']" @click="handleExport" />
                    </el-tooltip>
                    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
                </div>
            </div>
            <el-table class="base-table" v-loading="loading" :data="snmpList" @selection-change="handleSelectionChange">
                <el-table-column align="center" type="selection" width="55" />
                <el-table-column align="center" label="设备名称" prop="equipmentName" />
                <el-table-column align="center" label="设备地址" prop="ipAddress" />
                <el-table-column align="center" label="团体名" prop="community" />
                <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
                    <template slot-scope="scope">
                        <el-button v-hasPermi="['snmp:snmp:edit']" icon="el-icon-edit" size="small" type="primary" plain
                            style="padding: 5px; margin: 0" @click="OID(scope.row)">OID</el-button>
                        <el-button v-hasPermi="['snmp:snmp:edit']" icon="el-icon-edit" size="small" type="primary" plain
                            style="padding: 5px; margin: 0" @click="handleUpdate(scope.row)">修改</el-button>
                        <el-button v-hasPermi="['snmp:snmp:remove']" icon="el-icon-delete" size="small" type="danger"
                            plain style="padding: 5px; margin: 0" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum"
                :total="total" @pagination="getList" />
        </el-card>
        <!-- 添加或修改snmp协议主表对话框 -->
        <el-dialog :title="title" :visible.sync="open" append-to-body width="800px">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="设备名称" prop="equipmentName">
                    <el-input v-model="form.equipmentName" placeholder="请输入设备名称" />
                </el-form-item>
                <el-form-item label="设备地址" prop="ipAddress">
                    <el-input v-model="form.ipAddress" placeholder="请输入设备地址" />
                </el-form-item>
                <el-form-item label="团体名" prop="community">
                    <el-input v-model="form.community" placeholder="请输入团体名" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- snmp协议OID信息-->
        <el-dialog :title="OIDTitle" :visible.sync="OIDOpen" append-to-body width="800px">
            <div class="app-container">
                <el-form v-show="OIDshowSearch" ref="OIDqueryForm" :inline="true" :model="OIDqueryParams" label-width=""
                    size="small">
                    <el-form-item label="oid" prop="oid">
                        <el-input v-model="OIDqueryParams.oid" clearable placeholder="请输入oid"
                            @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item>
                        <el-button icon="el-icon-search" size="mini" type="primary" @click="OIDhandleQuery">{{
                            $t('search')
                            }}</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="OIDresetQuery">{{ $t('reset')
                        }}</el-button>
                    </el-form-item>
                </el-form>
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button v-hasPermi="['snmp:oid:add']" icon="el-icon-plus" plain size="mini" type="primary"
                            @click="OIDhandleAdd">{{ $t('add') }}</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button v-hasPermi="['snmp:oid:edit']" :disabled="OIDsingle" icon="el-icon-edit" plain
                            size="mini" type="success" @click="OIDhandleUpdate">修改</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button v-hasPermi="['snmp:oid:remove']" :disabled="OIDmultiple" icon="el-icon-delete" plain
                            size="mini" type="danger" @click="OIDhandleDelete">删除</el-button>
                    </el-col>
                    <right-toolbar :showSearch.sync="OIDshowSearch" @queryTable="OIDgetList"></right-toolbar>
                </el-row>

                <el-table :border="false" v-loading="OIDloading" :data="oidList"
                    @selection-change="OIDhandleSelectionChange">
                    <el-table-column align="center" type="selection" width="55" />
                    <el-table-column align="center" label="oid名称" prop="oidName" />
                    <el-table-column align="center" label="oid" prop="oid" />
                    <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
                        <template slot-scope="scope">
                            <el-button v-hasPermi="['snmp:oid:edit']" icon="el-icon-edit" size="mini" type="text"
                                @click="OIDhandleUpdate(scope.row)">修改</el-button>
                            <el-button v-hasPermi="['snmp:oid:remove']" icon="el-icon-delete" size="mini" type="text"
                                @click="OIDhandleDelete(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination v-show="OIDtotal > 0" :limit.sync="OIDqueryParams.pageSize"
                    :page.sync="OIDqueryParams.pageNum" :total="OIDtotal" @pagination="OIDgetList" />

                <!-- 添加或修改snmp协议OID子表对话框 -->
                <el-dialog :title="OIDtitle" :visible.sync="OIDopen" append-to-body width="500px">
                    <el-form ref="OIDform" :model="OIDform" :rules="OIDrules" label-width="80px">
                        <el-form-item v-show="oidadd" label="设备IP" prop="ipAddress">
                            <el-input v-model="OIDform.ipAddress" disabled placeholder="请输入设备IP" />
                        </el-form-item>
                        <el-form-item label="oid名称" prop="oidName">
                            <el-input v-model="OIDform.oidName" placeholder="请输入oio名字" />
                        </el-form-item>
                        <el-form-item label="oid" prop="oid">
                            <el-input v-model="OIDform.oid" placeholder="请输入oid" />
                        </el-form-item>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button type="primary" @click="OIDsubmitForm">确 定</el-button>
                        <el-button @click="OIDcancel">取 消</el-button>
                    </div>
                </el-dialog>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listSnmp, getSnmp, delSnmp, addSnmp, updateSnmp, delOid, listOid, getOid, updateOid, addOid } from '@/api/snmp/snmp';

export default {
    name: 'Snmp',
    data() {
        return {
            OIDshowSearch: true,

            OIDtotal: 0,
            OIDqueryParams: {
                oid: null,
                pageNum: 1,
                pageSize: 10,
                snmpId: null,
            },
            oidadd: false,
            OIDTitle: '',
            OIDOpen: false,
            // 遮罩层
            OIDloading: true,
            // 选中数组
            OIDids: [],
            // 非单个禁用
            OIDsingle: true,
            // 非多个禁用
            OIDmultiple: true,
            // snmp协议OID子表表格数据
            oidList: [],
            // 弹出层标题
            OIDtitle: '',
            // 是否显示弹出层
            OIDopen: false,

            // 表单参数
            OIDform: {},
            // 表单校验
            OIDrules: {},

            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 子表选中数据
            checkedSnmpOid: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // snmp协议主表表格数据
            snmpList: [],
            // snmp协议OID子表表格数据
            snmpOidList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                ipAddress: null,
                equipmentName: null,
                community: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                ipAddress: [
                    {
                        required: true,
                        trigger: 'blur',
                        message: '请输入设备IP地址',
                    },
                ],
                community: [
                    {
                        required: true,
                        trigger: 'blur',
                        message: '请输入团体名',
                    },
                ],
            },
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 搜索按钮操作 */
        OIDhandleQuery() {
            this.OIDqueryParams.pageNum = 1;
            this.OIDgetList();
        },
        /** 重置按钮操作 */
        OIDresetQuery() {
            this.resetForm('OIDqueryForm');
            this.OIDhandleQuery();
        },
        /** 查询snmp协议OID子表列表 */
        OIDgetList() {
            this.OIDloading = true;
            this.OIDqueryParams.snmpId = this.form.id;

            getSnmp(this.form.id).then((response) => {
                this.form = response.data;
                this.snmpOidList = response.data.snmpOidList;
            });
            this.OIDqueryParams.snmpId = this.form.id;
            listOid(this.OIDqueryParams).then((response) => {
                this.OIDtotal = response.total;
                this.oidList = response.rows;
                this.OIDOpen = true;
                this.OIDTitle = 'snmp协议OID信息';
                this.OIDloading = false;
            });
        },
        // 取消按钮
        OIDcancel() {
            this.OIDopen = false;
            this.oidadd = false;
            this.OIDreset();
        },
        // 表单重置
        OIDreset() {
            this.OIDform = {
                id: null,
                snmpId: null,
                ipAddress: null,
                oid: null,
                oidName: null,
                createTime: null,
                updateTime: null,
            };
            this.resetForm('OIDform');
        },
        // 多选框选中数据
        OIDhandleSelectionChange(selection) {
            this.OIDids = selection.map((item) => item.id);
            this.OIDsingle = selection.length !== 1;
            this.OIDmultiple = !selection.length;
        },
        /** 新增按钮操作 */
        OIDhandleAdd() {
            this.OIDreset();
            this.oidadd = false;
            this.OIDopen = true;
            this.OIDtitle = '添加snmp协议OID';
        },
        /** 修改按钮操作 */
        OIDhandleUpdate(row) {
            this.OIDreset();
            const id = row.id || this.OIDids;
            getOid(id).then((response) => {
                this.OIDform = response.data;
                this.oidadd = true;
                this.OIDopen = true;
                this.OIDtitle = '修改snmp协议OID';
            });
        },
        /** 提交按钮 */
        OIDsubmitForm() {
            this.$refs['OIDform'].validate((valid) => {
                if (valid) {
                    if (this.OIDform.id != null) {
                        updateOid(this.OIDform).then((response) => {
                            this.$modal.msgSuccess('修改成功');
                            this.OIDopen = false;
                            this.OIDgetList();
                        });
                    } else {
                        this.OIDform.snmpId = this.form.id;
                        this.OIDform.ipAddress = this.form.ipAddress;
                        this.OIDform.community = this.form.community;
                        addOid(this.OIDform).then((response) => {
                            this.$modal.msgSuccess('新增成功');
                            this.OIDopen = false;
                            this.OIDgetList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        OIDhandleDelete(row) {
            const ids = row.id || this.OIDids;
            this.$modal
                .confirm('是否确认删除snmp协议OID编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delOid(ids);
                })
                .then(() => {
                    this.OIDgetList();
                    this.$modal.msgSuccess('删除成功');
                })
                .catch(() => { });
        },

        OID(row) {
            this.OIDreset();
            const id = row.id || this.ids;
            this.OIDloading = true;

            //获取主数据
            getSnmp(id).then((response) => {
                this.form = response.data;
                this.snmpOidList = response.data.snmpOidList;
                this.OIDOpen = true;
                this.OIDTitle = 'snmp协议OID信息';
                this.OIDloading = false;
            });
            this.OIDqueryParams.snmpId = id;
            //获取主数据的子表数据
            listOid(this.OIDqueryParams).then((response) => {
                this.OIDtotal = response.total;
                this.oidList = response.rows;
                this.OIDOpen = true;
                this.OIDTitle = 'snmp协议OID信息';
                this.OIDloading = false;
            });
        },
        /** 查询snmp协议主表列表 */
        getList() {
            this.loading = true;
            listSnmp(this.queryParams).then((response) => {
                this.snmpList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                ipAddress: null,
                equipmentName: null,
                community: null,
                createTime: null,
                updateTime: null,
            };
            this.snmpOidList = [];
            this.resetForm('form');
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = '添加snmp协议';
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getSnmp(id).then((response) => {
                this.form = response.data;
                this.snmpOidList = response.data.snmpOidList;
                this.open = true;
                this.title = '修改snmp协议';
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    this.form.snmpOidList = this.snmpOidList;
                    if (this.form.id != null) {
                        updateSnmp(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功');
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addSnmp(this.form).then((response) => {
                            this.$modal.msgSuccess('新增成功');
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm('是否确认删除snmp协议编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delSnmp(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess('删除成功');
                })
                .catch(() => { });
        },
        /** snmp协议OID子表序号 */
        rowSnmpOidIndex({ row, rowIndex }) {
            row.index = rowIndex + 1;
        },
        /** snmp协议OID子表添加按钮操作 */
        handleAddSnmpOid() {
            if (this.snmpOidList.length >= 10) {
                this.$message({
                    type: 'warning',
                    message: '最多可新增10条!',
                });
            } else {
                let obj = {};
                obj.oid = '';
                obj.disabled = false;
                this.snmpOidList.push(obj);
            }
        },
        /** snmp协议OID子表删除按钮操作 */
        handleDeleteSnmpOid() {
            if (this.checkedSnmpOid.length == 0) {
                this.$modal.msgError('请先选择要删除的snmp协议OID数据');
            } else {
                const snmpOidList = this.snmpOidList;
                const checkedSnmpOid = this.checkedSnmpOid;
                this.snmpOidList = snmpOidList.filter(function (item) {
                    return checkedSnmpOid.indexOf(item.index) == -1;
                });
            }
        },
        /** 复选框选中数据 */
        handleSnmpOidSelectionChange(selection) {
            this.checkedSnmpOid = selection.map((item) => item.index);
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'iot/snmp/export',
                {
                    ...this.queryParams,
                },
                `snmp_${new Date().getTime()}.xlsx`
            );
        },
    },
};
</script>
