<template>
  <div class="contract-management-page">
    <!-- Tab切换 -->
    <div class="tab-container">
      <div class="tab-item" :class="{ active: activeTab === 'unsigned' }" @click="switchTab('unsigned')">
        <span>未签署</span>
      </div>
      <div class="tab-item" :class="{ active: activeTab === 'signed' }" @click="switchTab('signed')">
        <span>已签署</span>
      </div>
    </div>

    <!-- 表格 -->
    <div class="table-container">
      <ProTable
        ref="proTable"
        :columns="columns"
        :request-api="getTableList"
        :init-param="initParam"
        :data-callback="dataCallback"
        :refresh-reset-page="true"
      >
        <template #contractName="scope">
          <div class="contract-row">
            <div class="contract-indicator" />
            <span>{{ scope.row.file_name || "—" }}</span>
          </div>
        </template>
        <template #operation="scope">
          <template v-if="activeTab === 'unsigned'">
            <el-button type="primary" link @click="handleViewDetail(scope.row, false)"> 签署 </el-button>
          </template>
          <template v-else>
            <el-button type="primary" link @click="handleViewDetail(scope.row, true)"> 查看 </el-button>
            <el-button type="primary" link @click="handleDownload(scope.row)"> 下载 </el-button>
          </template>
        </template>
      </ProTable>
    </div>
  </div>
</template>

<script setup lang="tsx" name="contractManagement">
import { reactive, ref, computed } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import ProTable from "@/components/ProTable/index.vue";
import { ColumnProps, ProTableInstance } from "@/components/ProTable/interface";
import { getContractList, getContractSignUrl } from "@/api/modules/contractManagement";
import { localGet } from "@/utils";

const router = useRouter();
const proTable = ref<ProTableInstance>();

// 当前选中的tab
const activeTab = ref<"unsigned" | "signed">("unsigned");

// 初始化请求参数
const initParam = reactive({
  storeId: localGet("createdId") || ""
});

// 表格列配置
const columns = reactive<ColumnProps<any>[]>([
  {
    prop: "file_name",
    label: "合同名称",
    minWidth: 200,
    search: {
      el: "input",
      props: { placeholder: "请输入" }
    }
  },
  {
    prop: "store_name",
    label: "对方单位",
    minWidth: 250
  },
  {
    prop: "effective_time",
    label: "有效期",
    width: 300,
    render: (scope: any) => {
      if (scope.row.effective_time && scope.row.expiry_time) {
        const start = formatDate(scope.row.effective_time);
        const end = formatDate(scope.row.expiry_time);
        return `${start}-${end}`;
      }
      return "—";
    }
  },
  { prop: "operation", label: "操作", fixed: "right", width: 200 }
]);

// 格式化日期
const formatDate = (dateStr: string) => {
  if (!dateStr) return "";
  const date = new Date(dateStr);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");
  return `${year}/${month}/${day}`;
};

// 数据回调处理
const dataCallback = (data: any) => {
  // 根据实际接口返回格式处理
  if (data) {
    return {
      list: data.items || [],
      total: data.total || 0
    };
  }

  return {
    list: [],
    total: 0
  };
};

// 获取表格列表
const getTableList = async (params: any) => {
  const storeId = params.storeId || localGet("createdId");
  // 根据选中的tab设置状态参数
  const status = activeTab.value === "signed" ? 1 : 0; // 1-已签署, 0-未签署

  // 构建请求参数
  const requestParams: any = {
    page: params.pageNum || params.page || 1,
    page_size: params.pageSize || params.size || 10,
    status: status
  };

  // 添加搜索条件（从 ProTable 的 params 中获取）
  if (params.file_name) {
    requestParams.file_name = params.file_name;
  }

  const res = await getContractList(storeId, requestParams);
  // 包装返回格式以匹配 useTable 的期望格式 { data: ... }
  return { data: res };
};

// 切换tab
const switchTab = (tab: "unsigned" | "signed") => {
  if (activeTab.value === tab) return;
  activeTab.value = tab;
  // 刷新表格
  proTable.value?.reset();
};

// 查看合同详情（统一入口，未签署和已签署都跳转到详情页）
const handleViewDetail = (row: any, isSigned: boolean) => {
  const signFlowId = row.sign_flow_id || row.id;
  const contactPhone = row.contact_phone || "";

  if (!signFlowId) {
    ElMessage.error("缺少合同ID");
    return;
  }

  // 跳转到合同详情页
  router.push({
    path: "/contractManagement/detail",
    query: {
      signFlowId: signFlowId,
      contactPhone: contactPhone,
      signed: isSigned ? "true" : "false"
    }
  });
};

// 下载合同
const handleDownload = async (row: any) => {
  try {
    // 如果合同有文件URL，直接下载
    if (row.file_url || row.download_url || row.url) {
      const downloadUrl = row.file_url || row.download_url || row.url;
      // 创建临时链接下载
      const link = document.createElement("a");
      link.href = downloadUrl;
      link.download = row.file_name || "合同文件";
      link.target = "_blank";
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      ElMessage.success("下载开始");
      return;
    }

    // 如果没有直接的文件URL，尝试通过接口获取下载链接
    const res: any = await getContractSignUrl({
      sign_flow_id: row.sign_flow_id || row.id,
      contact_phone: row.contact_phone || ""
    });

    if (res && res.code === 200 && res.data) {
      // 如果返回的是文件URL，直接下载
      const link = document.createElement("a");
      link.href = res.data;
      link.download = row.file_name || "合同文件";
      link.target = "_blank";
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      ElMessage.success("下载开始");
    } else {
      ElMessage.error(res?.msg || "获取下载链接失败");
    }
  } catch (error: any) {
    console.error("下载合同失败:", error);
    ElMessage.error(error?.message || "下载合同失败，请重试");
  }
};
</script>

<style lang="scss" scoped>
.contract-management-page {
  min-height: 100%;
  padding: 20px;
  background: #ffffff;
  .tab-container {
    display: flex;
    gap: 0;
    margin-bottom: 20px;
    border-bottom: 1px solid #e4e7ed;
    .tab-item {
      position: relative;
      padding: 12px 24px;
      font-size: 14px;
      color: #666666;
      cursor: pointer;
      transition: all 0.3s;
      &:hover {
        color: #409eff;
      }
      &.active {
        font-weight: bold;
        color: #409eff;
        &::after {
          position: absolute;
          right: 0;
          bottom: -1px;
          left: 0;
          height: 2px;
          content: "";
          background: #409eff;
        }
      }
    }
  }
  .table-container {
    min-height: 400px;
    :deep(.el-table) {
      min-height: 300px;

      // 确保表格体有最小高度
      .el-table__body-wrapper {
        min-height: 300px;
      }

      // 确保空状态时表格有高度
      .el-table__empty-block {
        position: relative !important;
        display: flex !important;
        align-items: center;
        justify-content: center;
        min-height: 300px;

        // 确保空状态内容正常显示
        .table-empty {
          display: flex;
          flex-direction: column;
          gap: 12px;
          align-items: center;
          justify-content: center;
          line-height: normal;
          img {
            display: inline-flex !important;
            width: 180px;
            height: 180px;
          }
        }
      }
      .contract-row {
        position: relative;
        display: flex;
        align-items: center;
        padding-left: 8px;
        .contract-indicator {
          position: absolute;
          top: 0;
          bottom: 0;
          left: 0;
          width: 4px;
          background: linear-gradient(180deg, #ff6b35 0%, #ff8c5a 100%);
          border-radius: 2px;
        }
        span {
          margin-left: 12px;
        }
      }
    }
  }
}
</style>
