<template>
  <view class="break-all">
    <view class="py-3 px-4 bg-white flex items-center justify-between text-base">
      <view>锁编号</view>
      <view>{{ $bluetooth.currentLockSetting.lockBasicInfo.lockName }}</view>
    </view>
    <view class="py-3 px-4 bg-white flex items-center justify-between text-base mt-4rpx">
      <view>MAC/ID</view>
      <view
        >{{ $bluetooth.currentLockSetting.lockBasicInfo.mac }}/{{
          $bluetooth.currentLockSetting.lockBasicInfo.lockId
        }}</view
      >
    </view>
    <view
      class="py-3 px-4 bg-white flex items-center justify-between text-base mt-2.5"
      @click="toJump('syncElec')"
    >
      <view>电量</view>
      <view class="flex items-center">
        <view class="mr-2"
          >{{ $bluetooth.currentLockSetting.lockBasicInfo.electricQuantity }}%</view
        >
        <up-icon name="arrow-right"></up-icon>
      </view>
    </view>
    <view class="py-3 px-4 bg-white flex items-center justify-between text-base mt-4rpx">
      <view>有效期</view>
      <view>
        <view v-if="$bluetooth.currentLockSetting.lockBasicInfo.keyType === 4"
          >{{ timeFormat($bluetooth.currentLockSetting.lockBasicInfo.startDate, 'yyyy-mm-dd') }}～{{
            timeFormat($bluetooth.currentLockSetting.lockBasicInfo.endDate, 'yyyy-mm-dd')
          }}</view
        >
        <view v-else-if="$bluetooth.currentLockSetting.lockBasicInfo.keyType === 2">
          <view>{{
            timeFormat($bluetooth.currentLockSetting.lockBasicInfo.startDate, 'yyyy-mm-dd h:M')
          }}</view>
          <view>{{
            timeFormat($bluetooth.currentLockSetting.lockBasicInfo.endDate, 'yyyy-mm-dd h:M')
          }}</view>
        </view>
        <view v-else-if="$bluetooth.currentLockSetting.lockBasicInfo.keyType === 3">单次</view>
        <view v-else>永久</view>
      </view>
    </view>
    <view
      class="py-3 px-4 bg-white flex items-center justify-between text-base mt-4rpx"
      v-if="$bluetooth.currentLockSetting.lockBasicInfo.keyType === 4"
    >
      <view>有效日</view>
      <view>
        <view>{{
          $lock.convertWeekDaysToChineseString($bluetooth.currentLockSetting.lockBasicInfo.weekDays)
        }}</view>
      </view>
    </view>
    <view
      class="py-3 px-4 bg-white flex items-center justify-between text-base mt-4rpx"
      v-if="$bluetooth.currentLockSetting.lockBasicInfo.keyType === 4"
    >
      <view>有效时间</view>
      <view>
        <view
          >{{ timeFormat($bluetooth.currentLockSetting.lockBasicInfo.startDate, 'h:M') }}～{{
            timeFormat($bluetooth.currentLockSetting.lockBasicInfo.endDate, 'h:M')
          }}</view
        >
      </view>
    </view>
    <view class="mt-2.5"></view>
    <view
      v-if="$bluetooth.currentLockSetting.lockBasicInfo.keyRight === 1"
      class="py-3 px-4 bg-white flex items-center justify-between text-base mb-4rpx"
      @click="() => $refs.modalInput.open()"
    >
      <view>名称</view>
      <view class="flex items-center">
        <view class="mr-2 max-w-400">{{
          $bluetooth.currentLockSetting.lockBasicInfo.lockAlias
        }}</view>
        <up-icon name="arrow-right"></up-icon>
      </view>
    </view>
    <view
      class="py-3 px-4 bg-white flex items-center justify-between text-base mb-4rpx"
      @click="toJump('selectGroup')"
    >
      <view>锁分组</view>
      <view class="flex items-center">
        <view class="mr-2 max-w-400">{{
          $bluetooth.currentLockSetting.lockBasicInfo.groupName
        }}</view>
        <up-icon name="arrow-right"></up-icon>
      </view>
    </view>
    <view
      v-if="$bluetooth.currentLockSetting.lockBasicInfo.isLockOwner === 1"
      class="py-3 px-4 bg-white flex items-center justify-between text-base mb-4rpx"
      @click="() => $refs.modalPassword.open()"
    >
      <view>管理员开锁密码</view>
      <view class="flex items-center">
        <view class="mr-2">{{ $bluetooth.currentLockSetting.lockBasicInfo.adminPwd }}</view>
        <up-icon name="arrow-right"></up-icon>
      </view>
    </view>
    <view class="py-3 px-4 bg-white flex items-center justify-between text-base">
      <view>位置信息</view>
      <view class="flex items-center max-w-400">
        <view>{{ $bluetooth.currentLockSetting.lockBasicInfo.address }}</view>
      </view>
    </view>
    <ModalInput
      ref="modalInput"
      title="请输入名称"
      :autoClose="false"
      placeholder="请输入名称"
      :value="$bluetooth.currentLockSetting.lockBasicInfo.lockAlias"
      @confirm="changeName"
    />
    <ModalInput
      ref="modalPassword"
      title="修改密码"
      :autoClose="false"
      :maxlength="9"
      type="number"
      placeholder="密码为6-9位数字"
      :value="$bluetooth.currentLockSetting.lockBasicInfo.adminPwd"
      @confirm="changePassword"
    />
  </view>
