<template>
  <!-- 移动端布局 -->
  <div v-if="isMobile" class="mobile-requisition">
    <!-- 顶部操作栏 -->
    <div class="mobile-header">
      <div class="mobile-header__search">
        <el-input v-model="queryParams.no" placeholder="搜索请购单编号" clearable @keyup.enter="handleQuery" :prefix-icon="Search" />
      </div>
      <div class="mobile-header__actions">
        <el-button :icon="Filter" circle @click="filterVisible = true" />
        <el-button type="primary" :icon="Plus" circle @click="openForm('create')" v-hasPermi="['erp:purchase-requisition:create']" />
      </div>
    </div>

    <div class="mobile-header__quick-filter">
      <div
        class="quick-filter-item"
        :class="{ active: queryParams.status === undefined }"
        @click="handleQuickFilter(undefined)"
      >
        全部订单
      </div>
      <div
        class="quick-filter-item"
        :class="{ active: queryParams.status === 1 }"
        @click="handleQuickFilter(1)"
      >
        待审核
      </div>
      <div
        class="quick-filter-item"
        :class="{ active: queryParams.status === 2 }"
        @click="handleQuickFilter(2)"
      >
        已审核
      </div>
      <div
        class="quick-filter-item"
        :class="{ active: queryParams.status === 3 }"
        @click="handleQuickFilter(3)"
      >
        已驳回
      </div>
