<script setup lang="ts">
import { VxeUI, type VxeFormProps, type VxeFormListeners } from 'vxe-pc-ui';
import { useRequest } from 'alova/client';
import { addOrganizationMethod } from '@/request/api/system.api';
import { notification } from 'ant-design-vue';
import type { OrganizationModel } from '@/model/system.model';
(notification.config as any)({
  zIndex: 10000, // 直接设置层级
});

const props = defineProps<{ data: OrganizationModel }>();

const emits = defineEmits<{
  submit: [data?: OrganizationModel];
}>();

const model = ref<OrganizationModel>({} as OrganizationModel);

watchEffect(() => {
  if (props.data) {
    model.value = { ...props.data };
  }
});

const { loading: submitting, send: submit } = useRequest(addOrganizationMethod, { immediate: false }).onSuccess(() => {
  emits('submit');
});

const formItems = computed(() => {
  const baseItems: any[] = [
    {
      field: 'name',
      title: '组织名称',
      span: 20,
      itemRender: {
        name: 'VxeInput',
        props: {
          placeholder: '请输入',
        },
      },
    },
    {
      field: 'appId',
      title: '小程序ID',
      span: 20,
      itemRender: {
        name: 'VxeInput',
        props: {
          placeholder: '请输入',
        },
      },
    },
    {
      field: 'appSecret',
      title: '密钥',
      span: 20,
      itemRender: {
        name: 'VxeInput',
        props: {
          placeholder: '请输入',
        },
      },
    },
    {
      field: 'remark',
      title: '备注',
      span: 24,
      slots: {
        default: 'remarksSlot',
      },
    },
    { align: 'center', span: 24, slots: { default: 'active' } },
  ];
  return baseItems;
});

const formProps = reactive<VxeFormProps>({
  titleWidth: 100,
  titleAlign: 'right',
  titleColon: true,
  data: computed(() => model.value),
  items: [] as any, // 临时设置为空数组，我们将在模板中使用动态items
  rules: {
    name: [{ required: true, message: '请输入组织名称' }],
    appId: [{ required: true, message: '请输入小程序ID' }],
    appSecret: [{ required: true, message: '请输入密钥' }],
  },
});

const formEmits: VxeFormListeners = {
  submit({ data }) {
    data.remark ??= '';
    submit(data).then(() => {
      notification.success({
        message: '操作成功',
      });
      VxeUI.modal.close('organization-edit-modall');
    });
  },
};

function cancel() {
  VxeUI.modal.close('organization-edit-modal');
}

onBeforeMount(async () => {
  if (props.data) {
    model.value = { ...props.data };
  }
});


</script>

<template>
  <div class="form-container">
    <vxe-form
      :title-width="formProps.titleWidth"
      :title-align="formProps.titleAlign"
      :title-colon="formProps.titleColon"
      :data="formProps.data"
      :items="formItems"
      :rules="formProps.rules"
      v-on="formEmits"
      :loading="submitting"
    >
      <template #deviceIdTitleSlot> <span style="color: #f56c6c; font-size: 20px">*</span> 设备ID </template>

      <template #deviceIdSlot>
        <div class="device-ids-container">
          <vxe-input v-model="deviceIdsSafe[index]" placeholder="请输入" style="width: 200px" />
        </div>
      </template>

      <template #remarksSlot>
        <div class="section-container">
          <textarea
            v-model="model.remark"
            placeholder="请输入"
            rows="3"
            style="width: 100%; padding: 8px; border: 1px solid #d9d9d9; border-radius: 4px; resize: vertical; font-family: inherit"
          />
        </div>
      </template>

      <template #active>
        <vxe-button type="reset" content="取消" :disabled="submitting" @click="cancel"></vxe-button>
        <vxe-button type="submit" status="warning" content="确定" :loading="submitting"></vxe-button>
      </template>
    </vxe-form>
  </div>
</template>

<style scoped lang="scss">
.form-container {
  padding: 20px;
}

.device-ids-container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  gap: 8px;
  align-items: flex-start;

  .device-id-item {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
  }
}

.section-container {
  margin-bottom: 20px;

  .section-title {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 10px;
    color: #333;
  }

  .section-divider {
    height: 1px;
    background-color: #d9d9d9;
    margin-bottom: 15px;
  }
}

.process-config {
  .config-item {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
    flex-wrap: wrap;
    gap: 8px;

    .config-label {
      font-weight: bold;
      min-width: 80px;
    }

    .report-label {
      margin-left: 40px;
      color: #666;
      margin-right: 10px;
    }

    .radio-group {
      display: flex;
      align-items: center;
      gap: 30px;

      .radio-item {
        display: flex;
        align-items: center;
        gap: 4px;
        cursor: pointer;

        input[type='radio'] {
          margin: 0;
          cursor: pointer;
        }

        span {
          font-size: 14px;
        }
      }

      .badge {
        background-color: #faad14;
        color: white;
        border-radius: 12px;
        padding: 2px 8px;
        font-size: 12px;
        min-width: 20px;
        text-align: center;
      }
    }
  }
}

:deep(.vxe-form--item) {
  .vxe-form--item-wrapper {
    .vxe-form--item-content {
      .vxe-input {
        width: 100%;
      }
    }
  }
}

.required-field {
  :deep(.vxe-form--item-title) {
    position: relative;

    &::before {
      content: '*';
      color: #ff4d4f;
      position: absolute;
      left: -8px;
      top: 0;
    }
  }
}
</style>
