<!--送样-->
<template>
  <view class="page">
    <navbar-back :autoBack="false" title="样品分析-送样" @leftClick="customBack"></navbar-back>
    <u-row class="content-title" gutter="16">
      <u-col span="4">
        <view class="content-title-name">
          <text>任务列表</text>
        </view>
        <u-gap height="5" bg-color="#0055A2"></u-gap>
      </u-col>
      <u-col span="8">
        <view class="content-title-name">
          <text>样品列表</text>
        </view>
        <u-gap height="5" bg-color="#0055A2"></u-gap>
      </u-col>
    </u-row>
    <u-row class="content-main-height" gutter="16" align="top">
      <u-col span="4">
        <scroll-view
          scroll-y
          scroll-with-animation
          class="content-main-height content-main-left"
          :scroll-top="scrollTop"
        >
          <view
            v-for="(task, index) in taskList"
            :key="index"
            class="u-tab-item"
            :class="[current == index ? 'u-tab-item-active' : '']"
            :data-current="index"
            @tap.stop="swichTask(index)"
          >
            <u-row style="width: 100%">
              <u-col span="2" style="text-align: center">
                <u-icon :color="taskStyle(task)" name="tags-fill" size="34"></u-icon>
              </u-col>
              <u-col span="10">
                <view>
                  <text style="font-size: 42rpx">{{ task.taskNo }}</text>
                </view>
                <view style="margin-top: 20rpx">
                  <text>{{ task.taskName }}</text>
                </view>
                <view class="x-f" style="margin-top: 20rpx">
                  <u-icon color="" name="clock"></u-icon>
                  <text style="margin-left: 10rpx">{{ task.taskOperTime }}</text>
                </view>
              </u-col>
            </u-row>
          </view>
        </scroll-view>
      </u-col>
      <u-col span="8">
        <view class="content-main-height">
          <scroll-view scroll-y scroll-with-animation class="content-main-right">
            <block v-for="(sample, index) in sampleList" :key="index">
              <view style="padding: 10rpx; font-size: 36rpx">
                <u-row>
                  <u-col span="3" style="text-align: center">
                    <view>
                      <text>【{{ sample.sort }}】</text>
                    </view>
                    <!--                    <u-row>-->
                    <!--                      <u-col span="6" style="text-align: center;">-->
                    <!--                        <u-checkbox v-model="sample.checked" v-if="sample.sampleProcessNo == 'F39'" @change="selectSample(sample)"></u-checkbox>-->
                    <!--                      </u-col>-->
                    <!--                      <u-col span="6" style="text-align: center;">-->
                    <!--                        -->
                    <!--                      </u-col>-->
                    <!--                    </u-row>-->
                  </u-col>
                  <u-col span="9" class="sample_desc">
                    <view>
                      <view>
                        <text style="padding-left: 20rpx">{{ sample.sampleCode }}</text>
                      </view>
                      <view>
                        <text style="padding-left: 20rpx"
                          >{{ sample.dataSourceType | getDataSourceTypeShow }}{{ sample.sampleName }}</text
                        >
                      </view>
                    </view>
                    <view class="sample_desc_warn" v-if="sample.sampleProcessNo != 'F39'">
                      状态异常：{{ sample.sampleProcessNo }}
                    </view>
                  </u-col>
                </u-row>
                <u-line style="padding: 10rpx" color="#bbb" />
              </view>
            </block>
          </scroll-view>
          <view class="content-main-right-operation">
            <u-row>
              <u-col span="4"></u-col>
              <u-col span="4"></u-col>
              <u-col span="4">
                <u-button
                  class="btn-operation"
                  :disabled="this.taskList.length <= 0"
                  type="success"
                  @click="confirmReceipt"
                  >确认送样</u-button
                >
              </u-col>
            </u-row>
          </view>
        </view>
      </u-col>
    </u-row>
  </view>
</template>

