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

<script lang="ts" setup>
  import { useModalInner } from '/@/components/Modal';
  import { ref, watch, nextTick, toRaw } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { FormSchema } from '/@/components/Table';
  import { updateZyjhCzwc, findSzdzg, findJsdzg, queryCzwc } from '../api';
  import { useUserStore } from '/@/store/modules/user';
  // Emits声明

  const userStore: any = useUserStore();
  const jszgId = ref<any>({ realname: '' });
  const emit = defineEmits(['register', 'success']);
  const bm = ref();

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

  const AdviceFormSchema: FormSchema[] = [
    { label: '污染地块ID', field: 'id', component: 'Input', show: false },
    { 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: 'szdzgbm',
    //   required: true,
    //   component: 'Input',
    //   componentProps: {
    //     disabled: true,
    //   },
    // },
    // {
    //   label: '接收地主管部门',
    //   field: 'jsdzgbm',
    //   required: true,
    //   component: 'ApiSelect',
    //   componentProps: () => {
    //     if (userStore.userInfo.userType == 'S') bm.value = '';
    //     return {
    //       api: findJsdzg,
    //       params: { wrdkbm: bm.value },
    //       labelField: 'realname',
    //       valueField: 'id',
    //     };
    //   },
    // },
    {
      label: '处置时间',
      field: 'czsj',
      required: true,
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
        disabled: props.isRead,
      },
    },
    {
      label: '处置总量',
      field: 'czzl',
      required: true,
      component: 'Input',
      componentProps: {
        disabled: props.isRead,
      },
    },
    {
      label: '处置措施',
      field: 'czcs',
      required: true,
      component: 'CheckboxGroup',
      componentProps: {
        disabled: props.isRead,
        //options里面由一个一个的radio组成,支持disabled
        options: [
          { label: '水泥窑', value: '水泥窑' },
          { label: '集中处置场所', value: '集中处置场所' },
          { label: '填埋', value: '填埋' },
          { label: '其他', value: '其他' },
        ],
      },
    },
    {
      label: '与转运计划是否有偏移',
      field: 'yzyjhsfypy',
      required: true,
      component: 'Input',
      componentProps: {
        disabled: props.isRead,
      },
    },
    {
      label: '偏移情况说明',
      field: 'ypyqksm',
      component: 'Input',
      componentProps: {
        disabled: props.isRead,
      },
    },
  ];
  //表单配置
  const [registerForm, { getFieldsValue, resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 150,
    schemas: AdviceFormSchema,
    showActionButtonGroup: false,
    baseColProps: { span: 12 },
  });

  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>
