<template>
  <BasicForm @register="registerForm"> </BasicForm>
</template>
<script setup lang="ts">
  import { watch, nextTick } from 'vue';
  import { FormSchema } from '/@/components/Table';
  import { BasicForm, useForm } from '/@/components/Form/index';

  const props = defineProps({
    data: {
      type: Object,
      default: {},
    },
    showFooter: {
      type: Boolean,
      default: true,
    },
  });
  const formSchema: FormSchema[] = [
    {
      label: '是否存在违规开发利用情况',
      field: 'sfczwgkflyqk',
      component: 'Select',
      dynamicDisabled: () => {
        return !props.showFooter;
      },
      componentProps: {
        options: [
          { label: '否', value: 0 },
          { label: '是', value: 1 },
        ],
      },
    },
    {
      label: '处罚或整改措施',
      field: 'cfhzgcs',
      component: 'Select',
      dynamicDisabled: () => {
        return !props.showFooter;
      },
      componentProps: {
        options: [
          { label: '处罚', value: 0 },
          { label: '整改', value: 1 },
        ],
      },
    },
    {
      label: '中央、部领导批示',
      field: 'zybldps',
      component: 'Input',
      dynamicDisabled: () => {
        return !props.showFooter;
      },
    },
    {
      label: '中央、地方环保督察',
      field: 'zydfhbdc',
      component: 'Input',
      dynamicDisabled: () => {
        return !props.showFooter;
      },
    },
    {
      label: '舆情报道',
      field: 'yqbd',
      component: 'Input',
      dynamicDisabled: () => {
        return !props.showFooter;
      },
    },
  ];
  //表单配置
  const [registerForm, { resetFields, setFieldsValue, getFieldsValue, validate }] = useForm({
    labelWidth: 220,
    schemas: formSchema,
    showActionButtonGroup: false,
    //默认row行配置,当 layout 为 horizontal 生效
    rowProps: { gutter: 24, justify: 'start', align: 'middle' },
    //全局col列占比(每列显示多少位)，和schemas中的colProps属性一致
    baseColProps: { span: 12 },
    //使用labelCol的样式参数来控制标题宽度
    labelCol: { style: { width: '150px' } },
    //row行的样式
    baseRowStyle: { width: '100%' },
    //表单布局属性，支持（vertical,horizontal），默认为horizontal
    layout: 'horizontal',
  });

  watch(
    () => props.data,
    async (val: any) => {
      if (val) {
        //重置表单
        await nextTick();
        await resetFields();

        await setFieldsValue({
          ...val,
        });
      }
    },
    { deep: true, immediate: true }
  );

  // 获取子组件 列表
  async function getFormValue() {
    //获取所有值
    let fieldsValue = await getFieldsValue();
    fieldsValue = await validate();
    return fieldsValue;
  }

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