<template>
  <!-- 我关注的 -->
  <div class="gl-page-content-full">
    <taskTable
      showIndex
      ref="tableRef"
      tableName="SIMULATION_TASK_ATTENTION_LIST"
      :params="taskParams"
      exportFileName="我关注的任务列表"
      :actionList="actionList"
      @show="showTaskDetailFun"
    >
    </taskTable>
    <taskDetail
      v-if="showTaskDetailDialog"
      :taskId="currentTaskInfo.id"
      :currentTaskInfo="currentTaskInfo"
      :showApproval="false"
      :showFooter="false"
      :showLeftOptions="false"
      @closeFn="showTaskDetailDialog = false"
      @updateFn="updateTaskInfoFun"
    >
    </taskDetail>
  </div>
</template>

<script setup lang="ts">
import { attentionTaskApi, updateTaskStatusApi } from '@/api/project/task';
// import BaseTable from '@/components/common/table/baseTable.vue';
import { ElMessage } from 'element-plus';
import taskTable from '../components/taskTable.vue';
import { onMounted, ref } from 'vue';
import { disposeAttentionParams } from '../taskPage';
import taskDetail from '@/views/task/projectDetail/components/taskDetail.vue';
import { TASK_PROCESS_STATUS } from '@/utils/enum/task';
import { isNumber } from 'lodash-es';
import { getMemberListIds } from '@/utils/task';
import dayjs from 'dayjs';

// enum TaskType {
//   /** 待确认任务 */
//   CONFIRM = 'confirm',
//   /** 执行的任务 */
//   EXECUTE = 'execute',
//   /** 发起的任务 */
//   SPONSOR = 'sponsor',
//   /** 关注的任务 */
//   ATTENTION = 'attention',
//   /** 所有任务 */
//   ALL = 'all',
// }
const taskParams = ref({ type: 1 });

const tableRef = ref();

const showTaskDetailDialog = ref(false);

const actionList = ref<any>([
  {
    title: '详情',
    type: 'primary',
    click: (row: any) => {
      showTaskDetailFun(row);
    },
  },
  {
    title: '取消关注',
    type: 'danger',
    needConfirm: true,
    confirmTip: '确认取消关注吗？',
    click: (row: any) => {
      attentionTaskFun(row, false);
    },
  },
]);

const currentTaskInfo = ref({ id: '', uuid: '' });
const showTaskDetailFun = (row: any) => {
  showTaskDetailDialog.value = true;
  currentTaskInfo.value = row;
};

const updateTaskInfoFun = async (info: any) => {
  const { flag, data }: any = info;

  if (flag === 'info') {
    if (data.exeStatus === TASK_PROCESS_STATUS.COMPLETED && !isNumber(data.difficult)) {
      ElMessage({
        message: '关闭任务时需要填写难度系数！',
        type: 'warning',
      });
      return false;
    }

    const params: any = {
      taskIds: [data.uuid],
      req: {
        ...data,
        exeStatus: data.exeStatus,
        achieveStatus: data.achieveStatus,
        process: data.progress,
        pMemberList: getMemberListIds(data.pMemberList),
        nodeName: data.taskName,
      },
    };
    if (data.exeStatus === TASK_PROCESS_STATUS.COMPLETED) {
      params.req.finishTime = dayjs().format('YYYY-MM-DD HH:mm:ss');
    }
    const res: any = await updateTaskStatusApi(params);

    if (res.code === 200) {
      // row.exeStatus = status;
      ElMessage({
        message: '操作成功',
        type: 'success',
      });
    } else {
      ElMessage({
        message: '任务数据更新失败！',
        type: 'error',
      });
    }
  }

  showTaskDetailDialog.value = false;
};

const attentionTaskFun = async (row: any, isAttention: boolean) => {
  const res: any = await attentionTaskApi(disposeAttentionParams(row.uuid, isAttention));
  if (res.code === 200) {
    ElMessage.success('取消关注成功');
    tableRef.value.tableRef.resetFun();
  }
};
onMounted(() => {});
</script>
