<script lang="ts" setup>
import { reactive, ref, toRefs } from "vue";
import { format } from "date-fns";
import { useRoute, useRouter } from "vue-router";
import { getMessageList, readAllMsg, readMsg } from "@/api/message/message";
import iconApproveActive from "@/assets/images/message/approve-active.png";
import iconApprove from "@/assets/images/message/approve.png";
import iconSystemActive from "@/assets/images/message/system-active.png";
import iconSystem from "@/assets/images/message/system.png";
import iconArrowActive from "@/assets/images/message/arrow-active.png";
import iconArrow from "@/assets/images/message/arrow.png";
import iconClear from "@/assets/images/message/clear.png";
import iconArrowDown from "@/assets/images/message/arrow-down.png";

const emit = defineEmits(["goDetail"]);
const router = useRouter();
const route = useRoute();
const hasNextPage = ref(false);

const state = reactive({
  tabList: [
    {
      name: "审批通知",
      icon: iconApprove,
      activeIcon: iconApproveActive,
      value: 1,
    },
    {
      name: "系统消息",
      icon: iconSystem,
      activeIcon: iconSystemActive,
      value: 2,
    },
  ],
  list: [] as any,
  pageNo: 1,
});

const { list, tabList } = toRefs(state);

const tab = ref(1);

async function getList(type = "") {
  let res = await getMessageList({
    pageNo: state.pageNo,
    pageSize: "7",
    msgCategory: tab.value,
  });
  if (type == "more") {
    while (res.data.endRow > 0) {
      if (res.code == "OK") {
        if (tab.value === 1) {
          res.data.list.forEach((item) => {
            item.tag = JSON.parse(item.busJson);
          });
        }
        state.list = state.list.concat(res.data.list);
        state.pageNo++;
        res = await getMessageList({
          pageNo: state.pageNo,
          pageSize: "7",
          msgCategory: tab.value,
        });
      }
    }
    hasNextPage.value = res.data.hasNextPage;
  } else {
    if (res.code === "OK") {
      hasNextPage.value = res.data.hasNextPage;
      if (tab.value === 1) {
        res.data.list.forEach((item) => {
          item.tag = JSON.parse(item.busJson);
        });
      }
      state.list = state.list.concat(res.data.list);
    }
  }
}

getList();

async function clearMsg() {
  const res = await readAllMsg({ msgCategory: tab.value });
  if (res.code === "OK") {
    state.list = [];
    state.pageNo = 1;
    getList();
  }
}

function switchTab(type: number) {
  tab.value = type;
  state.list = [];
  state.pageNo = 1;
  getList();
}

function goFinalDetail(row) {
  console.log(row);
  router.push({
    name: "final-detail",
    query: { id: row.tag.taskId, packageid: row.packageid },
  });
}

function goDetail(item) {
  if (tab.value === 1) {
    clearMsgOne(item.id);
    goFinalDetail(item);
  }
  // emit('goDetail', item.id)
  else {
    router.push({ name: "message-detail", query: { id: item.id } });
  }
}

async function clearMsgOne(id) {
  const res = await readMsg({ msgid: id });
}

function getMore() {
  state.pageNo += 1;
  getList("more");
}
</script>

