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

  const { createMessage } = useMessage();
  const props = defineProps({
    data: {
      type: Object,
    },
    isRead: {
      type: Boolean,
      default: false,
    },
  });
  const formSchemas: FormSchema[] = [
    {
      label: '污染地块id',
      field: 'wrdkid',
      component: 'Input',
      show: false,
    },
    {
      label: '备注',
      field: 'bz',
      // required: true,
      component: 'InputTextArea',
      componentProps: () => {
        return {
          allowClear: true,
          disabled: props.isRead,
          autoSize:{
            //最小显示行数
            minRows: 2,
          },
        };
      },
    },
    {
      label: '其他材料',
      field: 'qtcl',
      // required: true,
      component: 'JUpload',
      componentProps: () => {
        return {
          disabled: props.isRead,
          showUploadList: false,
          fileName: '其他材料',
          accept: '.pdf,.zip',
          beforeUpload: (file) => {
            let isshow = false;
            const isFile = file.name.split('.')[file.name.split('.').length - 1];
            console.log(file, 'file');
            if (isFile == 'pdf' || isFile == 'zip' || isFile == 'PDF') {
              isshow = true;
            } else {
              createMessage.error('只能上传.pdf/.zip格式的文件');
              isshow = false;
            }
            return isshow;
          },
        };
      },
    },
  ];

  const [registerForm, { getFieldsValue, setFieldsValue, resetFields, validate, updateSchema }] = 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: '180px' } },
    //row行的样式
    baseRowStyle: { width: '100%' },
    //表单布局属性，支持（vertical,horizontal），默认为horizontal
    layout: 'horizontal',
  });
  watch(
    () => props?.data,
    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) => {
      if (val) {
        await nextTick();
        handleChange(val);
      }
    },
    { deep: true, immediate: true }
  );
  function handleChange(val) {
    console.log(val, 'val');
    updateSchema({
      label: '备注',
      field: 'bz',
      required: false,
      component: 'InputTextArea',
      componentProps: () => {
        return {
          allowClear: false,
          disabled: val,
          autoSize:{
            //最小显示行数
            minRows: 2,
          },
        };
      },
    },);
    updateSchema({
      label: '其他材料',
      field: 'qtcl',
      required: false,
      component: 'JUpload',
      componentProps: {
        disabled: val,
        showUploadList: false,
      },
    });
  }

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

    return fieldsValue;
  }
  defineExpose({
    getFormValue,
  });
</script>

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

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