<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="组织机构" prop="orgName">
            <el-input v-model="queryParams.orgName" :maxlength="50" clearable placeholder="请输入组织机构"/>
          </el-form-item>

          <el-form-item label="状态" prop="status">
            <el-select style="width: 100%;" clearable v-model="queryParams.status" placeholder="请选择状态"
            >
              <el-option v-for="dict in dict.type.deploy_upgrade_status" :key="dict.value" :label="dict.label"
                         :value="dict.value"/>
            </el-select>
          </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-upload2" size="mini" type="primary" @click="confirmExport"
            >导出
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </div>

      <el-table
        v-loading="loading"
        :data="upgradeHostList"
        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="batchCode" :show-overflow-tooltip="true" scoped-slot="batchCode"/>
        <el-table-column align="center" label="所属机构" prop="orgName" :show-overflow-tooltip="true" scoped-slot="orgName" width="320"/>
        <el-table-column align="center" label="版本号" prop="zipVersion"/>
        <el-table-column align="center" label="状态" prop="status">
          <template v-slot:default="scope">
            <span v-if="scope.row.status === 0" style="color: #000000">进行中</span>
            <span v-else-if="scope.row.status === 1" style="color: #00B83F">成功</span>
            <span v-else-if="scope.row.status === 2" style="color: red">失败</span>
            <span v-else-if="scope.row.status === 3" style="color: #f0ad4e">重试中</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="主机名称" prop="hostName"/>
        <el-table-column align="center" label="主机IP" prop="hostIp" :show-overflow-tooltip="true" scoped-slot="hostIp"/>
        <el-table-column align="center" label="创建时间" prop="createTime" :show-overflow-tooltip="true" scoped-slot="createTime"/>
        <el-table-column align="center" label="结束时间" prop="finishTime" :show-overflow-tooltip="true" scoped-slot="finishTime"/>
        <el-table-column
          class-name="small-padding fixed-width"
          label="操作"
          align="center"
          width="120"
        >
          <template slot-scope="{ row }">
            <el-button
              class="el-icon-view"
              size="mini"
              type="text"
              @click="showUpgradeApps(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"/>
    </div>

    <DialogCom
      @colse="onHide"
      :title="'查看升级应用详情'"
      :visible.sync="open"
      width="1200px"
    >
      <div class="page-body">
        <el-table
          v-loading="loading"
          :data="upgradeAppList"
          border
          height="650" size="small"
        >
          <el-table-column align="center" label="序号" width="100">
            <template v-slot:default="scope">
              <span v-text="getPageIndex(scope.$index)"> </span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="批次号" :show-overflow-tooltip="true" scoped-slot="batchNumber" prop="batchNumber"/>
          <el-table-column align="center" label="应用标识" :show-overflow-tooltip="true" scoped-slot="appId" prop="appId"/>
          <el-table-column align="center" label="当前版本号" prop="nowVersion"/>
          <el-table-column align="center" label="目标版本号" prop="targeVersion"/>
          <el-table-column align="center" label="进度描述" prop="deployDescription"/>
          <el-table-column align="center" label="状态" prop="taskStatus">
            <template v-slot:default="scope">
              <span v-if="scope.row.taskStatus === 0" style="color: #000000">进行中</span>
              <span v-else-if="scope.row.taskStatus === 1" style="color: #00B83F">成功</span>
              <span v-else-if="scope.row.taskStatus === 2" style="color: red">失败</span>
              <span v-else-if="scope.row.taskStatus === 3" style="color: #f0ad4e">重试中</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </DialogCom>

  </div>
</template>

<script>
import {
  upgradeHostList, upgradeAppList
} from "@/api/deploy/upgradeStatus";
import DataRangePicker from "@/components/dateTime/daterange.picker.vue";
import dayjs from "dayjs";
import {deleteBeringPackage} from "@/api/deploy/bering";

export default {
  name: "UpgradeStatus",
  computed: {
  },
  components: { DataRangePicker, dayjs},
  dicts: ['deploy_upgrade_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个停用
      single: true,
      // 非多个停用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表数据
      upgradeHostList: [],
      upgradeAppList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orgName: null,
        status: null,
      },
    };
  },
  created() {
    // agentVersionSelect().then((d) => {
    //   this.agentVersions = d.data;
    // });
    //this.getList();
  },
  mounted() {
    let id = this.$route.params.id;
    if (!id) {
      this.$tab.closePageAndPushPrev();
      return;
    }
    this.queryParams.id = id;
    this.getList();
  },
  methods: {
    getPageIndex($index) {
      //表格序号
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + $index + 1
    },
    /** 查询升级包列表 */
    getList() {
      upgradeHostList(this.queryParams).then(response => {
          this.upgradeHostList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      ).catch((err) => {
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    showUpgradeApps(row){
      upgradeAppList({hostId:row.hostId,batchNumber:row.batchCode}).then((d) => {
        this.upgradeAppList = d.data;
      });
      this.open = true;    },
    // 事件
    onHide() {
      this.open = false;
    },
    confirmExport(){
      this.$modal
        .confirm("导出符合当前查询条件的信息记录，确定执行？")
        .then(function () {
        })
        .then(() => {
          this.handleExportUpgradeInfo();
        });
    },
    handleExportUpgradeInfo() {
      this.download(
        "/api/deploy/appUpgrade/detailExport",
        {
          ...this.queryParams,
        },
        `升级主机详情_${new Date().getTime()}.xls`
      );
    },
  }
};
</script>