<script>
export default {
  data() {
    return {
      scrollTop: 0, //tab标题的滚动条位置
      current: 0, // 预设当前项的值
      menuHeight: 0, // 左边菜单的高度
      menuItemHeight: 0, // 左边菜单item的高度
      currentTask: '', //当前选中任务
      currentTaskNo: '', //当前选中的任务编号
      currentTaskType: '', //当前任务类型
      taskList: [],
      sampleList: []
    }
  },
  onLoad() {
    //获取任务列表
    this.getAssayTask()
  },
  methods: {
    //返回首页
    customBack() {
      uni.reLaunch({
        url: '/pages/analysis/index/index'
      })
    },
    selectSample(sample) {
      if (sample.checked) sample.checked = false
      else sample.checked = true
    },
    taskStyle(task) {
      if (task.weightTaskStatus == 0 && task.reviewCount > 0) {
        return 'red'
      }
      if (task.weightTaskStatus == 1 && task.reviewCount > 0) {
        return 'red'
      }
      if (task.weightTaskStatus == 2 && task.reviewCount > 0) {
        return 'green'
      }
      return ''
    },
    sampleStyle(sample) {
      //console.log(sample);
      if (sample.weightSubmitStatus == 0 && sample.reviewCount > 0) {
        return 'color: red'
      }
      if (sample.weightSubmitStatus == 1 && sample.reviewCount > 0) {
        //return 'color: #e0861a';
        return 'color: green'
      }
      if (sample.weightSubmitStatus == 2 && sample.reviewCount > 0) {
        return 'color: green'
      }
      return ''
    },
    //确认送样
    confirmReceipt() {
      //检查是否选中所有
      // let checkedSampleList = this.sampleList.filter(item => item.checked);
      // if (checkedSampleList.length != this.sampleList.length) {
      //   this.$helper.showToast({
      //     title: '样品未全部勾选，请检查！'
      //   });
      //   return;
      // }
      //检查是否所有样品都在F39
      const checkList = this.sampleList.filter(item => item.sampleProcessNo != 'F39')
      if (checkList.length > 0) {
        this.$helper.showToast({
          title: '部分样品状态异常，请联系技术支持人员处理！'
        })
        return
      }
      const checkedSampleList = this.sampleList
      uni.showModal({
        title: '提示',
        content: '确认送样？',
        cancelColor: '#0055A2',
        confirmColor: '#0055A2',
        success: res => {
          if (res.cancel) {
            return
          }
          const sampleIdList = checkedSampleList.map(item => item.busSubCsampleId)
          const data = {
            busAssayTaskId: this.currentTask.id,
            sampleSourceType: 2,
            sampleProcessNo: 'F39',
            isGenSampleHandover: false,
            sampleIdList: sampleIdList
          }
          uni.showLoading({
            title: '正在提交...'
          })
          this.$u.api
            .execSendSample(data)
            .then(res => {
              let msg = '发生错误，请稍后再试！'
              if (!res.success) {
                if (res.message) {
                  msg = res.message
                }
                this.$helper.showToast({
                  title: msg
                })
                return
              }
              this.$helper.showToast({
                title: '操作成功！'
              })
              this.getAssayTask()
            })
            .catch(err => {
              this.$helper.showToast({
                title: '提交失败！'
              })
              console.log(err)
            })
            .finally(() => {
              uni.hideLoading()
            })
        }
      })
    },
    //切换任务
    async swichTask(index) {
      if (index == this.current) return
      this.current = index
      // 如果为0，意味着尚未初始化
      // if (this.menuHeight == 0 || this.menuItemHeight == 0) {
      //   await this.getElRect('menu-scroll-view', 'menuHeight');
      //   await this.getElRect('u-tab-item', 'menuItemHeight');
      // }
      // // 将菜单菜单活动item垂直居中
      // this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;

      //获取任务详情
      this.currentTask = this.taskList[index]
      this.currentTaskNo = this.taskList[index].taskNo
      this.currentTaskType = this.taskList[index].taskType
      this.getAssayTaskDetail(this.currentTaskNo)
    },
    // 获取一个目标元素的高度
    getElRect(elClass, dataVal) {
      new Promise((resolve, reject) => {
        const query = uni.createSelectorQuery().in(this)
        query
          .select('.' + elClass)
          .fields({ size: true }, res => {
            // 如果节点尚未生成，res值为null，循环调用执行
            if (!res) {
              setTimeout(() => {
                this.getElRect(elClass)
              }, 10)
              return
            }
            this[dataVal] = res.height
          })
          .exec()
      })
    },
    getAssayTask() {
      //显示loading
      uni.showLoading({
        title: '加载中...'
      })
      const param = {
        operateType: 'send',
        assayOper: this.userInfo.realname
      }
      this.$u.api
        .getAssayTaskList(param)
        .then(res => {
          this.taskList = res.result
          if (!this.taskList || this.taskList.length == 0) {
            this.sampleList = []
            return
          }
          if (this.taskList && this.taskList.length > 0) {
            this.current = 0
            this.currentTask = this.taskList[0]
            this.currentTaskNo = this.taskList[0].taskNo
            this.currentTaskType = this.taskList[0].taskType
            this.getAssayTaskDetail(this.currentTaskNo)
          }
        })
        .catch(err => {
          console.log(err)
        })
        .finally(() => {
          uni.hideLoading()
        })
    },
    getAssayTaskDetail(taskNo) {
      uni.showLoading({
        title: '加载中...'
      })
      const param = {
        taskNo: taskNo,
        waiting_receive: '1' //标记是收样
      }
      this.$u.api
        .getAssayTaskDetailListByTaskNo(param)
        .then(res => {
          const list = res.result
          list.forEach(item => {
            item.checked = false
          })
          this.sampleList = list
          this.showLoading = false
        })
        .catch(err => {
          this.showLoading = false
          console.log(err)
        })
        .finally(() => {
          uni.hideLoading()
        })
    }
  },
  filters: {
    getDataSourceTypeShow(val) {
      if (val == 2) return '【筛上】'
      if (val == 3) return '【筛下】'
      return ''
    }
  }
}
</script>

<style lang="scss" scoped>
.content-title {
  height: 110rpx;
  width: 100%;
  font-size: 46rpx;
  font-weight: 300;
}

.content-title-name {
  padding: 20rpx;
  text-align: center;
}

.content-main-height {
  height: calc(100vh - 250rpx);
}

.content-main-left {
  background-color: #f6f6f6;
}

.u-tab-item {
  padding: 10rpx;
  height: 200rpx;
  background: #f6f6f6;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 36rpx;
  color: #444;
  font-weight: 400;
  line-height: 1;
  border-width: 4rpx;
  border-bottom: dotted;
}

.u-tab-item-active {
  position: relative;
  color: #0055a2;
  font-size: 36rpx;
  font-weight: 600;
  background: #fff;
}

.u-tab-item-active::before {
  content: '';
  position: absolute;

  height: 32rpx;
  left: 0;
  top: 39rpx;
}

.content-main-right {
  height: calc(100vh - 410rpx);
}

.content-main-right-operation {
  height: 160rpx;
  padding-top: 30rpx;
}

.btn-operation {
  height: 100rpx;
  font-size: 36rpx;
}

.sample_desc {
  width: 100%;
  display: flex;
  flex-wrap: nowrap; /* 禁止换行 */
  justify-content: space-between !important;
  padding-right: 15px !important;
}

.sample_desc_warn {
  color: red;
}
</style>
