<template>
  <el-container>
    <el-header height="220">
      <h3>构建依赖镜像</h3>
    </el-header>
    <el-main>
      <el-table
        :data="imageList"
        border
        height="480"
        style="width: 100%"
      >
        <el-table-column
          prop="repoTag"
          label="镜像"
        />
        <el-table-column
          prop="stage"
          label="依赖阶段"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.stage === '编译阶段'" :type="'warning'">
              {{ scope.row.stage }}
            </el-tag>
            <el-tag v-else-if="scope.row.stage === '打包阶段'" :type="'success'">
              {{ scope.row.stage }}
            </el-tag>
            <el-tag v-else>
              {{ scope.row.stage }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="used"
          label="使用量"
        >
          <template slot-scope="scope">
            <el-tag
              style="margin: 5px"
              size="mini"
            >
              <span>{{ scope.row.list.length }}</span>
            </el-tag>
            <el-button
              style="margin: 5px"
              size="mini"
              type="success"
              @click="handleShowApps(scope.$index, scope.row)"
            >查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>

    <el-dialog
      title="依赖镜像的编译器或应用"
      append-to-body
      :visible.sync="showAppDialog"
      center
    >
      <template>
        <div v-for="(item, index) in appList" :key="index">
          <el-col :md="4">
            <el-tag size="mini" style="margin: 5px">{{ item }}</el-tag>
          </el-col>
        </div>
      </template>
    </el-dialog>
  </el-container>
</template>

<script>
import { getDockerfileImageList } from '@/api/devops'

export default {
  name: 'DockerImage',
  data() {
    return {
      showImageDialog: false,
      imageList: [],
      showAppDialog: false,
      appList: []
    }
  },
  created() {
    document.title = '构建依赖镜像'
    this.getData()
  },
  methods: {
    getData() {
      getDockerfileImageList().then(resp => {
        if (resp.code === 0) {
          this.imageList = resp.data
          this.showImageDialog = true
        } else {
          this.$message.error(resp.msg)
        }
      }).catch(error => {
        this.$message.error(error.message)
      })
    },
    handleShowAdd() {
      this.showAddDialog = true
    },
    handleShowApps(index, row) {
      this.appList = row.list
      this.showAppDialog = true
    }
  }
}
</script>

<style>
</style>
