<script lang="ts" setup>
import { format } from 'date-fns'
import { computed, onMounted, ref, watch } from 'vue'
import type { PackageListItem } from '/#/workorder'
import { useDialog } from 'naive-ui'

defineOptions({ name: 'ListItem' })
const props = defineProps({
  selected: {
    type: Boolean,
    default: false,
  },
  listItem: {
    type: Object as PropType<PackageListItem>,
    required: true,
  },
  showFieldList: {
    type: Array as any,
    default: () => [],
  },
  dicts: {
    type: Array,
    default: () => [],
  },
  mouseOverTask: {
    type: Object,
    default: () => [],
  },
})
const emit = defineEmits(['dismisClick'])
const dialog = useDialog()
const svgName = computed(() => {
  return props.selected ? 'taskpack-select' : 'taskpack'
})
const showFull = ref(false)
const popconfirmTarget: any = ref(null)
const popconfirmRef: any = ref(null)

function handleDismissTask() {
  dialog.warning({
    title: '确认提示',
    content: '确认解散该任务包吗？',
    positiveText: '取消',
    negativeText: '确定',
    negativeButtonProps: {
      type: 'info',
      ghost: false,
    },
    positiveButtonProps: {
      type: 'default',
    },
    onPositiveClick: () => {},
    onNegativeClick: () => {
      emit('dismisClick', props.mouseOverTask.id)
    },
  })
  popconfirmRef.value[0]?.setShow(false) // 关闭 popconfirm
}

onMounted(async () => {})
</script>

<template>
  <div
    ref="popconfirmTarget"
    class="list-item relative"
    :class="{
      'list-item-selected': selected,
      'h200': showFieldList?.length > 5 && !showFull,
    }"
  >
    <div v-for="(item, index) in showFieldList" :key="index">
      <div v-if="item.id === 'name'" class="list-item-header">
        <div class="id-wrap">
          <SvgIcon :name="svgName" size="28" />
          <span
            class="list-item-header-name"
            :class="{ 'list-item-header-selected': selected }"
          >
            <n-ellipsis style="max-width: 180px"> #{{ listItem.name }} </n-ellipsis>
            <span class="list-item-header-selected">({{ listItem.pictureCount }})</span>
            <n-popconfirm
              id="taskPopconfirmRef"
              ref="popconfirmRef"
              :show-icon="false"
              :show-arrow="false"
              placement="bottom"
              trigger="click"
            >
              <template #trigger>
                <span
                  v-show="mouseOverTask?.id === listItem.id"
                  class="dismiss-task-pack"
                  style="cursor: pointer"
                >...</span>
              </template>
              <template #action>
                <span style="cursor: pointer" @click="handleDismissTask">解散任务包</span>
              </template>
            </n-popconfirm>
          </span>
        </div>
      </div>
      <ul v-else class="list-item-detail">
        <li v-if="item.id === 'statshisText'">
          审批状态：<n-ellipsis style="max-width: 170px">
            <span
              class="list-item-status"
              :class="
                listItem.statshisText === '通过'
                  ? 'list-item-success'
                  : listItem.statshisText === '不通过'
                    ? 'list-item-error'
                    : 'list-item-watting'
              "
            >{{ listItem.statshisText }}</span>
          </n-ellipsis>
        </li>
        <li v-else-if="item.id === 'createdate'">
          提交时间：<n-ellipsis style="max-width: 170px">
            {{
              (listItem.createdate
                && format(listItem.createdate, "yyyy-MM-dd HH:mm:ss"))
                || "全部"
            }}
          </n-ellipsis>
        </li>
        <li v-else-if="item.id === 'createTime'">
          生成时间：<n-ellipsis style="max-width: 170px">
            {{
              (listItem.createTime
                && format(listItem.createTime, "yyyy-MM-dd HH:mm:ss"))
                || "全部"
            }}
          </n-ellipsis>
        </li>
        <li v-else-if="item.id === 'submit_date_timestamp'">
          筛选日期：<n-ellipsis style="max-width: 170px">
            <!-- {{ listItem.submit_date_timestamp && format(listItem.submit_date_timestamp.split('-')[0], "yyyy年MM月dd日") || '全部' }} -->
            {{
              listItem.submit_date_timestamp
                && format(
                  new Date(listItem.submit_date_timestamp?.split("-")[0]),
                  "yyyy年MM月dd日",
                )
            }}
            至
            {{
              listItem.submit_date_timestamp
                && format(
                  new Date(listItem.submit_date_timestamp?.split("-")[1]),
                  "yyyy年MM月dd日",
                )
            }}
          </n-ellipsis>
        </li>
        <li v-else class="ellipsis">
          <span class="label">{{ item.name }}：</span><n-ellipsis style="max-width: 170px">
            {{ listItem[item.id] || "全部" }}
          </n-ellipsis>
        </li>
      </ul>
    </div>
    <template v-if="showFieldList?.length > 5">
      <div class="absolute bottom-0 py-[8px] pl-[36px] text-[#666] item-action">
        <span class="cursor-pointer" @click.stop="showFull = !showFull">
          <SvgIcon v-if="showFull" style="margin-left: 6px" name="expand" size="16" />
          <SvgIcon v-else style="margin-left: 6px" name="collapse" size="16" />
          {{ showFull ? "收起" : "展开" }}
        </span>
      </div>
    </template>
    <!-- <div class="list-item-header">
      <div style="display: flex;">
        <SvgIcon :name="svgName" size="28" />
        <span class="list-item-header-name" :class="{ 'list-item-header-selected': selected }">
          {{ listItem.packagename }}
        </span>
        <span class="list-item-header-selected">({{ listItem.pictureno }})</span>
      </div>
      <SvgIcon v-show="selected" size="14" name="more-ver" />
    </div> -->
    <!-- <ul class="list-item-detail">
      <li>筛选时间：{{ listItem.createTime }}</li>
      <li>执行人：{{ listItem.createBy }}</li>
    </ul> -->
  </div>
