<script lang="ts" setup>
import { onUnmounted, reactive, ref, toRefs, watch } from 'vue'
import type { PictureSortParam } from '/#/api'
import { useWorkOrder } from '@/store/modules/workOrder'
import { getPackageTaskList, getTaskDetailInfo, getTaskDetailPictureList } from '@/api/work/work'
import { getMessageList } 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'

const emit = defineEmits(['goDetail'])

const totalCount = ref(0)

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

const { list, tabList } = toRefs(state)

const packagepagination = reactive({
  pageNo: 1,
  pageSize: 10,
})
const taskpagination = reactive({
  pageNo: 1,
  pageSize: 10,
})
const sortBy: PictureSortParam = {
  orderbyname: 'desc',
  orderbyvalue: 'fromuptime',
}
const workStore = useWorkOrder()
const selectTask = ref<any>(null)
const taskList = ref<any[]>([])
const taskDetailInfo = ref<any>({})
const taskDetailPictureList = ref<any[]>([])

const tab = ref(0)

async function getList(value) {
  const res = await getMessageList({
    pageNo: '1',
    pageSize: '10',
    msgCategory: value,
  })
  console.log(res)
  state.list = res.list
}

getList('1')

watch(() => workStore.activeId, async (newValue, oldValue) => {
  const res = await getPackageTaskList(newValue, packagepagination)
  const { data } = res
  taskList.value = data

  if (taskList.value.length > 0)
    handleSelect(taskList.value[0])
})

async function handleSelect(item: any) {
  selectTask.value = item
  const taskId = item.id
  taskDetailInfo.value = await getTaskDetailInfo(taskId, workStore.activeId)

  const { data, total } = await getTaskDetailPictureList(workStore.activeId, taskId, { ...taskpagination, ...sortBy })
  taskDetailPictureList.value = data
  totalCount.value = total
}

function switchTab(type: number) {
  tab.value = type
  getList(type)
}

function goDetail(id) {
  emit('goDetail', id)
}
</script>

<template>
  <div class="wrapper-message">
    <div class="header flex justify-between">
      <div class="header-title">
        消息通知
      </div>
      <div class="clear">
        <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="item flex"
          @click="switchTab(item.value)"
        >
          <div class="item-left flex 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="index === 0 ? 'pt0' : ''" class="item" @click="goDetail(item)">
          <div class="left left">
            <div class="num">
              {{ item.pictureno }}
              <div class="point" />
            </div>
            <div class="date">
              {{ item.createTime }}
            </div>
          </div>
          <div class="middle">
            <div class="title">
              {{ item.titile }}
            </div>
            <!-- <div class="status">
              <div class="tag tag-blue">
                审批节点：初级审批
              </div>
              <div class="tag tag-red">
                审批节点：初级审批
              </div>
            </div> -->
            <div class="subtitle">
              <span class="name">{{ item.userName }}</span>
              <span class="time">{{ item.createTime }}</span>
            </div>
          </div>
          <div class="look">
            查看
          </div>
        </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);

  .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;
    padding-right: 24px;
    .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,.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;

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

      .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.10);
        color: #507afd;
      }

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

      .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>
