<template>
  <div>
    <div class="title"><span>资源池（队列）</span></div>
    <div class="tools-bar">
      <div>
        <el-select class="select" size="medium" v-model="selCluster" @change="selectChange">
          <el-option v-for="item in clusterList" :key="item.k" :label="item.v" :value="item.k" />
        </el-select>
        <el-select class="select" size="medium" v-model="selComputingCenter" @change="selectChange">
          <el-option v-for="item in computingCenterList" :key="item.k" :label="item.v" :value="item.k" />
        </el-select>
        <el-select class="select" size="medium" v-model="selComputingType" @change="selectChange">
          <el-option v-for="item in computingTypeList" :key="item.k" :label="item.v" :value="item.k" />
        </el-select>
        <el-select class="select" size="medium" v-model="selCardType" @change="selectChange">
          <el-option v-for="item in cardTypeList" :key="item.k" :label="item.v" :value="item.k" />
        </el-select>
      </div>
      <div>
        <el-button size="medium" icon="el-icon-refresh" @click="syncComputerNetwork">同步智算网络</el-button>
        <el-button type="primary" icon="el-icon-plus" size="medium" @click="showDialog('add')">新增资源池</el-button>
      </div>
    </div>
    <div class="table-container">
      <div>
        <el-table border :data="tableData" style="width: 100%" v-loading="loading" stripe>
          <el-table-column prop="id" label="ID" align="center" header-align="center" width="100"></el-table-column>
          <el-table-column prop="name" label="资源池（队列）名称" align="center" header-align="center"></el-table-column>
          <el-table-column prop="cluster" label="所属集群" align="center" header-align="center"></el-table-column>
          <el-table-column prop="computingCenterId" label="智算中心ID" align="center" header-align="center">
          </el-table-column>
          <el-table-column prop="computingCenter" label="智算中心" align="center" header-align="center"></el-table-column>
          <el-table-column prop="computingType" label="计算资源" align="center" header-align="center"></el-table-column>
          <el-table-column prop="cardType" label="卡类型" align="center" header-align="center"></el-table-column>
          <el-table-column prop="cardNum" label="卡数 " align="center" header-align="center">
            <template slot-scope="scope">
              {{ scope.row.cardNum }}
            </template>
          </el-table-column>
          <el-table-column prop="updateTime" label="最后更新时间" align="center" header-align="center"></el-table-column>
          <el-table-column prop="remark" label="备注" align="left" header-align="center">
            <template slot-scope="scope">
              {{ scope.row.remark }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" header-align="center" width="80">
            <template slot-scope="scope">
              <span class="op-btn" @click="showDialog('edit', scope.row)">修改</span>
            </template>
          </el-table-column>
          <template slot="empty">
            <span style="font-size: 12px">{{
                loading ? '加载中...' : '暂无数据'
            }}</span>
          </template>
        </el-table>
      </div>
      <div class="__r_p_pagination">
        <div style="margin-top: 2rem">
          <div class="center">
            <el-pagination background @current-change="currentChange" :current-page="pageInfo.curpage"
              :page-sizes="pageInfo.pageSizes" :page-size="pageInfo.pageSize"
              layout="total, sizes, prev, pager, next, jumper" :total="pageInfo.total">
            </el-pagination>
          </div>
        </div>
      </div>
    </div>
    <QueueDialog :visible.sync="queueDialogShow" :type="queueDialogType" :data="queueDialogData"
      @confirm="queueDialogConfirm"></QueueDialog>
  </div>
</template>

<script>
import QueueDialog from '../components/QueueDialog.vue';
import { getQueueList } from '~/apis/modules/resources';
import { CLUSTERS, AI_CENTER, COMPUTER_RESOURCES, ACC_CARD_TYPE } from '~/const';

export default {
  data() {
    return {
      selCluster: '',
      clusterList: [{ k: '', v: '全部集群' }, ...CLUSTERS],
      selComputingCenter: '',
      computingCenterList: [{ k: '', v: '全部智算中心' }, ...AI_CENTER],
      selComputingType: '',
      computingTypeList: [{ k: '', v: '全部计算资源' }, ...COMPUTER_RESOURCES],
      selCardType: '',
      cardTypeList: [{ k: '', v: '全部卡类型' }, ...ACC_CARD_TYPE],
      syncLoading: false,
      loading: false,
      tableData: [],
      pageInfo: {
        curpage: 1,
        pageSize: 10,
        pageSizes: [10],
        total: 0,
      },
      queueDialogShow: false,
      queueDialogType: 'add',
      queueDialogData: {},
    };
  },
  components: { QueueDialog },
  methods: {
    getTableData() {
      const params = {
        cluster: this.selCluster,
        computingCenter: this.selComputingCenter,
        computingType: this.selComputingType,
        cardType: this.selCardType,
        curpage: this.pageInfo.curpage,
        pagesize: this.pageInfo.pageSize,
      };
      console.log('params', params);
      this.loading = true;
      getQueueList(params).then(res => {
        this.loading = false;
        // const data = res.data.
        const data = new Array(20).fill(0).map((_, index) => {
          return {
            index: index,
            id: `id-${index}-${Math.random().toFixed(2)}`,
            name: `name-${index}-${Math.random().toFixed(2)}`,
            cluster: `cluster-${index}-${Math.random().toFixed(2)}`,
            computingCenterId: `computingCenterId-${index}-${Math.random().toFixed(2)}`,
            computingCenter: `computingCenter-${index}-${Math.random().toFixed(2)}`,
            computingType: `computingType-${index}-${Math.random().toFixed(2)}`,
            cardType: `cardType-${index}-${Math.random().toFixed(2)}`,
            cardNum: `cardNum-${index}-${Math.random().toFixed(2)}`,
            updateTime: `updateTime-${index}-${Math.random().toFixed(2)}`,
            remark: `remark-${index}-${Math.random().toFixed(2)}`,
          };
        });
        this.tableData = data;
        this.pageInfo.total = 99;
      }).catch(err => {
        console.log(err);
        this.loading = false;
      });
    },
    syncComputerNetwork() {
      // 
    },
    selectChange() {
      this.pageInfo.curpage = 1;
      this.getTableData();
    },
    currentChange(val) {
      this.pageInfo.curpage = val;
      this.getTableData();
    },
    showDialog(type, data) {
      this.queueDialogType = type;
      this.queueDialogData = data ? { ...data } : {};
      this.queueDialogShow = true;
    },
    queueDialogConfirm() {
      this.queueDialogShow = false;
      this.getTableData();
    }
  },
  mounted() {
    this.getTableData();
  },
  beforeDestroy() {
  },
};
</script>

<style scoped lang="less">
.title {
  height: 30px;
  display: flex;
  align-items: center;
  margin-bottom: 5px;

  span {
    font-weight: 700;
    font-size: 16px;
    color: rgb(16, 16, 16);
  }
}

.tools-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;

  .select {
    margin-right: 10px;

    /deep/ .el-input__inner {
      border-radius: 0;
    }
  }
}

.table-container {
  margin-bottom: 16px;

  /deep/ .el-table__header {
    th {
      background: rgb(245, 245, 246);
      font-size: 12px;
      color: rgb(36, 36, 36);
    }
  }

  /deep/ .el-table__body {
    td {
      font-size: 12px;
    }
  }

  .op-btn {
    cursor: pointer;
    font-size: 12px;
    color: rgb(25, 103, 252);
    margin: 0 5px;
  }
}

.center {
  display: flex;
  justify-content: center;
}
</style>
