<template>
  <el-card class="hl-card">
    <template #header>
      <span>BOM清单维护</span>
    </template>
    <ContentWrap class="borderxx">
      <!-- 搜索工作栏 -->
      <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="120px">
        <el-form-item label="BOM编号" prop="code">
          <el-input v-model="queryParams.code" placeholder="请输入BOM编号" clearable @keyup.enter="handleQuery" class="!w-240px" />
        </el-form-item>
        <el-form-item label="计划单号" prop="planCode">
          <el-input v-model="queryParams.planCode" 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="bomStatus">
          <el-select v-model="queryParams.bomStatus" 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:30px">
          <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="handleImport"
              v-hasPermi="['system:user:import']"
            >
              <Icon icon="ep:upload" /> 导入BOM
            </el-button>
            <el-button
            type="success"
            plain
            @click="handleExportDetail"
            :loading="exportLoading"
            v-hasPermi="['system:user:import']"
          >
            <Icon icon="ep:download" class="mr-5px" /> 导出BOM明细
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :show-overflow-tooltip="true" :row-class-name="tableRowClassName" class="hl-table">
				
				   
        <el-table-column fixed label="序号" align="center" type="index" width="60" />
        <el-table-column fixed label="BOM编号" align="center" prop="bomCode" min-width="430">
          <template #default="scope">
            <el-button type="primary" plain link @click="openDetail('detail', scope.row.id)">{{ scope.row.bomCode }}</el-button>
          </template>
        </el-table-column>
        <el-table-column fixed label="计划单号" align="center" prop="planCode" min-width="180" >
          <template #default="scope">
            <el-button type='primary' link @click="openPlanTaskDetail(scope.row.planCode)">{{ scope.row.planCode }}</el-button>
          </template>
        </el-table-column>
        <el-table-column fixed label="项目编号" align="center" prop="projectCode" min-width="220" />
        <el-table-column label="客户名称" align="center" prop="customerName" min-width="240" />
        <el-table-column label="项目名称" align="center" prop="projectName" min-width="180" />
        <el-table-column label="子项目名称" align="center" prop="projectSubName" min-width="180" />
        <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" min-width="220" />
        <el-table-column fixed="right" label="BOM版本号" align="center" prop="version" min-width="140" />
        <el-table-column fixed="right" label="单据状态" align="center" prop="bomStatus" min-width="140">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.HELI_BOM_STATUS" :value="scope.row.bomStatus" />
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" min-width="160">
          <template #default="scope">
            <el-button link type="primary" v-if="scope.row.bomStatus!=2 && scope.row.bomStatus!=6"  @click="openDetail('update', scope.row.id)">
              编辑
            </el-button>
            <el-button link type="primary" @click="openDetail('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>
  <!-- 用户导入对话框 -->
  <BomImportForm ref="importFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as ProcessBomApi from '@/api/heli/processbom'
import { useCommonStateWithOut } from '@/store/modules/common'
import BomImportForm from './bomImportForm.vue'
import * as planTaskBomApi from '@/api/heli/plantaskbom'

defineOptions({ name: 'ProcessBom' })

const commonStore = useCommonStateWithOut()
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,
  code: undefined,
  planCode: undefined,
  projectCode: undefined,
  customerName: undefined,
  projectName: undefined,
  projectSubName: undefined,
  version: undefined,
  bomStatus: undefined,
  remark: 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.code=id
	  }
    const data = await ProcessBomApi.getProcessBomPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
/** 导出按钮操作 */
const handleExportDetail = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ProcessBomApi.exportProcessBom(queryParams)
    download.excel(data, 'bom明细表.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

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

/** BOM导入 */
const importFormRef = ref()
const handleImport = () => {
  importFormRef.value.open()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

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

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ProcessBomApi.exportProcessBom(queryParams)
    download.excel(data, '工艺bom.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 添加/修改操作 */
const openDetail = (type: string, id?: number) => {
  commonStore.setStore('active', type)
  commonStore.setStore('id', id)
  router.push({
    name: 'ProcessBomDetail',
    query: {
      operateId: Math.random()
    }
  })
}
// 定义行类名的方法
const tableRowClassName = ({ row }) => {
  switch (row.bomEditStatus) {
    case 4:
      return 'status-4';
    default:
      return '';
  }
};
const openPlanTaskDetail = async (code) => {
  // var data = await planTaskBomApi.getPlanTaskId(id as number)
  router.push({ path: '/plan/PlanTasks', query: {  code: code } })
}
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
<style >
.status-4 {
  background-color: wheat !important;
}
</style>