<template>

  <Dialog
    v-model="visible"
    :diaTitle="pageTitle"
    :width="'30%'"
    :height="'50%'"
    :zIndex="100"
    @close="handleCloseFun"
    show-footer
  >

    <el-form v-loading="createLoading" label-width="120" ref="formRef" :model="runInfoFormData" @submit.prevent="">
      <el-form-item label="拼接工况信息：" v-if="operateType != 'editRun'">
        <el-radio-group v-model="runInfoFormData.isJointProjectInfo" @change="changeJoinFun">
          <el-radio :value="true">是</el-radio>
          <el-radio :value="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="算例名称：" prop="runNames">
        <el-input ref="runNameRef" placeholder="例：V1" v-model="runInfoFormData.runNames" :disabled="operateType === 'editRun'" />
      </el-form-item>
      <el-form-item
        label="流程模板："
        prop="templateName"
        :rules="[
          {
            required: true,
            pattern: /\S/,
            message: '请选择模板',
            trigger: 'check',
          },
        ]"
      >

        <el-select v-model="runInfoFormData.templateName" filterable @change="templateChangeFun">
          <el-option v-for="item in templateNameList" :key="item.uuid" :value="item.uuid" :label="item.templateName"></el-option>
        </el-select>

      </el-form-item>
      <el-form-item label="父级算例：" v-if="operateType === 'createChildRun'">
        <el-select v-model="runInfoFormData.parentRunVision" filterable >
          <el-option v-for="item in runAllVersion" :key="item.id" :value="item.id" :label="item.label"></el-option>
        </el-select>

      </el-form-item>
      <el-form-item label="算例说明：">
        <el-input rows="4" :autosize="{ minRows: 2, maxRows: 6 }" type="textarea" v-model="runInfoFormData.descript" />
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCloseFun">取消</el-button>
        <el-button type="primary" @click="createSuccessFun">确认</el-button>
      </span>
    </template>
  </Dialog>

</template>

<script setup lang="ts">
import { ref, defineProps, defineEmits, watch, reactive, onMounted } from 'vue';
import Dialog from '@/components/common/dialog/index.vue';
import { NODE_TYPE } from '@/utils/enum/node';
import { ElMessage } from 'element-plus';
import { queryFlowTemplateApi } from '@/api/capability/flow';
import { queryTaskRunApi } from '@/api/project/run';

const props = defineProps({
  divInfo: {
    type: Object,
    default: () => {

    },
  },
  operateFlag: {
    type: String,
    default: 'createRun',
  },
});

const visible = ref(true);
const createLoading = ref(false);

const runInfoFormData = reactive<any>({
  isJointProjectInfo: true,
  runNames: '',
  templateName: '',
  parentRunVision: '',
  descript: '',
});

const templateNameList = ref<any>([

]);

const templateChangeFun = () => {

};

const runAllVersion = ref<any>([

]);

const emits = defineEmits(['close', 'comfirm', 'update']);

const pageTitle = ref('创建算例');
const operateType = ref('');
const handleCloseFun = () => {
  emits('close');
};

// 确认事件
const createSuccessFun = () => {
  if (!runInfoFormData.templateName) {
    // runInfoFormData.templateName = 1;

    ElMessage.warning('未选择流程模板，无法新建算例！');
    return;
  }

  if ( operateType.value != 'editRun') {
    emits('comfirm', {
      info: runInfoFormData,
      flag: operateType.value,
    });
  } else {
    emits('update', {
      info: runInfoFormData,
      flag: operateType.value,
    });
  }

};

// 拼接改变
const changeJoinFun = () => {
  if (runInfoFormData.isJointProjectInfo) {
    runInfoFormData.runNames = props.divInfo.nodeName || props.divInfo.runName + '_V1' ;
  } else {
    runInfoFormData.runNames = '算例_' + 'V1';
  }
};

const getRunDetailInfoFun = async (id: any, flag:any) => {

  const param:any = {

  };
  if (flag === 'createRun' || flag === 'copyRun') {
    param.taskId = id;
  } else {
    param.runId = id;

  }

  const res: any = await queryTaskRunApi({
    taskId: id,
  });
  if (res && res.code === 200) {
    const length = res.data.length || 1;
    return length;
  }
};

watch(() => props.operateFlag, async (newVal) => {
  if (newVal) {
    let version:any = 1;
    operateType.value = newVal;
    if (newVal === 'createRun') {
      pageTitle.value = '创建算例';
    }

    if (newVal === 'createChildRun') {
      pageTitle.value = '创建子算例';

      runAllVersion.value = [
        {
          id: props.divInfo?.uuid,
          label: props.divInfo?.runName,
          nodeType: 'run',
        },
      ];
      runInfoFormData.parentRunVision = props.divInfo?.uuid;
    }

    if (newVal === 'copyRun') {
      pageTitle.value = '复制算例';
    }

    if (newVal === 'editRun') {
      pageTitle.value = '编辑算例信息';
      console.log(props.divInfo, 'props.divInfo');

      runInfoFormData.runNames = props.divInfo.nodeName || props.divInfo.runName;
      runInfoFormData.templateName = props.divInfo.flowTemplateName;

    } else {

      version = await getRunDetailInfoFun(props.divInfo.uuid, operateType.value);

      // 如果是新增算例
      if (props.divInfo && props.divInfo?.nodeType === NODE_TYPE.TASK) {
        if (runInfoFormData.isJointProjectInfo) {
          runInfoFormData.runNames = props.divInfo.nodeName + '_V' + version ;
        }
        runInfoFormData.templateName = props.divInfo.flowTemplateName;
      }

      // 如果是新增子算例或者复制算例
      if (props.divInfo && props.divInfo?.nodeType === NODE_TYPE.RUN) {
        if (runInfoFormData.isJointProjectInfo) {
          runInfoFormData.runNames = props.divInfo.runName + '_V' + version ;
        }
        runInfoFormData.templateName = props.divInfo.flowTemplateName;
      }
    }
  }

}, {
  immediate: true,
});

const geTemplateListFun = async () => {
  const res:any = await queryFlowTemplateApi({
    current: 1,
    size: 999,
    type: 1,
  });

  if (res && res.code === 200) {
    templateNameList.value = res.data.data;
  }
};

// 获取任务下所有的算例信息

onMounted(async () => {

  await geTemplateListFun();
  console.log(props.divInfo);

});

</script>

<style lang="scss" scoped></style>
