<template>
  <div class="app-container pageTopForm">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch" size="small" label-width="auto" @submit.native.prevent>
      <el-col :span="6">
        <el-form-item label="搜索" title="搜索" prop="queryName">
          <el-input
            v-model="queryParams.queryName"
            placeholder="请输入策略名称/MTRClientID/ClientID"
            clearable
            @keyup.enter.native="handleQuery"/>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item class="lastBtnSty">
          <el-button type="primary" size="mini" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-col>
    </el-form>

    <!-- 表格数据 -->
    <TableList :columns="columns" :config="config" :modelIdent="this.$options.name" :queryParams="queryParams" :tableList="roleList" @fnClick="callback" @fnRenderList="getList" @value-change="handleValueChange">
      <!--   是否探测   -->
      <template #tempProbe="{ row, column }">
        <dict-tag :options="dict.type.rm_register_snmp_detect" :value="row.probeFlag"/>
      </template>
      <!--   探测频率   -->
      <template #tempProbeFre="{ row, column }">
        <div v-if="row && row.probeFrequency === '300'">固定每5分钟</div>
      </template>
    </TableList>
  </div>
</template>

<script setup name="Register">
  import Form from '@/components/form/index.vue';
  import {listMtrPolicy, delMtrPolicy} from '@/api/disRevenue/resource';
  import TableList from '@/components/table/index.vue';
  export default {
    name: 'mtrProbe',
    components: {TableList,Form},
    dicts: ['rm_register_snmp_detect'],
    data() {
      return {
        roleList: [],
        showSearch: true,
        ids: [],
        single: true,
        // 列显隐信息
        columns: {
          id: { label: `ID`,width: '50'},
          policyName: { label: `策略名称`, minWidth: '120', visible: true},
          priority: { label: `优先级`,minWidth: '60', visible: true },
          mtrClientId: { label: `MTRClientID`, minWidth: '320', visible: true},
          serverGroup:{ label: `服务器集合`,minWidth: '320', visible: true},
          probeFlag: { label: `是否探测`, minWidth: '80', visible: true, slotName: 'tempProbe'},
          startTime: { label: `开始时间`, minWidth: '160'},
          endTime: { label: `结束时间`, minWidth: '160'},
          probeFrequency: { label: `探测频率`,minWidth: '100', slotName: 'tempProbeFre'},
          serveripGroup: { label: `探测目标IP集合`,minWidth: '200', customTooltip: true},
          updateTime:{ label: `修改时间`,minWidth: '160'},
          createTime: { label: `创建时间`,minWidth: '160'},
        },
        config: {
          tableButton: {
            top: [
              {content: '添加', fnCode: 'add', type: 'primary', icon: 'el-icon-plus', hasPermi: 'resource:mtrProbe:add'},
              {content: '删除', fnCode: 'delete', type: 'danger', icon: 'el-icon-delete', hasPermi: 'resource:mtrProbe:delete'},
              {content: '导出', fnCode: 'export', type: 'warning', icon: 'el-icon-download', hasPermi: 'resource:mtrProbe:export'},
            ],
            line: [
              {content: '修改', fnCode: 'edit', type: 'primary', icon: 'el-icon-edit', hasPermi: 'resource:mtrProbe:edit'},
              {content: '详情', fnCode: 'details', type: 'primary', icon: 'el-icon-view', hasPermi: 'resource:mtrProbe:details'},
              {content: '删除', fnCode: 'delete', type: 'danger', icon: 'el-icon-delete', hasPermi: 'resource:mtrProbe:delete'},
            ]
          }
        },
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          total: 0
        },
      }
    },
    created() {
      this.getList();
    },
    activated() {
      this.$nextTick(() => {
        this.getList();
      });
    },
    methods: {
      // 处理子组件传递的新值
      handleValueChange(newValue) {
        // 父组件更新自身数据，实现同步
        this.showSearch = newValue;
        // console.log('父组件拿到新值:', newValue);
      },
      /** 查询角色列表 */
      getList() {
        let params = JSON.parse(JSON.stringify(this.queryParams));
        if (params && !params.queryName) {
          params.queryName = null;
        }
        listMtrPolicy(this.addDateRange(params)).then(response => {
          this.roleList = response.rows;
          this.queryParams.total = response.total;
        })
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList()
      },

      /** 重置按钮操作 */
      resetQuery() {
        this.$refs['queryRef'].resetFields();
        this.queryParams = {pageNum: 1, pageSize: 10,total: 0};
        this.handleQuery();
      },

      /** 多选框选中数据 */
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.roleId);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      /** 复制代码成功 */
      clipboardSuccess() {
        this.$modal.msgSuccess("复制成功")
      },
      fnDialogResult(res){
        this.viewOpen = false;
        if (res && res.open) {
          this.getList();
        }
      },
      callback(result, rowData, selectChange, selectList) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'add':
              this.$router.push({
                path:'/resource/mtrProbe/view'
              });
              break;
            case 'edit':
              this.$router.push({
                path:'/resource/mtrProbe/view',
                query: {id: rowData.id}
              });
              break;
            case 'details':
              this.$router.push({
                path:'/resource/mtrProbe/view',
                query: {id: rowData.id, readonly: true}
              });
              break;
            case 'delete':
              let ids = null;
              if (rowData && rowData.id) {
                ids = rowData.id;
              } else {
                if (selectList && selectList.length <= 0) {
                  this.$modal.msgWarning("请选择数据！");
                  return;
                }
                ids = selectChange;
              }
              if (!ids) return;
              let content = '<p style="font-size: 1rem;font-weight: 600;">删除MTR探测策略</p>' +
                '<p style="height: 0px;margin-bottom: 50px;">删除后数据将不能恢复，请确认是否继续！！！</p>';
              this.$modal.confirm(content).then(function() {
                return delMtrPolicy(ids)
              }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功")
              }).catch(() => {});
              break;
            case 'export':
              let paramsList = Object.assign({}, this.queryParams,rowData);
              this.download("mtragent/mtrPolicyConfig/export", paramsList, `MTR探测策略_${new Date().getTime()}.xlsx`, null, 'json');
              break;
            default:

          }
        }
      },
    }
  }
</script>

<style scoped>
  ::v-deep .lastBtnSty .el-form-item__content{
    margin-left: 10px!important;
  }
  ::v-deep .el-dialog__body {
    padding: 0 0 10px!important;
  }
</style>
