<!--
 * @Author: liuzhenlin 461480418@qq.ocm
 * @Date: 2023-01-12 11:57:48
 * @LastEditors: liuzhenlin
 * @LastEditTime: 2023-09-22 14:38:38
 * @Description: file content
 * @FilePath: \labsop小程序\pages\schedule\myAppoint\index.vue
-->
<template>
  <div class="panel-wrap">
    <van-empty v-if="appointList.length == 0" mode="list" description="暂无正在上机"></van-empty>
    <van-list v-else v-model:loading="listloading" class="data-list" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <div class="inst-item mb40" v-for="(v, index) in appointList" :key="index" @click="toDetail(v)">
        <div class="flex flex-between mb20">
          <div>
            <div class="mr10">
              <span class="fontSize14 primary-color bold">{{ v.instName }}</span>
            </div>
          </div>
        </div>
        <div class="flex mb20">
          <div class="equ-tit">
            <span class="fontSize14 bold">设备型号：</span>
          </div>
          <div>
            <span class="fontSize14">{{ v.instNameEn }}</span>
          </div>
        </div>
        <div class="flex mb20" v-if="v.projectName">
          <div class="equ-tit">
            <span class="fontSize14 bold">课题组：</span>
          </div>
          <div>
            <span class="fontSize14">{{ v.projectName }}</span>
          </div>
        </div>
        <div class="flex mb20" v-if="v.serviceName">
          <div class="equ-tit">
            <span class="fontSize14 bold">服务：</span>
          </div>
          <div>
            <span class="fontSize14">{{ v.serviceName }}</span>
          </div>
        </div>
        <div class="flex mb20">
          <div class="equ-tit">
            <span class="fontSize14 bold">所在位置：</span>
          </div>
          <div>
            <span class="fontSize14">{{ v.placeAddress + '(' + (v.laboratoryName || '') + ')' }}</span>
          </div>
        </div>
        <div class="flex">
          <div class="equ-tit">
            <span class="fontSize14 bold">开始时间：</span>
          </div>
          <div>
            <span class="fontSize14">{{ v.startTime }}</span>
          </div>
        </div>
        <div class="flex mt20">
          <van-button
            style="width: 70px; height: 30px; margin: 0; font-size: 14px"
            class="scan-txt"
            type="danger"
            size="small"
            @click.native.stop="handleIsGetOff(v)"
          >
            下机
          </van-button>
        </div>
      </div>
    </van-list>
    <!-- <blue-tooth ref="bluetoothRef" @getOff="callbackOff"></blue-tooth> -->
  </div>
</template>

<script>
  // import BlueTooth from '../../../components/BlueTooth'
  import { useMyAppointApi } from '/@/api/appoint'
  const myAppointApi = useMyAppointApi()
  import { useInstrApi } from '/@/api/instr'
  const instApi = useInstrApi()
  import instAppointApi from '/@/api/instr/instAppoint'
  import to from 'await-to-js'
  import { showConfirmDialog, showNotify } from 'vant'
  export default {
    // components: { BlueTooth },
    data() {
      return {
        appointList: [],
        queryForm: {
          pageNum: 1,
          pageSize: 10
        },
        curAppointInfo: 0,
        total: 0,
        listloading: false,
        finished: false
      }
    },
    created() {},
    mounted() {
      this.queryForm.pageNum = 1
      this.getInstList()
    },
    methods: {
      // 重新加载
      onLoad() {
        this.queryForm.pageNum++
        this.getInstList()
      },
      // 查询列表
      async getInstList() {
        this.listloading = true
        const [err, res] = await to(myAppointApi.inProgressList(this.queryForm))
        this.listloading = false
        if (err) return
        if (res?.code === 200) {
          this.appointList = this.queryForm.pageNum == 1 ? [...(res?.data?.list || [])] : [...this.appointList, ...(res?.data?.list || [])]
          this.total = res?.data?.total
          if (this.queryForm.pageNum * this.queryForm.pageSize >= res.data.total) {
            this.finished = true
          }
        }
      },
      async handleIsGetOff(row) {
        const [err, res] = await to(instApi.getSettingDetail({ instId: row.instId, code: 'InstCfgUse' }))
        if (err) return
        // 上机时间
        const useTime = this.calcTimeDiff(row.usedStartTime)
        // 是否进行上机时间太短的提示
        // 需要
        if (res?.data?.config.shortWarningEnable) {
          const shortWraningTime = this.getShortWraningTime(res?.data?.config.shortWarning, res?.data?.config.shortWarningUnit)
          if (useTime > shortWraningTime) {
            if (row.controlMode === '30') {
              this.$refs.bluetoothRef.initBlue('close', row)
            } else {
              this.curAppointInfo = row
              showConfirmDialog({
                title: '提示',
                message: `本次上机时长共计${useTime}分钟，确定要下机吗？`
              })
                .then((e) => {
                  console.log(e)
                  this.getOff()
                })
                .catch(() => {
                  console.log('ssss')
                })
            }
          } else {
            if (row.controlMode === '30') {
              this.$refs.bluetoothRef.initBlue('close', row)
            } else {
              this.curAppointInfo = row
              showConfirmDialog({
                title: '提示',
                message: `本次上机时长共计${useTime}分钟，不足${shortWraningTime}分钟，确定要下机吗？`
              })
                .then((e) => {
                  console.log(e)
                  this.getOff()
                })
                .catch(() => {
                  console.log('ssss')
                })
            }
          }
        } else {
          if (row.controlMode === '30') {
            this.$refs.bluetoothRef.initBlue('close', row)
          } else {
            this.curAppointInfo = row
            showConfirmDialog({
              title: '提示',
              message: `本次上机时长共计${useTime}分钟，确定要下机吗？`
            })
              .then((e) => {
                console.log(e)
                this.getOff()
              })
              .catch(() => {
                console.log('ssss')
              })
          }
        }
      },
      // 计算时间转分钟
      getShortWraningTime(time, unit) {
        if (unit == 'hour') {
          return time * 60
        } else if (unit == 'minute') {
          return time
        }
      },
      calcTimeDiff(date) {
        var new_date = new Date() //新建一个日期对象，默认现在的时间
        var old_date = new Date(date) //设置过去的一个时间点，"yyyy-MM-dd HH:mm:ss"格式化日期
        var difftime = new_date - old_date //计算时间差
        return Math.round(difftime / 60000)
      },
      async getOff() {
        const params = { appointId: this.curAppointInfo.id }
        const [err, res] = await to(instAppointApi.getOff({ ...params }))
        if (err) return
        if (res.code == 200) {
          this.queryForm.pageNum = 1
          this.getInstList()
          showNotify({ type: 'success', message: '下机成功' })
        }
      },
      callbackOff() {
        this.queryForm.pageNum = 1
        this.getInstList()
      },
      toDetail(v) {
        this.$router.push(`/onlineInfo?appointId=${v.id}`)
      }
    }
  }
</script>
<style lang="scss" scoped>
  * {
    box-sizing: border-box;
  }
  .panel-wrap {
    height: 100%;
    .data-list {
      height: 100%;
      padding: 20px;
      overflow: auto;
      .inst-item {
        border-radius: 10px;
        padding: 15px;
        box-shadow: -2px 0px 9px rgba(0, 0, 0, 0.12);
        margin-bottom: 20px;
        .equ-tit {
          width: 74px;
        }
      }
    }
  }
</style>
