<template>
  <view>
    <view class="item">
      <view class="item-title">密码</view>
      <view class="item-content">{{ currentPasswordInfo.keyboardPwd }}</view>
    </view>
    <view class="item" style="margin-top: 2rpx">
      <view class="item-title">名称</view>
      <view class="item-content">{{ currentPasswordInfo.keyboardPwdName }}</view>
    </view>
    <view class="item" style="margin-top: 2rpx">
      <view class="item-title">有效期</view>
      <view v-if="currentPasswordInfo.keyboardPwdType === 2">永久</view>
      <view v-else-if="currentPasswordInfo.keyboardPwdType <= 4">
        <view class="item-content">{{ timeFormat(currentPasswordInfo.startDate, 'yyyy-mm-dd h:M') }}</view>
        <view class="item-content">{{ timeFormat(currentPasswordInfo.endDate, 'yyyy-mm-dd h:M') }}</view>
      </view>
      <view v-else>
        {{ currentPasswordInfo.timeText }}
      </view>
    </view>
    <view class="item" style="margin-top: 20rpx">
      <view class="item-title">发送人</view>
      <view class="item-content">{{ currentPasswordInfo.senderUsername }}</view>
    </view>
    <view class="item" style="margin-top: 2rpx">
      <view class="item-title">发送时间</view>
      <view class="item-content">{{ timeFormat(currentPasswordInfo.sendDate, 'yyyy-mm-dd h:M') }}</view>
    </view>
    <view class="tips">密码生成后，请在当日23：59前使用一次进行激活，否则过0点后未激活则失效。</view>
    <view class="button" @click="deletePassword">删除密码</view>
  </view>
</template>

<script>
import { mapActions, mapState } from 'pinia'
import { useLockStore } from '@/stores/lock'
import { timeFormat } from 'uview-plus'
import { deletePsaawordRequest } from '@/api/keyboardPwd'
import { useBluetoothStore } from '@/stores/bluetooth'
import { useUserStore } from '@/stores/user'
import { useBasicStore } from '@/stores/basic'

export default {
  data () {
    return {}
  },
  computed: {
    ...mapState(useLockStore, ['currentPasswordInfo', 'passwordSearch']),
    ...mapState(useBluetoothStore, ['currentLockInfo', 'keyId']),
    ...mapState(useUserStore, ['userInfo'])
  },
  methods: {
    timeFormat,
    ...mapActions(useBluetoothStore, ['setLockPassword', 'closeBluetoothConnection']),
    ...mapActions(useLockStore, ['updatePasswordSearch', 'getPasswordList']),
    ...mapActions(useBasicStore, ['backAndToast']),
    async deletePassword () {
      const that = this
      uni.showModal({
        title: '提示',
        content: '确定要删除该密码',
        async success(res) {
          if(res.confirm) {
            uni.showLoading({
              title: '删除中',
              mask: true
            })
            const timestamp = parseInt(new Date().getTime() / 1000)
            const { code } = await that.setLockPassword({
              keyId: that.keyId.toString(),
              uid: that.userInfo.uid.toString(),
              pwdNo: that.currentPasswordInfo.pwdUserNo,
              operate: 3,
              isAdmin: that.currentPasswordInfo.pwdRight,
              pwd: that.currentPasswordInfo.keyboardPwd,
              userCountLimit: 0xFFFF,
              startTime: timestamp,
              endTime: timestamp
            })
            if(code === 0) {
              that.closeBluetoothConnection()
              const { code: requestCode, message } = await deletePsaawordRequest({
                lockId: that.currentLockInfo.lockId,
                keyboardPwdId: that.currentPasswordInfo.keyboardPwdId,
                deleteType: 1
              })
              if(requestCode === 0) {
                uni.hideLoading()
                that.updatePasswordSearch({
                  ...that.passwordSearch,
                  pageNo: 1
                })
                that.getPasswordList(that.passwordSearch)
                that.backAndToast('删除成功')
              } else {
                uni.hideLoading()
                uni.showToast({
                  title: message,
                  icon: 'none'
                })
              }
            } else if(code === -1) {
              uni.hideLoading()
              uni.showToast({
                title: '删除失败，请保持在锁附近',
                icon: 'none'
              })
            } else {
              uni.hideLoading()
            }
          }
        }
      })
    }
  }
}
</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>
