<template>
  <div class="app-container">
    <div class="main-right-box">
      <!--    搜索条件    -->
      <div class="main-search-box">
        <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" size="small">


          <el-form-item label="组织机构">
            <org-tree
              v-model="queryParams.hostOrg"
              @defaultOrg="getDefaultOrg"
              @checkChange="checkChange"
              @click="clickTreeNode"
              ref="orgTree"
            ></org-tree>
          </el-form-item>

          <el-form-item label="白令海版本" prop="agentVersion">
            <el-select
              style="width: 100%"
              v-model="queryParams.agentVersion"
              placeholder="请选择白令海版本"
              @change="onAgentVersionChange"
              clearable
            >
              <el-option
                v-for="item in beringVersions"
                :key="item.id"
                :label="item.text"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="白令海目标版本" prop="uploadVersion">
            <el-select
              style="width: 100%"
              v-model="queryParams.uploadVersion"
              placeholder="请选择白令海版本"
              @change="onUploadVersionChange"
              clearable
            >
              <el-option
                v-for="item in beringVersions"
                :key="item.id"
                :label="item.text"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="白令海状态" prop="hostStatus">
            <el-select style="width: 100%;" clearable v-model="queryParams.hostStatus" placeholder="请选择白令海状态"
            >
              <el-option v-for="dict in dict.type.deploy_bering_status" :key="dict.value" :label="dict.label"
                         :value="dict.value"/>
            </el-select>
          </el-form-item>

          <el-form-item label="主机IP" prop="hostIp">
            <el-input v-model="queryParams.hostIp" :maxlength="50" clearable placeholder="请输入主机IP"/>
          </el-form-item>

        </el-form>


        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery"
            >搜索
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button icon="el-icon-refresh" size="mini" type="primary" @click="resetQuery"
            >重置
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button icon="el-icon-folder" size="mini" type="primary" @click="onManageContent"
            >白令海版本管理
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button icon="el-icon-position" size="mini" type="primary" @click="resetQuery"
            >批量升级
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </div>

      <el-table
        v-loading="loading"
        :data="packageInfoList"
        border
        height="646" size="small" >
        <el-table-column align="center" label="序号" width="60">
          <template v-slot:default="scope">
            <span v-text="getPageIndex(scope.$index)"> </span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="所属机构" prop="orgName"/>
        <el-table-column align="center" label="主机IP" prop="hostIp" :show-overflow-tooltip="true" scoped-slot="hostIp" width="320"/>
        <el-table-column align="center" label="白令海状态" prop="hostStatus">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.deploy_bering_status" :value="scope.row.hostStatus"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="白令海当前版本号" prop="agentVersion"/>
        <el-table-column align="center" label="白令海目标版本号" prop="uploadVersion"/>
        <el-table-column
          class-name="small-padding fixed-width"
          label="操作"
          align="center"
          width="120"
        >
          <template slot-scope="{ row }">
            <el-button v-if="row.upgradeFlag === 1 && row.hostStatus === 1"
              class="el-icon-position"
              size="mini"
              type="primary"
              @click="onManageContent(row)"
            >升级
            </el-button
            >
            <el-button
              v-else
             class="el-icon-position"
             size="mini"
             type="primary"
             disabled
            >升级
            </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"/>

    </div>
  </div>
</template>

<script>
import {
  upgradeBeringList,beringVersionList
} from "@/api/deploy/bering";
import OrgTree from "@/components/orgTree/orgQuerySelector.vue";

export default {
  name: "BeringStatus",
  computed: {
  },
  components: {OrgTree},
  dicts: ['deploy_bering_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个停用
      single: true,
      // 非多个停用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表数据
      packageInfoList: [],
      // 表数据
      packageServerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hostOrg: null,
        agentVersion: null,
        uploadVersion: null,
        hostIp: null,
        hostStatus:null
      },
      beringVersions:[],
    };
  },
  created() {
    beringVersionList().then((d) => {
      this.beringVersions = d.data;
    });
    this.getList();
  },
  methods: {
    getDefaultOrg(node) {
      this.queryParams.hostOrg = node.id;
      this.getList();
    },
    //单选框状态改变
    checkChange(state) {
      this.queryParams.checkSub = state;
      this.getList();
    },
    // 节点单击事件
    clickTreeNode(data) {
      if(data==null){
        return;
      }
      this.queryParams.hostOrg = data.id;
      this.getList();
    },
    getPageIndex($index) {
      //表格序号
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + $index + 1
    },
    /** 查询升级包列表 */
    getList() {
      this.loading = true;
      console.log("123456")
      this.queryParams.hostOrg = null;
      upgradeBeringList(this.queryParams).then(response => {
          this.packageInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      ).catch((err) => {
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    onManageContent(){
      //路由名称：upgradeHostManager
      //this.$router.push({ name:"upgradeHostManager",params:{id:id,hostId:row.hostId}});
      let path = `/deploy/beringpackage/content`;
      this.$router.push(path);
    },
    onAgentVersionChange(val){
      let data = this.beringVersions.find((a) => a.id === val);
      if (data) {
        this.queryParams.agentVersion = data.text;
      }
    },
    onUploadVersionChange(val){
      let data = this.beringVersions.find((a) => a.id === val);
      if (data) {
        this.queryParams.uploadVersion = data.text;
      }
    },
  }
};
</script>
<style lang="scss" scoped>
</style>

