<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="auto"
      label-position="left"
    >
      <el-form-item :label="t('ProductionPlan.TaskSummary.searchCodeLabel')" prop="code">
        <el-input
          v-model="queryParams.code"
          :placeholder="t('ProductionPlan.TaskSummary.searchCodePlaceholder')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item :label="t('ProductionPlan.TaskSummary.searchOrderLabel')" prop="orderDate">
        <el-date-picker
          v-model="queryParams.orderDate"
          @change="handleQuery"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          :start-placeholder="t('ProductionPlan.TaskSummary.searchOrderStartPlaceholder')"
          :end-placeholder="t('ProductionPlan.TaskSummary.searchOrderEndPlaceholder')"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item :label="t('ProductionPlan.TaskSummary.searchDeliveryLabel')" prop="deliveryDate">
        <el-date-picker
          v-model="queryParams.deliveryDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          @change="handleQuery"
          type="daterange"
          :start-placeholder="t('ProductionPlan.TaskSummary.searchDeliveryStartPlaceholder')"
          :end-placeholder="t('ProductionPlan.TaskSummary.searchDeliveryEndPlaceholder')"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item :label="t('ProductionPlan.TaskSummary.searchRemarkLabel')" prop="remark">
        <el-input
          v-model="queryParams.remark"
          :placeholder="t('ProductionPlan.TaskSummary.searchRemarkPlaceholder')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item :label="t('ProductionPlan.TaskSummary.searchCreateTimeLabel')" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          @change="handleQuery"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          :start-placeholder="t('ProductionPlan.TaskSummary.searchCreateTimeStartPlaceholder')"
          :end-placeholder="t('ProductionPlan.TaskSummary.searchCreateTimeEndPlaceholder')"
          :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" /> {{ t('ProductionPlan.TaskSummary.buttonSearchText') }}</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> {{ t('ProductionPlan.TaskSummary.buttonResetText') }}</el-button>

        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['mes:task:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> {{ t('ProductionPlan.TaskSummary.buttonExportText') }}
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-tabs v-model="activeName" @tab-click="handleTabClick">
      <el-tab-pane :label="t('ProductionPlan.TaskSummary.tabAllLabel')" name="" />
      <el-tab-pane :label="t('ProductionPlan.TaskSummary.tabIssuedLabel')" name="2" />
      <el-tab-pane :label="t('ProductionPlan.TaskSummary.tabPlanLabel')" name="3" />
      <el-tab-pane :label="t('ProductionPlan.TaskSummary.tabStartLabel')" name="4" />
      <el-tab-pane :label="t('ProductionPlan.TaskSummary.tabFinishedLabel')" name="5" />
      <el-tab-pane :label="t('ProductionPlan.TaskSummary.tabStoredLabel')" name="6" />
    </el-tabs>
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <el-table-column :label="t('ProductionPlan.TaskSummary.tableTaskCodeColumn')" align="center" prop="code" width="150px"/>
      <el-table-column :label="t('ProductionPlan.TaskSummary.tableOrderDateColumn')" align="center" prop="orderDate" :formatter="dateFormatter2" />
      <el-table-column :label="t('ProductionPlan.TaskSummary.tableDeliveryDateColumn')" align="center" prop="deliveryDate" :formatter="dateFormatter2"/>
      <el-table-column :label="t('ProductionPlan.TaskSummary.tableStatusColumn')" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MES_TASK_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column :label="t('ProductionPlan.TaskSummary.tableRemarkColumn')" align="center" prop="remark" />

      <el-table-column :label="t('ProductionPlan.TaskSummary.tableOperateColumn')" align="center" min-width="200px">
        <template #default="scope">
          <el-button link type="info" @click="openItemNeed(scope.row.code, scope.row.id)" v-hasPermi="['mes:task:query']">
            {{ t('ProductionPlan.TaskSummary.actionMaterialLabel') }}
          </el-button>
          <el-button link type="primary" @click="openPlan(scope.row.id)" v-hasPermi="['mes:plan:query']">
            {{ t('ProductionPlan.TaskSummary.actionViewPlanLabel') }}
          </el-button>
          <!-- <el-button link v-if="scope.row.status<5" type="success" @click="openTaskPlanForm(scope.row.id, scope.row.code)" v-hasPermi="['mes:task:plan']">
            排产
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 子表的列表 -->
  <ContentWrap>
    <el-tabs model-value="taskDetail">
      <el-tab-pane :label="t('ProductionPlan.TaskSummary.detailTabSummaryLabel')" name="taskDetail">
        <TaskDetailList :task-id="currentRow.id" />
      </el-tab-pane>
    </el-tabs>
  </ContentWrap>
  <!-- 物料列表 -->
  <ItemNeedIndex ref="formRef" @success="getList" />
  <!-- 排程弹窗 -->
  <TaskPlanForm ref="taskPlanFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter, dateFormatter2 } from '@/utils/formatTime'
import download from '@/utils/download'
import { TaskApi, TaskVO } from '@/api/mes/task'
import TaskDetailList from './components/TaskDetailList.vue'
import PlanForm from "@/views/mes/plan/PlanForm.vue";
import ItemNeedIndex from "@/views/mes/bom/ItemNeedIndex.vue";
import TaskPlanForm from "@/views/mes/tasksummary/components/TaskPlan.vue";

/** 生产任务单 列表 */
defineOptions({ name: 'TaskSummary' })

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

const loading = ref(true) // 列表的加载中
const list = ref<TaskVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  code: undefined,
  orderDate: [],
  deliveryDate: [],
  status: undefined,
  processInstanceId: undefined,
  remark: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const { push } = useRouter()
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await TaskApi.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 handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await TaskApi.exportTask(queryParams)
    download.excel(data, '生产任务单.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 选中行操作 */
const currentRow = ref({}) // 选中行
const handleCurrentChange = (row) => {
  currentRow.value = row
}

/** 初始化 **/
onMounted(() => {
  getList()
})
/** 跳转生产计划页面**/
const openPlan = (taskId: number) => {
  // 跳转页面并设置请求参数，使用 `query` 属性
  //const productId = null;
  //push({ name: 'TaskToPlan', params: { taskId, productId} })
  push('/mes/plan?taskId=' + taskId)
}
/** 物料需求 */
const formRef = ref()
const openItemNeed = (taskCode:string, taskId: number) => {
  formRef.value.open("task","任务单-"+taskCode, taskId)
}

/** 添加/修改操作 */
const taskPlanFormRef = ref()
const openTaskPlanForm = (taskId: number, taskCode:string) => {
  taskPlanFormRef.value.open(taskCode, taskId )
}
/** tab 切换 */
let activeName = ''
const handleTabClick = (tab: TabsPaneContext) => {
  queryParams.status = tab.paneName
  currentRow.value = {}
  handleQuery()
}
</script>