</template>

<script setup>
  import { timeFormat } from 'uview-plus'
  import { ref } from 'vue'
  import test from 'uview-plus/libs/function/test'
  import { useBluetoothStore } from '@/stores/bluetooth'
  import { useLockStore } from '@/stores/lock'
  import { updateAdminPasswordRequest, updateLockNameRequest } from '@/api/setting'
  import { useBasicStore } from '@/stores/basic'
  import { useUserStore } from '@/stores/user'
  import { checkPasswordRequest } from '@/api/keyboardPwd'

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

  const modalInput = ref(null)
  const modalPassword = ref(null)
  const pending = ref(false)

  const changeName = async name => {
    if (!name) {
      uni.showToast({
        title: '请输入名称',
        icon: 'none'
      })
      return
    }
    if (pending.value) return
    pending.value = true
    const { code, message } = await updateLockNameRequest({
      lockId: $bluetooth.currentLockInfo.lockId,
      lockName: name
    })
    pending.value = false
    if (code === 0) {
      modalInput.value.close()
      $lock.updateLockSearch({
        ...$lock.lockSearch,
        pageNo: 1
      })
      $lock.getLockList($lock.lockSearch)
      $bluetooth.updateCurrentLockSetting({
        ...$bluetooth.currentLockSetting,
        lockBasicInfo: {
          ...$bluetooth.currentLockSetting.lockBasicInfo,
          lockAlias: name
        }
      })
      $bluetooth.updateCurrentLockInfo({
        ...$bluetooth.currentLockInfo,
        lockAlias: name
      })
      uni.showToast({
        title: '更新成功',
        icon: 'none'
      })
    } else {
      uni.showToast({
        title: message,
        icon: 'none'
      })
    }
  }

  const changePassword = async password => {
    if (!test.rangeLength(password, [6, 9])) {
      uni.showToast({
        title: '密码为6-9位纯数字',
        icon: 'none'
      })
      return
    }
    const netWork = await $basic.getNetworkType()
    if (!netWork) {
      return
    }

    const { code: checkCode, message } = await checkPasswordRequest({
      lockId: $bluetooth.currentLockInfo.lockId,
      keyboardPwd: password
    })

    if (checkCode !== 0) {
      uni.showToast({
        title: message,
        icon: 'none'
      })
      return
    }

    if (pending.value) return
    pending.value = true

    uni.showLoading({
      title: '更新中'
    })

    const params = {
      keyId: $bluetooth.keyId.toString(),
      uid: $user.userInfo.uid.toString(),
      adminPwd: password,
      userCountLimit: 0xffff,
      startDate: Math.floor($bluetooth.currentLockSetting.lockBasicInfo.startDate / 1000),
      endDate: Math.floor($bluetooth.currentLockSetting.lockBasicInfo.endDate / 1000)
    }
    const { code } = await $bluetooth.updateAdminPassword(params)

    if (code === 0) {
      const { code, message } = await updateAdminPasswordRequest({
        password,
        lockId: $bluetooth.currentLockInfo.lockId,
        passwordType: '1'
      })
      uni.hideLoading()
      pending.value = false
      if (code === 0) {
        modalPassword.value.close()
        $bluetooth.updateCurrentLockSetting({
          ...$bluetooth.currentLockSetting,
          lockBasicInfo: {
            ...$bluetooth.currentLockSetting.lockBasicInfo,
            adminPwd: password
          }
        })
        uni.showToast({
          title: '更新成功',
          icon: 'none'
        })
      } else {
        uni.showToast({
          title: message,
          icon: 'none'
        })
      }
    } else if (code === -21) {
      uni.hideLoading()
      pending.value = false
    } else {
      uni.hideLoading()
      pending.value = false
      uni.showToast({
        title: '更新失败，请保持在锁附近',
        icon: 'none'
      })
    }
  }

  const toJump = name => {
    $basic.routeJump({
      name
    })
  }
</script>

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