<!-- 表单布局 -->
<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="请输入网址" :disabled="model[field] != 1" style="width: 77%" />
        </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: 91%" />
        </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: {},
    },
    obj: {
      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: `${props.obj[0].name}标题`,
      field: 'bt',
      required: true,
      component: 'Input',
    },
    {
      label: `${props.obj[1].name}完成日期`,
      field: 'fxpgwcsj',
      required: true,
      component: 'DatePicker',
      componentProps: {
        //日期格式化，页面上显示的值
        format: 'YYYY-MM-DD',
        //返回值格式化（绑定值的格式）
        valueFormat: 'YYYY-MM-DD',
        //是否显示今天按钮
        showToday: true,
      },
    },

    {
      label: '公示',
      field: 'gsfs',
      required: true,
      component: 'JDictSelectTag',
      slot: 'gstype',
      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: '252px' } },
    //row行的样式
    baseRowStyle: { width: '100%' },
    //表单布局属性，支持（vertical,horizontal），默认为horizontal
    layout: 'horizontal',
  });
  watch(
    () => props.data,
    async (val: any) => {
      val &&
        nextTick(async () => {
          const obj = toRaw(val);
          if (obj.gsfs == 3) {
            obj.qt = val.gswz;
            obj.gswz = '';
          }
          if (!obj.gsfs) obj.gsfs = '1';
          if (props.labelFirst == '效果评估') {
            obj.bt = obj.zlyxfxgpgbt;
            obj.fxpgwcsj = obj.zlyxfxgpgwcsj;
          }

          await setFieldsValue({
            ...obj,
          });
        });
    },
    { deep: true, immediate: true }
  );
  // 获取子组件 列表
  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;
    if (props.labelFirst == '效果评估') {
      fieldsValue.zlyxfxgpgbt = fieldsValue.bt;
      fieldsValue.zlyxfxgpgwcsj = fieldsValue.fxpgwcsj;
    }
    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>
