<template>
  <div class="app-container">
    <div v-show="!materialsDeclarationViewState.show && !materialsDeclarationEidtState.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="dispatching_purpose" />
        <el-table-column label="调度日期" align="center" prop="dispatch_date" />
        <el-table-column label="申报单位" align="center" prop="application_unit" />
        <el-table-column label="申请人" align="center" prop="declarant" />
        <el-table-column label="审批情况" align="center" prop="approval_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>
  </div>
  <MaterialsDeclarationView v-if="materialsDeclarationViewState.show" :event-id="materialsDeclarationViewState.eventId" @close="handleCancel" />
  <MaterialsDeclarationEidt v-if="materialsDeclarationEidtState.show" :event-id="materialsDeclarationEidtState.eventId" @close="handleCancel" />
  <MaterialsDistributionAdd v-if="materialsDistributionAddState.show" @close="handleCancel" />
</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 MaterialsDeclarationView from './materialsDeclarationView.vue'; // 查看详情组件
import MaterialsDeclarationEidt from './materialsDeclarationEidt.vue'; // 编辑组件
import MaterialsDistributionAdd from './materialsDistributionAdd.vue'; // 编辑组件
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({
  dispatching_purpose: '',
  dispatch_date: '',
  application_unit: '',
  declarant: '',
  approval_status: ''
});

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

const { queryParams, form } = toRefs(data);
const mockData = [
  {
    id: 1,
    dispatch_date: '2024-10-29 12:23:00',
    dispatching_purpose: '应对高州泰利台风前置',
    application_unit: '茂名市应急管理局',
    declarant: 'XX',
    approval_status: '未审批'
  },
  {
    id: 2,
    dispatch_date: '2024-10-30 12:23:00',
    dispatching_purpose: '应对电白泰利台风前置',
    application_unit: '茂名市应急管理局',
    declarant: 'XX',
    approval_status: '审批通过'
  },
  {
    id: 3,
    dispatch_date: '2024-10-31 12:23:00',
    dispatching_purpose: '应对茂南泰利台风前置',
    application_unit: '茂名市应急管理局',
    declarant: 'XX',
    approval_status: '审批不通过'
  }
];

const fetchWorkrData = () => {
  loading.value = true;
  tableData.value = mockData;
  total.value = mockData.length;

  loading.value = false;
};

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

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

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

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

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

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

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

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

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