<template>
  <div style="margin-top: -5vh; border: 1px solid gray; border-radius: 5px">
    <div style="margin-top: 1.2vh">
      <BasicForm :showResetButton="false" :showSubmitButton="false" @register="register" />
    </div>
  </div>
</template>
<script lang="ts" setup>
  import { reactive } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form';
  import { CoapSchemas } from './Coap';

  const emits = defineEmits(['prev']);
  const coapAllData = reactive({
    coapData: {},
  });
  const transportCoapData: any = reactive({
    coapDeviceTypeConfiguration: {
      coapDeviceType: null,
      transportPayloadTypeConfiguration: {
        transportPayloadType: null,
        deviceTelemetryProtoSchema: null,
        deviceAttributesProtoSchema: null,
        deviceRpcRequestProtoSchema: null,
        deviceRpcResponseProtoSchema: null,
      },
    },
    clientSettings: {
      powerMode: null,
      edrxCycle: null,
      pagingTransmissionWindow: null,
      psmActivityTimer: null,
    },
    type: 'COAP',
  });

  const [register, { validate, resetFields, setFieldsValue }] = useForm({
    labelWidth: 200,
    schemas: CoapSchemas,
    actionColOptions: {
      span: 14,
    },
  });
  const setFormData = (v) => {
    setFieldsValue({
      ...v?.clientSettings,
      ...v?.coapDeviceTypeConfiguration,
      ...v?.coapDeviceTypeConfiguration?.transportPayloadTypeConfiguration,
    });
  };
  const resetFormData = () => {
    resetFields();
  };
  function customResetFunc() {
    emits('prev');
  }
  const getFormData = async () => {
    const val = await validate();
    if (!val) return;
    for (let o in val) {
      for (let u in transportCoapData) {
        switch (u) {
          case 'coapDeviceTypeConfiguration':
            for (let t in transportCoapData.coapDeviceTypeConfiguration) {
              if (t === 'coapDeviceType') {
                Reflect.set(transportCoapData.coapDeviceTypeConfiguration, t, val[t]);
              }
              if (t === 'transportPayloadTypeConfiguration') {
                for (let t in transportCoapData.coapDeviceTypeConfiguration
                  .transportPayloadTypeConfiguration) {
                  if (t === o) {
                    Reflect.set(
                      transportCoapData.coapDeviceTypeConfiguration
                        .transportPayloadTypeConfiguration,
                      t,
                      val[t]
                    );
                  }
                }
              }
            }
            break;
          case 'clientSettings':
            for (let t in transportCoapData.clientSettings) {
              if (t === o) {
                Reflect.set(transportCoapData.clientSettings, t, val[t]);
              }
            }
            break;
        }
      }
    }
    coapAllData.coapData = {
      ...transportCoapData,
    };
    return coapAllData.coapData;
  };
  defineExpose({
    getFormData,
    resetFormData,
    setFormData,
    customResetFunc,
  });
</script>
<style lang="less" scoped></style>
