<template>
  <view>
    <view v-if="info">
      <view class="item">
        <view class="item-title">掌静脉号</view>
        <view class="item-content">{{ info.palmVeinNumber }}</view>
      </view>
      <view class="item" style="margin-top: 2rpx" @click="() => $refs.modalInput.open()">
        <view class="item-title">姓名</view>
        <view class="flex items-center">
          <view class="item-content mr-2">{{ info.palmVeinName }}</view>
          <up-icon name="arrow-right"></up-icon>
        </view>
      </view>
      <view class="item" style="margin-top: 2rpx" @click="updateTime">
        <view class="item-title">有效期</view>
        <view class="flex items-center">
          <view v-if="info.palmVeinType === 1" class="mr-2">永久</view>
          <view v-else-if="info.palmVeinType === 2" class="mr-2 w-400 text-right">
            <view class="item-content">{{ timeFormat(info.startDate, 'yyyy-mm-dd h:M') }}</view>
            <view class="item-content">{{ timeFormat(info.endDate, 'yyyy-mm-dd h:M') }}</view>
          </view>
          <view v-else class="mr-2">
            <view class="item-content">{{ timeFormat(info.startDate, 'yyyy-mm-dd') }}</view>
            <view class="item-content">{{ timeFormat(info.endDate, 'yyyy-mm-dd') }}</view>
          </view>
          <up-icon name="arrow-right"></up-icon>
        </view>
      </view>
      <view
        class="item"
        style="margin-top: 2rpx"
        v-if="info.palmVeinType === 4"
        @click="updateTime"
      >
        <view class="item-title">有效日</view>
        <view class="flex items-center">
          <view class="item-content mr-2">{{
            $lock.convertWeekDaysToChineseString(info.weekDay)
          }}</view>
          <up-icon name="arrow-right"></up-icon>
        </view>
      </view>
      <view
        class="item"
        style="margin-top: 2rpx"
        v-if="info.palmVeinType === 4"
        @click="updateTime"
      >
        <view class="item-title">有效时间</view>
        <view class="flex items-center">
          <view class="item-content mr-2">
            {{ timeFormat(info.startDate, 'h:M') }} - {{ timeFormat(info.endDate, 'h:M') }}
          </view>
          <up-icon name="arrow-right"></up-icon>
        </view>
      </view>
      <view class="item" style="margin-top: 20rpx">
        <view class="item-title">添加者</view>
        <view class="item-content">{{ info.senderUsername }}</view>
      </view>
      <view class="item" style="margin-top: 2rpx">
        <view class="item-title">添加时间</view>
        <view class="item-content">{{ timeFormat(info.createDate, 'yyyy-mm-dd h:M') }}</view>
      </view>

      <view class="item !py-2" style="margin-top: 20rpx">
        <view class="item-title">胁迫掌静脉</view>
        <switch
          :checked="info.isCoerced === 2"
          class="transform-scale-90"
          @click="changeCoerced"
          :disabled="true"
          color="#002ce5"
        />
      </view>
      <view class="item !py-2" style="margin-top: 2rpx">
        <view class="item-title">是否为管理员</view>
        <switch
          @click="disabled"
          :checked="info.palmVeinRight"
          class="transform-scale-90"
          :disabled="true"
          color="#002ce5"
        />
      </view>
      <view class="item" style="margin-top: 20rpx" @click="toRecordList">
        <view class="item-title">操作记录</view>
        <up-icon name="arrow-right"></up-icon>
      </view>
      <view class="button" @click="deletePassword">删除</view>
      <ModalInput
        ref="modalInput"
        title="请输入姓名"
        :autoClose="false"
        placeholder="请输入姓名"
        :value="info.palmVeinName"
        @confirm="changeName"
      />
    </view>
  </view>
</template>

