<template>
  <view>
    <navbar-back :autoBack="false" title="审核查询" @leftClick="customBack"></navbar-back>
    <u-row gutter="16">
      <u-col span="4">
        <view class="content-title-name">
          <text>任务列表</text>
          <up-badge v-if="taskList.length > 0" class="ml5" :value="taskList.length" type="warning"></up-badge>
        </view>
        <u-gap height="5" bg-color="#0055A2"></u-gap>
        <scroll-view
          style="height: 75vh"
          scroll-y
          scroll-with-animation
          class="content-main-left"
          :scroll-top="scrollTop"
        >
          <TaskItem
            v-for="(task, index) in taskList"
            :key="index"
            :task="task"
            :seq="index + 1"
            :active="current === index"
            @click="switchTask(index)"
          />
        </scroll-view>
      </u-col>
      <u-col span="8">
        <view class="content-title-name">
          <text>样品列表</text>
        </view>
        <u-gap height="5" bg-color="#0055A2"></u-gap>
        <view>
          <scroll-view scroll-y scroll-with-animation style="height: calc(75vh - 60px)">
            <block v-for="(sample, index) in sampleList" :key="index">
              <view class="p5 fs16">
                <u-row
                  @click="showSampleDetail(sample.id, index)"
                  :class="selectedIndex.value === index ? 'selected_Sample' : ''"
                >
                  <u-col span="2" class="text-center" :style="sampleStyle(sample)">
                    <view>
                      <text>【{{ index + 1 }}】</text>
                    </view>
                  </u-col>
                  <u-col span="5">
                    <view>
                      <text class="pl5">{{ sample.sampleAssayCode }}</text>
                    </view>
                    <view>
                      <text class="pl5">
                        {{ getDataSourceTypeShow(sample.dataSourceType) }}{{ sample.sampleName }}
                      </text>
                    </view>
                  </u-col>
                  <u-col span="5">
                    <view>
                      <text style="padding-left: 10px">{{ sample.assayProject }}</text>
                    </view>
                  </u-col>
                </u-row>
                <u-line class="p5" color="#bbb" />
              </view>
            </block>
          </scroll-view>
          <view class="content-main-right-operation">
            <u-row>
              <u-col span="3"></u-col>
              <u-col span="6">
                <u-button class="btn-operation" type="primary" @click="previewPDF" v-if="currentTask.id">
                  任务单预览
                </u-button>
              </u-col>
              <u-col span="3"> </u-col>
            </u-row>
          </view>
        </view>
      </u-col>
    </u-row>

    <sample-detail-popup
      ref="sampleDetailPopup"
      v-model:showPopup="showDetailPopup"
      :detailId="detailId"
    ></sample-detail-popup>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { onLoad, onBackPress } from '@dcloudio/uni-app'
import SampleDetailPopup from '@/components/sample/sample-detail-popup.vue'
import TaskItem from './components/task-item.vue'
import nx from '@/nx'
import { getDataSourceTypeShow } from '../common'

// 响应式数据
const scrollTop = ref(0)
const current = ref(0)
const currentTask = ref({})
const currentTaskId = ref('')
const reviewNum = ref(0)
const selectedIndex = ref(-1)
const taskList = ref([])
const sampleList = ref([])
const showDetailPopup = ref(false)
const detailId = ref('')

// 计算属性
const userInfo = computed(() => nx.$store('user').userInfo)

// 方法
const customBack = () => {
  uni.reLaunch({ url: '/pages/analysis/index/index' })
}

const sampleStyle = sample => {
  if ((sample.weightSubmitStatus === 0 || sample.weightSubmitStatus === 1) && sample.reviewCount > 0) {
    return 'color: red'
  }
  if (sample.weightSubmitStatus === 2 && sample.reviewCount > 0) {
    return 'color: green'
  }
  return ''
}

const switchTask = async index => {
  if (index === current.value) return
  current.value = index
  selectedIndex.value = -1
  const task = taskList.value[index]
  currentTask.value = task
  getAssayTaskDetail(task.id)
}

const showSampleDetail = (id, index) => {
  selectedIndex.value = index
  detailId.value = id
  showDetailPopup.value = true
}

const getAssayTask = () => {
  taskList.value = []
  sampleList.value = []
  current.value = 0
  const param = {
    taskAssignStatus: 'submitted',
    taskAssayStatus: 'submitted',
    finishStatus: 'not_start',
    flowStatus: 'not_start',
    assayOper: userInfo.value.realname
  }
  nx.$api.assayTask.getAssayTaskList(param).then(res => {
    taskList.value = res
    if (taskList.value.length > 0) {
      currentTask.value = taskList.value[0]
      getAssayTaskDetail(currentTask.value.id)
    }
  })
}

const getAssayTaskDetail = businessAssayTaskId => {
  reviewNum.value = 0
  nx.$api.assayTask.getAssayTaskDataList({ businessAssayTaskId }).then(res => {
    sampleList.value = res || []
  })
}

const previewPDF = () => {
  const { id, configReportTemplateKey } = currentTask.value
  const url = `/pages/analysis/sample/pdf-preview?businessAssayTaskId=${id}&reportKey=${configReportTemplateKey}`
  uni.navigateTo({ url })
}

// 生命周期
onMounted(() => {
  getAssayTask()
})

onBackPress(() => {
  customBack()
  return true
})
</script>

<style lang="scss" scoped>
.content-title-name {
  height: 50px;
  box-sizing: border-box;
  font-size: 20px;
  font-weight: 300;
  padding: 10px;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
}

.content-main-left {
  background-color: #f6f6f6;
}
.content-main-right-operation {
  height: 50px;
  padding-top: 10px;
  padding-right: 10px;
}
.btn-operation {
  font-size: 18px;
  width: 95%;
}
.selected_Sample {
  background-color: #d7e9fa;
}
</style>
