<template>
  <Dialog
    :title="dialogTitle"
    v-model="dialogVisible"
    :close-on-click-modal="true"
    :vLoading="formLoading"
  >
    <template #title>{{ dialogTitle }}</template>
    <el-form>
      <el-form-item label="当前工序">
        <el-input v-model="currentProcess" disabled></el-input>
      </el-form-item>
      <el-form-item label="选择人员">
        <el-select v-model="personSelected">
          <el-option v-for="item in personData" :key="item.workerCode" :label="item.workerName" />
        </el-select>
      </el-form-item>
      <el-form-item label="报工数量">
        <el-input-number v-model="reportCount" @change="handleCount"></el-input-number>
      </el-form-item>
      <el-form-item label="工时">
        <el-input-number v-model="workTerm"></el-input-number>
      </el-form-item>
      <el-form-item label="是否已质检">
        <el-switch v-model="checkFlag" active-value="true"> </el-switch>
      </el-form-item>
      <el-form-item label="合格数量">
        <el-input-number
          v-model="qualified"
          :disabled="!checkFlag"
          @change="handleCount"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="不合格数量">
        <el-input-number disabled v-model="unqualified"></el-input-number>
      </el-form-item>
    </el-form>

    <template #footer
      ><el-button @click="dialogVisible = false">关闭</el-button>
      <el-button type="primary" @click="saveReport">保存</el-button></template
    >
  </Dialog>
</template>
<script lang="ts" setup>
import { Dialog } from '@/components/Dialog'
import * as workschedulingApi from '@/api/mes/workScheduling'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import dayjs from 'dayjs'
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { wsCache } = useCache()
const rowData = ref()
const dialogVisible = ref(false)
const dialogTitle = ref()
const formLoading = ref(false)
const personSelected = ref()
const currentProcess = ref()
const personData = ref([{ value: '', label: '' }])
const reportCount = ref()
const qualified = ref()
const checkFlag = ref(false)
const unqualified = ref(0)
const workTerm = ref()
const user = wsCache.get(CACHE_KEY.USER)
const handleCount = () => {
    if (reportCount.value > rowData.value.planCount) {
    message.alert('报工数量不能超出计划数量！计划数【'+rowData.value.planCount+'】')
    reportCount.value =rowData.value.planCount
    return
  }

  if (checkFlag) {
    if (reportCount.value < qualified.value) {
      message.alert('合格数不能超出报工数！')
      return
    }
    if (qualified.value == undefined || qualified.value == 0) {
      qualified.value = reportCount.value
    }
    unqualified.value = reportCount.value - qualified.value
  }
}
const openDetail = (row: any, titleName: any) => {
  //console.log('workscheduling-finishReport-60', user)
  currentProcess.value = row.workingNode
  rowData.value = row
  dialogVisible.value = true
  dialogTitle.value = titleName
  getCurrentWorkerList(row)
}
const getCurrentWorkerList = async (row) => {
  let params = {
    planDayCode: row.planMasterCode,
    processCode: row.workingNode
  }
  personData.value = await workschedulingApi.getCurrentWorkerList(params)
}
//提交报工
const saveReport = () => {
  let data = {
    reportDate: dayjs(new Date()).format('YYYY-MM-DD HH:mm:sss'),
    schedulingCode: rowData.value.schedulingCode,
    processCode: currentProcess.value,
    list: [
      {
        reportCount: reportCount.value,
        workTerm: workTerm.value,
        reportPerson: personSelected.value,
        qualified: qualified.value,
        unqualified: unqualified.value
      }
    ]
  }
  workschedulingApi.completeHandle(data)
}
// 传递给父类
const emit = defineEmits(['success', 'close'])

defineExpose({ openDetail }) // 提供 open 方法，用于打开弹窗
</script>
