<template>
  <a-timeline style="padding-top: 20px">
    <a-timeline-item v-for="(record, index) in approverLogList" :key="index + 'aaa'">
      <template #dot>
        <a-badge>
          <template #count v-if="[2, 3, 4].includes(record.approvalMode)">
            <a-tag v-if="record.approvalMode == 4" color="orange">或签</a-tag>
            <a-tag v-else color="cyan">会签</a-tag>
          </template>
          <div class="avatar">
            <i class="iflowfont iflow-faqiren icon" v-if="record.taskType == 10"></i>
            <i class="iflowfont iflow-shenpi icon" v-else-if="record.taskType == 20"></i>
            <i class="iflowfont iflow-chaosong icon" v-else></i>
          </div>
        </a-badge>
      </template>
      <a-row>
        <a-col :span="24" style="padding: 0 16px 0 14px">
          <a-space direction="vertical" class="w-fill line-list">
            <b :style="{ color: colorList[index], fontWeight: 500 }"
              >{{ record.nodeName }}： <span class="m-r-8">{{ record.processUserIdWrapper?.realName }}</span>
              <TaskStatusTag :record="record" :isProjectWork="props.isProjectWork" :isProjectReview="props.isProjectReview"
            /></b>
            <span v-if="record.turnWrapper" class="approval-time" v-html="turnInfo(record)" />
            <span v-if="record.backTaskId" class="approval-time" v-html="backInfo(record)" />
            <span v-if="record.addSignTaskId" class="approval-time" v-html="addSignInfo(record)" />
            <span v-if="record.consultTaskId" class="approval-time" v-html="consultInfo(record)" />
            <span v-if="record.revokeTaskId" class="approval-time" v-html="revokeInfo(record)" />
            <span v-if="record.createTime" class="approval-time"><span class="time-title">接收时间：</span>{{ record.createTime }}</span>
            <span v-if="record.beginTime && record.taskStatus != 10" class="approval-time"
              ><span class="time-title">开始时间：</span>{{ record.beginTime }}</span
            >
            <span v-if="record.approveTime" class="approval-time"
              ><span class="time-title">{{ approveTitle }}时间：{{ record.approveTime }}</span></span
            >
            <span v-if="record.suggestContent" class="approval-time"
              ><span class="time-title">{{ approveTitle }}意见：</span></span
            >
            <span v-if="record.suggestContent" class="approval-remark">{{ record.suggestContent }}</span>
          </a-space>
        </a-col>
      </a-row>
    </a-timeline-item>
  </a-timeline>
</template>

<script setup name="ApprovalTimeLine">
import { deepClone } from '@/utils/common/util';
import { nextTick, watch, ref, computed } from 'vue';
import TaskStatusTag from './task-status-tag.vue';

// 接收属性
const props = defineProps({
  list: {
    type: Array,
    default: () => []
  },
  // 是否是项目工作流
  isProjectWork: {
    type: Boolean,
    default: false
  },
  // 是否是评审工作流
  isProjectReview: {
    type: Boolean,
    default: false
  }
});

// 颜色列表
const colorList = ref([]);

// 审批列表
const approverLogList = ref([]);

const approveTitle = computed(() => {
  return props.isProjectWork ? '办理' : props.isProjectReview ? '评审' : '审批';
});

