<template>
  <el-container>
    <el-header height="220">
      <h3>应用运行列表</h3>
      <el-row style="margin-top: 10px">
        <el-select
          v-model="queryInfo.env"
          clearable
          placeholder="环境"
          style="margin-left: 5px"
          @change="onSelectChange"
        >
          <el-option
            v-for="(item, index) in envList"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="queryInfo.appType"
          clearable
          placeholder="类型"
          style="margin-left: 5px"
          @change="onSelectChange"
        >
          <el-option
            v-for="(item, index) in appTypeList"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-row>
    </el-header>
    <el-main>
      <el-table
        :data="dataList"
        border
        height="480"
        style="width: 100%"
      >
        <el-table-column
          prop="appName"
          label="应用"
        />
        <el-table-column
          prop="bindPorts"
          label="监听端口"
        />
        <el-table-column
          prop="packagePath"
          label="包路径"
        />
        <el-table-column
          prop="totalDeployed"
          label="部署数量/运行中/未运行"
        >
          <template slot-scope="scope">
            <el-tag disable-transitions style="margin-top: 5px; margin-left: 5px">
              {{ scope.row.totalDeployed }}
            </el-tag>
            <el-tag :type="'success'" disable-transitions style="margin-top: 5px; margin-left: 5px">
              {{ scope.row.totalRunning }}
            </el-tag>
            <el-tag :type="'danger'" disable-transitions style="margin-top: 5px; margin-left: 5px">
              {{ scope.row.totalStopped }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="详情"
          width="280"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleDetail(scope.$index, scope.row)"
            >查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :small="screenWidth <= 768"
        layout="prev, pager, next"
        :page-size="pageSize"
        :current-page="currentPage"
        :total="totalSize"
        @current-change="handleCurrentChange"
        @prev-click="handleCurrentChange"
        @next-click="handleCurrentChange"
      />
    </el-main>

    <el-dialog
      title="应用状态列表"
      append-to-body
      :visible.sync="showStatDialog"
      width="70%"
      center
    >
      <template>
        <el-table
          :data="appStatList"
          border
          height="480"
          style="width: 100%"
        >
          <el-table-column
            prop="machineIpv4"
            label="机器地址"
          />
          <el-table-column
            prop="packagePath"
            label="包路径"
          />
          <el-table-column
            prop="status"
            label="运行状态"
          />
          <el-table-column
            prop="startTime"
            label="启动时间"
          />
          <el-table-column
            prop="pid"
            label="PID"
          />
          <el-table-column
            prop="lastCheck"
            label="上次检查"
          />
          <el-table-column
            fixed="right"
            label="操作"
            width="280"
          >
            <template slot-scope="scope">
              <el-button
                style="margin-top: 5px; margin-left: 5px"
                size="mini"
                type="danger"
                @click="handleRestart(scope.$index, scope.row)"
              >重启</el-button>
              <el-button
                style="margin-top: 5px; margin-left: 5px"
                size="mini"
                type="danger"
                @click="handleStop(scope.$index, scope.row)"
              >停止</el-button>
              <el-button
                style="margin-top: 5px; margin-left: 5px"
                size="mini"
                type="success"
                @click="handleStart(scope.$index, scope.row)"
              >启动</el-button>
              <el-button
                style="margin-top: 5px; margin-left: 5px"
                size="mini"
                @click="handleGetStat(scope.$index, scope.row)"
              >当前状态</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-dialog>
  </el-container>
</template>

<script>
import {
  getAppStat,
  getAppStatDeatil,
  getAppStatList,
  getEnvList,
  restartAppStat,
  startAppStat, stopAppStat
} from '@/api/devops'

export default {
  name: 'AppStat',
  data() {
    return {
      envList: [],
      appTypeList: [],
      queryInfo: {
        env: 'test',
        appType: '',
        pn: 1
      },
      // 屏幕宽度, 为了控制分页条的大小
      screenWidth: document.body.clientWidth,
      currentPage: 1,
      pageSize: 10,
      totalSize: 0,
      dataList: [],
      // **********************************************************************
      videoProp: null,
      showVideoResourceDialog: false,
      showEditScopeDialog: false,
      showStatDialog: false,
      appStatList: [],
      form: {
        videoId: null,
        scope: 1
      },
      videoResources: [],
      publishVideoDiaglog: false
    }
  },
  created() {
    getEnvList().then(resp => {
      if (resp.code === 0) {
        this.queryInfo.env = resp.data.userEnv
        this.queryInfo.appType = resp.data.userAppType
        this.envList = resp.data.envList
        this.appTypeList = resp.data.appTypeList

        const env = this.$route.query.env
        if (env !== undefined && env !== null) {
          this.queryInfo.env = env
        }
        const appType = this.$route.query.appType
        if (appType !== undefined && appType !== null) {
          this.queryInfo.appType = appType
        }
        const pageNumber = this.$route.query.pn
        if (pageNumber !== undefined && pageNumber !== null) {
          this.currentPage = parseInt(pageNumber)
          this.queryInfo.pn = parseInt(pageNumber)
        }

        this.getData()
      } else {
        this.$message.error(resp.msg)
      }
    }).catch(error => {
      this.$message.error(error.message)
    })

    document.title = '运行状态'
  },
  methods: {
    handleCurrentChange(pageNumber) {
      this.queryInfo.pn = pageNumber
      this.$router.push({
        path: '/bg/app/stat',
        query: this.queryInfo
      })
      this.getData()
      // 回到顶部
      scrollTo(0, 0)
    },
    getData() {
      this.dataList = []
      getAppStatList(this.queryInfo).then(resp => {
        if (resp.code === 0) {
          const respData = resp.data
          this.dataList = respData.list
          this.totalSize = respData.totalSize
        } else {
          this.$message.error(resp.msg)
        }
      }).catch(error => {
        this.$message.error(error.message)
      })
    },
    handleDetail(index, row) {
      getAppStatDeatil(row.appId).then(resp => {
        if (resp.code === 0) {
          this.appStatList = resp.data
          this.showStatDialog = true
        } else {
          this.$message.error(resp.msg)
        }
      }).catch(error => {
        this.$message.error(error.message)
      })
    },
    handleRestart(index, row) {
      const formData = new FormData()
      formData.append('appId', row.appId)
      formData.append('machineId', row.machineId)
      restartAppStat(formData).then(resp => {
        this.$message.info(resp.msg)
      }).catch(error => {
        this.$message.error(error.message)
      })
    },
    handleStop(index, row) {
      const formData = new FormData()
      formData.append('appId', row.appId)
      formData.append('machineId', row.machineId)
      stopAppStat(formData).then(resp => {
        this.$message.info(resp.msg)
      }).catch(error => {
        this.$message.error(error.message)
      })
    },
    handleStart(index, row) {
      const formData = new FormData()
      formData.append('appId', row.appId)
      formData.append('machineId', row.machineId)
      startAppStat(formData).then(resp => {
        this.$message.info(resp.msg)
      }).catch(error => {
        this.$message.error(error.message)
      })
    },
    handleGetStat(index, row) {
      const queryInfo = {}
      queryInfo.appId = row.appId
      queryInfo.machineId = row.machineId
      getAppStat(queryInfo).then(resp => {
        this.$message.info(resp.msg)
      }).catch(error => {
        this.$message.error(error.message())
      })
    },
    onSelectChange() {
      this.currentPage = 1
      this.queryInfo.pn = 1
      this.$router.push({
        path: '/bg/app/stat',
        query: this.queryInfo
      })
      this.getData()
    }
  }
}
</script>

<style>
</style>
