<script setup lang="ts">
import { ref, onMounted, watch } from "vue";
import { useRoute } from "vue-router";
import { type Ab98UserDetailDTO, getAb98UserDetailApi } from "@/api/ab98/user";
import { getOrderListApi, type OrderDTO } from "@/api/shop/order";
import { Ab98UserTagDTO, addAb98UserTagApi, deleteAb98UserTagConfirmApi, getAb98UserTagListApi } from "@/api/ab98/tag";
import { ElMessage, ElMessageBox } from "element-plus";
import { PureTableBar } from "@/components/RePureTableBar";

defineOptions({
  name: "Ab98UserDetail"
});

const route = useRoute();
const userInfo = ref<Ab98UserDetailDTO>({});
const loading = ref(false);
const tags = ref<Ab98UserTagDTO[]>([]);
const tagsLoading = ref(false);
const showAddTagDialog = ref(false);
const addTagForm = ref({
  tagName: ''
});

// 基础信息
const basicInfo = ref({
  registerTime: "2023-01-15",
  lastLogin: "2023-06-20",
  loginCount: 42,
  device: "iPhone 13"
});

// 订单记录
const orderRecords = ref<OrderDTO[]>([]);
const pagination = ref({
  pageSize: 5,
  currentPage: 1,
  total: 0
});
const orderLoading = ref(false);
const activeTab = ref('basic');

async function fetchOrders() {
  try {
    orderLoading.value = true;
    const { data } = await getOrderListApi({
      openid: userInfo.value.openid,
      pageSize: pagination.value.pageSize,
      pageNum: pagination.value.currentPage
    });
    orderRecords.value = data.rows;
    pagination.value.total = data.total;
  } finally {
    orderLoading.value = false;
  }
}

watch(activeTab, (newVal) => {
  if (newVal === 'orders') {
    fetchOrders();
  }
});

async function fetchUserDetail() {
  try {
    loading.value = true;
    const userId = route.query.id;
    const { data } = await getAb98UserDetailApi(Number(userId));
    userInfo.value = data;
    await fetchUserTags();
  } finally {
    loading.value = false;
  }
}

async function fetchUserTags() {
  try {
    tagsLoading.value = true;
    const { data } = await getAb98UserTagListApi({
      ab98UserId: userInfo.value.ab98UserId,
      pageSize: 6,
      pageNum: 1
    });
    tags.value = data.rows;
  } finally {
    tagsLoading.value = false;
  }
}

async function handleAddTag() {
  try {
    tagsLoading.value = true;
    await addAb98UserTagApi({
      ab98UserId: userInfo.value.ab98UserId,
      tagName: addTagForm.value.tagName
    });
    showAddTagDialog.value = false;
    addTagForm.value.tagName = '';
    await fetchUserTags();
    ElMessage.success('标签添加成功');
  } catch (error) {
    ElMessage.error('标签添加失败');
  } finally {
    tagsLoading.value = false;
  }
}

async function handleDeleteTag(tagId: number) {
  try {
    await ElMessageBox.confirm('确定要删除这个标签吗?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    });
    tagsLoading.value = true;
    await deleteAb98UserTagConfirmApi(tagId);
    await fetchUserTags();
    ElMessage.success('标签删除成功');
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('标签删除失败');
    }
  } finally {
    tagsLoading.value = false;
  }
}

onMounted(() => {
  fetchUserDetail();
});
</script>

