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

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

  const { createMessage } = useMessage();

  const props = defineProps({
    data: {
      type: Object,
    },
    title: {
      type: String,
      default: '检测',
    },
    isRead: {
      type: Boolean,
      default: false,
    },
    upload: {
      type: String,
      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',
      dynamicRules: () => {
        return [{ pattern: /^[0-9A-Z]{18}$/, message: '统一社会信用代码', required: true }];
      },
      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: 'Input',
      required: true,
      dynamicRules: () => {
        return [{ pattern: /^((\d{3,4}-)?\d{7,8}|1[3-9]\d{9})$/, message: '电话格式有误', 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: `${props.title == '施工' ? props.title + '报告总结' : props.title + '单位报告'}`,
      field: 'bglj',
      required: true,
      ifShow: () => {
        return props.upload == '检测单位' ? true : false;
      },

      component: 'JUpload',
      componentProps: () => {
        return {
          showUploadList: false,
          fileName: `${props.title == '施工' ? props.title + '报告总结' : props.title + '单位报告'}`,
          disabled: props.isRead,
          accept: '.pdf,.zip,.rar',
          beforeUpload: (file) => {
            let isshow = false;
            const isFile = file.name.split('.')[file.name.split('.').length - 1];
            if (isFile == 'pdf' || isFile == 'zip' || isFile == 'PDF' || isFile == 'rar') {
              isshow = true;
            } else {
              createMessage.error('只能上传.pdf/.zip/.rar格式的文件');
              isshow = false;
            }

            return isshow;
          },
        };
      },
    },
  ];
  /**
   * 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, props?.isRead);
        }
        await setFieldsValue({
          ...obj,
        });
      } else {
        await nextTick();
        await resetFields();
      }
      console.log(props.upload, 'upload编制单位');
    },
    { deep: true, immediate: true }
  );

  watch(
    () => props?.isRead,
    async (val: any) => {
      await nextTick();
      console.log(val, 'val');
      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',
      dynamicRules: () => {
        return [{ pattern: /^[0-9A-Z]{18}$/, message: '工商营业执照号格式有误', required: true }];
      },
      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: 'Input',
      required: true,
      dynamicRules: () => {
        return [{ pattern: /^((\d{3,4}-)?\d{7,8}|1[3-9]\d{9})$/, message: '', 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: `${props.title == '施工' ? props.title + '报告总结' : props.title + '单位报告'}`,
      field: 'bglj',
      required: true,
      ifShow: () => {
        return props.upload == '检测单位' ? true : false;
      },

      component: 'JUpload',
      componentProps: () => {
        return {
          showUploadList: false,
          fileName: `${props.title == '施工' ? props.title + '报告总结' : props.title + '单位报告'}`,
          disabled: props.isRead,
          accept: '.pdf,.zip,.rar',
          beforeUpload: (file) => {
            let isshow = false;
            const isFile = file.name.split('.')[file.name.split('.').length - 1];
            if (isFile == 'pdf' || isFile == 'zip' || isFile == 'PDF' || isFile == 'rar') {
              isshow = true;
            } else {
              createMessage.error('只能上传.pdf/.zip/.rar格式的文件');
              isshow = false;
            }

            return isshow;
          },
        };
      },
    });
  }
  const fieldsValue = ref<any>({});
  // 获取子组件 列表
  async function getFormValueBz() {
    //获取所有值
    fieldsValue.value = await getFieldsValue();
    fieldsValue.value = await validate();
    // 加密
    fieldsValue.value.lxdh = await encryptData({ str: fieldsValue.value.lxdh });
    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>
