<template>
  <div class="gl-page-content">
    <taskTable
      showIndex
      ref="exeTableRef"
      tableName="SIMULATION_TASK_SEND_LIST"
      :params="{type:3}"
      :api="queryTaskListApi"
      :actionsWidth="tableActionsLength['2-2']"
      exportFileName="我分发的任务列表"
    >
      <template #tableActions="{ row }">
        <div class="gl-table-actions">
          <el-link type="primary" @click="editTaskFun(row)">编辑</el-link>
          <el-link type="danger" @click="deleteTaskFun(row.uuid)">删除</el-link>
        </div>
      </template>
    </taskTable>
    <!-- <BaseTable
      ref="exeTableRef"
      tableName="SIMULATION_TASK_EXECUTE_LIST"
      :api="queryTaskListApi"
      :params="{type:0}"
      :searchLimitNum="3"
    >
      <template #tableActions="{ row }">
        <div class="actions">
          <el-link v-if="row.status === TASK_PROCESS_STATUS.NO_STARTED" type="danger" @click="changeTaskStatus(row, TASK_PROCESS_STATUS.REJECTED)">驳回</el-link>
          <el-link v-if="row.status === TASK_PROCESS_STATUS.NO_STARTED" type="danger" @click="changeTaskStatus(row, TASK_PROCESS_STATUS.IN_PROGRESS)">启动</el-link>
          <el-link v-if="row.status === TASK_PROCESS_STATUS.IN_PROGRESS" type="danger" @click="changeTaskStatus(row, TASK_PROCESS_STATUS.PAUSED)">暂停</el-link>
          <el-link v-if="row.status === TASK_PROCESS_STATUS.IN_PROGRESS" type="danger" @click="changeTaskStatus(row, TASK_PROCESS_STATUS.COMPLETED)">完成</el-link>
        </div>
      </template>
    </BaseTable> -->
    <Dialog
      v-model="formVisible"
      :loading="loadingInterface"
      diaTitle="编辑任务"
      :width="500"
      :height="500"
      @close="closeFun"
      show-footer
    >
      <TableForm
        ref="tableFormRef"
        tableName="SIMULATION_TASK_SEND_LIST"
        showDisabled
        @load="formLoad"
      >
      </TableForm>
      <template #footer>
        <div>
          <el-button @click="closeFun">取消</el-button>
          <el-button type="primary" @click="confirmFun">确认</el-button>
        </div>
      </template>
    </Dialog>
  </div>
</template>

<script setup lang="ts">
import { queryTaskListApi, updateTaskStatusApi } from '@/api/project/task';
// import BaseTable from '@/components/common/table/baseTable.vue';
import { TASK_CALCULATE_STATUS, TASK_CALCULATE_STATUS_OPTIONS, TASK_PROCESS_STATUS, TASK_PROCESS_STATUS_OPTIONS } from '@/utils/enum/task';
import { ElMessage } from 'element-plus';
import taskTable from '../components/taskTable.vue';
import { onMounted, ref } from 'vue';
import Dialog from '@/components/common/dialog/index.vue';
import TableForm from '@/components/common/table/tableForm.vue';
import { tableActionsLength } from '@/utils/common';
import dayjs from 'dayjs';
import { getMemberListIds } from '@/utils/task';
import { isNumber } from 'lodash-es';

const exeTableRef = ref();

const loadingInterface = ref(false);

// SIMULATION_TASK_SEND_LIST
// const headData = ref<any[]>([
//   { title: '任务名称', key: 'taskName', 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: 'project', isShow: true, inputMode: 'input', type: 1, inForm: false, required: true },
//   { title: '学科', key: 'discipline', 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 formVisible = ref(false);
const tableFormRef = ref();

