<script lang="ts" setup>
import { computed, defineOptions, nextTick, onBeforeMount, onMounted, reactive, ref, unref } from 'vue'
import CustomFieldModalVue from '../modal/CustomFieldModal.vue'
import WorkSheetList from './WorkSheetList.vue'
import { useWindowSizeFn } from '@/hooks/event/useWindowSizeFn'
import { getViewportOffset } from '@/utils/domUtils'
import type { PackageListItem } from '/#/workorder'
import { useWorkOrder } from '@/store/modules/workOrder'

defineOptions({ name: 'AsideContent' })

const collapse = ref(false)
const workStore = useWorkOrder()
const filterModalRef = ref(null)

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 listHeight = ref(700)

function computeListHeight() {
  const listEl = document.querySelector('.work-sheet-list')!
  const { bottomIncludeBody } = getViewportOffset(listEl)
  const height = bottomIncludeBody
  listHeight.value = height - 25
}

const listStyle = computed(() => {
  return {
    height: `${listHeight.value}px`,
  }
})

useWindowSizeFn(computeListHeight, 280)

onMounted(() => {
  nextTick(() => {
    computeListHeight()
  })
})

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

const pagination = reactive({
  pageNo: 1,
  pageSize: 10,
})

onBeforeMount(async () => {
  const orderList = await workStore.fetchOrderList(pagination)
  data.value = orderList
})

const asideEnter = ref(false)

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

function showFilter() {
  const modal = unref(filterModalRef)! 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 style="margin-left: 8px;">所有工单</span>
      </div>
      <div class="aside-header-right">
        <SvgIcon style="cursor: pointer;margin-left: 10px;" size="18" name="magnifying-1" />
        <SvgIcon style="cursor: pointer;margin-left: 10px;" size="18" name="filter" @click="showFilter" />
      </div>
    </div>
    <WorkSheetList :style="listStyle" class="work-sheet-list" :data="data" :active-id="workStore.getActiveId" />
    <CustomFieldModalVue ref="filterModalRef" />
  </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;

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