<template>
  <div class="task-performance-page">
    <TableForm
      ref="tableFormRef"
      :tableName="tableName"
      :colNum="2"
      :rule-data="ruleData"
      showDisabled
    >
      <template #form-flowTemplate>
        <flowTemplateSelect v-model="selectedFlowTemplate" />
      </template>
      <template #form-standard>
        <knowledgeSelect v-model="standard" />
      </template>
    </TableForm>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch, nextTick } from 'vue';
import TableForm from '@/components/common/table/tableForm.vue';
import { getTagKeyMap, NODE_TYPE } from '@/utils/enum/node';
import { getMemberListIds } from '@/utils/task';
import flowTemplateSelect from '@/components/common/treeCaseTable/flowTemplateSelect.vue';
import knowledgeSelect from '@/components/common/treeCaseTable/knowledgeSelect.vue';
import { disposeTagKey } from '@/views/task/projectDetail/components/project';

const props = defineProps({
  taskInfo: {
    type: Object,
    default: () => {},
  },
  tableName: {
    type: String,
    default: 'SIMULATION_TASK_SEND_LIST',
  },
  disabled: {
    type: Boolean,
    default: false,
  },
});
const tableFormRef = ref();

const localDetail = ref<any>({});

const ruleData = ref<any>({
  highValue: [
    {
      message: '指标类型为数值时，目标值应为数字',
      trigger: 'change',
      validator: (val: unknown) => {
        if (val === null || val === undefined || val === '') {
          return true;
        }
        const formData = tableFormRef.value?.getFormDataFun();
        if (formData.nodeType === NODE_TYPE.PERFORMANCE) {
          if (formData.performanceType === '1') {
            const s = String(val ?? '').trim();
            return s !== '' && Number.isFinite(Number(s));
          } else if (formData.performanceType === '2') {
            return true;
          } else {
            return true;
          }
        }
        return true;
      },
    },
  ],
});

// 流程模板相关
const selectedFlowTemplate = ref<any>(null);
const standard = ref();
watch(
  () => props.taskInfo,
  (newVal) => {
    if (newVal) {
      localDetail.value = newVal;
      nextTick(() => {
        tableFormRef.value?.setFormDataFun({
          ...localDetail.value,
          expStatus: localDetail.value.expStatus ? '1' : '0',
          eMemberList: getMemberListIds(localDetail.value.eMemberList),
          pMemberList: getMemberListIds(localDetail.value.pMemberList),
        });
        standard.value = localDetail.value.standard;
        if (localDetail.value.flowTemplate) {
          selectedFlowTemplate.value = localDetail.value.flowTemplate;
        }
      });
    }
  },
  {
    immediate: true,
    deep: true,
  }
);

const getFormData = async () => {
  const valid = await tableFormRef.value?.validateFun();

  if (valid) {
    const formData = tableFormRef.value?.getFormDataFun();

    formData.standard = standard.value;
    if (selectedFlowTemplate.value) {
      formData.flowTemplate = selectedFlowTemplate.value;
    }
    if (localDetail.value?.tagKeyList) {
      disposeTagKey(formData, localDetail.value?.tagKeyList || [], getTagKeyMap());
    } else {
      formData.tagKeyList = [];
    }
    // 处理时间范围
    if (formData.planTime) {
      formData.beginTime = formData.planTime[0];
      formData.endTime = formData.planTime[1];
    }
    formData.pMemberIds = '';
    if (formData.pMemberList?.length > 0) {
      formData.pMemberIds = formData.pMemberList;
    }
    formData.eMemberIds = '';
    if (formData.eMemberList?.length > 0) {
      formData.eMemberIds = formData.eMemberList;
    }

    return formData;
  } else {
    return null;
  }
};

defineExpose({
  getFormData,
});

onMounted(() => {});
</script>

<style lang="scss" scoped>
.task-performance-page {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  overflow: auto;

  .form-style {
    width: 70%;

    :deep(.el-form-item) {
      margin-bottom: 18px !important;
    }
  }
}
</style>
