<template>
  <Dialog
    :title="dialogTitle"
    v-model="dialogVisible"
    :close-on-click-modal="true"
    :vLoading="formLoading"
    width="600px"
  >
    <template #title>{{ dialogTitle }} </template>
    <el-form :model="form">
      <el-form-item label="工序">
         <el-input v-model="form.processCode" disabled />
      </el-form-item>
      <el-form-item label="生产人员">
        <el-select v-model="form.personSelected" multiple>
          <el-option v-for="item in personOption" :key="item.workerCode" :label="item.workerName" :value="item.workerCode" />
        </el-select>
      </el-form-item>
      <el-form-item label="质检人员">
        <el-input v-model="form.checkPerson"  />
      </el-form-item>
      <el-form-item label="合格数量" @change="handleCount" :key="qcount">
        <el-input-number
          v-model="form.qualified"
          @change="handleCount"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="不合格数量">
        <el-input-number  v-model="form.unqualified" @change="handleCount" :key="qcount"></el-input-number>
      </el-form-item>
      <el-form-item label="不合格原因">
        <el-input v-model="form.remark"></el-input>
      </el-form-item>
    </el-form>
  
    <template #footer>
      <el-button @click="dialogVisible = false">关闭</el-button>
      <el-button type="primary"  :disabled="saveFlag" @click="saveReport">保存</el-button>
    </template> </Dialog
  >
</template>
<script lang="ts" setup>
import { Dialog } from '@/components/Dialog'
import * as workschedulingApi from '@/api/mes/workScheduling'
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const rowData = ref()
const dialogVisible = ref(false)
const dialogTitle = ref()
const formLoading = ref(false)
const personOption=ref([])
const saveFlag=ref(false)
const planDayCode=ref()
const form = reactive({
  id:0,
  planDayCode: "",
  processCode:"",
        personSelected:[],
        qualified:0,
        unqualified:0,
        remark:"",
        checkPerson:""
})
const openDetail = async (row: any, titleName: any,code:string) => {
  rowData.value = row
  //console.log('row-68',row)
  dialogVisible.value = true
  dialogTitle.value = titleName
  form.processCode=row.nodeCode
  form.personSelected=[]
  form.planDayCode=code
  form.personSelected=[]
  form.qualified=0
  form.unqualified=0
  form.id=row.id
  planDayCode.value=code
  getCurrentWorkerList(row.nodeCode)
  
}

//获取工序人员
const getCurrentWorkerList = async (val) => {

  let params = {
    planDayCode: planDayCode.value,
    processCode: val
  }
  personOption.value = await workschedulingApi.getCurrentWorkerList(params)
  
}
const qcount=ref(0)
//数量处理
const handleCount = () => {
  qcount.value++
  if (form.qualified> rowData.value.planCount - form.unqualified) {
    message.alert('合格数量超限！计划总数【'+rowData.value.planCount+'】')
    form.qualified =rowData.value.planCount - form.unqualified

    return
  } 
  
  if (form.unqualified> rowData.value.planCount -form.qualified ) {
    message.alert('不合格数量超限！')
    form.unqualified =rowData.value.planCount-form.qualified
    return
  }
}
//提交质检
const saveReport = async() => {
 
  saveFlag.value = true
  try {
    await workschedulingApi.processQualified(form)
  }finally{
    saveFlag.value = false
    dialogVisible.value = false
    emit('close')
  }
  //console.log("report-saveReport-82",data)
  //await workschedulingApi.reportWorkByProcess(data)
 
}
// 传递给父类
const emit = defineEmits(['success', 'close'])
defineOptions({ name: 'qualifiedCheck' })
defineExpose({ openDetail }) // 提供 open 方法，用于打开弹窗
</script>
