<template>
  <div class="min-h-screen bg-base-100 p-4 md:p-6">
    <BreadcrumbHeader title="User Details" />
    
    <div class="max-w-3xl mx-auto">
      <div class="card card-bordered bg-base-100 shadow-sm" v-if="user">
        <div class="card-body space-y-5">
          <div class="flex flex-col md:flex-row items-center gap-6">
            <div class="avatar placeholder" v-if="!user?.avatar_url">
              <div
                class="glass bg-neutral text-neutral-content rounded-full w-16 sm:w-20">
                <span class="text-2xl sm:text-3xl">{{ user?.username?.[0]?.toUpperCase() }}</span>
              </div>
            </div>
            <div class="avatar" v-else>
              <div class="w-16 sm:w-20 rounded-full ring ring-primary ring-offset-base-100 ring-offset-2">
                <img :src="user?.avatar_url" :alt="user?.name" />
              </div>
            </div>

            <div class="flex-grow text-center md:text-left">
              <h2 class="text-2xl font-semibold text-base-content">
                {{ user?.name || user?.username }}
              </h2>
              <div class="flex items-center justify-center md:justify-start gap-2 mt-2">
                <span class="badge border-none px-0">
                  <CircleCheckBig v-if="user.active" class="h-5 w-5 bg-green-300 rounded-full" />
                  <CircleX v-else class="h-5 w-5 bg-rose-300 rounded-full" />
                </span>
                <span class="badge badge-outline" :class="user.role > 0 ? 'badge-warning' : 'badge-success'">{{
                  formatRole(user?.role) }}</span>
              </div>
            </div>

            <div class="flex gap-2 mt-4 md:mt-0">
              <input type="checkbox" class="toggle toggle-md" :class="user.active ? 'toggle-success' : 'toggle-error'"
                v-model="user.active" @change="updateStatus(user)" />
            </div>
          </div>

          <div class="divider mt-1 mb-0"></div>

          <form @submit.prevent="updateUser" class="space-y-4">

            <div class="space-y-3">
              <h3 class="text-base font-medium text-base-content mb-3">
                Basic Information
              </h3>
              <div class="grid grid-cols-1 md:grid-cols-2 gap-x-4 gap-y-3">
                <div class="form-control w-full">
                  <label for="name" class="label pb-1">
                    <span class="label-text text-sm font-medium text-base-content/80">Name</span>
                  </label>
                  <input id="name" type="text" v-model="user.name" placeholder="Full name"
                    class="input input-bordered input-sm w-full" />
                </div>

                <div class="form-control w-full">
                  <label for="username" class="label pb-1">
                    <span class="label-text text-sm font-medium text-base-content/80">
                      Username <span class="text-red-500">*</span>
                    </span>
                  </label>
                  <input id="username" type="text" v-model="user.username" placeholder="Select a username"
                    class="input input-bordered input-sm w-full" required />
                </div>

                <div class="form-control w-full">
                  <label for="email" class="label pb-1">
                    <span class="label-text text-sm font-medium text-base-content/80">Email Address</span>
                  </label>
                  <div class="relative">
                    <input id="email" type="email" v-model="user.email" placeholder="email@example.com"
                      class="input input-bordered input-sm w-full" />
                    <button type="button" @click="toggleEmailVerify" tabindex="-1"
                      class="absolute inset-y-0 right-0 px-3 flex items-center text-base-content/60 hover:text-base-content focus:outline-none rounded-r-md"
                      aria-label="Toggle password visibility">
                      <BadgeCheck v-if="user.email_verified" class="w-4 h-4 bg-green-300 rounded-full" />
                      <div v-else class="tooltip tooltip-top" data-tip="Send verification email">
                        <Send class="w-4 h-4" />
                      </div>
                    </button>
                  </div>

                </div>

                <div class="form-control w-full">
                  <label for="password" class="label pb-1">
                    <span class="label-text text-sm font-medium text-base-content/80">
                      Password <span class="text-xs text-base-content/60">(Leave blank to keep unchanged)</span>
                    </span>
                  </label>
                  <div class="relative">
                    <input id="password" :type="isPasswordVisible ? 'text' : 'password'" v-model="user.password"
                      placeholder="Enter new password" class="input input-bordered input-sm w-full pr-10" />
                    <button type="button" @click="togglePasswordVisibility" tabindex="-1"
                      class="absolute inset-y-0 right-0 px-3 flex items-center text-base-content/60 hover:text-base-content focus:outline-none rounded-r-md"
                      aria-label="Toggle password visibility">
                      <EyeOff v-if="!isPasswordVisible" class="w-4 h-4" />
                      <Eye v-else class="w-4 h-4" />
                    </button>
                  </div>
                </div>
              </div>
            </div>

            <div class="collapse collapse-arrow border border-base-300/30 rounded-md mt-4">
              <input type="checkbox" class="min-h-0 py-2" checked />
              <div class="collapse-title text-base font-medium min-h-0 py-2">
                Advanced Options
              </div>
              <div class="collapse-content">
                <div class="grid grid-cols-1 md:grid-cols-2 gap-x-4 gap-y-3 pt-2">
                  <div class="form-control w-full">
                    <label for="role" class="label pb-1">
                      <span class="label-text text-sm font-medium text-base-content/80">Role</span>
                    </label>
                    <select id="role" v-model="user.role" class="select select-bordered select-sm w-full">
                      <option :value="0">User</option>
                      <option :value="10">Admin</option>
                      <option v-if="user.role>10" :value="20">Root</option>

                    </select>
                  </div>

                  <div class="form-control w-full">
                    <label for="language" class="label pb-1">
                      <span class="label-text text-sm font-medium text-base-content/80">Language</span>
                    </label>
                    <select id="language" v-model="user.language" class="select select-bordered select-sm w-full">
                      <option value="en">English</option>
                      <option value="zh">中文</option>
                    </select>
                  </div>

                  <div class="md:col-span-2 form-control w-full">
                    <label for="quota" class="label pb-1">
                      <span class="label-text text-sm font-medium text-base-content/80">Quota</span>
                    </label>
                    <div class="flex items-center space-x-3">
                      <input id="quota" type="number" v-model="user.quota" placeholder="Enter quota amount"
                        class="input input-bordered input-sm flex-grow" :disabled="user.unlimited_quota" />
                      <label class="label cursor-pointer space-x-2 p-0">
                        <input type="checkbox" v-model="user.unlimited_quota" class="checkbox checkbox-sm" />
                        <span class="label-text text-sm text-base-content/90">Unlimited</span>
                      </label>
                    </div>
                  </div>

                </div>
              </div>
            </div>

            <div class="card-actions justify-end pt-4">
              <button type="submit" class="btn btn-outline btn-success btn-sm">
                Update
              </button>
            </div>
          </form>
        </div>
      </div>
      <div v-else class="max-w-3xl mx-auto">
        <div class="card card-bordered bg-base-100 shadow-sm">
          <div class="card-body">
            <div class="flex justify-center items-center py-10">
              <span class="loading loading-spinner loading-lg"></span>
            </div>
          </div>
        </div>
      </div>

      <!-- Token 显示 -->
      <div class="card card-bordered bg-base-100 shadow-sm mt-6" v-if="user">
        <div class="card-body">
          <h3 class="card-title text-lg">Tokens</h3>
          <div v-if="user.tokens && user.tokens.length" class="overflow-x-auto -mx-6">
            <table class="table table-sm w-full">
              <thead>
                <tr class="text-xs text-base-content/70 uppercase bg-base-200">
                  <th class="px-2 py-3">Token Name</th>
                  <th class="px-2 py-3">Key</th>
                  <th class="px-2 py-3">Expired At</th>
                  <th class="px-2 py-3">Quota</th>
                  <th class="px-2 py-3">Used Quota</th>
                  <th class="text-right px-2 py-3"></th>
                </tr>
              </thead>
              <tbody>
                <tr v-for="token in user.tokens" :key="token.id" class="hover">
                  <td class="font-mono text-xs px-2 py-3">{{ token.name }}</td>
                  <td class="font-mono text-xs px-2 py-3">{{ token.key }}</td>
                  <td class="px-2 py-3">{{ token.expiredAt == -1 ? 'Never' : formatDate(token.expiredAt) }}</td>
                  <td class="px-2 py-3">
                    <template v-if="token.unlimited_quota">
                      <Infinity />
                    </template>
                    <template v-else>{{ token.quota }}</template>
                  </td>
                  <td class="px-2 py-3">{{ token.used_quota }}</td>
                  <td class="text-right px-2 py-3">
                    <button v-if="token.name!=='default'" class="btn btn-ghost btn-xs btn-square text-error" @click="revokeToken(token.id)"
                      aria-label="Revoke token">
                      <TrashIcon class="w-4 h-4" />
                    </button>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>

          <p v-else class="text-center text-base-content/70 py-4">No tokens found</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, inject } from 'vue';