// 设置颜色
const generateGradientColors = (count, type) => {
  // 定义起始和结束颜色的RGB值
  let startColor;
  let endColor;
  if (type == 'name') {
    startColor = { r: 0x00, g: 0x00, b: 0x00 };
    endColor = { r: 0xad, g: 0xad, b: 0xad };
  } else {
    startColor = { r: 0xb8, g: 0xb8, b: 0xb8 };
    endColor = { r: 0xdd, g: 0xdd, b: 0xdd };
  }

  if (count === 1) {
    // 如果只有一个任务，只返回起始颜色
    return [`#${startColor.r.toString(16)}${startColor.g.toString(16)}${startColor.b.toString(16)}`];
  }

  // 计算颜色渐变的步长
  const step = {
    r: (endColor.r - startColor.r) / (count - 1),
    g: (endColor.g - startColor.g) / (count - 1),
    b: (endColor.b - startColor.b) / (count - 1)
  };

  // 生成颜色数组
  let gradientColors = [];
  for (let i = 0; i < count; i++) {
    // 计算每一步的颜色值
    let r = Math.round(startColor.r + step.r * i)
      .toString(16)
      .padStart(2, '0');
    let g = Math.round(startColor.g + step.g * i)
      .toString(16)
      .padStart(2, '0');
    let b = Math.round(startColor.b + step.b * i)
      .toString(16)
      .padStart(2, '0');
    // 将RGB值转换为十六进制颜色格式
    gradientColors.push(`#${r}${g}${b}`);
  }

  return gradientColors;
};

watch(
  () => props.list,
  val => {
    if (props.list.length) {
      approverLogList.value = deepClone(props.list).reverse();
      colorList.value = generateGradientColors(props.list.length, 'name');
      nextTick(() => {
        const lineList = generateGradientColors(props.list.length, 'line');
        const arr = document.getElementsByClassName('ant-timeline-item-tail');
        if (arr?.length) {
          for (let i = arr.length - 1; i >= 0; i--) {
            arr[i].style.borderColor = lineList[i];
            arr[i].style.borderWidth = '1px';
          }
        }
      });
    } else {
      approverLogList.value = [];
      colorList.value = [];
    }
  },
  { deep: true, immediate: true }
);

/**
 *  转交信息
 * @param {*} record
 */
const turnInfo = record => {
  return `收到 <b>${record.turnWrapper}</b> 的 <b>转交</b> 任务`;
};

/**
 *  退回信息
 * @param {*} record
 */
const backInfo = record => {
  return `收到 <b>${record.returnerWrapper}</b> 的 <b>退回</b> 任务`;
};

/**
 *  加签信息
 * @param {*} record
 */
const addSignInfo = record => {
  const approverLogList = approverLogList.value;
  for (let index = 0; index < approverLogList.length; index++) {
    const element = approverLogList[index];
    // 获取到发起加签人
    if (element.taskId == record.addSignTaskId) {
      // 如果发起加签人的beforeAddSignTaskId不为空，则为前加签
      let approvalModeName = '';
      if (element.beforeAddSignTaskId) {
        approvalModeName = '前';
      } else if (record.beforeAddSignTaskId) {
        approvalModeName = '后';
      }
      return ` 收到 <b>${element.approverWrapper}</b> 的 <b>${approvalModeName}加签</b> 任务`;
    }
  }
  return '';
};

/**
 *  征询信息
 * @param {*} record
 */
const consultInfo = record => {
  const approverLogList = approverLogList.value;
  for (let index = 0; index < approverLogList.length; index++) {
    const element = approverLogList[index];
    // 获取到发起加签人
    if (element.taskId == record.consultTaskId) {
      return ` 收到 <b>${element.approverWrapper}</b> 的 <b>征询</b> 任务`;
    }
  }
  return '';
};

/**
 *  撤回信息
 * @param {*} record
 */
const revokeInfo = record => {
  return `收到 <b>${record.revokeUserIdWrapper}</b> 的 <b>撤回</b> 任务`;
};
</script>
<style lang="less" scoped>
.approval-remark {
  background-color: #fff;
  background-image: none;
  border-radius: 5px;
  border-width: 2px;
  border-color: #dcdfe6;
  border-style: dotted;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  display: inline-block;
  font-size: inherit;
  line-height: 30px;
  outline: none;
  -webkit-transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  padding: 0px 9px;
  height: auto;
  width: 100%;
}
.line-list {
  font-size: 16px;
}
.approval-time {
  color: #adb5b8;
}
.line-title {
  color: #898e91;
}
.time-title {
  color: #adb5b8;
}
.avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgba(207, 221, 247, 0.4);
  .icon {
    color: #6f9ae7;
    font-size: 20px;
  }
}
.m-r-8 {
  margin-right: 8px;
}
</style>
