<template>
  <div class="app-container pageTopForm">
    <el-form :model="queryParams" ref="queryRef" size="small" v-show="showSearch" label-width="auto">
      <el-col :span="8">
        <el-form-item label="ClientID" prop="clientId">
          <el-input
            v-model="queryParams.clientId"
            placeholder="请输入ClientID"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="更新结果" prop="lastUpdateResult">
          <el-select
            v-model="queryParams.lastUpdateResult"
            placeholder="请选择更新结果"
            clearable>
            <el-option
              v-for="dict in dict.type.agent_update_result"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"/>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item class="lastBtnSty">
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-col>
    </el-form>
    <TableList :columns="columns" :config="config" :modelIdent="this.$options.name" :queryParams="queryParams" :tableList="tableList" @fnClick="callback" @fnRenderList="getList" @value-change="handleValueChange">
      <template #tempMethod="{ row, column }">
        <dict-tag :options="dict.type.policy_method" :value="row.method"/>
      </template>
      <template #tempStatus="{ row, column }">
        <dict-tag :options="dict.type.rm_register_online_state" :value="row.status"/>
      </template>
      <template #tempResult="{ row, column }">
        <dict-tag :options="dict.type.agent_update_result" :value="row.lastUpdateResult"/>
      </template>
    </TableList>
  </div>
</template>

<script setup>
  import TableList from "@/components/table/index.vue"
  import {listAgentManage, delAgentManage} from "@/api/disRevenue/resource"
  export default {
    name: 'AgentUpdate',
    components: {TableList},
    dicts: ['rm_register_online_state','eps_bandwidth_type', 'agent_update_result', 'policy_method'],
    data() {
      return {
        loading: true,
        showSearch: true,
        tableList: [],
        queryParams: {
          total: 0,
          pageNum: 1,
          pageSize: 10,
          queryName: '',
          lastUpdateResult: ''
        },
        // 列显隐信息
        columns: {
          id: { label: `ID`,width: '50'},
          hardwareSn: { label: `硬件SN`, minWidth: '150'},
          // resourceName: { label: `资源名称`, minWidth: '250', visible: true },
          // internalIp: { label: `内网IP`,minWidth: '100', visible: true},
          clientId: { label: `ClientID`, minWidth: '320', visible: true },
          managePublicIp: { label: `管理网-公网IP`,minWidth: '120', visible: true},
          status: { label: `状态`, minWidth: '100', slotName: 'tempStatus'},
          agentVersion: { label: `AGENT版本`,minWidth: '150', visible: true },
          method: { label: `更新方式`,minWidth: '200', slotName: 'tempMethod'},
          scheduledUpdateTime: { label: `定时执行时间`,minWidth: '160'},
          // fileUrlType: { label: `文件地址格式`,minWidth: '200'},
          fileUrl: { label: `文件地址`,minWidth: '200'},
          lastUpdateResult: { label: `最后一次更新结果`,minWidth: '160', slotName: 'tempResult', visible: true},
          lastUpdateTime: { label: `最后一次更新时间`,minWidth: '160', visible: true},
        },
        config: {
          tableButton: {
            top: [
              {content: '配置更新策略', fnCode: 'add', type: 'primary', icon: 'el-icon-plus', hasPermi: 'resource:agentUpdate:add'},
            ],
            line: [
              {content: '详情', fnCode: 'details', type: 'text', icon: 'el-icon-view', hasPermi: 'resource:agentUpdate:details'},
              {content: '修改更新策略', fnCode: 'edit', type: 'text', icon: 'el-icon-edit', hasPermi: 'resource:agentUpdate:edit'},
              // {content: '手动更新', fnCode: 'update', type: 'text', icon: 'el-icon-refresh-right', hasPermi: 'disRevenue:resource:agentUpdate:update'},
              {}
            ]
          }
        }
      }
    },
    created() {
      this.getList();
    },
    activated() {
      this.$nextTick(() => {
        this.getList();
      });
    },
    methods: {
      /** 查询列表 */
      getList() {
        this.loading = true;
        listAgentManage(this.queryParams).then(response => {
          this.tableList = response.rows;
          this.queryParams.total = response.total;
          this.loading = false;
        })
      },
      // 处理子组件传递的新值
      handleValueChange(newValue) {
        // 父组件更新自身数据，实现同步
        this.showSearch = newValue;
        // console.log('父组件拿到新值:', newValue);
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },

      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryRef");
        this.handleQuery();
      },

      callback(result, rowData, selectChange) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'add':
              this.$router.push({
                path:'/resource/agentUpdate/view'});
              break;
            case 'edit':
              this.$router.push({
                path:'/resource/agentUpdate/view',
                query:{
                  id: rowData.id
                }
              });
              break;
            case 'details':
              this.$router.push({
                path:'/resource/agentUpdate/view',
                query:{
                  id: rowData.id,
                  readonly: true
                }
              });
              break;
            case 'delete':
              this.$modal.confirm('是否确认删除数据项？').then(function() {
                return delAgentManage(selectChange)
              }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功")
              }).catch(() => {});
              break;
            case 'export':
              let paramsList = Object.assign({}, this.queryParams,rowData);
              this.download("system/agentManagement/export", paramsList, `AGENT更新_${new Date().getTime()}.xlsx`, null, 'json');
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style scoped>
  ::v-deep .lastBtnSty .el-form-item__content{
    margin-left: 10px!important;
  }
</style>
