<template>
  <view>
    <view class="view">
      <view class="view-button">
        <view>名称</view>
        <view class="view-button" style="padding: 0">
          <view class="info">{{currentLockInfo.lockAlias}}</view>
        </view>
      </view>
      <view class="view-button">
        <view>锁编号</view>
        <view class="view-button" style="padding: 0">
          <view class="info">{{currentLockInfo.name}}</view>
        </view>
      </view>
      <view class="view-line"></view>
      <view class="view-button">
        <view>MAC/ID</view>
        <view class="view-button" style="padding: 0">
          <view class="info">{{currentLockInfo.mac}}</view>
        </view>
      </view>
      <view class="view-line"></view>
      <view class="view-button">
        <view>电量</view>
        <view class="view-button" style="padding: 0">
          <view class="info">{{currentLockInfo.electricQuantity}}%</view>
        </view>
      </view>
    </view>
    <view class="view" v-if="currentLockInfo.userType === 110301">
      <view class="view-button">
        <view>开锁时是否需联网</view>
        <view class="view-button" style="padding: 0">
          <up-switch v-model="unlockApp" :size="40" activeColor="#63b8af" :asyncChange="true"
                     @change="changeUnlockApp" :activeValue="1" :inactiveValue="0"></up-switch>
        </view>
      </view>
    </view>
    <view class="button-logout" @click="deleteLock">删除</view>
  </view>
</template>

<script>
import { useBluetoothStore } from '@/stores/bluetooth'
import { useUserStore } from '@/stores/user'
import { mapActions, mapState } from 'pinia'
import { deleteLockRequest } from '@/api/lock'
import { useLockStore } from '@/stores/lock'
import { updateLockSettingRequest } from '@/api/lockSetting'
import { deleteKeyRequest } from '@/api/key'

export default {
  data () {
    return {
      unlockApp: 0
    }
  },
  computed: {
    ...mapState(useUserStore, ['userInfo']),
    ...mapState(useBluetoothStore, ['keyId', 'currentLockInfo'])
  },
  onLoad() {
    this.unlockApp = this.currentLockInfo.lockSetting.appUnlockOnline
  },
  methods: {
    ...mapActions(useBluetoothStore, ['resetDevice', 'updateCurrentLockInfo']),
    ...mapActions(useLockStore, ['getLockList']),
    async changeUnlockApp(value) {
      uni.showLoading({
        title: '更新中'
      })
      const { code, message } = await updateLockSettingRequest({
        lockId: this.currentLockInfo.lockId,
        appUnlockOnline: value
      })
      if(code === 0) {
        this.unlockApp = value
        const data = this.currentLockInfo
        data.lockSetting.appUnlockOnline = value
        this.updateCurrentLockInfo(data)
        uni.hideLoading()
        uni.showToast({
          title: '更新成功',
          icon: 'none'
        })
      } else {
        uni.hideLoading()
        uni.showToast({
          title: message,
          icon: 'none'
        })
      }
    },
    deleteLock() {
      const that = this
      uni.showModal({
        title: '提示',
        content: '确定删除该门锁吗？',
        success: async function (res) {
          if (res.confirm) {
            uni.showLoading({
              title: '删除中'
            })
            if(that.currentLockInfo.userType === 110301) {
              const { code: resetDeviceCode } = await that.resetDevice({
                name: that.currentLockInfo.name,
                authUid: that.userInfo.uid.toString(),
                keyId: that.keyId.toString()
              })
              if(resetDeviceCode === 0) {
                const { code, message } = await deleteLockRequest({
                  lockId: that.currentLockInfo.lockId
                })
                if(code === 0) {
                  uni.hideLoading()
                  that.getLockList({
                    pageNo: 1,
                    pageSize: 50
                  })
                  uni.navigateBack({
                    delta: 2,
                    complete: () => {
                      uni.showToast({
                        title: '删除成功',
                        icon: 'none'
                      })
                    }
                  })
                  uni.navigateBack()
                } else {
                  uni.hideLoading()
                  uni.showToast({
                    title: 'message',
                    icon: 'none'
                  })
                }
              } else {
                uni.hideLoading()
                uni.showToast({
                  title: '删除失败',
                  icon: 'none'
                })
              }
            } else {
              const { code } = await deleteKeyRequest({
                keyId: that.keyId
              })
              if(code === 0) {
                uni.hideLoading()
                that.getLockList({
                  pageNo: 1,
                  pageSize: 50
                })
                uni.navigateBack({
                  delta: 2,
                  complete: () => {
                    uni.showToast({
                      title: '删除成功',
                      icon: 'none'
                    })
                  }
                })
                uni.navigateBack()
              } else {
                uni.hideLoading()
                uni.showToast({
                  title: 'message',
                  icon: 'none'
                })
              }
            }
          }
        }
      })
    }
  }
}
</script>

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

<style lang="scss" scoped>
.button-logout {
  position: absolute;
  border-radius: 46rpx;
  bottom: calc(env(safe-area-inset-bottom) + 30rpx);
  width: 600rpx;
  height: 80rpx;
  line-height: 80rpx;
  text-align: center;
  margin-left: 75rpx;
  background: #ec433c;
  color: #ffffff;
  font-size: 40rpx;
  font-weight: bold;
}

.view {
  margin-top: 32rpx;
  border-radius: 32rpx;
  width: 710rpx;
  margin-left: 20rpx;
  background: #FFFFFF;
}

.view-button {
  padding: 0 40rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #292826;
  font-size: 32rpx;
  font-weight: bold;
  line-height: 80rpx;
}

.view-line {
  width: 100%;
  height: 3rpx;
  background: #EBEBEB;
}
</style>