<script setup>
  import { getCurrentInstance, ref } from 'vue'
  import { onLoad } from '@dcloudio/uni-app'
  import { timeFormat } from 'uview-plus'
  import { useLockStore } from '@/stores/lock'
  import { useBasicStore } from '@/stores/basic'
  import { useBluetoothStore } from '@/stores/bluetooth'
  import { useUserStore } from '@/stores/user'
  import { getPalmVeinRequest, updatePalmVeinRequest } from '@/api/palmVein'

  const instance = getCurrentInstance().proxy
  const eventChannel = instance.getOpenerEventChannel()

  const $lock = useLockStore()
  const $basic = useBasicStore()
  const $user = useUserStore()
  const $bluetooth = useBluetoothStore()

  const info = ref(null)

  const modalInput = ref(null)

  const pending = ref(false)

  const changeCoerced = async () => {
    const netWork = await $basic.getNetworkType()
    if (!netWork) {
      return
    }
    if (pending.value) return
    pending.value = true
    uni.showLoading({
      title: '更新中'
    })
    const { code } = await $bluetooth.registerAuthentication({
      type: 'palmVein',
      operate: 1,
      isAdmin: info.value.isAdmin,
      isForce: info.value.isCoerced === 2 ? 0 : 1,
      isRound: info.value.palmVeinType === 4 ? 1 : 0,
      weekDays: info.value.weekDay,
      no: info.value.palmVeinNumber,
      userCountLimit: 0xffff,
      keyId: $bluetooth.keyId.toString(),
      uid: $user.userInfo.uid.toString(),
      startDate: info.value.startDate,
      endDate: info.value.endDate,
      startTime: info.value.palmVeinType === 4 ? timeFormat(info.value.startDate, 'h:M') : '00:00',
      endTime: info.value.palmVeinType === 4 ? timeFormat(info.value.endDate, 'h:M') : '00:00'
    })
    if (code === 0) {
      const { code, message } = await updatePalmVeinRequest({
        lockId: $bluetooth.currentLockInfo.lockId,
        palmVeinId: info.value.palmVeinId,
        palmVeinType: info.value.palmVeinType,
        isCoerced: info.value.isCoerced === 2 ? 1 : 2,
        changeType: 1
      })
      pending.value = false
      uni.hideLoading()
      if (code === 0) {
        info.value.isCoerced = info.value.isCoerced === 2 ? 1 : 2
        eventChannel.emit('refresherList', {})
        uni.showToast({
          title: '更新成功',
          icon: 'none'
        })
      } else {
        uni.showToast({
          title: message,
          icon: 'none'
        })
      }
    } else {
      pending.value = false
      uni.hideLoading()
      uni.showToast({
        title: '操作失败，请重试',
        icon: 'none'
      })
    }
  }

  const changeName = async name => {
    if (!name) {
      uni.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return
    }
    if (pending.value) return
    pending.value = true
    const { code, message } = await updatePalmVeinRequest({
      lockId: $bluetooth.currentLockInfo.lockId,
      palmVeinId: info.value.palmVeinId,
      palmVeinType: info.value.palmVeinType,
      palmVeinName: name,
      changeType: 1
    })
    pending.value = false
    if (code === 0) {
      modalInput.value.close()
      eventChannel.emit('refresherList', {})
      info.value.palmVeinName = name
      uni.showToast({
        title: '更新成功',
        icon: 'none'
      })
    } else {
      uni.showToast({
        title: message,
        icon: 'none'
      })
    }
  }

  const updateTime = () => {
    $basic.routeJump({
      name:
        info.value.palmVeinType === 1 || info.value.palmVeinType === 2
          ? 'temporaryDate'
          : 'cycleDate',
      params: {
        info: JSON.stringify({ ...info.value, type: 'palmVein' })
      },
      events: {
        refresh() {
          eventChannel.emit('refresherList', {})
          getPalmVeinRequest({
            palmVeinId: info.value.palmVeinId
          }).then(res => {
            info.value = res.data
          })
        }
      }
    })
  }

  const disabled = () => {
    uni.showToast({
      title: '暂不支持修改',
      icon: 'none'
    })
  }

  onLoad(options => {
    if (options.info) {
      info.value = JSON.parse(options.info)
      console.log(info.value)
    }
  })

  const toRecordList = async () => {
    $basic.routeJump({
      name: 'typeRecordList',
      params: {
        name: info.value.palmVeinName,
        key: 'palmVeinId',
        id: info.value.palmVeinId
      }
    })
  }

  const deletePassword = async () => {
    eventChannel.emit('delete', {})
  }
</script>

<style lang="scss">
  page {
    background-color: $uni-bg-color-grey;
  }
</style>

<style lang="scss" scoped>
  .item-title {
    width: 350rpx;
  }

  .item {
    padding: 24rpx 32rpx;
    background-color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 32rpx;
    font-weight: 500;
  }

  .tips {
    padding: 24rpx 32rpx;
    font-size: 24rpx;
    color: #999999;
  }

  .button {
    margin: 32rpx;
    width: 686rpx;
    height: 88rpx;
    background-color: #df282d;
    color: white;
    text-align: center;
    line-height: 88rpx;
    border-radius: 44rpx;
    font-weight: bold;
  }
</style>
