<!-- 模型部署 -->
<template>
  <div style="position: relative">
    <div class="topBox">
      <div class="title">模型部署</div>
      <div class="instructions">模型部署在云端服务器，用户通过网页访问或者 API 接口调用等形式向云端服务器发出请求，云端收到请求后处理并返回结果。</div>
    </div>
    <div class="tableBox" :style="{ height: tableHeight }">
      <el-form ref="myDataSetPage" :model="myDataSetPage" label-width="75px" :size="defaultFormItemSize" label-position="right" @submit.native.prevent>
        <filter-box :item-width="350" @search="refresh()" @reset="onReset">
          <el-form-item label-width="0px">
            <el-button class="add" type="primary" icon="el-icon-plus" :size="defaultFormItemSize" @click="add()">部署模型</el-button>
          </el-form-item>
          <el-form-item label-width="0px"> </el-form-item>
          <el-form-item label="服务名称" prop="formFilter.serviceName" label-width="120px">
            <el-input class="filter-item" v-model="myDataSetPage.formFilter.serviceName" :clearable="true" placeholder="服务名称" />
          </el-form-item>
        </filter-box>
      </el-form>
      <vxe-table border show-header-overflow show-overflow :row-config="{ isHover: true }" :data="myDataSetPage.tableData.impl.dataList" min-height="96">
        <vxe-column field="serviceName" title="服务名称"></vxe-column>
        <vxe-column field="versionName" title="模型版本"></vxe-column>
        <vxe-column field="deployStatus" title="部署状态">
          <template slot-scope="scope">
            {{ DeploymentStatus.getValue(scope.row.deployStatus) }}
          </template>
        </vxe-column>
        <vxe-column field="createTime" title="创建时间"></vxe-column>
        <vxe-column field="updateTime" title="更新时间"></vxe-column>
        <vxe-column field="operation" title="操作">
          <template slot-scope="scope">
            <el-button type="text" :size="defaultFormItemSize" @click="particulars(scope.row)">详情</el-button>
            <el-button type="text" :size="defaultFormItemSize" @click="stop(scope.row)">卸载</el-button>
            <el-button type="text" :size="defaultFormItemSize" @click="deploy(scope.row,'start')" v-if="scope.row.deployStatus==-1||scope.row.deployStatus==2||scope.row.deployStatus==3">启动</el-button>
            <el-button type="text" :size="defaultFormItemSize" @click="deploy(scope.row,'reload')">重载</el-button>
            <el-button type="text" :size="defaultFormItemSize" @click="del(scope.row)">删除</el-button>
          </template>
        </vxe-column>
      </vxe-table>
      <el-row slot="pagination" type="flex" justify="end" style="margin-top: 16px">
        <el-pagination :total="myDataSetPage.tableData.impl.totalCount" :current-page="myDataSetPage.tableData.impl.currentPage" :page-size="myDataSetPage.tableData.impl.pageSize" :page-sizes="[10, 20, 50, 100]" layout="total, prev, pager, next, sizes" @current-change="myDataSetPage.tableData.impl.onCurrentPageChange" @size-change="myDataSetPage.tableData.impl.onPageSizeChange">
        </el-pagination>
      </el-row>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
