<template>
  <div class="base-dlg">
    <BaseDialog :visible.sync="dialogShow" :width="`700px`"
      :title="type === 'add' ? $t('resourcesManagement.addResSpecificationAndPriceInfo') : $t('resourcesManagement.editResSpecificationAndPriceInfo')"
      @open="open" @opened="opened" @close="close" @closed="closed">
      <div class="dlg-content">
        <div class="form">
          <div class="form-row">
            <div class="title required">
              <span>{{ $t('resourcesManagement.resQueue') }}</span>
            </div>
            <div class="content">
              <el-select v-model="dataInfo.QueueId" :disabled="type === 'edit'">
                <el-option v-for="item in this.queueList" :key="item.k" :label="item.v" :value="item.k" />
              </el-select>
            </div>
          </div>
          <div class="form-row">
            <div class="title">
              <span>{{ $t('resourcesManagement.sourceSpecCode') }}</span>
            </div>
            <div class="content">
              <el-input v-model="dataInfo.SourceSpecId" :placeholder="$t('resourcesManagement.sourceSpecCodeTips')"
                :disabled="type === 'edit'">
              </el-input>
            </div>
          </div>
          <div class="form-row">
            <div class="title required">
              <span>{{ $t('resourcesManagement.accCardsNum') }}</span>
            </div>
            <div class="content">
              <el-input v-model="dataInfo.AccCardsNum" type="number" placeholder="" :disabled="type === 'edit'">
              </el-input>
            </div>
          </div>
          <div class="form-row">
            <div class="title required">
              <span>{{ $t('resourcesManagement.cpuNum') }}</span>
            </div>
            <div class="content">
              <el-input v-model="dataInfo.CpuCores" type="number" placeholder="" :disabled="type === 'edit'"></el-input>
            </div>
          </div>
          <div class="form-row">
            <div class="title required">
              <span>{{ $t('resourcesManagement.gpuMem') }}(GB)</span>
            </div>
            <div class="content">
              <el-input v-model="dataInfo.GPUMemGiB" type="number" placeholder="" :disabled="type === 'edit'">
              </el-input>
            </div>
          </div>
          <div class="form-row">
            <div class="title required">
              <span>{{ $t('resourcesManagement.mem') }}(GB)</span>
            </div>
            <div class="content">
              <el-input v-model="dataInfo.MemGiB" type="number" placeholder="" :disabled="type === 'edit'"></el-input>
            </div>
          </div>
          <div class="form-row">
            <div class="title required">
              <span>{{ $t('resourcesManagement.shareMem') }}(GB)</span>
            </div>
            <div class="content">
              <el-input v-model="dataInfo.ShareMemGiB" type="number" placeholder="" :disabled="type === 'edit'">
              </el-input>
            </div>
          </div>
          <div class="form-row">
            <div class="title required">
              <span>{{ $t('resourcesManagement.unitPrice') }}({{ $t('resourcesManagement.point_hr') }})</span>
            </div>
            <div class="content">
              <el-input v-model="dataInfo.UnitPrice" type="number" placeholder=""></el-input>
            </div>
          </div>
          <div class="form-row">
            <div class="title required">
              <span>{{ $t('status') }}</span>
            </div>
            <div class="content">
              <el-select v-model="dataInfo.Status" :disabled="type === 'edit'">
                <el-option v-for="item in this.statusList" :key="item.k" :label="item.v" :value="item.k" />
              </el-select>
            </div>
          </div>
          <div class="form-row" style="margin-top: 20px">
            <div class="title"></div>
            <div class="content">
              <el-button type="primary" class="btn confirm-btn" @click="confirm">{{ $t('confirm') }}</el-button>
              <el-button class="btn" @click="cancel">{{ $t('cancel') }}</el-button>
            </div>
          </div>
        </div>
      </div>
    </BaseDialog>
  </div>
</template>
<script>
import BaseDialog from '~/components/BaseDialog.vue';
import { getResQueueCode, addResSpecification, updateResSpecification } from '~/apis/modules/resources';
import { SPECIFICATION_STATUS, CLUSTERS, AI_CENTER } from '~/const';
import { getListValueWithKey } from '~/utils';