const closeFun = () => {
  formVisible.value = false;
};
const editRow = ref();
const editTaskFun = (row: any) => {
  formVisible.value = true;
  editRow.value = row;
  console.log('formLoad', tableFormRef.value);
  // nextTick(() => {
  setFormData(editRow.value);
  // });
};
const setFormData = (row: any) => {
  if (tableFormRef.value) {
    tableFormRef.value.setFormDataFun({ ...row, achieveStatus: row.achieveStatus + '', eMemberList: getMemberListIds(row.eMemberList) });
  }
};

const deleteTaskFun = (uuid:string) => {
  console.log('uuid', uuid);
};
const formLoad = () => {
  console.log('formLoad', tableFormRef.value);
  // nextTick(() => {
  const exeStatusList = TASK_PROCESS_STATUS_OPTIONS.map((item: any) => {
    return {
      label: item.label,
      value: item.value,
      disabled: disposeDisabledExeStatus(editRow.value.exeStatus, item),
    };
  });
  const achieveStatusList = TASK_CALCULATE_STATUS_OPTIONS.map((item: any) => {
    return {
      label: item.label,
      value: item.value,
      disabled: disposeDisabledAchieveStatus(editRow.value.achieveStatus, item),
    };
  });
  tableFormRef.value.setOptionsFun('exeStatus', exeStatusList);
  tableFormRef.value.setOptionsFun('achieveStatus', achieveStatusList);
  setFormData(editRow.value);
  // });
};
const disposeDisabledExeStatus = (status: string, option: { value: TASK_PROCESS_STATUS; }) => {
  if (status === TASK_PROCESS_STATUS.COMPLETED) {
    return true;
  }
  if (status === TASK_PROCESS_STATUS.IN_PROGRESS) {
    return [TASK_PROCESS_STATUS.NO_STARTED].includes(option.value);
  }
  if (status === TASK_PROCESS_STATUS.NO_STARTED) {
    return [TASK_PROCESS_STATUS.PAUSED].includes(option.value);
  }
  if (status === TASK_PROCESS_STATUS.PAUSED) {
    return ![TASK_PROCESS_STATUS.IN_PROGRESS].includes(option.value);
  }
  if (status === TASK_PROCESS_STATUS.REJECTED) {
    return true;
  }
};

const disposeDisabledAchieveStatus = (status: string, option: { value: TASK_CALCULATE_STATUS; }) => {
  if (status === TASK_CALCULATE_STATUS.NO_CALCULATE) {
    return false;
  }
  if (status === TASK_CALCULATE_STATUS.QUALIFIED) {
    return [TASK_CALCULATE_STATUS.NO_CALCULATE].includes(option.value);
  }
  if (status === TASK_CALCULATE_STATUS.UNQUALIFIED) {
    return [TASK_CALCULATE_STATUS.NO_CALCULATE].includes(option.value);
  }
};

const confirmFun = async() => {
  if (await tableFormRef.value.validateFun()) {
    const fromData:any = tableFormRef.value.getFormDataFun();
    console.log('fromData', fromData);
    if (fromData.exeStatus === TASK_PROCESS_STATUS.COMPLETED && !isNumber(fromData.difficult)) {
      ElMessage({
        message: '关闭任务时需要填写难度系数！',
        type: 'warning',
      });
      return false;
    }
    loadingInterface.value = true;
    // console.log('tableFormRef.value.getFormDataFun()', fromData);
    const params:any = {
      taskIds: [fromData.uuid],
      req: {
        ...fromData,
        exeStatus: fromData.exeStatus,
        achieveStatus: fromData.achieveStatus,
        process: fromData.progress,
        pMemberList: getMemberListIds(fromData.pMemberList),
        nodeName: fromData.taskName,
      },
    };
    if (fromData.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',
      });
      formVisible.value = false;
      console.log('exeTableRef.value', exeTableRef.value);
      exeTableRef.value.tableRef.resetFun();
    } else {
      ElMessage({
        message: '任务数据更新失败！',
        type: 'error',
      });
    }
    loadingInterface.value = false;
  }
};
onMounted(() => {

});
</script>

