<template>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column :label="t('ProductionPlan.TaskSummary.detailTableTaskCodeColumn')" align="center" prop="taskCode" />
      <el-table-column :label="t('ProductionPlan.TaskSummary.detailTableProductCodeColumn')" align="center" prop="barCode" />
      <el-table-column :label="t('ProductionPlan.TaskSummary.detailTableProductNameColumn')" align="center" prop="productName" sortable/>
       <el-table-column :label="t('ProductionPlan.TaskSummary.detailTableTotalNumberColumn')" align="center" prop="totalNumber" />
      <el-table-column :label="t('ProductionPlan.TaskSummary.detailTablePlanNumberColumn')" align="center" prop="planNumber" />
      <el-table-column :label="t('ProductionPlan.TaskSummary.detailTableUnplanNumberColumn')" align="center">
        <template #default="scope">
          <span class="el-alert--warning" style="color: #e66126">
            {{scope.row.totalNumber-scope.row.planNumber>0 ? scope.row.totalNumber-scope.row.planNumber : 0}}
          </span>
        </template>
      </el-table-column>
      <el-table-column :label="t('ProductionPlan.TaskSummary.detailTableOperateColumn')" align="center" width="220px">
        <template #default="scope">
          <el-button
            link
            type="info"
            @click="openItemNeed(scope.row.productName, scope.row.productId,
            scope.row.totalNumber-scope.row.planNumber)"
            v-hasPermi="['mes:task:query']"
          >
            {{ t('ProductionPlan.TaskSummary.detailActionMaterialLabel') }}
          </el-button>
          <el-button
            link
            type="primary"
            @click="openPlan(scope.row.taskId, scope.row.productId)"
            v-hasPermi="['mes:plan:query']"
          >
            {{ t('ProductionPlan.TaskSummary.detailActionViewPlanLabel') }}
          </el-button>
          <el-button
            link
            type="success"
            @click="addPlanForm(scope.row.taskId, scope.row.productId,
            scope.row.totalNumber - scope.row.planNumber,scope.row.taskDetailIds)"
            v-hasPermi="['mes:task:plan']"
          >
            {{ t('ProductionPlan.TaskSummary.detailActionCreatePlanLabel') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

  </ContentWrap>
  <!-- 表单弹窗：添加/修改 -->
  <PlanForm ref="formRef" @success="getList" />
  <!-- 物料列表 -->
  <ItemNeedIndex ref="itemFormRef" @success="getList" />
</template>
<script setup lang="ts">
import { TaskApi } from '@/api/mes/task'
import PlanForm from "@/views/mes/plan/PlanForm.vue";
import ItemNeedIndex from "@/views/mes/bom/ItemNeedIndex.vue";

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const { push } = useRouter()
const props = defineProps<{
  taskId?: number // task ID（主表的关联字段）
}>()
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  taskId: undefined as unknown
})

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.taskId,
  (val: number) => {
    if (!val) {
      list.value = []
      return
    }
    queryParams.taskId = val
    handleQuery()
  },
    { immediate: true, deep: true }
)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    list.value = await TaskApi.getTaskDetailSummaryListByTaskId(queryParams.taskId)
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 添加/修改操作 */
const formRef = ref()
const addPlanForm = (taskId: number, productId: number, number?: number, taskDetailIds?: string) => {
  if (!props.taskId) {
    message.error(t('ProductionPlan.TaskSummary.detailSelectTaskTip'))
    return
  }
  formRef.value.open('create', undefined, taskId, productId, number,taskDetailIds)
}
const route = useRoute()
/** 跳转生产计划页面**/
const openPlan = (taskId: number, productId: number) => {
  // 跳转页面并设置请求参数，使用 `query` 属性
  route.matched.filter(item => item.meta?.title)[1].meta.title = String(taskId) + String(productId)
  push('/mes/plan?taskId=' + taskId + '&productId=' + productId)
}

/** 物料需求 */
const itemFormRef = ref()
const openItemNeed = (productName:string, productId: number, number:number) => {
  number = number>0 ? number : 0;
  itemFormRef.value.open("product",productName, productId, number)
}
</script>
