<template>
  <!-- 自定义表单 -->
  <el-divider content-position="left">项目入库详情</el-divider>
  <BasicForm @register="registerProjectForm" @submit="handleSubmit" />
</template>
<script lang="ts" name="initiatesProjectsApproval" setup>
import { onMounted, onUpdated } from 'vue'
import { useForm, BasicForm } from '@/components/Form';
import { formSchemas } from '../../projectLib/projectInfo/projectInfo.data';
import { queryProjectInfoById } from '../../projectLib/projectInfo/projectInfo.api'
import { modifyProjectInfo,modifyProjectInfoApproval } from '../../myWork/inComplete/inComplete.api';
import { getUserInfoByid } from '../../projectLib/initiatesProjects/initiatesProjects.api'


let dataTo = defineProps(["projectid", "isEdit", "IsApproval"])
const emit = defineEmits(['close']);
console.log("结果ddddd是", dataTo)

onMounted(async () => {
  let param: any = {
    projectid: dataTo.projectid
  }
  let res = await queryProjectInfoById(param)
  console.log("结果是", res, dataTo)
  setFieldsValue(res)
})
onUpdated(async () => {
  console.log("我更新了")
  let proform = getFieldsValueProjectForm()
  for (let key in proform) {
    if (key.indexOf("Contactor") != -1) {
      let userid = proform[key]
      let res = await getUserInfoByid({ id: userid })
      console.log(key, res.nickname)
      updateProjectSchema({
        field: key,
        componentProps: {
          options: [{
            value: userid,
            label: res.nickname
          }
          ],
        }
      })
    }
  }

}
)
/**
 * BasicForm绑定注册;src\views\ProcessApprovalSubPage\initiatesProjectsApproval\initiatesProjectsApproval.vue
 * useForm 是整个框架的核心用于表单渲染，里边封装了很多公共方法;
 * 支持（schemas: 渲染表单列，autoSubmitOnEnter：回车提交,submitButtonOptions：自定义按钮文本和图标等方法）；
 * 平台通过此封装，简化了代码，支持自定义扩展;
 */
const [registerProjectForm, { validate: validateProjectForm, setFieldsValue, getFieldsValue: getFieldsValueProjectForm, updateSchema: updateProjectSchema }] = useForm({
  //注册表单列
  schemas: formSchemas,
  showActionButtonGroup: dataTo.isEdit,
  //回车提交
  // autoSubmitOnEnter: true,
  // //不显示重置按钮
  // showResetButton: false,
  //自定义提交按钮文本和图标
  submitButtonOptions: { text: '提交', preIcon: '' },
  resetButtonOptions: { text: '取消', preIcon: '' },
  //查询列占比 24代表一行 取值范围 0-24
  resetFunc: customResetFunc,

  actionColOptions: { span: 14 },
  labelCol: { style: { width: '120px' } },
  wrapperCol: { style: { width: 'auto' } },
  disabled: dataTo.isEdit == true ? false : true,
});


async function handleSubmit() {
  let projectform = await getFieldsValueProjectForm();
  let param = {
    projectInfo: projectform
  }
  // let outrs = Object.assign({},param,projectform)
  console.log("param", param, "projectform", projectform)
  if (await validateProjectForm()) {
    if (dataTo.IsApproval) {
      modifyProjectInfoApproval(projectform)
      emit("close")
    } else {
      modifyProjectInfo(projectform)
      emit("close")
    }


  }
}
async function customResetFunc() {
  emit("close")
}
</script>
<style></style>