<template>
  <view class="quick-audit-card">
    <view v-for="(item, index) in list" :key="index" class="card-list" v-show="item.show">
      <CardList :item="item" :index="index" :taskList="taskListArr" @reviewFn="reviewFn" />
      <uv-divider text="分割线" textColor="#6eb657" lineColor="#6eb657"></uv-divider>
    </view>
  </view>
</template>

<script setup lang="ts">
import { onShow, onLoad, onReachBottom } from '@dcloudio/uni-app'
import { reactive, ref } from 'vue'
import {
  getListApi,
  getPackageTaskTypeApi,
  batchApprovalTask,
  wmreportBatchApprovalTask,
  wmreportFetchListApi
} from '@/service/modules/quickAuditCard'
import { useLoginStore } from '@/store/login'
import CardList from './components/cardList.vue'

const loginStore = useLoginStore()
let userInfo: any = loginStore.currentUserInfo
const roles: number[] = userInfo?.roles || []

const type = ref('')
const id = ref('')
onLoad((e) => {
  type.value = e?.type
  id.value = e?.id
  getTaskListArr()
  getList()
})

const taskListArr = ref<any[]>([])
const getTaskListArr = async () => {
  const res = await getPackageTaskTypeApi()
  taskListArr.value = res.data
}

let list = ref<any[]>([])
const getList = async () => {
  let res
  if (type.value === '1') {
    res = await getListApi({
      packageId: id.value
    })
  } else if (type.value === '2') {
    res = await wmreportFetchListApi({
      id: id.value
    })
  }
  if (res.code === 0) {
    list.value = res.data
  }
  // 针对图片处理
  list.value.forEach((item) => {
    if (item.config && item.config.length != 0) {
      item.config.forEach((iten: any) => {
        iten.taskFiledType = iten.taskFiledType.trim()
        if (iten.taskFiledType == 'img') {
          getImgList(item.wmTaskContent[iten.taskFiledKey], iten)
        }
      })
    }
    item.show = true
  })
}
const getImgList = (urlStr: any, item: any) => {
  if (item.taskFiledType != 'img') return ''
  if (!urlStr) return ''
  let uArr = urlStr.split(',')
  if (!uArr) return ''
  let imgList: any[] = []
  let previewList: any[] = []
  uArr.forEach((uItem: any) => {
    let type = '无 '
    if (uItem.includes(';1')) {
      type = '拍照'
      uItem = uItem.replace(';1', '')
    }
    if (uItem.includes(';2')) {
      type = '相册'
      uItem = uItem.replace(';2', '')
    }
    imgList.push({
      type,
      url: import.meta.env.VITE_APP_URL + uItem
    })
    previewList.push(import.meta.env.VITE_APP_URL + uItem)
  })
  item.imgList = imgList
  item.previewList = previewList
  return ''
}
// type  0 拒绝 1 通过
const reviewFn = async (result: number, index: number) => {
  const item = list.value[index]
  let res
  if (type.value === '1') {
    const obj = {
      id: item.id,
      taskStatus: result == 0 ? 4 : 3,
      taskIdToNodeIdMap: {
        [item.id]: item.taskInfo.checkState.curNodeId
      }
    }
    res = await batchApprovalTask(obj)
  } else if (type.value === '2') {
    const approvalOpinion = result == 1 ? 1 : 2
    let nodeId = item.taskInfo.checkState.nextNodeId
    if (roles.includes(42)) {
      nodeId = 8
    }
    // 40 商务组
    if (roles.includes(40)) {
      nodeId = 9
    }
    let obj = {
      reportId: id.value,
      taskIds: id,
      taskIdToNodeIdMap: {
        [item.id]: nodeId
      },
      approvalOpinion: approvalOpinion
    }
    res = await wmreportBatchApprovalTask(obj)
  }

  if (res.code == 0) {
    list.value[index].show = false
  }
}
</script>

<style lang="scss" scoped>
.quick-audit-card {
  background-color: #f8f8f8;
  .card-list {
    margin-bottom: 20rpx;
    padding-bottom: 20rpx;
    background-color: #fff;
  }
}
</style>