import { useRoute } from 'vue-router';
import { Eye, EyeOff, BadgeCheck, Send, CircleX, CircleCheckBig, TrashIcon, Infinity } from 'lucide-vue-next'; // Ensure lucide-vue-next is installed
import { useUserStore } from '../../stores/user';
import BreadcrumbHeader from '@/components/dashboard/BreadcrumbHeader.vue';

const route = useRoute();
const userStore = useUserStore();
const { setToast } = inject('toast');

const userId = computed(() => route.query.id);

onMounted(async () => {
  if (userId.value) {
    await userStore.getUser(userId.value);
  }
});

const user = computed(() => userStore.user);
const loading = computed(() => userStore.loading); // Access loading state

// 更新状态
const updateStatus = async (user) => {
  try {
    const action = user.active ? 'enable' : 'disable';
    const res = await userStore.userOption(action, [user.id]);
    if (res.data?.code === 200) {
      setToast(`User ${user.id} ${action} Success`, 'success');
    } else {
      setToast(res.data?.error || `用户 ${user.id} ${action} 失败`, 'error');
    }
    await userStore.refreshUser(user.id);
  } catch (error) {
    user.active = !user.active;
    console.error('状态更新失败:', error);
    // setToast(error.response.data?.error || '状态更新失败', 'error');
  }
};

