<template>
  <view style="padding: 20rpx 30rpx;">
    <view v-if="items.length > 0" class="wrapper">
      <view v-for="(item,index) in items" :key="index" style="margin: 0 0 50rpx 0;">
        <uni-section :title="item.date" type="line"></uni-section>
        <view>
          <image v-for="(url, i) in item.arr" :key="i" class="img" :src="url" mode="scaleToFill" @click="previewImage(item.arr,i)"></image>
        </view>
      </view>
    </view>
    <view v-else class="nodata-img-parent">
      <image src="https://minio.citupro.com/dev/static/nodata.png" mode="widthFix" class="nodata-img-child"></image>
    </view>
    <view class="bottom-sticky">
      <button type="primary" size="default" class="recomm-button" @click="addReport">新增实习报告</button>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { getStudentReportList, getStudentPracticeCompanyList } from '@/api/student.js'
import { onLoad, onShow } from '@dcloudio/uni-app'
import { formatName } from '@/utils/index.js'

const enterpriseId = ref(null)
const items = ref([])

const getCompanyList = async () => {
  try {
    const { data } = await getStudentPracticeCompanyList()
		data.forEach(e => {
			e.id = e.id.toString()
			e.enterpriseName = formatName(e.anotherName || e.name)
		})
    console.log(data, '实习企业列表')
	} catch {}
}

// 实习报告列表
const getList = async () => {
	items.value = []
	try {
		const { data } = await getStudentReportList(enterpriseId.value ? { enterpriseId: enterpriseId.value } : {})
		if (!data || !Object.keys(data).length) return
		for (let item in data) {
			items.value.push({ date: item, arr: data[item].map(e => e.url) })
		}
	} catch {}
}

onShow(() => {
  console.log('onSHow11111111')
  getCompanyList()
})

onLoad((options) => {
  console.log(options, 'options-实习报告')
  if (options.enterpriseId) {
    enterpriseId.value = options.enterpriseId
  }
  getList()
})

const addReport = () => {}

// 预览图片
const previewImage = (url, i) => {
  uni.previewImage({
    current: i,
    urls: url,
    longPressActions : {
    itemList: ['发送给朋友', '保存图片', '收藏']
    }
  })
}
</script>

<style lang="scss" scoped>
.img{
  width: 28%;
  height: 200rpx;
  margin: 10rpx;
  border: 1px solid #f4f4f4;
}
.wrapper{
  height: 84vh;
  overflow: auto;
}
:deep(.uni-section .uni-section-header__decoration) {
  background-color: #00B760 !important;
}
</style>
