<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="120px">
        <el-form-item label="生产任务单号" prop="taskNo">
          <el-input v-model="queryParams.taskNo" placeholder="生产任务单号" clearable @keyup.enter="handleQuery" class="!w-240px" />
        </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="projectCode">
          <el-input v-model="queryParams.projectCode" placeholder="项目编号" clearable @keyup.enter="handleQuery" class="!w-240px" />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="queryParams.customerName" 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="projectSubCode">
          <el-input v-model="queryParams.projectSubCode" 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_BOM_STATUS)" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item style="margin-left: 20px">
          <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 type="index" width="80px" fixed label="序号" align="center" />
        <el-table-column label="生产任务单号" align="center" prop="taskNo" width="170px" fixed >
           <template #default="scope">
            <el-button text type="primary" @click="openForm('detail', scope.row.id)">
              {{scope.row.taskNo}}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="客户名称" align="center" prop="customerName" width="170px" />
        <el-table-column label="项目名称" align="center" prop="projectName"    width="160px" />
        <el-table-column label="项目结束时间" align="center" prop="projectEndTime" width="140px">
          <template #default="scope">
            {{
              formatDate(scope.row.projectEndTime, 'YYYY-MM-DD')
            }}
          </template>
        </el-table-column>
        <el-table-column label="子项目名称" align="center" prop="projectSubName" width="160px" />
        <el-table-column label="BOM版本号" align="center" prop="bomVersion" width="130px" />
        <el-table-column label="生产计划单号" align="center" prop="planNo" width="150px"  />
        <el-table-column label="项目编号" align="center" prop="projectCode" width="150px"  />
        <el-table-column label="子项目编号" align="center" prop="projectSubCode" width="220px" />
        <el-table-column label="BOM清单号" align="center" prop="bomCode" width="220px" />
        <!-- <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="220" />
        <el-table-column label="备注" align="center" prop="description" width="220px" /> -->
        <el-table-column fixed="right" label="单据状态" align="center" prop="status" width="150">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.HELI_BOM_STATUS" :value="scope.row.status" />
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" fixed="right" width="220px">
          <template #default="scope">
            <el-button link type="primary" @click="openForm('update', 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 { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import {  formatDate } from '@/utils/formatTime'
import download from '@/utils/download'
import * as PlanTaskApi from '@/api/heli/plantask'
import { useRoute } from 'vue-router';
import routeParamsCache from '@/utils/routeParamsCache'
defineOptions({ name: 'PlanTask' })

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

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  taskNo: undefined,
  planNo: '',
  projectName: undefined,
  customerName: undefined,
  projectCode: undefined,
  projectSubCode: undefined,
  projectSubName: undefined,
  status: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
	   const id = history.state.idid;
	  	  if(id){
	  	  		 queryParams.planNo=id
	  	  }
    const data = await PlanTaskApi.getPlanTaskPage(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 formRef = ref()
const openForm = (type: string, id?: number) => {
  router.push({ path: '/plan/plantaskedit', query: { type: type, id: id } })
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await PlanTaskApi.exportPlanTask(queryParams)
    download.excel(data, '生产计划任务.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
const route = useRoute()
onMounted(async () => {
  let params = routeParamsCache.get(route.path);
  if (params ) {
    Object.assign(queryParams, params);
  }
  const code = route.query.code;
  if(code != null && code!=''){
    queryParams.planNo = code as string;
  }
  getList()
})
onBeforeUnmount(() => {
  const plainParams = JSON.parse(JSON.stringify(queryParams));
  routeParamsCache.set(route.path, plainParams);
});

window.addEventListener('beforeunload', () => {
  const plainParams = JSON.parse(JSON.stringify(queryParams));
  routeParamsCache.set(route.path, plainParams);
});
</script>
