<!-- 表单布局 -->
<template>
  <!-- 自定义表单 -->
  <BasicForm @register="registerForm">
    <template #gstype="{ model, field }">
      <!-- 如果是组件需要进行双向绑定，model当前表单对象，field当前字段名称  -->
      <a-radio-group v-model:value="model[field]" style="display: flex">
        <div class="w-[50%]">
          <a-radio :value="1" :style="{ height: '30px', lineHeight: '30px', width: '60px' }" @change="model['qt'] = ''">网址</a-radio>
          <a-input v-model:value="model['gswz']" placeholder="请输入网址" @blur="onBlur(model)" :disabled="model[field] != 1" style="width: 73%" />
        </div>
        <div class="w-[50%]">
          <a-radio :value="3" :style="{ height: '30px', lineHeight: '30px', width: '60px' }" @change="model['gswz'] = ''">其它</a-radio>
          <a-input v-model:value="model['qt']" placeholder="请输入信息" :disabled="model[field] != 3" style="width: 73%" />
        </div>
      </a-radio-group>
    </template>
  </BasicForm>
</template>

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

  const { createMessage } = useMessage();

  const props = defineProps({
    labelFirst: {
      type: String,
      default: '初步',
    },
    data: {
      type: Object,
      default: {},
    },
  });
  //自定义表单字段
  const formSchemas: FormSchema[] = [
    {
      label: '污染地块id',
      field: 'wrdkid',
      component: 'Input',
      show: false,
    },
    {
      label: 'id',
      field: 'id',
      component: 'Input',
      show: false,
    },
    {
      label: '网址',
      field: 'gswz',
      component: 'Input',

      show: false,
    },
    {
      label: '风险评估标题',
      field: 'fxpgbt',
      component: 'Input',
      required: true,
    },
    {
      label: '报告编制完成日期',
      field: 'fxpgwcsj',
      required: true,
      component: 'DatePicker',
      componentProps: {
        //日期格式化，页面上显示的值
        format: 'YYYY-MM-DD',
        //返回值格式化（绑定值的格式）
        valueFormat: 'YYYY-MM-DD',
        //是否显示今天按钮
        showToday: true,
      },
    },

    {
      label: '公示',
      field: 'gsfs',
      component: 'JDictSelectTag',
      required: true,
      slot: 'gstype',
      defaultValue: 1,
      colProps: { span: 24 },
    },
    {
      label: '其他',
      field: 'qt',
      component: 'JDictSelectTag',
      show: 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: '150px' } },
    //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?.gswz != oldVal?.gswz) || (newVal?.fxpgwcsj != oldVal?.fxpgwcsj) || (newVal?.gsfs != oldVal?.gsfs)) &&  
        nextTick(async () => {
          const obj = toRaw(newVal);
          if (obj.gsfs == 3) {
            obj.qt = newVal.gswz;
            obj.gswz = '';
          }
          if (!obj.gsfs) obj.gsfs = 1;
          await setFieldsValue({
            ...obj,
          });
        });
    },
    { deep: true, immediate: true }
  );
  // 失去焦点校验网址
  function onBlur(model) {
    console.log(model.gswz, 'v');

    const reg = /^(http|https):\/\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|localhost|[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z]{2,})(:\d{2,5})?(\/\S*)?$/;
    if (!reg.test(model.gswz)) createMessage.info('请输入正确的网址');
  }
  // 获取子组件 列表
  async function getFormValueFxpg() {
    console.log('子组件执行');
    //获取所有值
    let fieldsValue = await getFieldsValue();
    fieldsValue = await validate();
    if ((fieldsValue.gsfs == 3 && !fieldsValue.qt) || (fieldsValue.gsfs == 1 && !fieldsValue.gswz)) {
      createMessage.warn('请先填写公示信息');
      return
    }
    if (fieldsValue.gsfs == 3) {
      fieldsValue.gswz = fieldsValue.qt;
      delete fieldsValue.qt;
    } else delete fieldsValue.qt;

    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>
