<template>
  <BasicForm @register="registerForm" />
</template>

<script lang="ts" setup>
  import { ref, watch, nextTick, toRaw, computed  } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { FormSchema } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  const { createMessage } = useMessage();
  // Emits声明
  const emit = defineEmits(['register', 'success']);

  const props = defineProps({
    data: {
      type: Object,
    },
    title: {
      type: String,
      default: '检测',
    },
    isRead: {
      type: Boolean,
      default: false,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    upload: {
      type: String,
      default: '',
    },
    dkxx: {
      type: Object,
    },
    
  });

  const AdviceFormSchema: FormSchema[] = [
    { label: '污染地块ID', field: 'wrdkid', component: 'Input', show: false },
    { label: '接收地编码', field: 'jsdzgbm', component: 'Input', show: false },
    { label: '所在地编码', field: 'szdzgbm', component: 'Input', show: false },
    {
      label: '所在地主管部门',
      field: 'szdRealname',
      component: 'Input',
      componentProps: {
        disabled: true,
      },
    },
    {
      label: '接收地主管部门',
      field: 'jsdRealname',
      component: 'Input',
      componentProps: {
        disabled: true,
      },
    },
    // {
    //   label: '接收地主管部门',
    //   field: 'jsdzgbm',
    //   required: true,
    //   component: 'Input',
    // },
    {
      label: '实际转运时间',
      field: 'sjzysj',
      required: true,
      component: 'Input',
      // component: 'DatePicker',
      componentProps: {
        // format: 'YYYY-MM-DD',
        // valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      label: '实际转运总量',
      field: 'sjzyzl',
      required: true,
      component: 'Input',
    },
    {
      label: '与转运计划是否有偏移',
      field: 'jhsfypy',
      required: true,
      component: 'Input',
    },
    {
      label: '偏移情况说明',
      field: 'pyqksm',
      component: 'Input',
    },
    {
      label: '转运联单',
      field: 'zyldlj',
      component: 'JUpload',
      componentProps: {
        showUploadList: false,
        accept: '.pdf,.zip',
        beforeUpload: (file) => {
          let isshow = false;
          const isFile = file.name.split('.')[file.name.split('.').length - 1];
          if (isFile == 'pdf' || isFile == 'zip' || isFile == 'PDF') {
            isshow = true;
          } else {
            createMessage.error('只能上传.pdf/.zip格式的文件');
            isshow = false;
          }
          return isshow;
        },
      },
    },
  ];
  //表单配置
  const [registerForm, { resetFields, setFieldsValue, getFieldsValue, validate }] = useForm({
    labelWidth: 160,
    schemas: AdviceFormSchema,
    showActionButtonGroup: false,
    baseColProps: { span: 12 },
    disabled: computed(() => (props.disabled))
  });

  watch(
    () => props?.data,
    async (val: any) => {
      if (val) {
        await nextTick();
        const obj: any = toRaw(props.data);

        await setFieldsValue({
          ...obj,
        });
      } else {
        await nextTick();
        await resetFields();
      }
      // console.log(props.upload, 'upload编制单位');
    },
    { deep: true, immediate: true }
  );

  const fieldsValue = ref<any>({});
  // 获取子组件 列表
  async function getFormValueBz() {
    //获取所有值
    fieldsValue.value = await getFieldsValue();
    fieldsValue.value = await validate();
    // 加密
    return fieldsValue.value;
  }

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

<style lang="less" scoped>
  :deep(.ant-input-number) {
    width: 100%;
  }
</style>