/* eslint-disable-next-line */
import { TableWidget } from '@/utils/widget.js'
import { ModelDeployment } from '@/api/gptController.js'
import editOrAdd from './dialog/editOrAdd'
import particulars from './particulars/index'
export default {
  data() {
    return {
      myDataSetPage: {
        formFilter: {
          serviceName: '',
          isBaseModel: 0
        },

        tableData: {
          impl: new TableWidget(this.getwidgetData, true, true, false, undefined, false)
        }
      }
    }
  },

  components: {},

  computed: {
    ...mapGetters(['getMainContextHeight']),
    tableHeight() {
      return this.getMainContextHeight - 145 + 'px'
    }
  },

  methods: {
    getwidgetData(params) {
      if (params == null) params = {}
      params = {
        modelDeployDtoFilter: {
          ...this.myDataSetPage.formFilter
        },
        ...params,
        // orderParam: [
        //   {
        //     asc: true,
        //     dateAggregateBy: '',
        //     fieldName: ''
        //   }
        // ],
        // pageParam: {
        //   pageNum: 0,
        //   pageSize: 0
        // },
        promptTemplateDtoFilter: { ...this.myDataSetPage.formFilter }
      }

      return new Promise((resolve, reject) => {
        ModelDeployment.list(this, params)
          .then((res) => {
            resolve({
              dataList: res.data.dataList,
              totalCount: res.data.totalCount
            })
          })
          .catch((e) => {
            reject(e)
          })
      })
    },
    add() {
      this.$dialog
        .show(
          '创建服务',
          editOrAdd,
          {
            area: ['100%', '100%']
          },
          { isEdit: false }
        )
        .then((res) => {
          this.refresh()
        })
        .catch((e) => {})
    },
    edit(item) {
      this.$dialog
        .show(
          '修改服务',
          editOrAdd,
          {
            area: ['100%', '100%']
          },
          { isEdit: true, item: item }
        )
        .then((res) => {
          this.refresh()
        })
        .catch((e) => {})
    },
    del(item) {
      this.$confirm('是否确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = { deployId: item.deployId }
        ModelDeployment.delete(this, params)
          .then((res) => {
            this.$message.success('删除成功')
            this.refresh()
          })
          .catch((e) => {
            console.log(e)
          })
      })
    },
    particulars(item) {
      this.$dialog
        .show(
          '详情',
          particulars,
          {
            area: ['100%', '100%']
          },
          { tableItem: item }
        )
        .then((res) => {
          this.refresh()
        })
        .catch((e) => {})
    },
    deploy(item, type) {
      // 部署
      this.$confirm('是否确认部署', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = { modelDeployDto: { deployId: item.deployId }, type }
        ModelDeployment.deploy(this, params)
          .then((res) => {
            this.$message.success('部署成功')
            this.refresh()
          })
          .catch((e) => {
            console.log(e)
          })
      })
    },
    stop(item) {
      // 卸载
      this.$confirm('是否确认卸载', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = { modelDeployDto: { deployId: item.deployId } }
        ModelDeployment.stop(this, params)
          .then((res) => {
            this.$message.success('卸载成功')
            this.refresh()
          })
          .catch((e) => {
            console.log(e)
          })
      })
    },
    refresh(reloadData = false) {
      if (reloadData) {
        this.myDataSetPage.tableData.impl.refreshTable(true, 1)
      } else {
        this.myDataSetPage.tableData.impl.refreshTable()
      }
    },
    onReset() {
      this.$refs.myDataSetPage.resetFields()
      this.refresh(true)
    },
    formInit() {
      this.refresh()
    }
  },

  mounted() {
    this.formInit()
  }
}
</script>
<style lang="scss" scoped>
@import '@/assets/style/element-variables.scss';

.topBox {
  background-color: white;
  width: 100%;
  height: 95px;
  margin-bottom: 16px;

  .title {
    color: $--color-text-primary;
    font-size: 16px;
    font-weight: 500;
    margin-bottom: 8px;
    padding: 20px 20px 10px 20px;
  }
}

.instructions {
  padding: 0px 20px 20px 20px;

  .describe {
    color: $--color-text-secondary;
    margin-bottom: 16px;
  }

  .itemTitle {
    color: $--color-text-primary;
    font-size: 16px;
    font-weight: 500;
    margin-bottom: 8px;
  }

  .head-index {
    font-size: 24px;
    color: #e8e9eb;
    line-height: 32px;
  }

  .itemDescribe {
    width: 170px;
    color: $--color-text-secondary;
  }

  .img {
    height: 72px;
    width: 140px;
  }
}

.tableBox {
  background-color: white;
  width: 100%;
  padding: 20px;
  overflow: auto;

  .add {
    margin-bottom: 20px;
  }
}
</style>
