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

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

  const props = defineProps({
    data: {
      type: Object,
      default: {},
    },
  });

  //自定义表单字段
  const formSchemas: FormSchema[] = [
    {
      label: '污染地块id',
      field: 'wrdkid',
      component: 'Input',
      show: false,
    },
    {
      label: '是否存在污染土壤外运处置',
      field: 'sfczwrtrwycz',
      component: 'Select',
      required: !props.data.disabled,
      componentProps: {
        options: [
          { value: 1, label: '是' },
          { value: 0, label: '否' },
        ],
        disabled: props.data.disabled || false,
      },
    },
  ];

  const [registerForm, { getFieldsValue, setFieldsValue, validate }] = useForm({
    //注册表单列
    schemas: formSchemas,
    //不显示查询和重置按钮
    showActionButtonGroup: false,
    rowProps: { gutter: 24, justify: 'start', align: 'middle' },
    //全局col列占比(每列显示多少位)，和schemas中的colProps属性一致
    baseColProps: { span: 12 },
    //使用labelCol的样式参数来控制标题宽度
    labelCol: { style: { width: '250px' } },
    //row行的样式
    baseRowStyle: { width: '100%' },
    //表单布局属性，支持（vertical,horizontal），默认为horizontal
    layout: 'horizontal',
  });
  watch(
    () => props.data,
    async (val: any) => {
      val &&
        nextTick(async () => {
          await setFieldsValue({
            ...val,
          });
        });
    },
    { deep: true, immediate: true }
  );

  // 获取子组件 列表
  async function getFormValue() {
    console.log('子组件执行');
    //获取所有值
    let fieldsValue = await getFieldsValue();
    fieldsValue = await validate();

    return fieldsValue;
  }

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

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

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

  table {
    border-collapse: collapse;
    border: 1px #dfe6ec solid;
  }

  tbody tr td {
    padding: 6px;
    height: 40px;
  }

  .bgColor {
    background-color: rgba(244, 247, 249, 0.8);
    text-align: center;
  }

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