</template>

<style lang="less" scoped>
.list-item {
  padding: 10px 0px 10px 8px;
  position: relative;
  border-bottom: 1px solid #e8e8e8;
  // · transition: height: 0.2s ease-out;
  // transform: ;
  &.h200 {
    height: 206px;
    overflow: hidden;
  }
  &-header {
    display: flex;
    align-items: center;

    .id-wrap {
      display: flex;
      align-items: center;
      margin-bottom: 6px;
    }

    .dismiss-task-pack {
      position: absolute;
      right: 15px;
    }

    .list-item-header-name {
      width: 250px;
      font-size: 16px;
      font-family: PingFang SC, PingFang SC-Medium;
      font-weight: 700;
      color: #333333;
      margin-bottom: 0 0 8px 8px;
      display: flex;
      justify-content: flex-start;
      align-items: center;

      .list-item-title {
        width: 200px;
        display: inline-block;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }

    &-name {
      font-size: 16px;
      font-weight: bold;
      color: #333333;
      line-height: 22px;
      margin: 0px 0px 8px 8px;
    }
    /*j*/
    &-selected {
      color: #507afd !important;
    }
  }
  .item-action {
    background-color: #fff;
    width: calc(100% - 8px);
  }
  &-selected {
    background-color: #f2f5fe;
    .item-action {
      background-color: #f2f5fe;
    }
  }

  &-detail {
    margin-left: 36px;

    li {
      color: #666666;
      line-height: 18px;
      margin-bottom: 8px;
      font-size: 13px;
      font-family: PingFang SC, PingFang SC-Regular;
      font-weight: Regular;
      text-align: left;
      color: #666666;
      line-height: 18px;
    }
  }

  &-divider {
    width: 280px;
    height: 1px;
    background-color: #e8e8e8;
    position: absolute;
    bottom: 0px;
  }
}
</style>
../types
