<!--
 * user.vue
 * Copyright (C) 2024 veypi <i@veypi.com>
 * 2024-06-06 16:32
 * Distributed under terms of the MIT license.
 -->
<template>
  <div>
    <h1 class="page-h1">账号设置</h1>
    <div class="flex justify-center pt-10">
      <crud ref="table" editable :keys="keys" :data="[oaer.local()]" @update="newData = $event[0]">
        <template #k_icon="{ value, set }">
          <div class="w-full flex justify-center">
            <uploader class="" @success="set" dir="/usr/icon">
              <img class="rounded-full h-8 w-8" :src="value">
            </uploader>
          </div>
        </template>
        <template #k_pass>
          <div class="vbtn" @click="prompt = true" size='sm' color='secondary'>修改</div>
        </template>
      </crud>
    </div>
    <UModal v-model="prompt" persistent>
      <UCard style="min-width: 350px">
        <template #header>
          <div class="text-h6">请输入新密码</div>
        </template>
        <vinput type='password' class="my-8" v-model="pass[0]">
        </vinput>
        <vinput type='password' class="my-8" v-model="pass[1]">
        </vinput>

        <template #footer class="">
          <div class="flex justify-end gap-8">
            <div class="vbtn bg-vignore w-32" @click="prompt = false">{{ $t('c.cancel') }}</div>
            <div class="vbtn bg-vprimary w-32" @click="reset">{{ $t('c.ok') }}</div>
          </div>
        </template>
      </UCard>
    </UModal>

    <div v-if="newData" class="flex justify-center gap-8 mt-6">
      <div class="vbtn bg-vignore w-32" @click="table.reload">{{ $t('c.cancel') }}</div>
      <div class="vbtn bg-vprimary w-32" @click="save">{{ $t('c.ok') }}</div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import msg from '@veypi/msg';
import oaer from '@veypi/oaer'

let prompt = ref(false)
let table = ref()
const keys = ref<any>([
  { name: 'id', label: 'ID', editable: false },
  { name: 'username', label: '用户名' },
  { name: 'pass', label: '密码' },
  { name: 'nickname', label: '昵称' },
  { name: 'icon', label: 'logo' },
  { name: 'email', label: '邮箱' },
  { name: 'phone', label: '手机号' },
])

const newData = ref()
const save = () => {
  api.user.Patch(oaer.local().id, newData.value)
    .then(e => {
      msg.Info('更新成功')
      Object.assign(oaer.logic().user, newData.value)
      newData.value = null
    }).catch(e => {
      msg.Warn('更新失败 ' + e)
    })
}
let pass = ref(['', ''])
const reset = () => {
  if (pass.value[0] == '' || pass.value[1] == '') {
    msg.Warn('密码不能为空')
    return
  }
  if (pass.value[0] != pass.value[1]) {
    msg.Warn('两次密码不一致')
    return
  }
  prompt.value = false
  // api.user.reset(u.id, pass.value[0]).then((e) => {
  //   msg.Info('密码重置成功')
  // }).catch(e => {
  //   msg.Warn('密码重置失败 ' + e)
  // })
}
</script>

<style scoped></style>
