<template>
  <BaseTable
    ref="tableRef"
    showIndex
    :actionsWidth="actionsWidth"
    :tableName="tableName"
    :params="{...params,sortOrder:1}"
    :api="api"
    :searchLimitNum="3"
    @searchChange="changeFun($event,'search')"
    v-bind="$attrs"
    @load="tableOnLoadFun"
    :exportApi="exportDemandApi"
    :exportFileName="exportFileName"
    :exportParams="{...params,sortOrder:1}"
    :exportDict="{
      demandType: 'DEMANED_TYPE',
      simType: 'DEMAND_TYPE1',
      achieveStatus: 'RESULT_ACHIEVE_STATUS',
      demandStatus:'DEMAND_EXE_STATUS'
    }"
  >
    <template #leftOptions>
      <slot name="leftOptions">
      </slot>
    </template>
    <template #tableActions="{row}">
      <slot name="tableActions" :row="row"></slot>
    </template>
    <!-- <template #demandStatus="{row}">

    </template> -->
    <template #demandStatus="{row}">
      <span
        :class="
          row.demandStatus === TASK_PROCESS_STATUS.COMPLETED?
            'dot dot-success':
            row.demandStatus === TASK_PROCESS_STATUS.IN_PROGRESS?
              'dot dot-in-precess':
              row.demandStatus === TASK_PROCESS_STATUS.NO_STARTED?
                'dot dot-default':
                row.demandStatus === TASK_PROCESS_STATUS.PAUSED?
                  'dot dot-approving':
                  row.demandStatus === DEMAND_STATUS.NO_SEND?'dot dot-default':'dot dot-refuse'"
      >
      </span>
      {{ DEMAND_STATUS_MAP[row.demandStatus as keyof typeof DEMAND_STATUS_MAP] }}
    </template>
    <!-- <template #achieveStatus="{row}">
      {{ TASK_CALCULATE_STATUS_OBJ[row.achieveStatus as keyof typeof DEMAND_STATUS_MAP] || '未分析' }}
    </template> -->
    <template #achieveStatus="{row}">
      <span
        :class="
          row.achieveStatus === TASK_CALCULATE_STATUS.QUALIFIED?
            'dot dot-qualified':
            row.achieveStatus === TASK_CALCULATE_STATUS.UNQUALIFIED?
              'dot dot-unqualified':row.achieveStatus === TASK_CALCULATE_STATUS.NO_CALCULATE?
                'dot dot-no-calculate':'dot dot-no-calculate'"
      >
      </span>
      {{ TASK_CALCULATE_STATUS_OBJ[row.achieveStatus as keyof typeof DEMAND_STATUS_MAP] || '未分析' }}
    </template>
    <template #progress="{ row }">
      {{ row.progress || 0 }}%
    </template>
    <template #approvalStatus="{ row }">
      {{ row.approvalStatus || '未审批' }}
    </template>
    <template #attachments="{ row }">
      <el-link type="primary" @click="seeAttachments(row.uuid)">查看附件</el-link>
    </template>
    <template #demandType="{row}">
      {{ DEMAND_TYPE.O[row.demandType] }}
    </template>
    <template #simType="{row}">
      {{ disposeSimType(row.demandType,row.simType) }}
    </template>
    <template #projectId="{ row }">
      {{ row.projectName }}
    </template>
    <template #phaseId="{ row }">
      {{ row.phaseName }}
    </template>
    <template #pMemberList="{row}">
      <!-- {{ row.pMemberList }} -->
      {{ disposeMemberList(row,'pMemberList') }}
    </template>
  </BaseTable>
  <attachments :demandId="demandInfo.uuid" v-model:visible="attachmentsVisible" ></attachments>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref, type PropType } from 'vue';
import BaseTable from '@/components/common/table/baseTable.vue';
import { getChildrenNodeList } from '../../projectDetail/components/projectApi';
import { NODE_TYPE } from '@/utils/enum/node';
import { CommonStore } from '@/stores/common';
import attachments from '@/views/task/simulationTask/components/attachments.vue';
import { disposeMemberList } from '../../projectDetail/components/project';
import { useDict } from '@/utils/useDict';
import { disposeSimType } from '@/utils/task';
import { DEMAND_STATUS_MAP, DEMAND_STATUS, TASK_CALCULATE_STATUS_OBJ, TASK_PROCESS_STATUS, TASK_CALCULATE_STATUS } from '@/utils/enum/task';
import { setDemandSearchOptions } from '../taskPage';
import { exportDemandApi } from '@/api/project/demand';