export default {
  name: "SpecificationDialog",
  props: {
    visible: { type: Boolean, default: false },
    title: { type: String, default: '' },
    type: { type: String, defalut: 'add' },
    data: { type: Object, default: () => ({}) },
  },
  components: {
    BaseDialog
  },
  data() {
    return {
      dialogShow: false,
      dataInfo: {},
      queueList: [],
      statusList: [...SPECIFICATION_STATUS],
      clusterList: [...CLUSTERS],
      aiCenterList: [...AI_CENTER],
    };
  },
  watch: {
    visible: function (val) {
      this.dialogShow = val;
    },
  },
  methods: {
    resetDataInfo() {
      this.dataInfo = {
        QueueId: '',
        AccCardsNum: '',
        CpuCores: '',
        MemGiB: '',
        ShareMemGiB: '',
        GPUMemGiB: '',
        UnitPrice: '',
        Status: '1',
      }
    },
    getQueueList() {
      getResQueueCode().then(res => {
        res = res.data;
        if (res.Code === 0) {
          const data = res.Data;
          const list = [];
          for (let i = 0, iLen = data.length; i < iLen; i++) {
            const item = data[i];
            list.push({
              k: item.ID,
              v: `${item.QueueCode}(${getListValueWithKey(this.clusterList, item.Cluster)} - ${getListValueWithKey(this.aiCenterList, item.AiCenterCode)})`,
            });
          }
          this.queueList.splice(0, Infinity, ...list);
        }
      }).catch(err => {
        console.log(err);
      });
    },
    open() {
      this.resetDataInfo();
      this.getQueueList();
      if (this.type === 'add') {
        //
      } else if (this.type === 'edit') {
        this.dataInfo = Object.assign(this.dataInfo, { ...this.data, Status: '2' });
      }
      this.$emit("open");
    },
    opened() {
      this.$emit("opened");
    },
    close() {
      this.$emit("close");
    },
    closed() {
      this.$emit("closed");
      this.$emit("update:visible", false);
    },
    confirm() {
      if (!this.dataInfo.QueueId || !this.dataInfo.AccCardsNum || !this.dataInfo.CpuCores || !this.dataInfo.MemGiB || !this.dataInfo.ShareMemGiB || !this.dataInfo.GPUMemGiB
        || !this.dataInfo.UnitPrice || !this.dataInfo.Status
      ) {
        this.$message({
          type: 'info',
          message: this.$t('pleaseCompleteTheInformationFirst')
        });
        return;
      }
      if (parseInt(this.dataInfo.AccCardsNum) != Number(this.dataInfo.AccCardsNum)) {
        this.$message({
          type: 'info',
          message: this.$t('pleaseEnterPositiveIntegerCardsTotalNum')
        });
        return;
      }
      const setApi = this.type === 'add' ? addResSpecification : updateResSpecification;
      setApi({
        ...this.dataInfo,
        action: this.type === 'edit' ? 'on-shelf' : undefined,
        AccCardsNum: Number(this.dataInfo.AccCardsNum),
        CpuCores: Number(this.dataInfo.CpuCores),
        MemGiB: Number(this.dataInfo.MemGiB),
        ShareMemGiB: Number(this.dataInfo.ShareMemGiB),
        GPUMemGiB: Number(this.dataInfo.GPUMemGiB),
        UnitPrice: Number(this.dataInfo.UnitPrice),
        Status: Number(this.dataInfo.Status),
      }).then(res => {
        res = res.data;
        if (res.Code === 0) {
          this.$message({
            type: 'success',
            message: this.$t('submittedSuccessfully')
          });
          this.$emit("confirm");
        } else {
          this.$message({
            type: 'error',
            message: this.$t('submittedFailed')
          });
        }
      }).catch(err => {
        console.log(err);
        this.$message({
          type: 'error',
          message: this.$t('submittedFailed')
        });
      })
    },
    cancel() {
      this.dialogShow = false;
      this.$emit("update:visible", false);
    }
  },
  mounted() {
    this.resetDataInfo();
  },
};
</script>
<style scoped lang="less">
.dlg-content {
  margin: 20px 0 25px 0;
  display: flex;
  justify-content: center;

  .form {
    width: 600px;

    .form-row {
      display: flex;
      min-height: 42px;
      margin-bottom: 4px;

      .title {
        width: 160px;
        display: flex;
        justify-content: flex-end;
        align-items: center;
        margin-right: 20px;
        color: rgb(136, 136, 136);
        font-size: 14px;

        &.required {
          span {
            position: relative;
          }

          span::after {
            position: absolute;
            right: -10px;
            top: -2px;
            vertical-align: top;
            content: '*';
            color: #db2828;
          }
        }
      }

      .content {
        width: 300px;
        display: flex;
        align-items: center;

        /deep/ .el-select {
          width: 100%;
        }
      }
    }
  }

  .btn {
    color: rgb(2, 0, 4);
    background-color: rgb(194, 199, 204);
    border-color: rgb(194, 199, 204);

    &.confirm-btn {
      color: #fff;
      background-color: rgb(56, 158, 13);
      border-color: rgb(56, 158, 13);
    }
  }
}
</style>
