<template>
  <div class="page-content top-select-list">
    <div class="top-select border-bottom-1">
      <van-grid :border="false" :gutter="10" :column-num="3">
        <van-grid-item>
          <top-select1 :type="'report_type'" :initialName="'类型'" @refresh="refreshByReportType"
            :selectName="reportTypeName" :selectValue="reportType" />
        </van-grid-item>
        <van-grid-item>
          <top-date1 :initialName="'日期'" @refresh="refreshByDate" :selectDate="date" />
        </van-grid-item>
        <van-grid-item>
          <top-select2 :type="'stock'" :initialName="'股票'" @refresh="refreshByStock" :selectName="stockName"
            :selectValue="stockCode" />
        </van-grid-item>
      </van-grid>
    </div>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh" v-if="list.length > 0">
      <van-list v-model:loading="loading" :finished="finished" finished-text="没有更多了" @load="onLoad"
        :immediate-check="false">
        <van-cell class="cell" v-for="(item, index) in list" :key="index" :id="item.reportId"
          @click="showDetail(item, index)">
          <div class="img-box">
            <van-image :src="item.url" class="img"></van-image>
            <div class="title">{{ item.reportName }}</div>
          </div>
          <div class="road-info flex-1 text-start d-flex justify-content-between">
            <div class="title">{{ item.reportName }}</div>
            <div class="flex-1">
              <div>
                <span>{{ item.stockName }}</span>
                <span class="line">|</span>
                <van-tag size="medium" :class="item.reportColor" class="white-text-color">
                  {{ item.reportTypeName }}
                </van-tag>
              </div>
              <div class="report-desc">
                <div class="detail text-ellipsis" v-for="(row, idx) in item.reportDescList" :key="idx">{{ row }}</div>
              </div>
            </div>
            <div class="report-time">{{ item.reportDate }}</div>
          </div>
        </van-cell>
      </van-list>
    </van-pull-refresh>
    <van-empty v-else />
  </div>
</template>
<script setup lang="ts">
import { ref, nextTick, onUnmounted } from 'vue'
import TopSelect1 from '@/components/top-select1.vue'
import TopSelect2 from '@/components/top-select2.vue'
import TopDate1 from '@/components/top-date1.vue'
import emitter from '@/utils/mitt'

import { stockReportList } from '@/utils/api'
import { listLoadAndRefresh } from '@/mixins/list-load-and-refresh'

const getData = async () => {
  const { data } = await stockReportList(`${reportType.value ? '?reportType=' + reportType.value : ''}`, {
    curPage: curPage.value,
    limit: 20,
    stockCode: stockCode.value || null,
    reportDate: date.value !== '日期' ? date.value : null
  })
  data.list.map((ele: any) => {
    ele.url = `/img/${ele.reportType}.jpeg`
    ele.reportDescList = ele.reportTitle?.split('；') || []
    ele.reportName = ele.reportName
      ? ele.reportName.substring(0, ele.reportName.length - 4)
      : ''
    switch (ele.reportType) {
      case 'SFBG':
        ele.reportColor = 'primary-background-color'
        break
      case 'GXBG':
        ele.reportColor = 'danger-background-color'
        break
      case 'XLL':
        ele.reportColor = 'success-background-color'
        break
      case 'CLBG':
        ele.reportColor = 'warning-background-color'
        break
    }
  })
  return data
}

import reportInfoStore from '@/stores/reportInfo'
const reportInfo = reportInfoStore()

const scrollPage = () => {
  nextTick(() => {
    if (reportInfo.reportId && reportInfo.index) {
      stockCode.value = reportInfo.stockCode
      stockName.value = reportInfo.stockName
      date.value = reportInfo.date
      reportTypeName.value = reportInfo.reportTypeName
      reportType.value = reportInfo.reportType
      let pageIndex1 = reportInfo.index / 20
      let pageIndex2 = reportInfo.index % 20
      if (pageIndex2 > 1) {
        pageIndex1++
      }
      if (pageIndex1 > curPage.value) {
        onLoad()
      } else {
        const el = document.getElementById(reportInfo.reportId)
        if (el) {
          el.scrollIntoView({ behavior: 'smooth' })
        }
        reportInfo.reportId = ''
        reportInfo.index = 0
        emitter.emit('hiddenLoading', '')
      }
    } else {
      emitter.emit('hiddenLoading', '')
    }
  })
}
const { refreshing, finished, loading, list, onLoad, onRefresh, curPage } = listLoadAndRefresh(getData, 'report', scrollPage)

import { showFile } from '@/mixins/show-file'
import userInfoStore from '@/stores/userInfo'
const { loadingFile } = showFile()
const showDetail = (item: any, index: number) => {
  reportInfo.index = index
  reportInfo.reportId = item.reportId
  reportInfo.stockCode = stockCode.value
  reportInfo.stockName = stockName.value
  reportInfo.date = date.value
  reportInfo.reportTypeName = reportTypeName.value
  reportInfo.reportType = reportType.value
  const userInfo = userInfoStore()
  loadingFile(`${import.meta.env.VITE_BASE_URL}/stock/report/${item.reportId}?token=${userInfo.token}`)
}

let reportType = ref('')
let reportTypeName = ref('类型')
let date = ref('日期')
let stockCode = ref('')
let stockName = ref('股票')

const refreshByReportType = (e: any) => {
  reportType.value = e.value
  reportTypeName.value = e.name
  onRefresh()
}

const refreshByDate = (e: any) => {
  date.value = e
  onRefresh()
}

const refreshByStock = (e: any) => {
  stockCode.value = e.value
  stockName.value = e.name
  onRefresh()
}

const onRefresh1 = () => {
  stockCode.value = reportInfo.stockCode
  stockName.value = reportInfo.stockName
  date.value = reportInfo.date
  reportTypeName.value = reportInfo.reportTypeName
  reportType.value = reportInfo.reportType
  onRefresh()

}
onRefresh1()

onUnmounted(() => {
  reportInfo.stockCode = ''
  reportInfo.stockName = '股票'
  reportInfo.date = '日期'
  reportInfo.reportTypeName = '类型'
  reportInfo.reportType = ''
})
</script>

<style lang='scss' scoped>
.van-cell {
  width: 100%;
  max-width: 678px;
  margin: 0;
  box-shadow: 0 0;
}

.cell {
  :deep(.van-cell__value) {
    display: flex;
  }

  .img-box {
    width: 30%;
    max-width: 100PX;
    max-height: 130PX;
    position: relative;

    .img {
      width: 100%;
      height: 100%;
    }

    .title {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      padding: 10px;
      zoom: 0.8;
      height: 40px;
      overflow: hidden;
      color: #fff;
      text-align: center;
      font-size: 12px;
    }
  }

  .road-info {
    margin-left: 10px;
    flex-direction: column;
    width: 70%;

    .report-time {
      font-size: 12px;
      line-height: 16px;
    }

    .title {
      color: #000;
      font-weight: 600;
    }

    .report-desc {
      color: #999;
      height: 48px;
      margin-top: 6px;
      overflow: hidden;
      font-size: 12px;

      .detail {
        line-height: 16px;
        height: 16px;
        padding-left: 8px;
        position: relative;

        &::before {
          content: '';
          width: 4px;
          height: 4px;
          border-radius: 50%;
          background-color: #999;
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }

    .line {
      margin: 0 10px;
    }
  }
}
</style>
