<template>
  <div class="app-container">
    <div v-show="!materialsDistributionViewState.show && !materialsDistributionAddState.show">
      <h1>物资储备管理</h1>
      <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>
      <h3>申报列表</h3>
      <!-- 表格组件 -->
      <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="dispatch_purpose" />
        <el-table-column label="调度日期" align="center" prop="dispatch_date" />
        <el-table-column label="申报单位" align="center" prop="dispatch_unit_name" />
        <el-table-column label="申请人" align="center" prop="dispatch_person_name" />
        <el-table-column label="审批情况" align="center" prop="dispatch_status" />
        <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.approval_status === '未审批'" 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>
    <MaterialsDistributionView v-if="materialsDistributionViewState.show" :id="materialsDistributionViewState.id" @close="handleCancel" />
    <MaterialsDistributionAdd v-if="materialsDistributionAddState.show" @close="handleCancel" @refresh="fetchWorkrData" />
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref, toRefs } from 'vue';
import { ElTable, ElTableColumn, ElButton, ElText } from 'element-plus';
import { ComponentInternalInstance, getCurrentInstance } from 'vue';
import Pagination from '@/components/Pagination/index.vue'; // 假设这是分页组件的路径
import MaterialsDistributionView from './materialsDistributionView.vue'; // 查看详情组件
import MaterialsDistributionAdd from './materialsDistributionAdd.vue';
import { getDispatchtList } from '@/api/comprehensiveGuarantee/materialReserveManagement/materialsDistribution'; // 编辑组件
const loading = ref(true);
const showSearch = ref(true);
const multiple = ref(true);
const ids = ref<Array<number | string>>([]);
const single = ref(true);
const total = ref(0);
const tableData = ref<any[]>([]);
const selectedRow = ref<any | null>(null);

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const initFormData = reactive({
  dispatch_purpose: '',
  dispatch_date: '',
  dispatch_unit: '',
  dispatch_unit_name: '',
  dispatch_person_name: '',
  dispatch_person: '',
  dispatch_status: ''
});

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

const { queryParams, form } = toRefs(data);

const statusMap = {
  '1': '未审批',
  '2': '审批通过',
  '3': '审批不通过'
};
const fetchWorkrData = () => {
  loading.value = true;
  getDispatchtList(queryParams.value)
    .then((res) => {
      const transformedData = res.data.map(item => ({
        ...item,
        dispatch_status: statusMap[item.dispatch_status] || '未知状态'
      }));
      tableData.value = transformedData;
      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 materialsDistributionViewState = reactive({
  show: false,
  id: ''
});

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

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

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

const handleCancel = () => {
  materialsDistributionViewState.show = false;
  materialsDistributionAddState.show = false;
};

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

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