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

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

  import { decrypts } from '@/utils/checkTip';

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

  //自定义表单字段
  const formSchemas: FormSchema[] = [
    {
      label: '单位名称',
      field: 'dwmc',
      component: 'Input',
      required: true,
      componentProps: {
        disabled: props.isRead,
      },
    },
    {
      label: '工商营业执照号',
      field: 'gsyyzzh',
      required: true,
      component: 'Input',
      dynamicRules: () => {
        return [{ pattern: /^([0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}|[1-9]\d{14})$/, message: '工商营业执照号格式有误', required: true }];
      },
      componentProps: {
        disabled: props.isRead,
      },
    },

    {
      label: '住所',
      field: 'zs',
      component: 'Input',
      required: true,
      componentProps: {
        disabled: props.isRead,
      },
    },
    {
      label: '联系人',
      field: 'lxr',
      component: 'Input',
      required: true,
      componentProps: {
        disabled: props.isRead,
      },
    },
    {
      label: '联系电话',
      field: 'lxdh',
      component: 'Input',
      required: true,
      dynamicRules: () => {
        return [{ pattern: /^((\d{3,4}-)?\d{7,8}|1[3-9]\d{9})$/, message: !props?.isRead ? '电话格式有误' : '', required: true }];
      },
      componentProps: {
        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();
        const obj: any = toRaw(props.data);
        if (obj.lxdh) {
          obj.lxdh = await decrypts(obj.lxdh, true);
        }
        await setFieldsValue({
          ...obj,
        });
      } 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: 'dwmc',
      component: 'Input',
      componentProps: {
        disabled: val,
      },
    });
    updateSchema({
      label: '工商营业执照号',
      field: 'gsyyzzh',
      component: 'Input',
      componentProps: {
        disabled: val,
      },
    });

    updateSchema({
      label: '住所',
      field: 'zs',
      component: 'Input',
      componentProps: {
        disabled: val,
      },
    });
    updateSchema({
      label: '联系人',
      field: 'lxr',
      component: 'Input',

      componentProps: {
        disabled: val,
      },
    });
    updateSchema({
      label: '联系电话',
      field: 'lxdh',
      component: 'Input',
      dynamicRules: () => {
        return [{ pattern: /^((\d{3,4}-)?\d{7,8}|1[3-9]\d{9})$/, message: '', required: true }];
      },
      componentProps: {
        disabled: val,
      },
    });
  }
  const fieldsValue = ref<any>({});
  // 获取子组件 列表
  async function getFormValue() {
    //获取所有值
    fieldsValue.value = await getFieldsValue();
    fieldsValue.value = await validate();
    // 加密
    fieldsValue.value.lxdh = await encryptData({ str: fieldsValue.value.lxdh });
    return fieldsValue.value;
  }

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

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

  :deep(.ant-picker) {
    width: 100%;
  }
</style>