defineProps({
  params: {
    type: Object,
    default: () => ({}),
  },
  tableName: {
    type: String,
    default: '',
  },
  api: {
    type: Function as PropType<(params: any) => Promise<any>>,
    default: () => {},
  },
  actionsWidth: {
    type: Number,
    default: 95,
  },
  exportFileName: {
    type: String,
    default: '',
  },
});

const commonStore = CommonStore();

const { DEMAND_TYPE } = useDict('DEMAND_TYPE');

const attachmentsVisible = ref(false);

const tableRef = ref();

const tableFormRef = ref();

const demandInfo = reactive({
  id: 0,
  uuid: '',
});

// SIMULATION_TASK_DEMAND_CREATE
// const headData = ref<any[]>([
//   { title: '任务名称', key: 'demandName', isShow: true, inputMode: 'input', type: 1, inForm: false, required: true },
//   { title: '任务状态', key: 'taskStatus', isShow: true, inputMode: 'input', type: 1, inForm: false, required: true },
//   { title: '风险状态', key: 'achievement', isShow: true, inputMode: 'input', type: 1, inForm: false, required: true },
//   { title: '任务进度', key: 'progress', isShow: true, inputMode: 'input', type: 1, inForm: false, required: true },
//   { title: '审批状态', key: 'approvalStatus', isShow: true, inputMode: 'input', type: 1, inForm: false, required: true },
//   { title: '仿真负责人', key: 'simLeader', isShow: true, inputMode: 'input', type: 1, inForm: false, required: true },
//   { title: '项目', key: 'projectId', isShow: true, inputMode: 'input', type: 1, inForm: false, required: true },
//   { title: '阶段', key: 'phaseId', isShow: true, inputMode: 'input', type: 1, inForm: false, required: true },
//   { title: '学科', key: 'nodeId', isShow: true, inputMode: 'input', type: 1, inForm: false, required: true },
//   { title: '计划开始时间', key: 'beginTime', isShow: true, inputMode: 'input', type: 1, inForm: false, required: true },
//   { title: '计划结束时间', key: 'endTime', isShow: true, inputMode: 'input', type: 1, inForm: false, required: true },
//   { title: '实际结束时间', key: 'actualEndTime', isShow: true, inputMode: 'input', type: 1, inForm: false, required: true },
// ]);

const changeFun = async (val: any, type:string) => {
  console.log('val', val);
  let optionRef :any;
  if (type === 'search') {
    optionRef = tableRef.value;
  } else {
    optionRef = tableFormRef.value;
  }
  if (val.key === 'projectId') {
    console.log('changeFun', val);
    let nodeType = '';
    let nextKey = '';
    let nodeId = '';
    if (val.key === 'projectId') {
      nodeType = NODE_TYPE.PHASE;
      nextKey = 'phaseId';
      nodeId = val.data.projectId;
    }
    const optionList = await getPhaseList(nodeType, nodeId);
    optionRef.setOptionsFun(nextKey, optionList);
  }
  if (val.key === 'demandType') {
    const simTypeList: any = commonStore.getDictData(val.data.demandType);
    optionRef.setOptionsFun('simType', simTypeList.A);
  }
};
const getPhaseList = async (nodeType:string, projectUuid: string) => {
  let optionList = await getChildrenNodeList(nodeType, projectUuid );
  optionList = optionList.map((item: any) => {
    return {
      label: item.nodeName,
      value: item.uuid,
    };
  });
  return optionList;
};

const seeAttachments = (demandUId:string) => {
  attachmentsVisible.value = true;
  console.log('demandId', demandUId);
  demandInfo.uuid = demandUId;
};

const tableOnLoadFun = () => {
  console.log('tableOnLoadFun', tableRef.value);
  setDemandSearchOptions(tableRef.value);
};

// const searchChange = async (val: any) => {
//   console.log('val', val);
//     const optionList = await getPhaseList(nodeType, nodeId);
//     tableFormRef.value.setOptionsFun(nextKey, optionList);
// };
defineExpose({ tableRef });
onMounted(() => {
}
);

</script>