<!--      <div-->
<!--        class="quick-filter-item"-->
<!--        :class="{ active: queryParams.status === 4 }"-->
<!--        @click="handleQuickFilter(4)"-->
<!--      >-->
<!--        已取消-->
<!--      </div>-->
      <div
        class="quick-filter-item"
        :class="{ active: queryParams.status === 5 }"
        @click="handleQuickFilter(5)"
      >
        已采购
      </div>
    </div>

    <!-- 卡片列表 -->
    <div class="mobile-list" v-loading="loading">
      <div v-if="list.length === 0 && !loading" class="mobile-empty">
        <el-empty description="暂无请购单" />
      </div>
      <div v-for="item in list" :key="item.id" class="mobile-card" @click="viewDetails(item)">
        <div class="mobile-card__header">
          <span class="mobile-card__no">{{ item.no }}</span>
          <div class="mobile-card__tags">
            <el-tag :type="getStatusTagType(item.status)" size="small">{{ getStatusLabel(item.status) }}</el-tag>
            <el-tag :type="getPriorityTagType(item.priority)" size="small">{{ getPriorityLabel(item.priority) }}</el-tag>
          </div>
        </div>
        <div class="mobile-card__body">
          <div class="mobile-card__row">
            <span class="mobile-card__label">请购类型</span>
            <el-tag :type="getTypeTagType(item.type)" size="small">{{ getTypeLabel(item.type) }}</el-tag>
          </div>
          <div class="mobile-card__row">
            <span class="mobile-card__label">请购人</span>
            <span class="mobile-card__value">{{ item.requesterNickname || '-' }}</span>
          </div>
          <div class="mobile-card__row">
            <span class="mobile-card__label">请购部门</span>
            <span class="mobile-card__value">{{ item.requesterDeptName || '-' }}</span>
          </div>
          <div class="mobile-card__row">
            <span class="mobile-card__label">请购时间</span>
            <span class="mobile-card__value">{{ formatDate2(item.requestTime) }}</span>
          </div>
          <div class="mobile-card__row">
            <span class="mobile-card__label">期望到货</span>
            <span class="mobile-card__value">{{ formatDate2(item.expectedTime) }}</span>
          </div>
          <div class="mobile-card__row">
            <span class="mobile-card__label">紧急程度</span>
            <el-tag :type="getEmergencyTagType(item.emergencyDegree)" size="small">{{ getEmergencyLabel(item.emergencyDegree) }}</el-tag>
          </div>
        </div>
        <div class="mobile-card__footer">
          <el-button size="small" @click.stop="viewDetails(item)">详情</el-button>
          <el-button size="small" type="primary" @click.stop="openForm('update', item.id)" v-hasPermi="['erp:purchase-requisition:update']" v-if="item.status === 1">编辑</el-button>
          <el-button size="small" type="success" @click.stop="handleUpdateStatus(item.id, 2)" v-if="item.status === 1" v-hasPermi="['erp:purchase-requisition:update-status']">审核</el-button>
          <el-button size="small" type="warning" @click.stop="handleUpdateStatus(item.id, 3)" v-if="item.status === 1" v-hasPermi="['erp:purchase-requisition:approve']">驳回</el-button>
          <el-button size="small" type="success" @click.stop="handleSubmitApproval(item.id)" v-hasPermi="['erp:purchasere-quisition:submit-approval']" v-if="item.status === 1 && !item.hasApprovalRecords">提交审批</el-button>
          <el-button size="small" type="info" @click.stop="handleViewApproval(item.id)" v-hasPermi="['erp:purchasere-quisition:query-approval']" v-if="item.hasApprovalRecords">审批记录</el-button>
          <el-button size="small" type="primary" @click.stop="handleProcessApproval(item.id)" v-hasPermi="['erp:approval-record:process']" v-if="item.hasApprovalRecords && item.status !== 2 && item.status !== 3 && item.status !== 4 && item.status !== 5">处理审批</el-button>
          <el-button size="small" type="danger" @click.stop="handleDelete(item.id)" v-hasPermi="['erp:purchase-requisition:delete']" v-if="item.status === 1">删除</el-button>
        </div>
      </div>
    </div>

    <!-- 分页 -->
    <div class="mobile-pagination" v-if="total > 0">
      <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" :page-sizes="[10, 20]" layout="total, prev, pager, next" :pager-count="5" @pagination="getList" />
    </div>

    <!-- 筛选抽屉 -->
    <el-drawer v-model="filterVisible" title="筛选条件" direction="btt" size="70%">
      <el-form :model="queryParams" ref="queryFormRef" label-position="top">
        <el-form-item label="请购类型" prop="type">
          <el-select v-model="queryParams.type" placeholder="请选择" clearable style="width: 100%">
            <el-option label="请购" :value="1" />
            <el-option label="补货" :value="2" />
            <el-option label="其他" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="优先级" prop="priority">
          <el-select v-model="queryParams.priority" placeholder="请选择" clearable style="width: 100%">
            <el-option label="低" :value="1" />
            <el-option label="中" :value="2" />
            <el-option label="高" :value="3" />
            <el-option label="紧急" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择" clearable style="width: 100%">
            <el-option label="待审核" :value="1" />
            <el-option label="已审核" :value="2" />
            <el-option label="已驳回" :value="3" />
            <el-option label="已取消" :value="4" />
            <el-option label="已采购" :value="5" />
          </el-select>
        </el-form-item>
        <el-form-item label="请购人" prop="requesterName">
          <el-input v-model="queryParams.requesterName" clearable placeholder="请输入请购人" />
        </el-form-item>
        <el-form-item label="请购部门" prop="requesterDeptName">
          <el-input v-model="queryParams.requesterDeptName" clearable placeholder="请输入部门" />
        </el-form-item>
        <el-form-item label="紧急程度" prop="emergencyDegree">
          <el-select v-model="queryParams.emergencyDegree" placeholder="请选择" clearable style="width: 100%">
            <el-option label="普通" :value="1" />
            <el-option label="较急" :value="2" />
            <el-option label="紧急" :value="3" />
            <el-option label="特急" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="请购时间" prop="requestTime">
          <el-date-picker v-model="queryParams.requestTime" value-format="YYYY-MM-DD HH:mm:ss" type="daterange" start-placeholder="开始" end-placeholder="结束" :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" style="width: 100%" />
        </el-form-item>
        <el-form-item label="期望到货时间" prop="expectedTime">
          <el-date-picker v-model="queryParams.expectedTime" value-format="YYYY-MM-DD HH:mm:ss" type="daterange" start-placeholder="开始" end-placeholder="结束" :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" style="width: 100%" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="handleFilterConfirm">确认筛选</el-button>
      </template>
    </el-drawer>

    <!-- 表单弹窗：添加/修改/详情 -->
    <PurchaseRequisitionForm ref="formRef" @success="getList" />

    <!-- 提交审批对话框 -->
    <SubmitApprovalDialog ref="submitApprovalDialogRef" @success="getList" />

    <!-- 审批记录对话框 -->
    <ApprovalRecordsDialog ref="approvalDialogRef" />

    <!-- 处理审批对话框 -->
    <ProcessApprovalDialog ref="processApprovalDialogRef" @success="getList" />
  </div>

  <!-- PC端布局 -->
  <div v-else>
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="120px"
      >
        <el-form-item label="请购单编号" prop="no">
          <el-input
            v-model="queryParams.no"
            placeholder="请输入请购单编号"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="请购类型" prop="type">
          <el-select
            v-model="queryParams.type"
            placeholder="请选择请购类型"
            clearable
            class="!w-240px"
          >
            <el-option label="请购" :value="1" />
            <el-option label="补货" :value="2" />
            <el-option label="其他" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="优先级" prop="priority">
          <el-select
            v-model="queryParams.priority"
            placeholder="请选择优先级"
            clearable
            class="!w-240px"
          >
            <el-option label="低" :value="1" />
            <el-option label="中" :value="2" />
            <el-option label="高" :value="3" />
            <el-option label="紧急" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择状态"
            clearable
            class="!w-240px"
          >
            <el-option label="待审核" :value="1" />
            <el-option label="已审核" :value="2" />
            <el-option label="已驳回" :value="3" />
            <el-option label="已取消" :value="4" />
            <el-option label="已采购" :value="5" />
          </el-select>
        </el-form-item>
        <el-form-item label="请购人" prop="requesterName">
          <el-input
            v-model="queryParams.requesterName"
            placeholder="请输入请购人姓名"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="请购部门" prop="requesterDeptName">
          <el-input
            v-model="queryParams.requesterDeptName"
            placeholder="请输入部门名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="请购时间" prop="requestTime">
          <el-date-picker
            v-model="queryParams.requestTime"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-220px"
          />
        </el-form-item>
        <el-form-item label="期望到货时间" prop="expectedTime">
          <el-date-picker
            v-model="queryParams.expectedTime"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-220px"
          />
        </el-form-item>
        <el-form-item label="紧急程度" prop="emergencyDegree">
          <el-select
            v-model="queryParams.emergencyDegree"
            placeholder="请选择紧急程度"
            clearable
            class="!w-240px"
          >
            <el-option label="普通" :value="1" />
            <el-option label="较急" :value="2" />
            <el-option label="紧急" :value="3" />
            <el-option label="特急" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            v-model="queryParams.createTime"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-220px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
          <el-button
            type="primary"
            plain
            @click="openForm('create')"
            v-hasPermi="['erp:purchase-requisition:create']"
          >
            <Icon icon="ep:plus" class="mr-5px" /> 新增
          </el-button>
          <el-button
            type="success"
            plain
            @click="handleExport"
            :loading="exportLoading"
            v-hasPermi="['erp:purchase-requisition:export']"
          >
            <Icon icon="ep:download" class="mr-5px" /> 导出
          </el-button>
          <el-button
            type="danger"
            plain
            :disabled="isEmpty(checkedIds)"
            @click="handleDeleteBatch"
            v-hasPermi="['erp:purchase-requisition:delete']"
          >
            <Icon icon="ep:delete" class="mr-5px" /> 批量删除
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @selection-change="handleRowCheckboxChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="请购单编号" align="center" prop="no" min-width="120">
          <template #default="scope">
            <el-link type="primary" @click="viewDetails(scope.row)">
              {{ scope.row.no }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="请购类型" align="center" prop="type" width="100">
          <template #default="scope">
            <el-tag :type="getTypeTagType(scope.row.type)">
              {{ getTypeLabel(scope.row.type) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="优先级" align="center" prop="priority" width="100">
          <template #default="scope">
            <el-tag :type="getPriorityTagType(scope.row.priority)">
              {{ getPriorityLabel(scope.row.priority) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status" width="100">
          <template #default="scope">
            <el-tag :type="getStatusTagType(scope.row.status)">
              {{ getStatusLabel(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="请购人" align="center" prop="requesterNickname" width="100" />
        <el-table-column label="请购部门" align="center" prop="requesterDeptName" width="120" />
        <el-table-column
          label="请购时间"
          align="center"
          prop="requestTime"
          :formatter="dateFormatter"
          width="160px"
        />
        <el-table-column
          label="期望到货时间"
          align="center"
          prop="expectedTime"
          :formatter="dateFormatter"
          width="160px"
        />
        <el-table-column
          label="紧急程度"
          align="center"
          prop="emergencyDegree"
          width="100"
        >
          <template #default="scope">
            <el-tag :type="getEmergencyTagType(scope.row.emergencyDegree)">
              {{ getEmergencyLabel(scope.row.emergencyDegree) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
          width="160px"
        />
        <el-table-column label="操作" align="center" min-width="180px">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="viewDetails(scope.row)"
            >
              详情
            </el-button>
            <el-button
              link
              type="success"
              @click="openForm('update', scope.row.id)"
              v-hasPermi="['erp:purchase-requisition:update']"
              v-if="scope.row.status === 1"
            >
              编辑
            </el-button>
            <el-button
              link
              type="danger"
              @click="handleDelete(scope.row.id)"
              v-hasPermi="['erp:purchase-requisition:delete']"
              v-if="scope.row.status === 1"
            >
              删除
            </el-button>
            <el-button
              link
              type="success"
              @click="handleUpdateStatus(scope.row.id, 2)"
              v-if="scope.row.status === 1"
              v-hasPermi="['erp:purchase-requisition:update-status']"
            >
              审核
            </el-button>
            <el-button
              link
              type="success"
              @click="handleSubmitApproval(scope.row.id)"
              v-hasPermi="['erp:purchasere-quisition:submit-approval']"
              v-if="scope.row.status === 1 && !scope.row.hasApprovalRecords"
            >
              提交审批
            </el-button>
            <el-button
              link
              type="info"
              @click="handleViewApproval(scope.row.id)"
              v-hasPermi="['erp:purchasere-quisition:query-approval']"
              v-if="scope.row.hasApprovalRecords"
            >
              审批记录
            </el-button>
            <el-button
              link
              type="primary"
              @click="handleProcessApproval(scope.row.id)"
              v-hasPermi="['erp:approval-record:process']"
              v-if="scope.row.hasApprovalRecords && scope.row.status !== 2 && scope.row.status !== 3 && scope.row.status !== 4 && scope.row.status !== 5"
            >
              处理审批
            </el-button>
            <el-button
              link
              type="warning"
              @click="handleUpdateStatus(scope.row.id, 3)"
              v-if="scope.row.status === 1"
              v-hasPermi="['erp:purchase-requisition:approve']"
            >
              驳回
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>

    <!-- 表单弹窗：添加/修改/详情 -->
    <PurchaseRequisitionForm ref="formRef" @success="getList" />
    <!-- 提交审批对话框 -->
    <SubmitApprovalDialog ref="submitApprovalDialogRef" @success="getList" />
    <!-- 审批记录对话框 -->
    <ApprovalRecordsDialog ref="approvalDialogRef" />
    <!-- 处理审批对话框 -->
    <ProcessApprovalDialog ref="processApprovalDialogRef" @success="getList" />
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { Search, Filter, Plus } from '@element-plus/icons-vue'
import { isEmpty } from '@/utils/is'
import { dateFormatter, formatDate } from '@/utils/formatTime'
import download from '@/utils/download'
import { PurchaseRequisitionApi, PurchaseRequisition } from '@/api/erp/purchaserequisition'
import PurchaseRequisitionForm from './PurchaseRequisitionForm.vue'
import { SubmitApprovalDialog, ApprovalRecordsDialog, ProcessApprovalDialog } from '@/components/Approval'
import { ApprovalRecordApi, ApprovalRecordVO } from '@/api/erp/approval/index'
import { useWindowSize } from '@vueuse/core'

/** 请购单 列表 */
defineOptions({ name: 'PurchaseRequisition' })

const { width } = useWindowSize()
const isMobile = computed(() => width.value < 768)

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<PurchaseRequisition[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const filterVisible = ref(false) // 筛选抽屉

const formatDate2 = (date: any) => {
  if (!date) return '-'
  return formatDate(new Date(date), 'YYYY-MM-DD')
}
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  no: undefined,
  type: undefined,
  priority: undefined,
  status: undefined,
  requesterName: undefined,
  requesterDeptName: undefined,
  requestTime: [],
  expectedTime: [],
  emergencyDegree: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PurchaseRequisitionApi.getPurchaseRequisitionPage(queryParams)

    // 为每个请购计划检查是否有审批记录
    const listWithApprovalStatus = await Promise.all(
      data.list.map(async (item) => {
        try {
          const records = await ApprovalRecordApi.getApprovalRecordListByBiz(item.id,'erp_purchase_requisition')
          return {
            ...item,
            hasApprovalRecords: records && records.length > 0
          }
        } catch (error) {
          console.error('获取审批记录失败', error)
          return {
            ...item,
            hasApprovalRecords: false
          }
        }
      })
    )
    list.value = listWithApprovalStatus
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

const handleFilterConfirm = () => {
  filterVisible.value = false
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await PurchaseRequisitionApi.deletePurchaseRequisition(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除请购单 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await PurchaseRequisitionApi.deletePurchaseRequisitionList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: PurchaseRequisition[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await PurchaseRequisitionApi.exportPurchaseRequisition(queryParams)
    download.excel(data, '请购单.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 提交审批 */
const submitApprovalDialogRef = ref()
const handleSubmitApproval = (id: number) => {
  submitApprovalDialogRef.value.open(String(id), 'erp_purchase_requisition')
}

/** 查看审批记录 */
const approvalDialogRef = ref()
const handleViewApproval = (id: number) => {
  approvalDialogRef.value.open(String(id), 'erp_purchase_requisition')
}

/** 处理审批 */
const processApprovalDialogRef = ref()
const handleProcessApproval = (id: number) => {
  processApprovalDialogRef.value.open(String(id), 'erp_purchase_requisition')
}

/** 获取请购类型标签类型 */
const getTypeTagType = (type: number) => {
  switch (type) {
    case 1: return 'success'
    case 2: return 'warning'
    case 3: return 'info'
    default: return ''
  }
}

/** 获取请购类型标签文本 */
const getTypeLabel = (type: number) => {
  switch (type) {
    case 1: return '请购'
    case 2: return '补货'
    case 3: return '其他'
    default: return '未知'
  }
}

/** 获取优先级标签类型 */
const getPriorityTagType = (priority: number) => {
  switch (priority) {
    case 1: return 'info'
    case 2: return ''
    case 3: return 'warning'
    case 4: return 'danger'
    default: return ''
  }
}

/** 获取优先级标签文本 */
const getPriorityLabel = (priority: number) => {
  switch (priority) {
    case 1: return '低'
    case 2: return '中'
    case 3: return '高'
    case 4: return '紧急'
    default: return '未知'
  }
}

/** 获取状态标签类型 */
const getStatusTagType = (status: number) => {
  switch (status) {
    case 1: return 'warning'
    case 2: return 'success'
    case 3: return 'danger'
    case 4: return 'info'
    case 5: return ''
    default: return ''
  }
}

/** 获取状态标签文本 */
const getStatusLabel = (status: number) => {
  switch (status) {
    case 1: return '待审核'
    case 2: return '已审核'
    case 3: return '已驳回'
    case 4: return '已取消'
    case 5: return '已采购'
    default: return '未知'
  }
}

/** 获取紧急程度标签类型 */
const getEmergencyTagType = (emergencyDegree: number) => {
  switch (emergencyDegree) {
    case 1: return 'info'
    case 2: return ''
    case 3: return 'warning'
    case 4: return 'danger'
    default: return ''
  }
}

/** 获取紧急程度标签文本 */
const getEmergencyLabel = (emergencyDegree: number) => {
  switch (emergencyDegree) {
    case 1: return '普通'
    case 2: return '较急'
    case 3: return '紧急'
    case 4: return '特急'
    default: return '未知'
  }
}

/** 查看明细 */
const viewDetails = (row: PurchaseRequisition) => {
  formRef.value.open('detail', row.id)
}

/** 审核/驳回操作 */
const handleUpdateStatus = async (id: number, status: number) => {
  try {
    const statusText = status === 2 ? '审核' : '驳回'
    await message.confirm(`确定${statusText}该请购单吗？`)
    await PurchaseRequisitionApi.updatePurchaseRequisitionStatus(id, status)
    message.success(`${statusText}成功`)
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})

/** 快捷分类筛选 */
const handleQuickFilter = (status: number | undefined) => {
  queryParams.status = status
  queryParams.pageNo = 1
  getList()
}

/** 行点击操作 */
const handleRowClick = (row: PurchaseRequisition, column: any, event: MouseEvent) => {
  const target = event.target as HTMLElement
  if (target.tagName === 'BUTTON' || target.tagName === 'A' || target.tagName === 'I' || target.tagName === 'svg' || target.closest('button') || target.closest('a') || target.closest('.el-button') || target.closest('.el-checkbox')) {
    return
  }
  viewDetails(row)
}
</script>

<style lang="scss" scoped>


.mobile-header__quick-filter {
  display: flex;
  gap: 12px;
  margin: 8px 0;
  justify-content: flex-start;

  .quick-filter-item {
    padding: 4px 12px;
    font-size: 14px;
    border-radius: 20px;
    cursor: pointer;
    color: #909399;
    background: transparent;
    transition: all 0.2s;

    &.active {
      color: #fff;
      background: #409eff;
    }
  }
}

.mobile-requisition {
  padding: 12px;
  background: #f5f5f5;
  min-height: 100vh;
}
.mobile-header {
  display: flex; gap: 8px; align-items: center; margin-bottom: 12px;
  &__search { flex: 1; }
  &__actions { display: flex; gap: 4px; flex-shrink: 0; }
}
.mobile-list { display: flex; flex-direction: column; gap: 10px; }
.mobile-empty { padding: 40px 0; }
.mobile-card {
  background: #fff; border-radius: 10px; padding: 14px; box-shadow: 0 1px 4px rgba(0,0,0,0.06);
  &__header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
  &__no { font-weight: 600; font-size: 15px; color: #303133; }
  &__tags { display: flex; gap: 4px; }
  &__body { font-size: 13px; }
  &__row { display: flex; justify-content: space-between; align-items: center; padding: 3px 0; }
  &__label { color: #909399; flex-shrink: 0; margin-right: 12px; }
  &__value { color: #606266; text-align: right; }
  &__footer { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; padding-top: 10px; border-top: 1px solid #f0f0f0; }
}
.mobile-pagination {
  margin-top: 12px; display: flex; justify-content: center;
  :deep(.el-pagination) { flex-wrap: wrap; justify-content: center; }
}
</style>
