<template>
  <app-dialog v-model="visible" :loading="loading" :title="title" append-to-body class="detail-dialog" width="660px" @cancel="onCancel" @confirm="onConfirm">
    <el-form ref="form" :model="form" :rules="rules" class="demo-form ml-20 mr-20" label-width="120px">
      <el-row :gutter="20">
        <app-filter-item :span="24" label="DS任务名称" prop="routineName">
          <el-input v-model="form.routineName" placeholder="请输入DS任务名称" clearable />
        </app-filter-item>

        <app-filter-item :span="24" label="发布版本" prop="versionId">
          <el-select v-model="form.versionId" clearable placeholder="请选择">
            <el-option v-for="opt in versions" :key="opt.id" :label="opt.versionCode" :value="opt.id">
              <span style="float: left">{{ opt.versionCode }}</span>
              <span style="float: right; color: #8492a6; font-size: 12px">{{ opt.createTime }}</span>
            </el-option>
          </el-select>
        </app-filter-item>
      </el-row>
    </el-form>
  </app-dialog>
</template>

<script>
import dialogMixin from '@/mixins/dialog.js';
import API from '@/api/business/arithmeticTask.js';
export default {
  name: 'publishDialog',
  mixins: [dialogMixin],
  data() {
    return {
      form: {
        id: '',
        routineName: '',
        versionId: '',
        version: null,
      },
      rules: {
        routineName: [{ required: true, message: 'DS任务不能为空' }],
        versionId: [{ required: true, message: '发布版本不能为空' }],
      },

      versions: [],
    };
  },
  methods: {
    initData(data) {
      this.taskData = data;
      this.form = {
        id: data.id,
        routineName: '',
        versionId: '',
      };
      this.getVersions();
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    getVersions() {
      const { id } = this.taskData;
      const query = {
        pageBean: {
          pageSize: -1,
        },
        querys: [
          {
            group: 'main',
            operation: 'EQUAL',
            property: 'taskId',
            relation: 'AND',
            value: id,
          },
        ],
      };
      API.getInferenceVersion(query)
        .then((res) => {
          this.versions = res.rows || [];
        })
        .catch((error) => {
          this.$message.error(error || '获取版本列表失败！');
        });
    },
    onCancel() {
      this.visible = false;
    },
    onConfirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          try {
            this.loading = true;
            const { id, routineName, versionId } = this.form;
            const data = new FormData();
            data.append('id', id);
            data.append('routineName', routineName);
            data.append('versionId', versionId);
            API.publishInferenceTask(data)
              .then((res) => {
                if (res.state) {
                  this.$message.success('发布算法任务成功！');
                  this.onCancel();
                  this.$emit('confirm');
                } else {
                  this.$message.error(res.message || '发布算法任务失败！');
                }
              })
              .catch((error) => {
                this.$message.error(error || '发布算法任务失败！');
              })
              .finally(() => {
                this.loading = false;
              });
          } catch (error) {
            this.$message.error(error || '发布算法任务失败！');
            this.loading = false;
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep {
  .app-dialog-content {
    height: 40vh !important;
  }
}
</style>
