<template>
  <div class="host-setting gradient-background-circles">
    <!-- 使用 GeneralHeader 替换 MobileHeader -->
    <GeneralHeader
      :title="t('host_setting')"
      :showBack="true"
      :showLanguageList="true"
      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/agency.png"
            alt=""
            height="25px"
            style="display: block; 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;
          "
        >
          <!-- 用户信息 -->
          <div style="display: flex; align-items: center">
            <img
              :src="userInfo.userAvatar || ''"
              alt=""
              width="50px"
              style="aspect-ratio: 1/1; border-radius: 50%; object-fit: cover"
              @error="defaultAvatarUrl"
            />
            <div style="flex: 1; margin-left: 12px">
              <div style="margin-bottom: 4px; font-weight: 600">
                {{ userInfo.userNickname || '' }}
              </div>
              <div style="font-size: 0.9em; color: rgba(0, 0, 0, 0.4)">
                ID: {{ 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 { useI18n } from 'vue-i18n'
import { useRouter } from 'vue-router'
import GeneralHeader from '@/components/GeneralHeader.vue'
import { showError } from '@/utils/toast.js'
import { getMyAgency, leaveAgency } from '@/api/host'
import maskLayer from '@/components/MaskLayer.vue'
import { getTeamId } from '@/utils/userStore.js'
import { setDocumentDirection } from '@/locales/i18n'

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

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

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

const defaultAvatarUrl = (e) => {
  console.log('头像资源出错')
  e.target.onerror = null //防止循环
  e.target.src = new URL('/src/assets/images/WeeklyStar/defaultAvatar.png', import.meta.url).href
}

// 确定离开
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);
  font-family: 'SF Pro Text';
}

.host-setting {
  font-family: -apple-system, BlinkMacSystemFont, sans-serif;
}

.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>
