<template>
  <view>
    <view v-if="info">
      <view class="item">
        <view class="item-title">人脸号</view>
        <view class="item-content">{{ info.faceNumber }}</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 max-w-550 break-all">{{ info.faceName }}</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.faceType === 1" class="mr-2">永久</view>
          <view v-else-if="info.faceType === 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.faceType === 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.faceType === 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
          @click="disabled"
          :checked="info.faceRight"
          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.faceName"
        @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 { getFaceRequest, updateFaceRequest } from '@/api/face'

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

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

  const info = ref(null)

  const modalInput = 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 updateFaceRequest({
      lockId: $bluetooth.currentLockInfo.lockId,
      faceId: info.value.faceId,
      faceType: info.value.faceType,
      faceName: name,
      changeType: 1
    })
    pending.value = false
    if (code === 0) {
      modalInput.value.close()
      eventChannel.emit('refresherList', {})
      info.value.faceName = name
      uni.showToast({
        title: '更新成功',
        icon: 'none'
      })
    } else {
      uni.showToast({
        title: message,
        icon: 'none'
      })
    }
  }

  const updateTime = () => {
    $basic.routeJump({
      name: info.value.faceType === 1 || info.value.faceType === 2 ? 'temporaryDate' : 'cycleDate',
      params: {
        info: JSON.stringify({ ...info.value, type: 'face' })
      },
      events: {
        refresh() {
          eventChannel.emit('refresherList', {})
          getFaceRequest({
            lockId: $bluetooth.currentLockInfo.lockId,
            faceId: info.value.faceId
          }).then(res => {
            info.value = res.data
          })
        }
      }
    })
  }

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

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

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

  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>
