<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="" prop="itemId">
        <el-select v-model="queryParams.itemId" @change="handleQuery" clearable filterable placeholder="请选择" class="!w-240px">
          <el-option
            v-for="item in productList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="明细类别" prop="detailType">
        <el-select
          v-model="queryParams.detailType"
          @change="handleQuery"
          placeholder="请选择明细类别"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.MES_STOCK_WORKSHOP_DETAIL_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="操作人员" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入操作人员"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="单据时间" prop="operateTime">
        <el-date-picker
          v-model="queryParams.operateTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><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="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['mes:stock-workshop-detail:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-tabs v-model="activeName" @tab-click="handleTabClick">
      <el-tab-pane label="所有" name="" />
      <el-tab-pane label="投料出库" name="out" />
      <el-tab-pane label="领料入库" name="in" />
      <el-tab-pane label="损耗调整" name="clear" />
    </el-tabs>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="明细类别" align="center" prop="detailType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MES_STOCK_WORKSHOP_DETAIL_TYPE" :value="scope.row.detailType" />
        </template>
      </el-table-column>
      <el-table-column label="原料" align="center" prop="itemName" />
      <el-table-column label="单位" align="center" prop="unitName" />
      <el-table-column label="调整数" align="center" prop="count" />
      <el-table-column label="库存数" align="center" prop="stockCount" />

      <el-table-column label="领料单" align="center" prop="itemRequisitionCode" />
      <el-table-column label="投料单" align="center" prop="feedingRecordCode" />
      <el-table-column label="操作人员" align="center" prop="userName" />
      <el-table-column
        label="单据时间"
        align="center"
        prop="operateTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />

    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

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

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { StockWorkshopDetailApi, StockWorkshopDetailVO } from '@/api/mes/stockworkshop'
import StockWorkshopDetailForm from './StockWorkshopDetailForm.vue'
import {ProductApi, ProductVO} from "@/api/erp/product/product";

/** 车间仓明细 列表 */
defineOptions({ name: 'StockWorkshopDetail' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const productList = ref<ProductVO[]>([]) // 产品列表
const loading = ref(true) // 列表的加载中
const list = ref<StockWorkshopDetailVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  itemId: undefined,
  count: undefined,
  detailType: undefined,
  itemRequisitionCode: undefined,
  itemRequisitionDetailId: undefined,
  feedingRecordCode: undefined,
  feedingRecordId: undefined,
  userId: undefined,
  operateTime: [],
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await StockWorkshopDetailApi.getStockWorkshopDetailPage(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) => {
  formRef.value.open(type, id)
}

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

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await StockWorkshopDetailApi.exportStockWorkshopDetail(queryParams)
    download.excel(data, '车间仓明细.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  await getList()
  // 加载产品、
  productList.value = await ProductApi.getItemSimpleList()
})
/** tab 切换 */
let activeName = ''
const handleTabClick = (tab: TabsPaneContext) => {
  queryParams.detailType = tab.paneName
  handleQuery()
}
</script>
