<!-- 表单布局 -->
<template>
  <!-- 自定义表单 -->
  <BasicForm @register="registerForm"> </BasicForm>
</template>

<script lang="ts" setup>
  //引入依赖
  import { nextTick, watch, toRaw } from 'vue';
  import { useForm, BasicForm, FormSchema } from '/@/components/Form';

  const props = defineProps({
    labelFirst: {
      type: String,
      default: '风险管控',
    },
    data: {
      type: Object,
      default: {},
    },
    obj: {
      type: Array,
      default: [],
    },
    uploadXgpg: {
      type: Function
    }
  });
  //自定义表单字段
  const formSchemas: FormSchema[] = [
    {
      label: '污染地块id',
      field: 'wrdkid',
      component: 'Input',
      show: false,
    },
    {
      label: 'id',
      field: 'id',
      component: 'Input',
      show: false,
    },
    {
      label: `${(props.obj&&props.obj.length>0) ? props.obj[0].name : '风险管控范围(m²)'}`,
      field: `${(props.obj&&props.obj.length>0) ? 'mj' : 'fxgkfw'}`,
      required: true,
      component: 'InputNumber',
      componentProps: {
      onBlur: (val: any) => {
        props.uploadXgpg(val.target.value)
      }
      },
    },
    {
      label: `${(props.obj&&props.obj.length>1) ? props.obj[1].name : ''}`,
      field: 'xftrfl',
      required: true,
      component: 'InputNumber',
      ifShow: () => {
        console.log(props.labelFirst, 'props.labelFirst');
        return props.labelFirst == '修复' || props.labelFirst == '修复效果评估' || props.labelFirst == '管控修复效果评估' ? true : false;
      },
    },
  ];
 
  /**
   * BasicForm绑定注册;
   */
  const [registerForm, { getFieldsValue, setFieldsValue, validate }] = useForm({
    //注册表单列
    schemas: formSchemas,
    //不显示查询和重置按钮
    showActionButtonGroup: false,
    //默认row行配置,当 layout 为 horizontal 生效
    rowProps: { gutter: 24, justify: 'start', align: 'middle' },
    //全局col列占比(每列显示多少位)，和schemas中的colProps属性一致
    baseColProps: { span: 12 },
    //使用labelCol的样式参数来控制标题宽度
    labelCol: { style: { width: '252px' } },
    //row行的样式
    baseRowStyle: { width: '100%' },
    //表单布局属性，支持（vertical,horizontal），默认为horizontal
    layout: 'horizontal',
  });
  watch(
    () => props.data,
    async (newVal: any, oldVal: any) => {
      ((newVal?.id != oldVal?.id) || (newVal?.wrdkid != oldVal?.wrdkid) || (newVal?.kzfxgktrmj != oldVal?.kzfxgktrmj) || (newVal?.sjxftfl != oldVal?.sjxftfl) || (newVal?.xftrfl != oldVal?.xftrfl)  ) &&
        nextTick(async () => {
          const obj = toRaw(newVal);
          if (props.labelFirst == '修复') {
            obj.mj = obj.kzfxgktrmj;
          } else if (props.labelFirst == '风险管控效果评估') {
            obj.mj = obj.fxgkfw;
          } else if (props.labelFirst == '修复效果评估') {
            obj.mj = obj.sjxffw;
            obj.xftrfl = obj.sjxftfl;
          } else if(props.labelFirst == '管控修复效果评估'){
            obj.mj = obj.sjxffw;
            obj.xftrfl = obj.sjxftfl;
          }
          await setFieldsValue({
            ...obj,
          });
        });
    },
    { deep: true, immediate: true }
  );
  // 获取子组件 列表
  async function getFormValueFxpg() {
    console.log('子组件执行');
    //获取所有值
    let fieldsValue = await getFieldsValue();
    fieldsValue = await validate();

    if (props.labelFirst == '修复') {
      fieldsValue.kzfxgktrmj = fieldsValue.mj;
      delete fieldsValue.mj;
    } else if (props.labelFirst == '效果评估') {
      fieldsValue.sjxftfl = fieldsValue.mj;
      delete fieldsValue.mj;
    }  else if (props.labelFirst == '风险管控效果评估') {
      // fieldsValue.sjxftfl = fieldsValue.mj;
      fieldsValue.fxgkfw = fieldsValue.mj;
      delete fieldsValue.mj;
    } else if (props.labelFirst == '修复效果评估') {
      fieldsValue.sjxffw = fieldsValue.mj;
      fieldsValue.sjxftfl = fieldsValue.xftrfl;

      delete fieldsValue.xftrfl;
      delete fieldsValue.mj;
    }else if (props.labelFirst == '管控修复效果评估') {
      fieldsValue.sjxffw = fieldsValue.mj;
      fieldsValue.sjxftfl = fieldsValue.xftrfl;

      delete fieldsValue.mj;
      delete fieldsValue.xftrfl;
    }

    return fieldsValue;
  }

  // 通过ref必须把在父组件里面的方法导出
  defineExpose({
    getFormValueFxpg,
  });
</script>

<style scoped>
  /** 时间和数字输入框样式 */
  :deep(.ant-input-number) {
    width: 100%;
  }

  :deep(.ant-picker) {
    width: 100%;
  }

  /* :deep(.ant-radio-wrapper) {
    width: 120px;
    text-align: left;
} */
</style>
