<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"  sortable />
      <el-table-column :label="t('ProductionPlan.TaskSummary.detailTableProductCodeColumn')" align="center" prop="barCode"  sortable />
      <el-table-column :label="t('ProductionPlan.TaskSummary.detailTableProductNameColumn')" align="center" prop="productName" sortable/>
       <el-table-column :label="t('ProductionPlan.TaskSummary.detailTableTotalNumberColumn')" align="center" prop="number" />
      <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.number-scope.row.planNumber>0 ? scope.row.number-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.number-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
            v-if="(scope.row.number - scope.row.planNumber) > 0"
            link
            type="success"
            @click="addPlanForm(scope.row.taskId, scope.row.productId,
            scope.row.number - scope.row.planNumber,scope.row.id)"
            v-hasPermi="['mes:task:plan']"
          >
            {{ t('ProductionPlan.TaskSummary.detailActionCreatePlanLabel') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

  </ContentWrap>
  <!-- 表单弹窗：添加/修改 -->
  <PlanForm ref="formRef" @success="handlePlanSuccess" />
  <!-- 物料列表 -->
  <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（主表的关联字段）
  taskDeliveryDate?: string | number
}>()
const emit = defineEmits(['success'])
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 {
    const data = await TaskApi.getTaskDetailPage(queryParams)
    list.value = data.list
  } 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, props.taskDeliveryDate)
}
const handlePlanSuccess = () => {
  getList()
  emit('success')
}
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>
