<template>
  <Dialog title="任务转发" v-model="dialogVisible">
    <el-form :model="formData" label-width="auto" style="max-width: 600px">
      <el-form-item label="转发用户" prop="name" style="max-width: 500px">
        <el-select v-model="formData.userId" placeholder="请选择用户">
          <el-option
                v-for="item in userList"
                :key="item.id"
                :label=" item.deptName +'-'+ item.realName "
                :value="item.id!"
              />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { EnterpriseInspectionsApi, EnterpriseInspectionsVO } from '@/api/enterpriseinspections'
import { getSimpleUserZGList, UserVO } from '@/api/system/user'


/** 企业检查记录表，用于记录与企业相关的环保检查信息。 表单 */
defineOptions({ name: 'EnterpriseInspectionsForm' })

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

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const changeForm = ref({
  id: undefined,
  taskId: undefined,
  enterpriseId: undefined,
})

const taskName = ref()

const form = reactive({
  userId: '',
  resUserId: '',
  inspectionsId: [],
  taskName: ''
})

// const formRules = reactive({
// })
const formRef = ref() // 表单 Ref


const userList = ref()
const getUserList = async () => {
  const data = await getSimpleUserZGList();
  userList.value = data
}


const formData:any = ref({
  userId: undefined,
  inspectionsId: []
});

/** 打开弹窗 */
const open = async (inspectionsId) => {
  dialogVisible.value = true
  formData.value.inspectionsId.push(inspectionsId)
  getUserList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  // await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    
    await EnterpriseInspectionsApi.passOn(formData.value)
    message.success(t('更改成功'))

    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

// /** 重置表单 */
// const resetForm = () => {
//   formData.value = {
//     id: undefined,
//     taskId: undefined,
//     enterpriseId: undefined,
//   }
//   formRef.value?.resetFields()
// }
</script>
<style scoped>
  .box{
    display: flex;
    flex-direction: column;
    .img{
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      justify-content: center;
      gap: 15px
    }
    .text{
      padding: 15px;
    }
  }
</style>
