<template>
  <view style="padding: 20rpx 30rpx;">
    <view class="d-flex align-center">
      <uni-section title="企业："></uni-section>
      <uni-data-picker v-model="enterpriseId" class="picker" :localdata="companyList" placeholder="请选择要查看的企业" popup-title="选择企业" @change="getList" :map="{ text: 'enterpriseName', value: 'id' }"></uni-data-picker>
      <button type="primary" size="mini" @click="getList" style="height: 35px; line-height: 35px; margin: 0;">刷新</button>
    </view>
    <view class="line ss-m-y-20"></view>
    <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/getText'

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

const getCompanyList = async () => {
  try {
    const { data } = await getStudentPracticeCompanyList()
		data?.length && data.forEach(e => {
			e.id = e.id.toString()
			e.enterpriseName = formatName(e.anotherName || e.name)
		})
    companyList.value = data || []
	} catch {}
}

// 实习报告列表
const getList = async () => {
	try {
		const { data } = await getStudentReportList(enterpriseId.value ? { enterpriseId: enterpriseId.value } : {})
    items.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 {}
}

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

onShow(() => {
  getCompanyList()
  getList()
})

onLoad((options) => {
  if (options.enterpriseId) {
    enterpriseId.value = options.enterpriseId
  }
})

const addReport = () => {
  if (!companyList.value?.length) {
    uni.showToast({
			title: '没有查到实习企业记录！',
			icon: 'none'
		})
    return
  }
  const list = JSON.stringify(companyList.value)
  uni.navigateTo({ url: `/pagesA/student/addReport?companyList=${list}` })
}
</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;
}
.line {
  border-top: 1px solid #ccc;
}
.picker {
  flex: 1;
  overflow: hidden;
  margin-right: 12px;
}
.reportPopup {
  width: 96vw;
  .uploadTip {
    color: #00B760;
    margin-bottom: 20px;
    font-size: 13px;
  }
  .dialog-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    color:#767a82;
    padding: 20rpx;
    .title {
      font-weight: bold;
      margin-left: 10rpx;
    }
  }
  .dialog-content{
    padding: 20rpx;
    padding-bottom: 50rpx;
  }
  .dialog-bottom{
    width: 100%;
    height: 44px;
    line-height: 44px;
    text-align: center;
    color: #fff !important;
    background-color: #00B760 !important;
  }
}
.upload-img{
  position: relative;
  width: 200rpx;
  height: 200rpx;
  border: 1px solid #f1f1f1;
  margin: 10rpx;
}
.upload-file{
  width: 200rpx;
  height: 200rpx;
  border: 1px solid #f1f1f1;
  margin: 10rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 10rpx;
}
</style>
