<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" :request-api="getTableList" :data-callback="dataCallback">
      <template #tableHeader>
        <el-button
          type="primary"
          :icon="Download"
          :loading="exportLoading"
          :disabled="!isAdmin || !hasExportableData"
          @click="handleExport"
        >
          导出
        </el-button>
      </template>
      <template #operation="scope">
        <el-button type="primary" link :icon="View" @click="handleDetail(scope.row)"> 查看详情 </el-button>
        <el-button
          type="warning"
          link
          :icon="Number(scope.row.isRecommended) === 1 ? StarFilled : Star"
          :disabled="!isAdmin"
          @click="handleRecommend(scope.row)"
        >
          {{ Number(scope.row.isRecommended) === 1 ? "取消推荐" : "设为推荐" }}
        </el-button>
      </template>
    </ProTable>
    <el-drawer v-model="detailVisible" title="律师详情" :size="480" append-to-body>
      <el-descriptions v-if="detailData" :column="1" border>
        <el-descriptions-item v-for="field in detailFields" :key="field.prop" :label="field.label">
          {{ getDetailValue(field) }}
        </el-descriptions-item>
      </el-descriptions>
      <div v-else class="detail-empty">暂无数据</div>
    </el-drawer>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, computed } from "vue";
import { ElMessage } from "element-plus";
import ProTable from "@/components/ProTable/index.vue";
import type { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { getLawyerPage, recommendLawyer, exportLawyerList } from "@/api/modules/lawyer";
import { useDownload } from "@/hooks/useDownload";
import { Download, View, Star, StarFilled } from "@element-plus/icons-vue";
import { useUserStore } from "@/stores/modules/user";

const proTable = ref<ProTableInstance>();
const exportLoading = ref(false);
const tableTotal = ref(0);
const hasExportableData = computed(() => tableTotal.value > 0);
const detailVisible = ref(false);
const detailData = ref<Record<string, any> | null>(null);
const userStore = useUserStore();
const isAdmin = computed(() => userStore.userInfo?.name === "admin");
const loginFirmId = computed(() => userStore.userInfo?.roleId || "");

interface DetailField {
  label: string;
  prop: string;
  formatter?: (row: Record<string, any>) => string;
}

const statusText = (status?: number | string) => {
  return Number(status) === 1 ? "接单中" : "暂停接单";
};

const recommendText = (value?: number | string) => {
  return Number(value) === 1 ? "已推荐" : "未推荐";
};

const detailFields: DetailField[] = [
  { label: "律师姓名", prop: "name" },
  { label: "联系电话", prop: "phone" },
  { label: "法律场景", prop: "firstLevelScenario" },
  {
    label: "接单状态",
    prop: "status",
    formatter: row => statusText(row.status)
  },
  { label: "收款账号", prop: "paymentNum" },
  { label: "所属律所", prop: "firmName" },
  {
    label: "推荐状态",
    prop: "isRecommended",
    formatter: row => recommendText(row.isRecommended)
  }
];

const getDetailValue = (field: DetailField) => {
  const data = detailData.value;
  if (!data) return "-";
  if (field.formatter) return field.formatter(data);
  const value = data[field.prop];
  return value === undefined || value === null || value === "" ? "-" : value;
};

const ensureAdmin = () => {
  if (isAdmin.value) return true;
  ElMessage.warning("仅管理员可使用该功能");
  return false;
};

const normalizeRequestParams = (params: Record<string, any> = {}) => {
  const newParams: Record<string, any> = { ...params };
  if (Reflect.has(newParams, "pageNum")) {
    newParams.page = newParams.pageNum;
    delete newParams.pageNum;
  }
  if (Reflect.has(newParams, "pageSize")) {
    newParams.size = newParams.pageSize;
    delete newParams.pageSize;
  }
  if (Array.isArray(newParams.time) && newParams.time.length === 2) {
    [newParams.startTime, newParams.endTime] = newParams.time;
  }
  delete newParams.time;
  if (!isAdmin.value && loginFirmId.value) {
    newParams.firmId = loginFirmId.value;
  }
  return newParams;
};

const columns = reactive<ColumnProps<any>[]>([
  { label: "序号", type: "index", width: 60, align: "center" },
  { label: "律师姓名", prop: "name", width: 140, search: { el: "input", props: { placeholder: "请输入律师姓名" } } },
  { label: "联系电话", prop: "phone", width: 140, search: { el: "input", props: { placeholder: "请输入联系电话" } } },
  {
    label: "从业时间",
    prop: "time",
    isShow: false,
    search: {
      el: "date-picker",
      props: {
        type: "datetimerange",
        valueFormat: "YYYY-MM-DD",
        format: "YYYY-MM-DD",
        startPlaceholder: "开始日期",
        endPlaceholder: "结束日期",
        rangeSeparator: "至",
        unlinkPanels: true
      }
    }
  },
  // { label: "专业领域", prop: "expertiseAreaInfo" },
  { label: "法律场景", prop: "scenarioNames" },
  {
    label: "接单状态",
    prop: "status",
    width: 120,
    tag: true,
    enum: [
      { label: "接单中", value: 1, tagType: "success" },
      { label: "暂停接单", value: 0, tagType: "info" }
    ],
    fieldNames: { label: "label", value: "value" }
  },
  {
    label: "推荐状态",
    prop: "isRecommended",
    width: 120,
    tag: true,
    enum: [
      { label: "已推荐", value: 1, tagType: "success" },
      { label: "未推荐", value: 0, tagType: "info" }
    ],
    fieldNames: { label: "label", value: "value" }
  },
  { label: "收款账号", prop: "paymentNum", width: 220 },
  { label: "所属律所", prop: "firmName", width: 220 },
  { label: "操作", prop: "operation", width: 240, fixed: "right" }
]);

const getTableList = async (params: any) => {
  return getLawyerPage(normalizeRequestParams(params));
};

const dataCallback = (data: any) => {
  const total = Number(data.total) || 0;
  tableTotal.value = total;
  return {
    list: data.records,
    total
  };
};

const refreshTable = () => {
  proTable.value?.getTableList();
};

const collectTableParams = () => {
  const table = proTable.value;
  if (!table) return {};
  const params: Record<string, any> = { ...(table.searchParam || {}) };
  if (table.pageable) {
    params.pageNum = table.pageable.pageNum;
    params.pageSize = table.pageable.pageSize;
  }
  return params;
};

const handleExport = async () => {
  if (!ensureAdmin() || !hasExportableData.value) return;
  exportLoading.value = true;
  try {
    const params = normalizeRequestParams(collectTableParams());
    await useDownload(exportLawyerList, "律师列表", params);
  } finally {
    exportLoading.value = false;
  }
};

const handleDetail = (row: any) => {
  detailData.value = { ...row };
  detailVisible.value = true;
};

const handleRecommend = async (row: any) => {
  if (!ensureAdmin()) return;
  const targetId = row?.id;
  if (!targetId) {
    ElMessage.error("未获取到律师ID");
    return;
  }
  const current = Number(row?.isRecommended) === 1 ? 1 : 0;
  const nextStatus = current === 1 ? 0 : 1;
  try {
    await recommendLawyer({ lawyerId: targetId, isRecommended: nextStatus });
    ElMessage.success(nextStatus === 1 ? "设置推荐成功" : "取消推荐成功");
    refreshTable();
  } catch (error) {
    console.error("更新推荐状态失败", error);
  }
};

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

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