<template>
  <div class="host-setting gradient-background-circles">
    <!-- 使用 GeneralHeader 替换 MobileHeader -->
    <GeneralHeader
      :showBack="true"
      :showLanguageList="true"
      :title="t('host_setting')"
      color="black"
      style="box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); z-index: 999"
    />

    <div style="padding: 16px; position: relative; z-index: 2">
      <!-- My Agency 部分 -->
      <div style="margin-bottom: 20px">
        <div style="display: flex; align-items: center; margin-bottom: 12px">
          <div style="font-size: 1.4em; font-weight: 600">{{ t('my_agency') }}</div>
          <img
            src="/src/assets/icon/identity/agency.png"
            alt=""
            height="25px"
            style="display: block; flex-shrink: 0; margin-left: 8px"
          />
        </div>

        <div
          style="
            background-color: white;
            padding: 16px;
            border-radius: 12px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
            display: flex;
            justify-content: space-between;
            gap: 4px;
          "
        >
          <!-- 用户信息 -->
          <div style="display: flex; align-items: center; min-width: 0">
            <img
              :src="userInfo.userAvatar || ''"
              alt=""
              width="50px"
              style="aspect-ratio: 1/1; border-radius: 50%; object-fit: cover"
              @error="handleAvatarImageError"
            />
            <div style="flex: 1; min-width: 0; margin-left: 12px">
              <div
                style="
                  margin-bottom: 4px;
                  font-weight: 600;
                  white-space: nowrap;
                  overflow: hidden;
                  text-overflow: ellipsis;
                "
              >
                {{ userInfo.userNickname || '' }}
              </div>
              <div style="font-size: 0.9em; color: rgba(0, 0, 0, 0.4)">
                {{ t('user_id_prefix') }} {{ userInfo.account }}
              </div>
            </div>
          </div>
          <!-- 按钮 -->
          <div style="display: flex; align-items: center">
            <div class="leave-btn" @click="maskLayerShow = true">{{ t('leave') }}</div>
          </div>
        </div>
      </div>
    </div>

    <maskLayer :maskLayerShow="maskLayerShow" @click="maskLayerShow = false">
      <div style="min-height: 100vh; display: flex; justify-content: center; align-items: center">
        <div
          style="position: relative; margin: 20% 5%; background-color: white; border-radius: 12px"
          @click.stop
        >
          <div style="padding: 20px 20px 0 20px">
            <div
              style="margin: 0; font-size: 18px; font-weight: 600; color: #333; text-align: center"
            >
              {{ t('tips') }}
            </div>
          </div>

          <div style="padding: 16px 20px; text-align: center">
            <div style="margin: 0 0 8px 0; font-size: 14px; color: #666; line-height: 1.5">
              {{ t('leave_agent_tips') }}
            </div>
          </div>

          <div style="display: flex; gap: 12px; padding: 0 20px 20px 20px">
            <button class="cancel-btn" @click="maskLayerShow = false">{{ t('cancel') }}</button>
            <button class="confirm-btn" @click="confirmLeave">{{ t('confirm') }}</button>
          </div>
        </div>
      </div>
    </maskLayer>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { setDocumentDirection } from '@/locales/i18n'
import { getTeamId } from '@/utils/userStore.js'
import { handleAvatarImageError } from '@/utils/imageHandler.js'

import { getMyAgency, leaveAgency } from '@/api/host'

import GeneralHeader from '@/components/GeneralHeader.vue'
import maskLayer from '@/components/MaskLayer.vue'

const { t, locale } = useI18n()
const maskLayerShow = ref(false)

// 监听语言变化并设置文档方向
locale.value && setDocumentDirection(locale.value)

// 用户信息
const userInfo = ref({})

// 确定离开
const confirmLeave = async () => {
  console.log('确定离开')
  const resLeave = await leaveAgency({ reason: 'QUIT', teamId: getTeamId() })
  if (resLeave.status) {
    // 可以添加成功离开后的处理逻辑
  }
  maskLayerShow.value = false
}

const getMyAgencyInfo = async () => {
  const resAgencyInfo = await getMyAgency()
  if (resAgencyInfo.status && resAgencyInfo.body) {
    userInfo.value = resAgencyInfo.body
  }
}

onMounted(() => {
  getMyAgencyInfo()
})
</script>

<style scoped>
* {
  color: rgba(0, 0, 0, 0.8);
}

.leave-btn {
  background-color: #fff;
  border: 1px solid #e6e6e6;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 0.9em;
  font-weight: 500;
  transition: background-color 0.2s;
  cursor: pointer;
}

.leave-btn:active {
  background-color: #00000010;
}

.cancel-btn,
.confirm-btn {
  flex: 1;
  padding: 12px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s;
}

.cancel-btn {
  border: 1px solid #e6e6e6;
  background-color: #fff;
  color: rgba(0, 0, 0, 0.4);
}

.confirm-btn {
  color: rgba(255, 255, 255, 0.8);
  background: linear-gradient(
    152deg,
    rgba(198, 112, 255, 0.6) 7.01%,
    rgba(119, 38, 255, 0.6) 92.99%
  );
}

.cancel-btn:active,
.confirm-btn:active {
  transform: scale(0.98);
}

@media screen and (max-width: 360px) {
  * {
    font-size: 10px;
  }
}

@media screen and (min-width: 360px) {
  * {
    font-size: 12px;
  }
}

@media screen and (min-width: 768px) {
  * {
    font-size: 24px;
  }
}

/* RTL支持 */
[dir='rtl'] .leave-btn {
  margin-right: 0;
  margin-left: 8px;
}

[dir='rtl'] .agency-icon {
  margin-left: 0;
  margin-right: 8px;
}
</style>
