<script setup>
import XAddNativeForm from '@vue2-client/base-client/components/common/XAddNativeForm/XAddNativeForm.vue'
import { ref, computed, useAttrs } from 'vue'

const xAddNativeFormRef = ref()

const attrs = useAttrs()
const props = defineProps({
  // 是否启用 horizontal 模式的自定义配置
  enableHorizontalCustom: {
    type: Boolean,
    default: false
  }
})
// 从 radio-style_1 属性值中获取 padding
const radioPadding = computed(() => {
  const val = attrs['radio-style_1']
  if (val !== undefined && val !== '' && val !== null) {
    const num = Number(val)
    return isNaN(num) ? 9 : num
  }
  return 9
})

// 包装器样式对象
const wrapperStyleObject = computed(() => {
  return {
    '--radio-padding': `${radioPadding.value}px`
  }
})

// 启动时间选择框弹出到body
const enablePopupToBody = computed(() => {
  const a = attrs
  return a.enablePopupToBody === 'true' || a.enablePopupToBody === true
})
const wrapperClassObject = computed(() => {
  const a = attrs
  const classes = {}

  // 1) 多个布尔型样式开关（存在且为真则生效）
  const booleanStyleKeys = [
    'query-conditions',
    'padding-50',
    'label-text-horizontal',
    'label-text-justify',
    'simple-inline',
    'simple-inline-show-label',
    'label-justify',
    'charge-form',
    'mini-form',
    'content-right',
    'label-font-style'
  ]
  // 只要属性存在就生效的样式开关
  const existStyleKeys = ['radio-style_1']
  for (const key of booleanStyleKeys) {
    const val = a[key]
    const truthy = val === true || val === '' || val === 'true'
    if (truthy) classes[`h-form-${key}`] = true
  }
  // 检查存在即生效的样式开关
  for (const key of existStyleKeys) {
    if (key in a) classes[`h-form-${key}`] = true
  }

  return classes
})

defineExpose({
  getXAddNativeFormInstance: () => xAddNativeFormRef.value,
  init: (params) => xAddNativeFormRef.value && xAddNativeFormRef.value.init && xAddNativeFormRef.value.init(params),
  asyncSubmit: () => xAddNativeFormRef.value && xAddNativeFormRef.value.asyncSubmit && xAddNativeFormRef.value.asyncSubmit(),
  validateForm: () => xAddNativeFormRef.value && xAddNativeFormRef.value.validateForm && xAddNativeFormRef.value.validateForm(),
  /** 等待配置结束的异步方法，委托给内部 XAddNativeForm */
  waitConfigEnd: () => xAddNativeFormRef.value?.waitConfigEnd?.() ?? Promise.resolve()
})

</script>

<template>
  <div
    class="h-form-wrapper"
    :class="[
      wrapperClassObject
    ]"
    :style="wrapperStyleObject"
  >

    <x-add-native-form
      ref="xAddNativeFormRef"
      v-bind="$attrs"
      :enable-popup-to-body="enablePopupToBody"
      :enable-horizontal-custom="props.enableHorizontalCustom"
      @configEnd="$emit('configEnd')"
      v-on="$listeners"
    >
      <template v-for="(_, name) in $slots" #[name]="slotData">
        <slot :name="name" v-bind="slotData" />
      </template>
    </x-add-native-form>
  </div>
</template>

<style scoped lang="less">
// 单选框 padding 变量
@radio-padding: var(--radio-padding, 9px);

