<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="下达" prop="orderDate">
        <el-date-picker
          v-model="queryParams.orderDate"
          @change="handleQuery"
          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 label="交货" prop="deliveryDate">
        <el-date-picker
          v-model="queryParams.deliveryDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          @change="handleQuery"
          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 label="" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          @change="handleQuery"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MES_TASK_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          @change="handleQuery"
          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="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['mes:task:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['mes:task:export']"
        >
          <Icon icon="ep:download" 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"
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <el-table-column label="任务单" align="center" prop="code" />
      <el-table-column label="下达日期" align="center" prop="orderDate" :formatter="dateFormatter2" />
      <el-table-column label="交货日期" align="center" prop="deliveryDate" :formatter="dateFormatter2"/>
      <el-table-column label="状态" 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="备注" align="center" prop="remark" />

      <el-table-column label="操作" 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']"
          >
            物料
          </el-button>
          <el-button
            link
            type="success"
            @click="openPlan(scope.row.id)"
            v-hasPermi="['mes:plan:query']"
          >
            查看计划
          </el-button>
          <el-button
            link
            type="success"
            @click="openTaskPlanForm(scope.row.id, scope.row.code)"
            v-hasPermi="['mes:plan:query']"
          >
            排产
          </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="任务单汇总明细" 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 )
}
</script>