<template>
  <div class="detail-container">
    <div class="flex-container">
      <el-card class="user-info-card">
        <div class="user-header">
          <el-avatar :size="100" :src="userInfo.faceImg" fit="cover" shape="square">
            <svg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
              <circle cx="50" cy="50" r="48" fill="#f5f5f5" stroke="#e0e0e0" stroke-width="1" />
              <circle cx="50" cy="40" r="12" fill="#9e9e9e" />
              <rect x="40" y="52" width="20" height="30" rx="2" fill="#9e9e9e" />
            </svg>
          </el-avatar>
          <div class="user-name">{{ userInfo.name }}</div>
        </div>

        <el-divider />

        <el-descriptions class="user-details" :column="1" border>
          <el-descriptions-item label="性别">{{ userInfo.sex }}</el-descriptions-item>
          <el-descriptions-item label="手机号">{{ userInfo.tel }}</el-descriptions-item>
          <el-descriptions-item label="身份证号">{{ userInfo.idnum }}</el-descriptions-item>
          <el-descriptions-item label="住址">{{ userInfo.address }}</el-descriptions-item>
        </el-descriptions>
      </el-card>
      <el-card class="info-card">
        <div class="tab-header">
          <el-tabs type="card" v-model="activeTab">
            <el-tab-pane label="基础信息" name="basic"></el-tab-pane>

            <el-tab-pane label="订单记录" name="orders"></el-tab-pane>
          </el-tabs>
        </div>

        <el-descriptions class="info-details" v-if="activeTab === 'basic'" :column="2" border>
          <el-descriptions-item label="会员ID">{{ userInfo.ab98UserId }}</el-descriptions-item>
          <el-descriptions-item label="微信openid">{{ userInfo.openid }}</el-descriptions-item>
          <el-descriptions-item label="注册时间" :span="2">{{ userInfo.createTime }}</el-descriptions-item>
          <el-descriptions-item label="标签" :span="2">
            <div class="tag-container">
              <div class="user-tags" v-if="tags.length > 0">
                <el-tag v-for="tag in tags" :key="tag.tagId" class="tag-item" closable
                  @close="handleDeleteTag(tag.tagId)">
                  {{ tag.tagName }}
                </el-tag>
              </div>
              <el-button type="primary" size="small" @click="showAddTagDialog = true">添加标签</el-button>
            </div>
          </el-descriptions-item>
        </el-descriptions>



        <div class="info-details" v-if="activeTab === 'orders'">
          <PureTableBar title="订单列表" @refresh="fetchOrders">
            <el-table ref="tableRef" v-loading="orderLoading" :data="orderRecords" row-key="orderId" border>
              <el-table-column label="订单ID" prop="orderId" width="120" />
              <el-table-column label="商品名称" prop="goodsNames" width="180">
                <template #default="{ row }">
                  <span v-if="row.goodsNames">
                    {{ row.goodsNames.split(',').join(', ') }}
                  </span>
                  <span v-else>-</span>
                </template>
              </el-table-column>
              <el-table-column label="商品封面" prop="coverImgs" width="100">
                <template #default="{ row }">
                  <div v-if="row.coverImgs" class="flex gap-2">
                    <el-image v-for="(img, index) in row.coverImgs.split(',')" :key="index" :src="img"
                      :preview-src-list="row.coverImgs.split(',')" :z-index="9999" :preview-teleported="true"
                      :hide-on-click-modal="true" fit="cover" class="rounded" width="60" height="60" />
                  </div>
                  <span v-else>-</span>
                </template>
              </el-table-column>
              <el-table-column label="姓名" prop="name" width="120" />
              <el-table-column label="手机号" prop="mobile" width="120" />
              <el-table-column label="订单金额" prop="totalAmount" width="120">
                <template #default="{ row }">{{ row.totalAmount }}元</template>
              </el-table-column>
              <el-table-column label="订单状态" prop="status" width="120">
                <template #default="{ row }">
                  <el-tag :type="row.status === 2 ? 'success' : row.status === 5 ? 'danger' : 'info'">
                    {{ { 1: '待付款', 2: '已付款', 3: '已发货', 4: '已完成', 5: '已取消' }[row.status] }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column label="支付状态" prop="payStatus" width="120">
                <template #default="{ row }">
                  <el-tag :type="row.payStatus === 2 ? 'success' : 'info'">
                    {{ { 1: '未支付', 2: '已支付', 3: '退款中', 4: '已退款' }[row.payStatus] }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column label="支付方式" prop="paymentMethod" width="120">
                <template #default="{ row }">
                  {{ { wechat: '微信支付', balance: '借呗支付' }[row.paymentMethod] || row.paymentMethod }}
                </template>
              </el-table-column>
              <el-table-column label="支付时间" prop="payTime" width="180">
                <template #default="{ row }">
                  {{ row.payTime ? new Date(row.payTime).toLocaleString() : '-' }}
                </template>
              </el-table-column>
            </el-table>
            <el-pagination v-model:current-page="pagination.currentPage" v-model:page-size="pagination.pageSize"
              :page-sizes="[5, 10, 20, 50]" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"
              @size-change="fetchOrders" @current-change="fetchOrders" />
          </PureTableBar>
        </div>
      </el-card>
    </div>
    <el-dialog v-model="showAddTagDialog" title="添加标签" width="30%">
      <el-form :model="addTagForm" label-width="80px">
        <el-form-item label="标签名称">
          <el-input v-model="addTagForm.tagName" placeholder="请输入标签名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="handleAddTag">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.detail-container {
  display: flex;
  flex-direction: column;
  height: 100%;

  .flex-container {
    display: flex;
    flex: 1;
    gap: 20px;
    min-height: 0;

    .user-info-card {
      width: 20%;
    }

    .info-card {
      width: 80%;
    }
  }

  .user-info-card,
  .shop-info-card {
    height: 100%;
    min-height: 85vh;
  }

  .user-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 20px;

    .user-name {
      margin-top: 15px;
      font-size: 20px;
      font-weight: bold;
    }
  }

  .tab-header {
    margin-bottom: 0px;
  }

  .tag-container {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;

    .user-tags {
      flex: 1;
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
    }

    .el-button {
      width: 80px;
      margin-left: auto;
    }
  }

  .el-button {
    margin-top: 5px;
    background-color: #409eff;
    color: white;
    border: none;
    border-radius: 4px;
    padding: 8px 16px;
    font-size: 14px;
    transition: all 0.3s;

    &:hover {
      background-color: #66b1ff;
      box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
    }

    &:active {
      background-color: #3a8ee6;
    }
  }

  .order-item {
    margin-bottom: 20px;
    padding: 10px;
    background-color: #f9f9f9;
    border-radius: 4px;

    .order-id {
      font-weight: bold;
      margin-bottom: 8px;
    }

    .order-detail {
      display: flex;
      justify-content: space-between;
      font-size: 13px;
      color: #666;
    }
  }

  .detail-item {
    margin: 15px 0;
    font-size: 14px;

    .label {
      color: #606266;
      margin-right: 10px;
    }

    .value {
      color: #303133;
    }
  }
}
</style>