<script lang="ts" setup>
import { useTaskStore } from '@/store/modules/task'
import { useUser } from '@/store/modules/user'
import emitter from '@/utils/mitt'
import { computed, onBeforeMount, onMounted, onUnmounted, reactive, ref, unref, watch } from 'vue'
import TaskList from './TaskList.vue'
import type { TaskListItem } from '/#/task'



const CustomFieldModalRef = ref(null);
const collapse = ref(false)
const taskStore = useTaskStore()
const userStore = useUser()
const pagination = reactive({
  pageNo: 1,
  pageSize: 10,
})

function collapseHandler() {
  collapse.value = !collapse.value
}

const asideWidth = computed(() => {
  return collapse.value ? 0 : 308
})

const asideStyle = computed(() => {
  return {
    width: `${asideWidth.value}px`,
  }
})

const collapseIcon = computed(() => {
  return collapse.value ? 'expand-cir' : 'collapse-cir'
})

const data = ref<TaskListItem[]>([])

onBeforeMount(async () => {
  const id = userStore.getUserInfo.id
  const orderList = await taskStore.fetchApprovalList(pagination, id)
  data.value = orderList
})

const count = computed(() => {
  return data.value.length
})

const asideEnter = ref(false)

const showCollapse = computed(() => {
  return collapse.value ? true : asideEnter.value
})

onMounted(() => {
  emitter.on('refresh', refreshHandler)
})

onUnmounted(() => {
  emitter.off('refresh', refreshHandler)
})

async function refreshHandler() {
  pagination.pageNo = 1
  pagination.pageSize = 10

  const id = userStore.getUserInfo.id
  const orderList = await taskStore.fetchApprovalList(pagination, id)
  data.value = orderList
}

watch(() => taskStore.immersion, () => {
  collapse.value = true
})
const showFilterModal = ()=>{
  const modal = unref(CustomFieldModalRef)! as any;
  modal.showModal();
}
</script>

<template>
  <div class="aside" :style="asideStyle" @mouseenter="asideEnter = true" @mouseleave="asideEnter = false">
    <div v-show="showCollapse" class="aside-collapse">
      <div class="aside-collapse-btn" @click="collapseHandler">
        <SvgIcon :name="collapseIcon" size="40" />
      </div>
    </div>
    <div class="aside-header">
      <div class="aside-header-left">
        <svg-icon name="all-worksheet" size="32" />
        <span class="aside-header-title">所有请求({{ count }})</span>
      </div>
      <div class="aside-header-right">
        <SvgIcon size="18" name="magnifying-1" />
        <SvgIcon style="margin-left: 10px;cursor: pointer;" size="18" name="filter"  @click="showFilterModal"/>
      </div>
    </div>
    <TaskList style="height: calc(100vh - 146px);" class="work-sheet-list" :data="data" :active-id="taskStore.getActiveId!" />
    <CustomFieldModal ref="CustomFieldModalRef" />
  </div>
</template>

<style lang="less" scoped>
.aside {
  display: flex;
  position: relative;
  flex-direction: column;
  background: #FFF;
  border: 1px solid rgb(239, 239, 245);
  border-radius: 3px;
  box-sizing: border-box;

  &-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    width: 100%;
    overflow: hidden;

    &-title {
      margin-left: 8px;
      color: #333333;
      font-size: 16px;
      font-weight: bold;
    }

    &-left {
      display: flex;
      align-items: center;
      overflow: hidden;
    }

    &-right {
      display: flex;
      align-items: center;
      overflow: hidden;
    }
  }

  &-divider {
    margin-left: 16px;
    width: 292px;
    height: 1px;
    background-color: #e8e8e8;
  }

  &-collapse{
    width: 2px;
    height: 100%;
    background: #507afd;
    position: absolute;
    right: 0;
    top: 0;
  }

  &-collapse-btn {
    position: absolute;
    cursor: pointer;
    width: 40px;
    height: 40px;
    top: calc(15%);
    right: -20px;
    z-index: 10;
  }
}
</style>
../types
