<template>
    <section class="flex flex-col gap-6 w-96">
        <div class="flex flex-col gap-2 items-center self-center">
            <Avatar :image="user.avatarMedia" shape="circle" class="w-24 h-24" />
            <h2 class="text-xl">{{ user.name }} {{ user.lastName }}</h2>
        </div>
        <ul class="flex flex-col gap-5">
            <li class="flex items-center justify-between">
                <strong>{{ $t('phoneNumber') }}</strong>
                <div class="flex items-center gap-2 w-max">
                    <span>{{ user.phone }}</span>
                </div>
            </li>
            <li class="flex items-center justify-between">
                <strong>{{ $t('id') }}</strong>
                <div class="flex items-center gap-2 w-max">
                    <span>{{ user.id }}</span>
                </div>
            </li>
            <li class="flex items-center justify-between">
                <strong>{{ $t('status') }}</strong>
                <span>{{ user.statusText }}</span>
            </li>
            <li class="flex items-center justify-between">
                <strong>{{ $t('تغییر وضعیت') }}</strong>
                <Tag v-if="user.status === 'DeActivate'" @click="changeToActive" class="cursor-pointer" severity="success" value="فعال سازی"></Tag>
                <Tag v-if="user.status === 'Active'" @click="changeToDeActive" class="cursor-pointer" severity="danger" value="غیرفعال سازی"></Tag>
            </li>
        </ul>
    </section>
</template>

<script setup>
import { inject, reactive } from 'vue';
const dialog = inject('dialogRef')
import { useUsersStore } from '@/stores/users';
const store = useUsersStore()
const user = reactive(Object.assign({}, dialog.value.data.user))
const { toast, t } = inject("service");
const confirmToSales = (value) => {
    dialog.value.data.confirm(user, value)
}

const changeToDeActive = async() =>{
    const change = await store.changeStatus({
        userId: user.id,
        status: 'DeActivate'
    })
    if (change === true) {
  toast.add({
      life: 2000,
      severity: "success",
      summary: t("successful"),
      detail: t("وضعیت ویرایش شد"),
    });
    store.index()
    dialog.value.close()
 }else{
  return toast.add({
      life: 2000,
      severity: "error",
      summary: t("error"),
      detail: t("خطا، محدد تلاش کنید"),
    });
 }
    
}
const changeToActive = async() => {
   const change = await store.changeStatus({
    userId: user.id,
        status: 'Active'
    })
    if (change === true) {
  toast.add({
      life: 2000,
      severity: "success",
      summary: t("successful"),
      detail: t("وضعیت ویرایش شد"),
    });
    store.index()
    dialog.value.close()
 }else{
  return toast.add({
      life: 2000,
      severity: "error",
      summary: t("error"),
      detail: t("خطا، محدد تلاش کنید"),
    });
 }
    
};


</script>

<style lang="scss" scoped></style>