<template>
  <view>
    <up-popup :show="addConfigShow" round="10" @close="addConfigShow = false">
      <view class="text-center">样品选择</view>
      <view class="x-bc mt8">
        <up-button size="small" style="width: 60px" text="取消" @click="handleCancel"></up-button>
        <up-button size="small" style="width: 60px" type="primary" text="确认" @click="handleAdd"></up-button>
      </view>
      <scroll-view style="height: 60vh" scroll-y scroll-with-animation class="mt16 pl16">
        <template v-if="taskList.length > 0">
          <u-checkbox-group placement="column" v-model="selectRows">
            <view v-for="(task, index) in taskList">
              <u-checkbox :name="task.id">
                <template #label>
                  <view class="x-f pb4 pl8 border-b full-width" @click="current = index">
                    <view class="fs20">{{ task.taskNo }}</view>
                    <view class="pl32 pr32">{{ task.taskName }}{{ task.assayOper }}</view>
                    <view class="x-f">
                      <u-icon name="clock"></u-icon>
                      <text class="ml5">{{ nx.$helper.formateToDateTime(task.taskAssignTime) }}</text>
                    </view>
                  </view>
                </template>
              </u-checkbox>
            </view>
          </u-checkbox-group>
        </template>
        <up-empty v-else text="暂无数据" mode="list"> </up-empty>
      </scroll-view>
    </up-popup>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue'

import nx from '@/nx'

let emits = defineEmits(['confirm'])
const addConfigShow = ref(false)
function show() {
  addConfigShow.value = true
  selectRows.value = []
  current.value = undefined
  getAssayTaskList()
}
const userInfo = computed(() => nx.$store('user').userInfo)
const taskList = ref([])
const current = ref(undefined)
const getAssayTaskList = async () => {
  const param = {
    taskAssignStatus: 'submitted',
    taskAssayStatusList: ['not_start', 'saved'],
    assayOperator: userInfo.value.nickname
  }
  const res = await nx.$api.assayTask.getAssayTaskList(param)
  taskList.value = res
}
function handleCancel() {
  addConfigShow.value = false
}
const selectRows = ref([])

async function handleAdd() {
  // 1. 校验是否选择了数据
  if (!selectRows.value || !selectRows.value.length) {
    return uni.showToast({
      title: '请选择任务单',
      icon: 'none'
    })
  }
  try {
    const requestPromises = selectRows.value.map(businessAssayTaskId => {
      return nx.$api.assayTask.getAssayTaskDetailList({
        businessAssayTaskId
      })
    })
    const results = await Promise.all(requestPromises)

    let sampleList = []
    results.forEach(res => {
      const list = res || []
      sampleList = sampleList.concat(list)
    })

    // 5. 执行回调和关闭弹窗
    emits('confirm', sampleList)
    addConfigShow.value = false
  } catch (error) {
    console.error('批量获取详情失败:', error)
    uni.showToast({
      title: '数据加载失败',
      icon: 'none'
    })
  }
}

defineExpose({
  show
})
</script>

<style lang="scss" scoped>
:deep(.u-checkbox__label-wrap) {
  width: 100%;
}
.active {
  color: #0055a2;
  font-weight: 500;
}
</style>