const updateUser = async () => {
  if (!user.value) return;
  try {
    const payload = {
      name: user.value.name,
      username: user.value.username,
      email: user.value.email,
      role: user.value.role,
      language: user.value.language,
      quota: user.value.quota,
      unlimited_quota: user.value.unlimited_quota,
    };
    // Only include password if it's not empty
    if (user.value.password) {
      payload.password = user.value.password;
    }
    const res = await userStore.editUser(userId.value, payload);
    console.log('updateUser',res)
    if (res.data?.code == 200) {
      setToast(`User ${userId.value} updated`, 'success');
    }
    await userStore.refreshUser(userId.value);
  } catch (err) {
    console.error('Error updating user:', err.response?.data?.data?.error);
  }
};



//显示密码
const isPasswordVisible = ref(false);
const togglePasswordVisibility = () => {
  isPasswordVisible.value = !isPasswordVisible.value;
};

// 格式化角色
const formatRole = (role) => {
  switch (true) {
    case role>10:
      return 'Root';
    case role>0:
      return 'Admin';
    default:
      return 'U';
  }
};


// const updateStatus = async (updatedUser) => {
//   try {
//     const response = await userStore.editUser(updatedUser.id, { active: updatedUser.active });
//     if (response.data?.data?.code == 200) {
//       setToast('User ${updatedUser.id} status updated', 'success');
//     }
//   } catch (err) {
//     updatedUser.active = !updatedUser.active;
//     console.log('Error updating user status:', err.response?.data?.data?.error);
//     setToast(err.response?.data?.data?.error, 'error')
//   }
// }

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return null;
  try {
    return new Intl.DateTimeFormat('sv-SE', { dateStyle: 'short', timeStyle: 'short' }).format(new Date(dateString * 1000)); // Multiply by 1000 for JavaScript Date
  } catch (e) {
    console.error("Error formatting date:", e);
    return 'Invalid Date';
  }
};

// 删除token
const revokeToken = (tokenId) => {
  console.log('Revoking token:', tokenId);
};

const toggleEmailVerify = () => {
  if (user.value && !user.value.email_verified) {
    // todo
    return
  }
}

</script>