<template>
<el-card class="hl-card">
  <template #header>
    <span>物料需求计划管理</span>
  </template>
  <ContentWrap class="borderxx">
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="130px">
      <el-form-item label="需求计划单号" prop="projectMaterialPlanNo">
        <el-input v-model="queryParams.projectMaterialPlanNo" placeholder="物料计划单号" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="queryParams.projectName" placeholder="项目名称" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="子项目名称" prop="projectSubName">
        <el-input v-model="queryParams.projectSubName" placeholder="请输入子项目名称" clearable @keyup.enter="handleQuery" class="!w-240px" />
      </el-form-item>
      <el-form-item label="零件名称" prop="materialName">
        <el-input v-model="queryParams.materialName" placeholder="请输入零件名称" clearable @keyup.enter="handleQuery" class="!w-240px" />
      </el-form-item>
      <el-form-item style="margin-left:15px">
        <el-button @click="handleQuery" type="primary">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>

      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" class="hl-table">
      <el-table-column label="序号" type="index" align="center" fixed min-width="70px" />
      <el-table-column label="物料需求计划单号" align="center" prop="projectMaterialPlanNo" min-width="180" fixed>
        <template #default="scope">
          <el-button text type="primary" @click="openForm('detail', scope.row.id)">
            {{ scope.row.projectMaterialPlanNo }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="projectName" min-width="180px" />
      <el-table-column label="子项目名称" align="center" prop="name" min-width="180px" />
      <el-table-column label="客户简称" align="center" prop="brief" min-width="180px" />
      <el-table-column label="制单日期" align="center" prop="matPlanDate" :formatter="dateFormatter1" min-width="150" />
      <el-table-column label="需求计划类型" align="center" prop="matType"  min-width="150">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.HELI_MAT_TYPE" :value="scope.row.matType" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" min-width="180">
        <template #default="scope">
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
          >
            删除
          </el-button>
          <el-button link type="primary" @click="openForm('detail', scope.row.id)">
            详情信息
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

</el-card>

</template>

<script setup lang="ts">
import {  DICT_TYPE } from '@/utils/dict'
import { dateFormatter1} from '@/utils/formatTime'
import * as MaterialPlanApi from '@/api/heli/materialplan'


defineOptions({ name: 'MaterialPlan' })
const router = useRouter()
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  id: undefined,
  projectMaterialPlanNo: undefined,
  projectId: undefined,
  projectPlanId: undefined,
  submitUserId: undefined,
  projectName: undefined,
  projectSubName: undefined,
  materialName:undefined,
  submitTime: [],
  auditor: undefined,
  auditTime: [],
  status: undefined,
  description: undefined,
  createTime: [],
  matType:0,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MaterialPlanApi.getMaterialPlanPages(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */

const openForm = (type: string, id?: number) => {

  switch (type) {
    case 'create':
      router.push({ path: '/purchase/materialplanadd', query: { id: id } })
      break;
    case 'update':
      router.push({ path: '/purchase/materialplanedit', query: { id: id } })
      break;
    case 'detail':
      router.push({ path: '/purchase/managementdetail', query: { id: id } })
      break;
    default:
      break;
  }

}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await MaterialPlanApi.delMaterialPlan(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}


const userInit = ref()
/** 初始化 **/
onMounted(async () => {
  //用户列表数据
  // userInit.value = await UserApi.getSimpleUserList()
  getList()
})
</script>
