<template>
  <!-- 所有任务 -->
  <div class="gl-page-content-full">
    <taskTable
      showIndex
      ref="tableRef"
      :tableName="currentTableName"
      :params="taskParams"
      :searchLimitNum="searchLimitNum"
      exportFileName="所有任务列表"
      :actionList="canOperate ? actionList : []"
      @show="showTaskDetailFun"
    >
    </taskTable>
    <Dialog
      v-model="dialogVisible"
      diaTitle="编辑"
      :width="500"
      :height="300"
      show-confirm-button
      show-cancel-button
      show-footer
    >
      <div class="content">
        <TableForm
          ref="tableFormRef"
          tableName="SIMULATION_TASK_ALL_LIST"
          v-model:data="editRowInfo"
          :formAttrs="formAttrs"
        />
      </div>
      <template #footer>
        <div>
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmFun">确定</el-button>
        </div>
      </template>
    </Dialog>
    <taskDetail
      v-if="showTaskDetailDialog"
      :taskId="currentTaskInfo.id"
      :currentTaskInfo="currentTaskInfo"
      @closeFn="showTaskDetailDialog = false"
      :showApproval="false"
      :showFooter="false"
      :showLeftOptions="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 { TASK_ATTENTION_STATUS, TASK_PROCESS_STATUS } from '@/utils/enum/task';
import { ref } from 'vue';
import { disposeAttentionParams } from '../taskPage';
import taskDetail from '@/views/task/projectDetail/components/taskDetail.vue';
import { isNumber } from 'lodash-es';
import { getMemberListIds } from '@/utils/task';
import dayjs from 'dayjs';
import { hasPermission } from '@/utils/permission';
import Dialog from '@/components/common/dialog/index.vue';
import TableForm from '@/components/common/table/tableForm.vue';

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

// 这里的default都是“所有任务”页面需要的默认值
defineProps({
  // 使用的动态表格名称
  currentTableName: {
    type: String,
    default: 'SIMULATION_TASK_ALL_LIST',
  },
  // 是否展示操作栏
  canOperate: {
    type: Boolean,
    default: true,
  },
  // 搜索限制条数
  searchLimitNum: {
    type: Number,
    default: 3,
  },
});
const formAttrs = ref({
  confidence: {
    min: 0,
    max: 1,
    precision: 2,
    step: 0.01,
  },
});

const tableRef = ref();
const showTaskDetailDialog = ref(false);

const actionList = ref<any>([
  {
    title: '详情',
    type: 'primary',
    click: (row: any) => {
      showTaskDetailFun(row);
    },
  },
  {
    title: '编辑',
    type: 'primary',
    click: (row: any) => {
      openDialog(row);
    },
    hide: () => {
      return !hasPermission('task_all_edit');
    },
  },
  {
    title: '关注',
    type: 'primary',
    click: (row: any) => {
      attentionTaskFun(row, true);
    },
    hide: (row: any) => {
      return row.attentionFlag !== TASK_ATTENTION_STATUS.NO_ATTENTION;
    },
  },
  {
    title: '取消关注',
    type: 'danger',
    needConfirm: true,
    confirmTip: '确认取消关注吗？',
    click: (row: any) => {
      attentionTaskFun(row, false);
    },
    hide: (row: any) => {
      return row.attentionFlag !== TASK_ATTENTION_STATUS.ATTENTION;
    },
  },
]);

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

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 dialogVisible = ref(false);
const editRowInfo = ref();
// 打开编辑弹窗
const openDialog = (row: any) => {
  editRowInfo.value = { ...row };
  dialogVisible.value = true;
};
const confirmFun = async () => {
  const params: any = {
    taskIds: [editRowInfo.value.uuid],
    req: {
      difficult: Number(editRowInfo.value.difficult || 0),
      confidence: String(editRowInfo.value.confidence || 0),
      expStatus: Number(editRowInfo.value.expStatus || 0),
    },
  };
  const res: any = await updateTaskStatusApi(params);
  if (res && res.code === 200) {
    ElMessage.success('编辑成功');
    dialogVisible.value = false;
    tableRef.value.tableRef.resetFun();
  } else {
    ElMessage.warning(res?.message);
  }
};
</script>
