<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="任务单" prop="taskId">
            <el-select
              v-model="queryParams.taskId"
              @change="handleQuery"
              clearable
              filterable
              placeholder="请选择"
              class="!w-180px"
            >
              <el-option
                v-for="item in taskList"
                :key="item.id"
                :label="item.code"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="计划编码" prop="code">
            <el-input
              v-model="queryParams.code"
              placeholder="请输入计划编码"
              clearable
              @keyup.enter="handleQuery"
              class="!w-180px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="产品" prop="productId">
            <el-select
              v-model="queryParams.productId"
              @change="handleQuery"
              clearable
              filterable
              placeholder="请选择产品"
              class="!w-180px"
            >
              <el-option
                v-for="item in productList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="queryParams.remark"
              placeholder="请输入备注"
              clearable
              @keyup.enter="handleQuery"
              class="!w-180px"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="计划开始" prop="planStartTime">
            <el-date-picker
              v-model="queryParams.planStartTime"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="daterange"
              @change="handleQuery"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
              class="!w-160px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="计划结束" prop="planEndTime">
            <el-date-picker
              v-model="queryParams.planEndTime"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="daterange"
              @change="handleQuery"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
              class="!w-160px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <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:plan:export']"
            >
              <Icon icon="ep:download" class="mr-5px" /> 导出
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-tabs v-model="activeName" @tab-click="handleTabClick">
      <el-tab-pane label="所有" name="" />
      <el-tab-pane label="计划" name="0" />
      <el-tab-pane label="派工" name="1" />
      <el-tab-pane label="开工" name="2" />
      <el-tab-pane label="暂停" name="3" />
      <el-tab-pane label="完工" name="4" />
      <el-tab-pane label="入库" name="5" />
    </el-tabs>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">

      <!-- 进度子表的列表 -->
      <el-table-column  type="expand" >
        <template #default="scope">
          <el-tabs  model-value="progressList">
            <el-tab-pane label="工序进度" name="progressList">
              <ProgressList :plan-id="scope.row.id" :plan-number="scope.row.planNumber"/>
            </el-tab-pane>
            <el-tab-pane v-if="scope.row.status > 0" label="投料明细" name="itemRequisitionDetail">
              <ItemRequisitionDetailList :item-requisition-id="scope.row.requisitionId" />
            </el-tab-pane>
          </el-tabs>
        </template>
      </el-table-column>
      <el-table-column label="计划编码" align="center" prop="code" min-width="140px" />
      <el-table-column label="产品" align="center" prop="productName"  min-width="150px" />
      <el-table-column label="计划成型数" align="center" prop="planNumber" />
      <el-table-column label="计划热压数" align="center" prop="reyaNumber" />
      <el-table-column label="状态" align="center" prop="status" sortable>
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MES_PLAN_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="计划开始时间" align="center" sortable prop="planStartTime" :formatter="dateFormatter2" width="150px"/>
      <el-table-column label="计划结束时间" align="center" sortable prop="planEndTime" :formatter="dateFormatter2" width="150px"/>
      <el-table-column label="备注" align="center" prop="remark" />

      <el-table-column label="操作" align="center" fixed="right" width="250">
        <template #default="scope">
          <el-button
            link
            type="info"
            @click="message(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>


</template>

<script setup lang="ts">
import { getIntDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import {dateFormatter, dateFormatter2} from '@/utils/formatTime'
import download from '@/utils/download'
import { PlanApi, PlanVO } from '@/api/mes/plan'

import {ProductApi, ProductVO} from "@/api/erp/product/product";
import {TaskApi, TaskVO} from "@/api/mes/task";

import ProgressList from "./ProgressList.vue";

import ItemRequisitionDetailList
  from "@/views/mes/itemrequisition/components/ItemRequisitionDetailList.vue";

const productList = ref<ProductVO[]>([]) // 产品列表
const taskList = ref<TaskVO[]>([]) // 列表
/** 生产计划 列表 */
defineOptions({ name: 'PlanProgress' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const {query}  = useRoute() // 查询参数
const loading = ref(true) // 列表的加载中
const list = ref<PlanVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  code: undefined,
  productId: undefined,
  taskDetailId: undefined,
  taskId: undefined,
  planNumber: undefined,
  finishNumber: undefined,
  status: undefined,
  planStartTime: [],
  planEndTime: [],
  startTime: [],
  endTime: [],
  productionManagerId: undefined,
  remark: undefined,
  isEnable: undefined,
  feedingPipeline: undefined,
  requisitionId: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PlanApi.getPlanPage(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 handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await PlanApi.exportPlan(queryParams)
    download.excel(data, '生产计划.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  // 加载产品、任务单
  productList.value = await ProductApi.getMesProductSimpleList()
  taskList.value = await TaskApi.getTaskList()
  await getList()

})

/** tab 切换 */
let activeName = ''
const handleTabClick = (tab: TabsPaneContext) => {
  queryParams.status = tab.paneName
  handleQuery()
}
</script>