.h-form-wrapper {
  // 基础样式
  :deep(.ant-form-item) {
    margin-bottom: 4px;
  }

  &.h-form-mini-form {
    :deep(.ant-form-inline) {
      .ant-row-flex {
        .ant-col {
          flex: auto !important;
          max-width: 250px !important;
        }
      }
    }

    :deep(.ant-form-item) {
      margin-right: 0px !important;
    }
  }

  &.h-form-content-right {
    :deep(.ant-form-inline) {
      .ant-row-flex {
        justify-content: right;
      }
    }
  }

  // query-conditions 样式
  &.h-form-query-conditions {
    :deep(.ant-card-body) {
      padding: 0px;
    }

    :deep(.ant-row-flex) {
      .x-form-col-wrapper {
        .ant-form-item {
          display: flex !important;
          width: 100%;
          height: 100%;
          margin-bottom: 0px;
          justify-content: space-between;

          .ant-form-item-label {
            display: flex;
            align-items: center;
            height: 100%;

            label {
              height: auto;
              font-family: Source Han Sans;
              font-size: 16px;
              font-weight: normal;
              line-height: normal;
              color: #313131;
              display: block !important; // 保持 block 以支持两端对齐
              width: 100%;
              white-space: nowrap;
              margin: 0;
              text-align: justify;
              text-align-last: justify;
              text-justify: inter-ideograph;
              box-sizing: border-box;
              padding-right: 0 !important; // 移除冒号预留空间

              // 使用 line-height 实现垂直居中
              line-height: 40px; // 设置与容器高度相同的行高
            }

            & > label::after {
              display: none !important;
            }
          }
        }
      }

      .ant-col-24 {
        padding: 0px 4px !important;
      }
    }
  }

  // 单选框样式设计图中切换模式的样式
  &.h-form-radio-style_1 {
    :deep(.ant-radio-button-wrapper) {
      padding: 0 @radio-padding;
    }

    :deep(.ant-radio-button-wrapper-checked) {
      background-color: #0057FE;
      color: white;
    }
  }

  &.h-form-simple-inline {
    :deep(.ant-form-item-label) {
      display: none !important;
    }

    :deep(.ant-form-item-control-wrapper) {
      width: 100%;
    }
  }

  &.h-form-padding-50 {
    // ant-row-flex 样式
    :deep(.ant-row-flex) {
      padding-left: 50px;
      padding-right: 50px
    }
  }

  // 单选框样式设计图中切换模式的样式
  &.h-form-radio-style_1 {
    :deep(.ant-radio-button-wrapper) {
      padding: 0 @radio-padding;
    }

    :deep(.ant-radio-button-wrapper-checked) {
      background-color: #0057FE;
      color: white;
    }
  }

  // charge-form 样式
  &.h-form-charge-form {
    margin-top: 2px !important;
    // 定义变量
    @font-common: {
      font-family: 'Source Han Sans', sans-serif;
      font-size: 16px;
      font-weight: normal;
      letter-spacing: 0em;
      font-feature-settings: "kern" on;
    };

    @color-primary: #313131;
    @color-placeholder: #999999;

    // 定义混合宏
    .input-base() {
      width: 100%;
      height: 30px;
      line-height: 30px;
      vertical-align: top;
      margin: 0;
      text-align: left;
      color: @color-primary;
      opacity: 1;
      @font-common();
    }

    .placeholder-base() {
      color: @color-placeholder;
      opacity: 1;
      @font-common();
    }

    .option-group-base() {
      height: 30px;
      line-height: 30px;
      display: flex;
      align-items: center;

      .ant-radio-wrapper,
      .ant-checkbox-wrapper {
        height: 30px;
        line-height: 30px;
        margin-right: 16px;
      }
    }

    :deep(.ant-form-item) {
      margin-bottom: 16px;
      margin-top: 0;
      padding: 0;
      display: flex;
      align-items: center;

      .ant-form-item-label {
        text-align: left;
        margin: 0 11px 0 0 !important;
        width: 64px;
        min-width: 64px;
        max-width: 64px;
        flex-shrink: 0;

        label {
          height: 23px;
          opacity: 1;
          line-height: 23px;
          color: @color-primary;
          margin: 0;
          padding: 0;
          white-space: nowrap;
          display: block;
          @font-common();

          // 移除冒号
          &::after {
            content: '';
          }
        }
      }

      .ant-form-item-control-wrapper {
        margin: 0;
        padding: 0;
        flex: 1;
        min-width: 0;
        display: flex;
        align-items: center;

        .ant-form-item-control {
          margin: 0;
          padding: 0;
          width: 100%;
          text-align: left;
          line-height: 30px !important;

          // 输入框样式统一
          .ant-input,
          .ant-input-affix-wrapper,
          .ant-select,
          .ant-input-number,
          .ant-picker,
          .ant-radio-group,
          .ant-checkbox-group {
            .input-base();
          }

          // 选择器样式
          .ant-select {
            .ant-select-selector {
              .input-base();
              height: 30px;
              line-height: 30px;

              .ant-select-selection-item {
                .input-base();
                line-height: 30px;
              }

              .ant-select-selection-placeholder {
                .placeholder-base();
                line-height: 30px;
                color: #313131;
              }
            }
          }

          // 日期选择器样式
          .ant-picker {
            height: 30px;

            .ant-picker-input {
              height: 30px;
              line-height: 30px;
              .input-base();

              input {
                .input-base();
                height: 30px;
                line-height: 30px;
              }
            }
          }

          // 数字输入框样式
          .ant-input-number {
            height: 30px;

            .ant-input-number-input {
              .input-base();
              height: 30px;
              line-height: 30px;
            }
          }

          // 单选框组样式
          .ant-radio-group {
            .option-group-base();
          }

          // 复选框组样式
          .ant-checkbox-group {
            .option-group-base();
          }
        }
      }
    }

    // 重置可能的容器边距
    :deep(.ant-row) {
      margin: 0;
      padding: 0;

      .ant-col {
        margin: 0;
        padding: 0;
      }
    }

    // 强制重置所有可能的表单项样式
    :deep(.ant-form-item) {
      margin: 0 0 16px 0 !important;

      // 重置可能的内部边距
      .ant-form-item-control-input,
      .ant-form-item-control-input-content {
        margin: 0 !important;
        padding: 0 !important;
      }
    }
  }

  // label-text-horizontal 样式 - 只控制label文字从左到右排列
  &.h-form-label-text-horizontal {
    :deep(.ant-form-item-label) {
      text-align: left;
      direction: ltr;

      // 标签文字水平排列
      .ant-form-item-label-text {
        display: inline-block;
        text-align: left;
        direction: ltr;
        unicode-bidi: normal;
      }

      // 必填标识水平排列
      .ant-form-item-required::before {
        margin-right: 4px;
        margin-left: 0;
      }
    }
  }

  // label-text-justify 样式 - 标签文字两端对齐分散占满 必填*可能会有问题
  &.h-form-label-text-justify {
    :deep(.ant-form-item-label) {
      padding-left: 0;
      padding-right: 8px;

      label {
        position: relative;
        display: block;
        width: 100%;
        white-space: nowrap;
        margin: 0;
        text-indent: 0;
        text-align: justify;
        text-align-last: justify;
        text-justify: inter-ideograph;
        box-sizing: border-box;
        padding-right: 12px; // 给绝对定位的冒号留出空间
      }

      // 将冒号绝对定位到最右侧，避免参与两端对齐计算
      label::after {
        position: absolute;
        right: 0;
      }
    }
  }

  // label-justify 样式 - 两端对齐，应用指定字体样式
  &.h-form-label-justify {
    padding-left: 8px;

    :deep(.ant-form-item-label) {
      padding-left: 0;
      padding-right: 8px;
      // 限制label容器的宽度，强制两端对齐
      width: 80px !important;
      min-width: 80px;
      max-width: 80px;

      label {
        position: relative;
        display: block;
        width: 100%;
        white-space: nowrap;
        margin: 0;
        text-indent: 0;
        text-align: justify;
        text-align-last: justify;
        text-justify: inter-ideograph;
        box-sizing: border-box;
        padding-right: 12px; // 给绝对定位的冒号留出空间

        // 应用指定的字体样式
        height: 39px;
        opacity: 1;
        font-family: 'Source Han Sans', sans-serif;
        font-size: 16px;
        font-weight: normal;
        line-height: 39px;
        color: #313131;
      }

      // 去掉冒号
      label::after {
        content: '';
      }
    }
  }

  // label-font-style 样式 - 独立的label字体样式
  &.h-form-label-font-style {
    :deep(.ant-form-item-label) {
      label {
        height: 23px;
        opacity: 1;
        font-family: Source Han Sans;
        font-size: 16px;
        font-weight: normal;
        line-height: normal;
        letter-spacing: 0em;
        font-feature-settings: "kern" on;
        color: #313131;
      }
    }
  }

  &.h-form-simple-inline-show-label {
    // ========== 公共字体样式变量 ==========
    @common-font: {
      font-family: Source Han Sans !important;
      font-size: 16px !important;
      font-weight: normal !important;
      letter-spacing: 0em !important;
      font-feature-settings: "kern" on !important;
    };

    @common-text-color: #313131;
    @common-placeholder-color: #999;
    @common-border-color: #E5E9F0;
    @common-focus-color: #0057FE;

    // ========== 表单项基础布局 ==========
    :deep(.ant-form-item) {
      display: flex !important;
      flex-direction: row !important;
      align-items: flex-start !important;
      margin: 0 !important;
      padding: 0 !important;
      margin-bottom: 0 !important;
    }

    // 修复 ant-row 伪元素
    :deep(.ant-row) {
      &::before,
      &::after {
        display: table !important;
        content: '' !important;
      }
    }

    // ========== Label 样式 ==========
    :deep(.ant-form-item-label) {
      display: flex !important;
      align-items: flex-start !important;
      padding: 0 8px 0 0 !important;
      width: 90px !important;
      min-width: 90px !important;
      max-width: 90px !important;
      flex-shrink: 0 !important;
      flex: none !important;

      label {
        position: relative !important;
        display: block !important;
        width: 100% !important;
        height: 39px !important;
        margin: 0 !important;
        padding: 0 !important;
        opacity: 1 !important;
        @common-font();
        line-height: 39px !important;
        color: @common-text-color !important;
        text-align: justify !important;
        text-align-last: justify !important;
        text-justify: inter-ideograph !important;
        text-indent: 12px !important;
        box-sizing: border-box !important;

        &::after {
          display: none !important;
          content: none !important;
        }

        &:not(.ant-form-item-required)::before {
          display: none !important;
          content: none !important;
        }
      }
    }

    // 必填项星号
    :deep(.ant-form-item-required) {
      &::before {
        display: block !important;
        visibility: visible !important;
        content: '*' !important;
        position: absolute !important;
        left: -8px !important;
        top: 40% !important;
        transform: translateY(-50%) !important;
        color: #ff4d4f !important;
        font-size: 14px !important;
        font-family: SimSun, sans-serif !important;
        z-index: 1 !important;
        line-height: 1 !important;
      }
    }

    // ========== 控件容器 ==========
    :deep(.ant-form-item-control-wrapper) {
      flex: 1 !important;
      min-width: 0 !important;
      width: auto !important;

      .ant-form-item-control {
        width: 100% !important;

        .ant-form-item-children {
          display: block !important;
          width: 100% !important;
          padding-right: 41px;
          padding-left: 6px;
        }
      }
    }

    // ========== 公共输入框样式 Mixin ==========
    .input-common() {
      border-radius: 6px !important;
      box-sizing: border-box !important;
      border: 1px solid @common-border-color !important;
      @common-font();
      color: @common-text-color !important;

      &:hover,
      &:focus {
        border-color: @common-focus-color !important;
        box-shadow: none !important;
      }

      &[readonly] {
        background-color: #f5f5f5;
        cursor: not-allowed;
      }
    }

    // ========== 普通输入框（不包括文本域） ==========
    :deep(.ant-input:not(textarea)) {
      .input-common();
      height: 39px !important;
      line-height: 37px !important;
      padding: 0 11px !important;

      &::placeholder {
        line-height: 37px !important;
        color: @common-placeholder-color !important;
      }
    }

    // ========== 文本域样式 ==========
    :deep(textarea.ant-input) {
      .input-common();
      min-height: 80px !important;
      padding: 8px 11px !important;
      line-height: 22px !important;
      resize: vertical !important;

      &::placeholder {
        color: @common-placeholder-color !important;
        line-height: 22px !important;
      }
    }

    // ========== 下拉框样式 ==========
    :deep(.ant-select) {
      .ant-select-selection {
        border-radius: 6px !important;
        border: 1px solid @common-border-color !important;
        height: 39px !important;

        &:hover {
          border-color: @common-focus-color !important;
        }
      }

      .ant-select-selection__rendered {
        height: 39px !important;
        line-height: 39px !important;
        margin: 0 !important;
        padding: 0 11px !important;
      }

      .ant-select-selection__placeholder {
        @common-font();
        color: @common-placeholder-color !important;
        line-height: 37px !important;
        height: 37px !important;
        top: 0 !important;
        margin-top: 0 !important;
        padding-left: 11px;
      }

      .ant-select-selection-selected-value {
        @common-font();
        color: @common-text-color !important;
        line-height: 37px !important;
        padding: 0 !important;
      }

      .ant-select-arrow {
        right: 11px !important;
        top: 50% !important;
        transform: translateY(-50%) !important;

        .ant-select-arrow-icon {
          color: @common-placeholder-color !important;
        }
      }
    }

    :deep(.ant-select-focused) {
      .ant-select-selection {
        border-color: @common-focus-color !important;
        box-shadow: none !important;
      }
    }

    // ========== 单选框样式 ==========
    :deep(.ant-radio-group) {
      display: flex !important;
      align-items: center !important;
      height: 39px !important;
      line-height: 39px !important;
    }

    :deep(.ant-radio-wrapper) {
      @common-font();
      line-height: 39px !important;
      color: @common-text-color !important;
      margin-right: 16px !important;
      height: 39px !important;
      display: inline-flex !important;
      align-items: center !important;

      // 单选框后面的文字
      span:not(.ant-radio) {
        @common-font();
        line-height: 39px !important;
        color: @common-text-color !important;
        padding: 0 !important;
      }
    }

    // 单选框圆圈
    :deep(.ant-radio) {
      margin-right: 4px !important;

      .ant-radio-inner {
        width: 16px !important;
        height: 16px !important;
        background: #FFFFFF !important;
        border: 1px solid #CDCFD4 !important;
        border-radius: 50% !important;
        position: relative !important;

        &::after {
          width: 10px !important;
          height: 10px !important;
          background: @common-focus-color !important;
          border-radius: 50% !important;
          position: absolute !important;
          top: 50% !important;
          left: 50% !important;
          transform: translate(-50%, -50%) !important;
          margin: 0 !important;
        }
      }

      // 选中状态
      &.ant-radio-checked .ant-radio-inner {
        border-color: @common-focus-color !important;
        background: #FFFFFF !important;
      }

      // hover 状态
      &:hover .ant-radio-inner {
        border-color: @common-focus-color !important;
      }
    }
  }
}
</style>
