<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="createTime">
        <el-date-picker class="!w-240px" v-model="queryParams.createTime" type="date" value-format="x"
          placeholder="单据日期" />
      </el-form-item>
      <el-form-item label="生产计划单号" prop="planNo">
        <el-input v-model="queryParams.planNo" 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="status">
        <el-select v-model="queryParams.status" placeholder="下拉选择" clearable class="!w-240px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.HELI_PROJECT_MATERIAL_PLAN_STATUS)" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </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-button type="primary" plain @click="openForm('create')">
          <Icon icon="ep:plus" 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="createTime" :formatter="dateFormatter" min-width="180px" fixed>
        <template #default="scope">
          {{
            formatDate(scope.row.createTime, 'YYYY-MM-DD')
          }}
        </template>
      </el-table-column>
      <el-table-column label="生产计划单号" align="center" prop="planNo" min-width="180px" />
      <el-table-column label="项目名称" align="center" prop="projectName" min-width="180px" />
      <el-table-column label="备注" align="center" prop="description" min-width="180px" />
      <el-table-column label="创建人" align="center" min-width="120px" prop="creator" >
        <template #default="scope">
            {{ userInit.find((user) => user.id == scope.row.creator)?.nickname }}
          </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" min-width="220" />
      <el-table-column label="送审人" align="center" prop="submitUserId" min-width="120" >
        <template #default="scope">
            {{ userInit.find((user) => user.id == scope.row.submitUserId)?.nickname }}
          </template>
      </el-table-column>
      <el-table-column label="送审时间" align="center" prop="submitTime" :formatter="dateFormatter" min-width="220" />
      <el-table-column label="审核人" align="center" prop="auditor" min-width="120" >
        <template #default="scope">
            {{ userInit.find((user) => user.id == scope.row.auditor)?.nickname }}
          </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="auditTime" :formatter="dateFormatter" min-width="220" />
      <el-table-column label="单据状态" align="center" prop="status" fixed="right" min-width="150">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.HELI_PROJECT_MATERIAL_PLAN_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" fixed="right" min-width="180">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)" v-if="scope.row.status == 1 || scope.row.status ==4">
            编辑
          </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>

  <!-- 表单弹窗：添加/修改 -->
  <MaterialPlanForm ref="formRef" @success="getList" />
</el-card>

</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter, formatDate } from '@/utils/formatTime'
import download from '@/utils/download'
import * as MaterialPlanApi from '@/api/heli/materialplan'
import * as UserApi from '@/api/system/user'

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,
  submitTime: [],
  auditor: undefined,
  auditTime: [],
  status: undefined,
  description: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

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

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  queryParams.createTime= formatDate(queryParams.createTime, 'YYYY-MM-DD')
  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/materialplandetail', query: { id: id } })
      break;
    default:
      break;
  }

}

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

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await MaterialPlanApi.exportMaterialPlan(queryParams)
    download.excel(data, '物料需求计划.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

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