<template>
  <div class="app-container">
    <div v-show="!materialsDeclarationViewState.show && !materialsDeclarationAddState.show">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="Plus" @click="handleAdd">新增采购</el-button>
        </el-col>
      </el-row>
      <!-- 表格组件 -->
      <el-table ref="multipleTable" v-loading="loading" :data="tableData" style="width: 100%" :max-height="400">
        <el-table-column label="序号" align="center" width="55" fixed="left">
          <template #default="scope">
            {{ (queryParams.page - 1) * queryParams.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="申报日期" align="center" prop="declaration_date" />
        <el-table-column label="申报金额（元）" align="center" prop="declaration_amount" />
        <el-table-column label="申报单位" align="center" prop="declaration_unit_name" />
        <el-table-column label="申报人" align="center" prop="declaration_person_name" />
        <el-table-column label="审批情况" align="center" prop="declaration_details">
          <template #default="scope">
            {{ getApprovalStatusText(scope.row.declaration_details) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
          <template #default="scope">
            <el-text class="common-btn-text-primary" @click="handleView(scope.row)">详情</el-text>
            <el-text v-if="scope.row.declaration_details === '1'" class="common-btn-text-primary" @click="handleUpdate(scope.row)">编辑</el-text>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.pageSize"
        :total="total"
        @pagination="fetchWorkrData"
      />
    </div>
    <MaterialsDeclarationView v-if="materialsDeclarationViewState.show" :id="materialsDeclarationViewState.id" @close="handleCancel" />
    <MaterialsDeclarationAdd v-if="materialsDeclarationAddState.show" :id="materialsDeclarationAddState.id" @close="handleCancel" />
  </div>
</template>

<script setup lang="ts">
import { getDeclarationList } from '@/api/comprehensiveGuarantee/materialReserveManagement/materialsDeclaration';
import Pagination from '@/components/Pagination/index.vue'; // 假设这是分页组件的路径
import MaterialsDeclarationView from './materialsDeclarationView.vue'; // 查看详情组件
import MaterialsDeclarationAdd from './materialsDeclarationAdd.vue';
const loading = ref(true);
const total = ref(0);
const tableData = ref<any[]>([]);

const initFormData = reactive({
  declaration_date: '',
  declaration_amount: '',
  declaration_unit_name: '',
  declaration_person_name: '',
  declaration_details: ''
});

const data = reactive({
  form: { ...initFormData },
  queryParams: {
    page: 1,
    pageSize: 10
  }
});

const { queryParams, form } = toRefs(data);
// 获取物资申报列表
const fetchWorkrData = () => {
  loading.value = true;
  getDeclarationList(queryParams.value)
    .then((res) => {
      tableData.value = res.data;
      total.value = res.total;
    })
    .finally(() => {
      loading.value = false;
    });
};

const handleQuery = () => {
  queryParams.value.page = 1;
  fetchWorkrData();
};

const resetQuery = () => {
  queryParams.value = { page: 1, pageSize: 10 };
  handleQuery();
};

let materialsDeclarationViewState = reactive({
  show: false,
  id: ''
});

let materialsDeclarationAddState = reactive({
  show: false,
  id: ''
});

const handleAdd = () => {
  materialsDeclarationAddState.id = '';
  materialsDeclarationAddState.show = true;
};

const handleUpdate = (row: any) => {
  materialsDeclarationAddState.id = row.id;
  materialsDeclarationAddState.show = true;
};

const handleView = (row: any) => {
  materialsDeclarationViewState.id = row.id;
  materialsDeclarationViewState.show = true;
};

const handleCancel = (needUpdate) => {
  materialsDeclarationViewState.show = false;
  materialsDeclarationAddState.show = false;
  if (!!needUpdate) {
    handleQuery();
  }
};


// 定义审批状态映射
const approvalStatusMap = {
  '1': '未审核',
  '2': '通过',
  '3': '不通过'
};

// 方法用于将数字转换为对应的审批状态文本
const getApprovalStatusText = (status: string) => {
  return approvalStatusMap[status] || '未知';
};
onMounted(() => {
  fetchWorkrData();
});
</script>

<style scoped>
.app-container {
  overflow-x: auto; /* 当内容溢出时允许水平滚动 */
}
</style>
