<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="1200px">
  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="工序" align="center" prop="type" >
      <template #default="scope">
          <dict-tag :type="DICT_TYPE.MES_ORG_TYPE" :value="scope.row.type" />
        </template>
      </el-table-column>  
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="上限值" align="center" prop="upperVal" />
      <el-table-column label="下限值" align="center" prop="lowerVal" />
      <el-table-column label="实际值" align="center" prop="realVal" >
         <template #default="{ row }">
        <el-input :disabled = "planisZj === 1" v-model="row.realVal" placeholder="请输入实际值" />
          </template>
        </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" >
        <template #default="{ row }">
         <!-- <el-form-item :prop="`${$index}.remark`"> -->
        <el-input :disabled = "planisZj === 1" v-model="row.remark" placeholder="请输入备注" />
        <!-- </el-form-item> -->
          </template>
        </el-table-column>
    </el-table>
    <!-- 分页 -->
    <!-- <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    /> -->
  </ContentWrap>
  <template #footer>
      <el-button v-if = "planisZj === 0" @click="submitForm" type="primary" :disabled="formLoading" >确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { ZjProductRecordApi, ZjProductRecordVO } from '@/api/mes/zjproductrecord'
import {DICT_TYPE} from '@/utils/dict'
import { PlanApi } from '@/api/mes/plan'

/** 试产质检记录 列表 */
defineOptions({ name: 'ZjProductRecord' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const loading = ref(false) // 列表的加载中
const list = ref<ZjProductRecordVO[]>([]) // 列表的数据
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 100,
  type: undefined,
  name: undefined,
  unit: undefined,
  upperVal: undefined,
  lowerVal: undefined,
  productId: undefined,
  realVal: undefined,
  remark: undefined,
  createTime: [],
  planId: undefined
})


/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ZjProductRecordApi.getZjProductRecordPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const planId = ref('')
const planType = ref('')
const planStatus = ref(0)
const planisZj = ref(0)

/** 打开弹窗 */
const open = async (productId?: number,type?:string, status?:number,id?:number,isZj?:number) => {
  planType.value = type
  planStatus.value = status
  planId.value = id
  planisZj.value = isZj
  dialogVisible.value = true
  dialogTitle.value = t('质检')
  loading.value = true
    console.log('hhhh')
  try {
    queryParams.productId = productId
    const data = await ZjProductRecordApi.getZjProductRecordPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗


/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  // await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    await ZjProductRecordApi.updateZjProductRecordBatch(list.value)
    // 发起
    const data ={
      id: planId.value,
      code: planType.value,
      status: planStatus.value // 状态
    }
    await PlanApi.updatePlanZjStatus(data) 
    message.success(t('common.createSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
/** 初始化 **/
// onMounted(() => {
//   getList()
// })
</script>