<template>
  <div class="wrapper-message">
    <div class="flex justify-between header">
      <div class="header-title">消息通知</div>
      <div class="clear" @click="clearMsg">
        <img class="icon-clear" :src="iconClear" alt="" />
        清除未读
      </div>
    </div>
    <div class="content">
      <div class="slider">
        <div
          v-for="item in tabList"
          :key="item.value"
          :class="{ 'item-active': tab === item.value }"
          class="flex item"
          @click="switchTab(item.value)"
        >
          <div class="flex item-left align-center">
            <img
              class="icon"
              :src="tab === item.value ? item.activeIcon : item.icon"
              alt=""
            />
            <div :class="{ 'text-active': tab === item.value }" class="text">
              {{ item.name }}
            </div>
          </div>
          <img
            class="icon-arrow"
            :src="tab === item.value ? iconArrowActive : iconArrow"
            alt=""
          />
          <div v-if="tab === item.value" class="line" />
        </div>
      </div>
      <div class="list">
        <div
          v-for="(item, index) in list"
          :key="item.id"
          :class="{ pt0: index === 0, 'item-disabled': item.readFlag }"
          class="item"
          @click="goDetail(item)"
        >
          <div class="left">
            <div class="num">
              {{ format(item.sendTime, "dd") }}
              <div v-if="!item.readFlag" class="point" />
            </div>
            <div class="date">
              {{ format(item.sendTime, "yyyy-MM") }}
            </div>
          </div>
          <div class="middle">
            <div v-if="tab === 1" class="title">
              {{ `<任务ID：${item.tag.taskId}>` }}{{ item.tag.taskName }}
            </div>
            <div v-else class="title">
              {{ item.titile }}
            </div>
            <div v-if="tab === 1" class="status">
              <div class="tag tag-blue">审批节点：{{ item.tag.nodeName }}</div>
              <div :class="item.tag.states === 5 ? 'tag-red' : 'tag-green'" class="tag">
                审批状态：{{ item.tag.states }}
              </div>
            </div>
            <div v-if="tab === 1" class="subtitle">
              <span class="name">{{ item.tag.sendUserName }}</span>
              <span class="time">{{ format(item.sendTime, "yyyy-MM-dd HH:mm:ss") }}</span>
            </div>
            <div v-else class="subtitle">
              <span class="name">{{ item.userName }}</span>
              <span class="time">{{ format(item.sendTime, "yyyy-MM-dd HH:mm:ss") }}</span>
            </div>
          </div>
          <div class="look">查看</div>
        </div>

        <div v-if="hasNextPage" class="more" @click="getMore">
          查看更多<img class="icon-more" :src="iconArrowDown" alt="" />
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.wrapper-message {
  display: flex;
  flex: 1;
  flex-direction: column;
  box-sizing: border-box;
  background: #fff;
  border-radius: 3px;
  height: calc(100vh - 88px);
  position: relative;

  .header {
    padding: 24px;

    &-title {
      font-size: 20px;
      font-family: PingFang SC, PingFang SC-Medium;
      font-weight: 600;
      color: #333333;
      line-height: 28px;
    }

    .clear {
      font-size: 14px;
      font-family: PingFang SC, PingFang SC-Regular;
      color: #666666;
      line-height: 20px;
      cursor: pointer;
      display: flex;
      align-items: center;

      .icon-clear {
        width: 16px;
        height: 16px;
        margin-right: 6px;
      }
    }
  }

  .content {
    display: flex;
    .slider {
      .icon {
        width: 17px;
        height: 17px;
      }

      .icon-arrow {
        width: 14px;
        height: 14px;
      }

      .item {
        width: 200px;
        height: 44px;
        position: relative;
        padding: 0 24px;
        box-sizing: border-box;
        align-items: center;
        justify-content: space-between;
        cursor: pointer;
        &-active {
          background: rgba(80, 122, 253, 0.1);
        }
      }

      .item-left {
        align-items: center;
      }

      .text {
        font-size: 14px;
        font-family: PingFang SC, PingFang SC-Medium;
        font-weight: 600;
        color: #333333;
        margin-left: 12px;
        &-active {
          color: #507afd;
        }
      }

      .icon-arrow {
        width: 14px;
        height: 14px;
      }

      .line {
        position: absolute;
        width: 3px;
        height: 44px;
        background: #5376ff;
        left: 0;
        top: 0;
      }
    }

    .list {
      margin-left: 24px;
      flex: 1;
      overflow-y: auto;
      padding-bottom: 97px;
      box-sizing: border-box;
      height: calc(100vh - 88px - 76px);
      padding-right: 24px;

      .more {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        font-family: PingFang SC, PingFang SC-Regular;
        text-align: left;
        color: #666666;
        position: absolute;
        bottom: 40px;
        left: 50%;
        background: #fff;
        cursor: pointer;

        .icon-more {
          width: 14px;
          height: 14px;
          margin-left: 6px;
        }
      }

      .item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex: 1;
        padding: 16px 0 8px 25px;
        border-bottom: 1px solid #eeeeee;

        &-disabled {
          opacity: 0.5;
        }
      }

      .pt0 {
        padding-top: 0;
      }

      .num {
        font-size: 32px;
        font-family: HarmonyOS Sans SC, HarmonyOS Sans SC-Medium;
        font-weight: 600;
        text-align: left;
        color: #507afd;
        line-height: 46px;
        position: relative;
      }

      .date {
        font-size: 12px;
        font-family: PingFang SC, PingFang SC-Regular;
        text-align: left;
        color: #999999;
        line-height: 17px;
      }

      .point {
        position: absolute;
        right: -2px;
        top: 5px;
        width: 8px;
        height: 8px;
        border-radius: 50%;
        background: #ff4e4f;
      }

      .left {
        width: 80px;
        height: 64px;
        border-right: 1px solid #e8e8e8;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
      }

      .middle {
        flex: 1;
        padding-left: 20px;
      }

      .status {
        display: flex;
        align-items: center;
        // margin-top: 8px;
      }

      .tag {
        padding: 0 8px;
        height: 24px;
        border-radius: 2px;
        margin-right: 8px;
        font-size: 14px;
        line-height: 14px;
        font-family: PingFang SC, PingFang SC-Medium;
        font-weight: 600;
        display: flex;
        align-items: center;
      }

      .tag-blue {
        background: rgba(80, 122, 253, 0.1);
        color: #507afd;
      }

      .tag-red {
        color: #ff4e4f;
        background: rgba(255, 78, 79, 0.1);
      }

      .tag-green {
        color: #3ee199;
        background: rgba(62, 225, 153, 0.1);
      }

      .title {
        font-size: 16px;
        font-family: PingFang SC, PingFang SC-Medium;
        font-weight: 600;
        text-align: left;
        color: #666666;
        line-height: 25px;
      }

      .subtitle {
        font-size: 12px;
        font-family: PingFang SC, PingFang SC-Regular;
        text-align: left;
        color: #999999;
        line-height: 19px;
        // margin-top: 8px;
      }

      .name {
        margin-right: 16px;
      }

      .look {
        font-size: 14px;
        font-family: PingFang SC, PingFang SC-Regular;
        text-align: left;
        color: #5376ff;
        line-height: 20px;
        cursor: pointer;
      }
    }
  }
}

// ::v-deep(.n-divider:not(.n-divider--vertical)) {
//   margin-top: 12px;
//   margin-bottom: 12px;
// }
</style>
