<!-- 表单布局 -->
<template>
  <!-- 自定义表单 -->
  <BasicForm @register="registerForm">
     <template #xfcs="{ model, field }">
        <a-radio-group v-model:value="gcgklx" name="checkboxgroup">
          <a-radio value="阻隔">阻隔</a-radio>
          <a-checkbox-group v-model:value="model['zglx']" v-if="gcgklx=='阻隔'">
            <a-checkbox value="土壤">土壤</a-checkbox>
            <a-checkbox value="地下水">地下水</a-checkbox>
          </a-checkbox-group>
          <a-radio value="其他">其他</a-radio>
        </a-radio-group>
        <a-input v-model:value="gc" placeholder="请输入其它措施" :disabled="gcgklx != '其他'" style="width: 45%" />
    </template> 
  </BasicForm>
</template>

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

  const props = defineProps({
    labelFirst: {
      type: String,
      default: '风险管控',
    },
    data: {
      type: Object,
      default: {},
    },
    obj: {
      type: Array,
      default: [],
    },
    uploadXgpg: {
      type: Function
    },
    dkxx: {
      type: Object,
      default: {
        wrdkmc: '',
        wrdkbm: '',
      },
    },
  });
  
  const gcgklx = ref('');
  const gc = ref('');
  //自定义表单字段
  const formSchemas: FormSchema[] = [
    {
      label: '污染地块id',
      field: 'wrdkid',
      component: 'Input',
      show: false,
    },
    {
      label: 'id',
      field: 'id',
      component: 'Input',
      show: false,
    },
    {
      label: '阻隔类型',
      field: 'zglx',
      component: 'Input',
      show: false,
    },
    {
      label: `${(props.obj&&props.obj.length>0) ? props.obj[0].name : '风险管控范围(m²)'}`,
      field: `${(props.obj&&props.obj.length>0) ? 'mj' : 'fxgkfw'}`,
      required: true,
      component: 'InputNumber',
      componentProps: {
      onBlur: (val: any) => {
        props.uploadXgpg(val.target.value)
      }
      },
    },
    {
      label: `${(props.obj&&props.obj.length>1) ? props.obj[1].name : ''}`,
      field: 'xftrfl',
      required: true,
      component: 'InputNumber',
      ifShow: () => {
        console.log(props.labelFirst, 'props.labelFirst');
        return props.labelFirst == '修复' || props.labelFirst == '修复效果评估' || props.labelFirst == '管控修复效果评估' ? true : false;
      },
    },
    {
      label: '管控措施其他',
      field: 'gkcsQt',
      component: 'Input',
      show: false,
    },
    {
      label: '修复措施',
      field: '',
      required: true,
      component: 'Input',
      slot: 'xfcs',
      ifShow: () => {
        return props.labelFirst == '修复' ? true : false;
      },
    },
  ];
 
  /**
   * BasicForm绑定注册;
   */
  const [registerForm, { getFieldsValue, setFieldsValue, 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: '252px' } },
    //row行的样式
    baseRowStyle: { width: '100%' },
    //表单布局属性，支持（vertical,horizontal），默认为horizontal
    layout: 'horizontal',
  });
  watch(
    () => props.data,
    async (newVal: any, oldVal: any) => {
      ((newVal?.id != oldVal?.id) || (newVal?.wrdkid != oldVal?.wrdkid) || (newVal?.kzfxgktrmj != oldVal?.kzfxgktrmj) || (newVal?.sjxftfl != oldVal?.sjxftfl) || (newVal?.xftrfl != oldVal?.xftrfl)  ) &&
        nextTick(async () => {
          const obj = toRaw(newVal);
          if (props.labelFirst == '修复') {
            obj.mj = obj.kzfxgktrmj;
            if (obj.gkcsQt == '阻隔') {
              gcgklx.value = obj.gkcsQt;
              gc.value = '';
            } else {
              gcgklx.value = '其他';
              gc.value = obj.gkcsQt;
            }
          } else if (props.labelFirst == '风险管控效果评估') {
            obj.mj = obj.fxgkfw;
          } else if (props.labelFirst == '修复效果评估') {
            obj.mj = obj.sjxffw;
            obj.xftrfl = obj.sjxftfl;
          } else if(props.labelFirst == '管控修复效果评估'){
            obj.mj = obj.sjxffw;
            obj.xftrfl = obj.sjxftfl;
          }
          await setFieldsValue({
            ...obj,
          });
        });
    },
    { deep: true, immediate: true }
  );
  // 获取子组件 列表
  async function getFormValueFxpg() {
    console.log('子组件执行');
    //获取所有值
    let fieldsValue = await getFieldsValue();
    fieldsValue = await validate();

    if (props.labelFirst == '修复') {
      fieldsValue.kzfxgktrmj = fieldsValue.mj;
      delete fieldsValue.mj;
    } else if (props.labelFirst == '效果评估') {
      fieldsValue.sjxftfl = fieldsValue.mj;
      delete fieldsValue.mj;
    }  else if (props.labelFirst == '风险管控效果评估') {
      // fieldsValue.sjxftfl = fieldsValue.mj;
      fieldsValue.fxgkfw = fieldsValue.mj;
      delete fieldsValue.mj;
    } else if (props.labelFirst == '修复效果评估') {
      fieldsValue.sjxffw = fieldsValue.mj;
      fieldsValue.sjxftfl = fieldsValue.xftrfl;

      delete fieldsValue.xftrfl;
      delete fieldsValue.mj;
    }else if (props.labelFirst == '管控修复效果评估') {
      fieldsValue.sjxffw = fieldsValue.mj;
      fieldsValue.sjxftfl = fieldsValue.xftrfl;

      delete fieldsValue.mj;
      delete fieldsValue.xftrfl;
    }

    if (gcgklx.value == '其他') {
      fieldsValue.gkcsQt = gc.value;
      fieldsValue.zglx = '';
      fieldsValue.qygd = '';
      fieldsValue.qygdFile = '';
      fieldsValue.qyshp = '';
      fieldsValue.qyshpgeo = '';
      fieldsValue.gdFile = '';
    } else fieldsValue.gkcsQt = gcgklx.value;
    return fieldsValue;
  }

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

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

  :deep(.ant-picker) {
    width: 100%;
  }

  /* :deep(.ant-radio-wrapper) {
    width: 120px;
    text-align: left;
} */
</style>
