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

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

  const props = defineProps({
    data: {
      type: Object,
    },
    isRead: {
      type: Boolean,
      default: false,
    },
    upload: {
      type: Number,
      default: '',
    },
    dkxx: {
      type: Object,
    },
  });

  //自定义表单字段
  const formSchemas: FormSchema[] = [
    {
      label: '污染地块Id',
      field: 'wrdkid',
      component: 'Input',
      show: false,
    },
    {
      label: 'id',
      field: 'id',
      component: 'Input',
      show: false,
    },
    {
      label: '单位名称',
      field: 'syqdwmc',
      component: 'Input',
      required: true,
      componentProps: {
        disabled: props.isRead,
      },
    },
    {
      label: '统一社会信用代码',
      field: 'gsyyzzh',
      required: true,
      component: 'Input',
      componentProps: {
        disabled: props.isRead,
      },
    },
    {
      label: '法定代表人/负责人姓名',
      field: 'frdb',
      component: 'Input',
      required: true,
      componentProps: {
        disabled: props.isRead,
      },
    },
    {
      label: '住所',
      field: 'wrdkdz',
      component: 'Input',
      required: true,
      componentProps: {
        disabled: props.isRead,
      },
    },
    {
      label: '联系人',
      field: 'lxr',
      component: 'Input',
      required: true,
      componentProps: {
        disabled: props.isRead,
      },
    },
    {
      label: '联系电话',
      field: 'lxdh',
      component: 'InputNumber',
      required: true,
      componentProps: {
        disabled: props.isRead,
      },
    },
    {
      label: '直接负责的主管人员',
      field: 'zjzgfzry',
      component: 'Input',
      required: true,
      componentProps: {
        disabled: props.isRead,
      },
    },
    {
      label: '其他直接责任人员',
      field: 'qtzgfzry',
      component: 'Input',
      required: true,
      componentProps: {
        disabled: props.isRead,
      },
    },

    {
      label: '委托调查合同金额',
      field: 'wthtje',
      component: 'InputNumber',
      required: true,
      suffix: '万元',
      componentProps: {
        disabled: props.isRead,
      },
    },
    {
      label: '检测单位报告',
      field: 'bglj',
      required: true,
      ifShow: () => {
        return props.upload ? true : false;
      },

      component: 'JUpload',
      componentProps: () => {
        return {
          showUploadList: false,
          returnUrl: true,
          fileName: '检测单位报告',
          disabled: props.isRead,
        };
      },
    },
  ];
  /**
   * BasicForm绑定注册;
   */
  const [registerForm, { getFieldsValue, setFieldsValue, resetFields, updateSchema, 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: '150px' } },
    //row行的样式
    baseRowStyle: { width: '100%' },
    //表单布局属性，支持（vertical,horizontal），默认为horizontal
    layout: 'horizontal',
  });
  watch(
    () => props?.data?.id,
    async (val: any) => {
      if (val) {
        await nextTick();
        await setFieldsValue({
          ...props.data,
        });
      } else {
        await nextTick();
        await resetFields();
      }
    },
    { deep: true, immediate: true }
  );
  watch(
    () => props?.isRead,
    async (val: any) => {
      await nextTick();
      handleChange(val);
    },
    { deep: true, immediate: true }
  );

  // 根据isRead字段判断是否禁用
  function handleChange(val) {
    updateSchema({
      label: '单位名称',
      field: 'syqdwmc',
      component: 'Input',
      required: true,
      componentProps: {
        disabled: val,
      },
    });
    updateSchema({
      label: '统一社会信用代码',
      field: 'gsyyzzh',
      required: true,
      component: 'Input',
      componentProps: {
        disabled: val,
      },
    });
    updateSchema({
      label: '法定代表人/负责人姓名',
      field: 'frdb',
      component: 'Input',
      required: true,
      componentProps: {
        disabled: val,
      },
    });
    updateSchema({
      label: '住所',
      field: 'wrdkdz',
      component: 'Input',
      required: true,
      componentProps: {
        disabled: val,
      },
    });
    updateSchema({
      label: '联系人',
      field: 'lxr',
      component: 'Input',
      required: true,
      componentProps: {
        disabled: val,
      },
    });
    updateSchema({
      label: '联系电话',
      field: 'lxdh',
      component: 'InputNumber',
      required: true,
      componentProps: {
        disabled: val,
      },
    });
    updateSchema({
      label: '直接负责的主管人员',
      field: 'zjzgfzry',
      component: 'Input',
      required: true,
      componentProps: {
        disabled: val,
      },
    });
    updateSchema({
      label: '其他直接责任人员',
      field: 'qtzgfzry',
      component: 'Input',
      required: true,
      componentProps: {
        disabled: val,
      },
    });

    updateSchema({
      label: '委托调查合同金额',
      field: 'wthtje',
      component: 'InputNumber',
      required: true,
      suffix: '万元',
      componentProps: {
        disabled: val,
      },
    });
    updateSchema({
      label: '检测单位报告',
      field: 'bglj',
      required: true,
      ifShow: () => {
        return props.upload ? true : false;
      },

      component: 'JUpload',
      componentProps: () => {
        return {
          showUploadList: false,
          returnUrl: true,
          fileName: '检测单位报告',
          disabled: val,
        };
      },
    });
  }
  const fieldsValue = ref<any>({});
  // 获取子组件 列表
  async function getFormValueBz() {
    //获取所有值
    fieldsValue.value = await getFieldsValue();
    fieldsValue.value = await validate();

    return fieldsValue.value;
  }

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

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

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

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