<!-- 应急物资分类表 -->
<template>
  <basic-container>
    <el-form class="whole_form" :inline="true">
      <el-form-item label="仓库名称">
        <el-select v-model="searchList.warehouseId" style="width: 200px" filterable clearable>
          <el-option
            v-for="item in ckNameOptions"
            :key="item.deptId"
            :label="item.abbreviationName"
            :value="item.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="物资名称">
        <el-input v-model="searchList.materialNameVo" style="width: 200px" clearable />
      </el-form-item>
      <el-form-item>
        <el-button :icon="Search" type="primary" @click="getList">查询</el-button>
        <el-button :icon="RefreshRight" @click="clearSearch">清空</el-button>
        <el-button type="primary" :icon="Plus" @click="openDetail">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="width: 100%" stripe border>
      <el-table-column type="index" label="序号" align="center" width="60">
        <template #default="scope">
          {{ (page.currentPage - 1) * page.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="deptName" label="垂管局" align="center" />
      <el-table-column prop="warehouseName" label="仓库" align="center" />
      <el-table-column prop="materialName" label="物资名称" align="center" />
      <el-table-column prop="inboundTime" label="入库时间" align="center" width="115" />
      <el-table-column prop="unitPrice" label="单价(元)" align="center" width="100" />
      <el-table-column prop="status" label="状态" align="center" width="100">
        <template #default="scope">
          {{ scope.row.status == 0 ? "未完成" : "已完成" }}
        </template>
      </el-table-column>
      <el-table-column prop="submitTimeVo" label="填报时间" align="center" width="115" />
      <el-table-column label="操作" width="150" align="center">
        <template #default="{ row }">
          <el-button link type="success" size="small" @click="openDetail(row, 'look')">查看</el-button>
          <el-button link type="primary" size="small" @click="openDetail(row, 'edit')">编辑</el-button>
          <el-button link type="danger" size="small" @click="deleteClick(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="page.total > 0"
      v-model:page="page.currentPage"
      v-model:limit="page.pageSize"
      :total="page.total"
      @pagination="getList"
    />
  </basic-container>
</template>

<script setup lang="ts">
import { Plus, Search, RefreshRight } from "@element-plus/icons-vue";
import { ref, reactive } from "vue";
import { getDeptTypeListCk, getListPage, deleteData } from "@/api/equipmentFailure";
import { ElMessage, ElMessageBox } from "element-plus";
import { setQuery } from "@/hooks/detailQuery/index";

export interface Department {
  deptId: string;
  abbreviationName: string;
}

const ckNameOptions = ref<Department[]>([]);
const searchList = ref({
  warehouseId: "",
  materialNameVo: ""
});
const tableData: any = ref([]);
const page = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
});

/** 初始化 **/
getDeptTypeListCk().then((res: any) => {
  if (res.code === 0) {
    ckNameOptions.value = res.data;
  }
});

// 清空搜索条件
const clearSearch = () => {
  searchList.value = {
    warehouseId: "",
    materialNameVo: ""
  };
  getList();
};
/** 添加/修改/查看进入详情操作 */
const openDetail = (row: any, type: string) => {
  setQuery("/equipmentFailure/faultMngDetail", { id: row.id, type: type });
};
/** 列表数据获取 */
const getList = () => {
  const data = reactive({
    size: page.pageSize,
    current: page.currentPage,
    ...searchList.value
  });
  getListPage(data).then((res: any) => {
    if (res.code === 0) {
      tableData.value = res.data.records;
      page.total = res.data.total;
    }
  });
};
/** 删除操作 */
const deleteClick = (row: any) => {
  ElMessageBox.confirm(`是否删除此条数据？`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    deleteData(row.id).then((res: any) => {
      if (res.code === 0) {
        getList();
        ElMessage({
          message: "删除成功！",
          type: "success"
        });
      } else {
        ElMessage({
          message: "删除失败！",
          type: "error"
        });
      }
    });
  });
};

getList();
</script>

<style lang="scss" scoped></style>
