<template>
  <div class="modern-page-container">
    <div class="page-wrapper">
      <!-- 搜索表单卡片 -->
      <el-card v-show="showSearch" class="search-card" shadow="never">
        <template #header>
          <div class="card-header">
            <span class="card-title">
              <el-icon class="title-icon"><Search /></el-icon>
              筛选条件
            </span>
          </div>
        </template>
        <el-form :model="state.queryForm" ref="searchFormRef" :inline="true" @keyup.enter="getDataList" class="search-form">
          <el-form-item label="采购编号" prop="purchaseNo">
            <el-input 
              v-model="state.queryForm.purchaseNo" 
              placeholder="请输入采购编号" 
              clearable
              style="width: 200px" />
          </el-form-item>
          <el-form-item label="采购项目名称" prop="projectName">
            <el-input 
              v-model="state.queryForm.projectName" 
              placeholder="请输入采购项目名称" 
              clearable
              style="width: 200px" />
          </el-form-item>
          <el-form-item label="项目类别" prop="projectType">
            <el-select 
              v-model="state.queryForm.projectType" 
              placeholder="请选择项目类别" 
              clearable
              style="width: 200px">
              <el-option label="货物" value="A" />
              <el-option label="工程" value="B" />
              <el-option label="服务" value="C" />
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select 
              v-model="state.queryForm.status" 
              placeholder="请选择状态" 
              clearable
              style="width: 200px">
              <el-option label="撤回" value="-2" />
              <el-option label="暂存" value="-1" />
              <el-option label="运行中" value="0" />
              <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="isCentralized">
            <el-select 
              v-model="state.queryForm.isCentralized" 
              placeholder="请选择是否集采" 
              clearable
              style="width: 200px">
              <el-option label="否" value="0" />
              <el-option label="是" value="1" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="getDataList">查询</el-button>
            <el-button icon="Refresh" @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <!-- 内容卡片 -->
      <el-card class="content-card" shadow="never">
        <template #header>
          <div class="card-header">
            <span class="card-title">
              <el-icon class="title-icon"><Document /></el-icon>
              采购申请管理
            </span>
            <div class="header-actions">
              <el-button 
                icon="FolderAdd" 
                type="primary" 
                @click="handleAdd">
                新增
              </el-button>
              <right-toolbar v-model:showSearch="showSearch" class="ml10" @queryTable="getDataList" />
            </div>
          </div>
        </template>

        <!-- 表格 -->
        <el-table 
          ref="tableRef"
          :data="state.dataList" 
          v-loading="state.loading" 
          stripe
          :cell-style="tableStyle.cellStyle" 
          :header-cell-style="tableStyle.headerCellStyle"
          class="modern-table">
          <el-table-column type="index" label="序号" width="70" align="center">
            <template #header>
              <el-icon><List /></el-icon>
            </template>
          </el-table-column>
          <el-table-column prop="purchaseNo" label="申请单编号" min-width="140" show-overflow-tooltip>
            <template #header>
              <el-icon><DocumentCopy /></el-icon>
              <span style="margin-left: 4px">申请单编号</span>
            </template>
          </el-table-column>
          <el-table-column prop="projectName" label="采购项目名称" min-width="200" show-overflow-tooltip>
            <template #header>
              <el-icon><Document /></el-icon>
              <span style="margin-left: 4px">采购项目名称</span>
            </template>
          </el-table-column>
          <el-table-column prop="applyDate" label="填报日期" width="120" align="center" show-overflow-tooltip>
            <template #header>
              <el-icon><Calendar /></el-icon>
              <span style="margin-left: 4px">填报日期</span>
            </template>
          </el-table-column>
          <el-table-column prop="deptName" label="需求部门" min-width="150" show-overflow-tooltip>
            <template #header>
              <el-icon><OfficeBuilding /></el-icon>
              <span style="margin-left: 4px">需求部门</span>
            </template>
          </el-table-column>
          <el-table-column prop="projectType" label="项目类别" min-width="200" align="left" show-overflow-tooltip>
            <template #header>
              <el-icon><Collection /></el-icon>
              <span style="margin-left: 4px">项目类别</span>
            </template>
            <template #default="scope">
              <div>
                <el-tag v-if="scope.row.projectType === 'A'" type="success" style="margin-right: 8px;">货物</el-tag>
                <el-tag v-else-if="scope.row.projectType === 'B'" type="warning" style="margin-right: 8px;">工程</el-tag>
                <el-tag v-else-if="scope.row.projectType === 'C'" type="info" style="margin-right: 8px;">服务</el-tag>
                <span v-if="scope.row.categoryName" style="color: #606266; font-size: 12px;">
                  {{ scope.row.categoryName }}
                </span>
                <span v-else-if="scope.row.categoryCode" style="color: #909399; font-size: 12px;">
                  {{ scope.row.categoryCode }}
                </span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="budget" label="项目预算(元)" width="130" align="right">
            <template #header>
              <el-icon><Money /></el-icon>
              <span style="margin-left: 4px">项目预算</span>
            </template>
            <template #default="scope">
              {{ scope.row.budget ? Number(scope.row.budget).toLocaleString() : '-' }}
            </template>
          </el-table-column>
          <el-table-column prop="isSpecial" label="是否特殊" width="100" align="center">
            <template #header>
              <el-icon><Warning /></el-icon>
              <span style="margin-left: 4px">是否特殊</span>
            </template>
            <template #default="scope">
              <el-tag v-if="String(scope.row.isSpecial) === '1'" type="warning">紧急</el-tag>
              <el-tag v-else-if="String(scope.row.isSpecial) === '2'" type="danger">单一</el-tag>
              <el-tag v-else-if="String(scope.row.isSpecial) === '3'" type="info">进口</el-tag>
              <el-tag v-else-if="String(scope.row.isSpecial) === '0'" type="info">否</el-tag>
              <span v-else>-</span>
            </template>
          </el-table-column>
          <el-table-column prop="isCentralized" label="是否集采" width="100" align="center">
            <template #header>
              <el-icon><CircleCheck /></el-icon>
              <span style="margin-left: 4px">是否集采</span>
            </template>
            <template #default="scope">
              <el-tag v-if="String(scope.row.isCentralized) === '1'" type="success">是</el-tag>
              <el-tag v-else-if="String(scope.row.isCentralized) === '0'" type="info">否</el-tag>
              <span v-else>-</span>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="审核状态" width="100" align="center">
            <template #header>
              <el-icon><InfoFilled /></el-icon>
              <span style="margin-left: 4px">审核状态</span>
            </template>
            <template #default="scope">
              <el-tooltip v-if="scope.row.flowInstId" content="点击查看审批过程" placement="top">
                <el-tag
                  v-if="scope.row.status === '-2'"
                  type="info"
                  class="status-tag-clickable"
                  @click="handleShowFlowComment(scope.row)">撤回</el-tag>
                <el-tag
                  v-else-if="scope.row.status === '-1'"
                  type="warning"
                  class="status-tag-clickable"
                  @click="handleShowFlowComment(scope.row)">暂存</el-tag>
                <el-tag
                  v-else-if="scope.row.status === '0'"
                  type="primary"
                  class="status-tag-clickable"
                  @click="handleShowFlowComment(scope.row)">运行中</el-tag>
                <el-tag
                  v-else-if="scope.row.status === '1'"
                  type="success"
                  class="status-tag-clickable"
                  @click="handleShowFlowComment(scope.row)">完成</el-tag>
                <el-tag
                  v-else-if="scope.row.status === '2'"
                  type="danger"
                  class="status-tag-clickable"
                  @click="handleShowFlowComment(scope.row)">作废</el-tag>
                <el-tag
                  v-else-if="scope.row.status === '3'"
                  type="info"
                  class="status-tag-clickable"
                  @click="handleShowFlowComment(scope.row)">终止</el-tag>
                <span v-else>-</span>
              </el-tooltip>
              <template v-else>
                <el-tag v-if="scope.row.status === '-2'" type="info">撤回</el-tag>
                <el-tag v-else-if="scope.row.status === '-1'" type="warning">暂存</el-tag>
                <el-tag v-else-if="scope.row.status === '0'" type="primary">运行中</el-tag>
                <el-tag v-else-if="scope.row.status === '1'" type="success">完成</el-tag>
                <el-tag v-else-if="scope.row.status === '2'" type="danger">作废</el-tag>
                <el-tag v-else-if="scope.row.status === '3'" type="info">终止</el-tag>
                <span v-else>-</span>
              </template>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" fixed="right" width="150">
            <template #default="scope">
              <div class="op-cell">
                <el-button
                  type="primary"
                  link
                  icon="View"
                  @click="handleView(scope.row)">
                  查看
                </el-button>
                <ActionDropdown
                  :items="getActionMenuItems(scope.row)"
                  @command="(command) => handleMoreCommand(command, scope.row)"
                />
              </div>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <pagination
          v-if="state.pagination && state.pagination.total && state.pagination.total > 0"
          :total="state.pagination.total"
          :current="state.pagination.current"
          :size="state.pagination.size"
          @sizeChange="sizeChangeHandle"
          @currentChange="currentChangeHandle"
        />
      </el-card>
    </div>

    <!-- 新增/编辑/查看：统一使用 form.vue 弹窗（iframe 引入 add.vue） -->
    <FormDialog 
      ref="formDialogRef" 
      :dict-data="dictData"
      @refresh="getDataList" />

    <!-- 履约验收弹窗 -->
    <PurchasingAcceptModal ref="acceptModalRef" @refresh="getDataList" />

    <!-- 查看审批过程（参考 hi-job 流程弹窗） -->
    <el-dialog
      v-model="showFlowComment"
      v-if="showFlowComment"
      title="查看审批过程"
      top="20px"
      width="90%"
      append-to-body
      destroy-on-close
      @close="currFlowJob = null">
      <FlowCommentTimeline v-if="currFlowJob" :key="currFlowJob.flowInstId" :curr-job="currFlowJob" />
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="PurchasingRequisition">
import { ref, reactive, defineAsyncComponent, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { BasicTableProps, useTable } from "/@/hooks/table";
import { getPage, delObj, submitObj } from "/@/api/finance/purchasingrequisition";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { getDicts } from '/@/api/admin/dict';
import { getTree } from '/@/api/finance/purchasingcategory';
import { List, Document, DocumentCopy, Search, Collection, Money, CircleCheck, InfoFilled, Calendar, OfficeBuilding, Warning, DocumentChecked, Edit, Delete, Upload } from '@element-plus/icons-vue'

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const ActionDropdown = defineAsyncComponent(() => import('/@/components/tools/action-dropdown.vue'));
const PurchasingAcceptModal = defineAsyncComponent(() => import('./accept/PurchasingAcceptModal.vue'));
const FlowCommentTimeline = defineAsyncComponent(() => import('/@/views/jsonflow/comment/timeline.vue'));

// 字典数据和品目树数据
const dictData = ref({
  fundSourceList: [] as any[],
  isCentralizedList: [] as any[],
  isSpecialList: [] as any[],
  purchaseTypeDeptList: [] as any[],
  purchaseModeSchoolList: [] as any[],
  purchaseTypeUnionList: [] as any[],
  categoryTreeData: [] as any[],
});

// 定义变量内容
const router = useRouter()
const tableRef = ref()
const formDialogRef = ref()
const acceptModalRef = ref()
const searchFormRef = ref()
const showSearch = ref(true)
/** 审批过程弹窗：是否显示、当前行对应的流程 job（供 Comment 组件用） */
const showFlowComment = ref(false)
const currFlowJob = ref<{ id?: number; flowInstId?: number } | null>(null)

/**
 * 定义响应式表格数据
 */
const state: BasicTableProps = reactive<BasicTableProps>({
  pageList: getPage,
  queryForm: {
    purchaseNo: '',
    projectName: '',
    projectType: '',
    status: '',
    isCentralized: '',
  },
  createdIsNeed: true
});

/**
 * 使用 useTable 定义表格相关操作
 */
const { getDataList, tableStyle, sizeChangeHandle, currentChangeHandle } = useTable(state);

/**
 * 重置搜索表单
 */
const handleReset = () => {
  searchFormRef.value?.resetFields();
  getDataList();
};

/**
 * 新增采购申请 - 统一通过 form.vue 弹窗（iframe 引入 add.vue）
 */
const handleAdd = () => {
  formDialogRef.value?.openDialog('add')
};

/**
/**
 * 点击审核状态：若有流程实例则打开「查看审批过程」弹窗（参考 hi-job.vue）
 * @param row - 当前行数据（需含 flowInstId）
 */
const handleShowFlowComment = (row: any) => {
  if (!row?.flowInstId) {
    useMessage().info('暂存状态无审批过程');
    return;
  }
  currFlowJob.value = { id: row.id, flowInstId: row.flowInstId };
  showFlowComment.value = true;
};

/**
 * 打开查看对话框
 * @param row - 当前行数据
 */
const handleView = (row: any) => {
  formDialogRef.value?.openDialog('view', row);
};

/**
 * 打开编辑对话框
 * @param row - 当前行数据
 */
const handleEdit = (row: any) => {
  formDialogRef.value?.openDialog('edit', row);
};

/**
 * 履约验收
 * @param row - 当前行数据
 */
const handleAccept = (row: any) => {
  acceptModalRef.value?.open(row);
}; 

/**
 * 删除当前行
 * @param row - 当前行数据
 */
const handleDelete = async (row: any) => {
  try {
    await useMessageBox().confirm('确定要删除该记录吗？');
  } catch {
    return;
  }

  try {
    await delObj(row.id);
    useMessage().success('删除成功');
    getDataList();
  } catch (err: any) {
    useMessage().error(err.msg || '删除失败');
  }
};

/** 暂存状态下提交采购申请（启动流程） */
const handleSubmit = async (row: any) => {
  try {
    await useMessageBox().confirm('确定要提交该采购申请并启动流程吗？');
  } catch {
    return;
  }
  try {
    await submitObj({ id: row.id });
    useMessage().success('提交成功');
    getDataList();
  } catch (err: any) {
    useMessage().error(err?.msg || '提交失败');
  }
};

/** 操作栏「更多」菜单项配置 */
const getActionMenuItems = (row: any) => {
  const isTemp = row?.status === '-1';
  return [
    {
      command: 'edit',
      label: '编辑',
      icon: Edit,
      visible: () => isTemp,
    },
    {
      command: 'submit',
      label: '提交',
      icon: Upload,
      visible: () => isTemp,
    },
    {
      command: 'delete',
      label: '删除',
      icon: Delete,
      visible: () => isTemp,
    },
    {
      command: 'accept',
      label: '履约验收',
      icon: DocumentChecked,
      visible: () => true,
    },
  ];
};

/** 处理更多操作下拉菜单命令 */
const handleMoreCommand = (command: string, row: any) => {
  switch (command) {
    case 'edit':
      handleEdit(row);
      break;
    case 'submit':
      handleSubmit(row);
      break;
    case 'delete':
      handleDelete(row);
      break;
    case 'accept':
      handleAccept(row);
      break;
  }
};

// 获取字典数据和品目树数据
const loadDictData = async () => {
  try {
    const [
      fundSourceRes,
      isCentralizedRes,
      isSpecialRes,
      purchaseTypeDeptRes,
      purchaseModeSchoolRes,
      purchaseTypeUnionRes,
      categoryTreeRes
    ] = await Promise.all([
      getDicts('PURCHASE_FUND_SOURCE'),
      getDicts('PURCHASE_IS_CEN'),
      getDicts('PURCHASE_IS_SPEC'),
      getDicts('PURCHASE_TYPE_DEPT'),
      getDicts('PURCHASE_MODE_SCHOOL'),
      getDicts('PURCHASE_TYPE_UNION'),
      getTree()
    ]);

    // 处理资金来源字典
    if (fundSourceRes.data && Array.isArray(fundSourceRes.data)) {
      dictData.value.fundSourceList = fundSourceRes.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      }));
    } else {
      dictData.value.fundSourceList = [
        { label: '切块经费', value: '0' },
        { label: '设备购置费', value: '1' },
        { label: '专项经费', value: '2' },
        { label: '代办费', value: '3' },
        { label: '培训经费', value: '4' },
        { label: '日常公用经费', value: '5' },
        { label: '技能大赛经费', value: '6' },
        { label: '基本建设资金', value: '7' },
        { label: '暂存款', value: '8' },
        { label: '会议费', value: '9' },
      ];
    }

    // 处理是否集采字典
    if (isCentralizedRes.data && Array.isArray(isCentralizedRes.data)) {
      dictData.value.isCentralizedList = isCentralizedRes.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      }));
    } else {
      dictData.value.isCentralizedList = [
        { label: '否', value: '0' },
        { label: '政府集中采购', value: '1' },
        { label: '学校集中采购', value: '2' }
      ];
    }

    // 处理是否特殊情况字典
    if (isSpecialRes.data && Array.isArray(isSpecialRes.data)) {
      dictData.value.isSpecialList = isSpecialRes.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      }));
    } else {
      dictData.value.isSpecialList = [
        { label: '否', value: '0' },
        { label: '紧急', value: '1' },
        { label: '单一', value: '2' },
        { label: '进口', value: '3' }
      ];
    }

    // 处理部门采购方式字典
    if (purchaseTypeDeptRes.data && Array.isArray(purchaseTypeDeptRes.data)) {
      dictData.value.purchaseTypeDeptList = purchaseTypeDeptRes.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      }));
    }

    // 处理学校采购形式字典
    if (purchaseModeSchoolRes.data && Array.isArray(purchaseModeSchoolRes.data)) {
      dictData.value.purchaseModeSchoolList = purchaseModeSchoolRes.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      }));
    } else {
      dictData.value.purchaseModeSchoolList = [
        { label: '政府采购', value: '1' },
        { label: '学校自主采购', value: '2' }
      ];
    }

    // 处理学校统一采购方式字典
    if (purchaseTypeUnionRes.data && Array.isArray(purchaseTypeUnionRes.data)) {
      dictData.value.purchaseTypeUnionList = purchaseTypeUnionRes.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      }));
    }

    // 处理品目树数据
    if (categoryTreeRes.data && Array.isArray(categoryTreeRes.data)) {
      dictData.value.categoryTreeData = categoryTreeRes.data;
    } else {
      dictData.value.categoryTreeData = [];
    }
  } catch (err) {
    console.error('加载字典数据失败', err);
    // 设置默认值
    dictData.value.fundSourceList = [
      { label: '切块经费', value: '0' },
      { label: '设备购置费', value: '1' },
      { label: '专项经费', value: '2' },
      { label: '代办费', value: '3' },
      { label: '培训经费', value: '4' },
      { label: '日常公用经费', value: '5' },
      { label: '技能大赛经费', value: '6' },
      { label: '基本建设资金', value: '7' },
      { label: '暂存款', value: '8' },
      { label: '会议费', value: '9' },
    ];
    dictData.value.isCentralizedList = [
      { label: '否', value: '0' },
      { label: '政府集中采购', value: '1' },
      { label: '学校集中采购', value: '2' }
    ];
    dictData.value.isSpecialList = [
      { label: '否', value: '0' },
      { label: '紧急', value: '1' },
      { label: '单一', value: '2' },
      { label: '进口', value: '3' }
    ];
    dictData.value.purchaseModeSchoolList = [
      { label: '政府采购', value: '1' },
      { label: '学校自主采购', value: '2' }
    ];
    dictData.value.categoryTreeData = [];
  }
};

// 页面加载时获取字典数据和品目树数据
onMounted(() => {
  loadDictData();
});

</script>

<style scoped lang="scss">
@import '/@/assets/styles/modern-page.scss';

.op-cell {
  display: flex;
  align-items: center;
  justify-content: center;
}

.status-tag-clickable {
  cursor: pointer;
}
</style>

