<template>
  <div class="proDog-setting">
    <a-form
      ref="formRef"
      name="basic"
      :label-col="{ style: { width: '140px' } }"
      :wrapper-col="{ span: 8 }"
      :model="identitySetting"
      autocomplete="off"
    >
      <h4>字段配置</h4>
      <a-divider />
      <a-form-item label="no(用户编号)" name="userNo">
        <a-input
          v-model:value="identitySetting.userNo"
          placeholder="选填，默认读取 user 字段"
        />
      </a-form-item>
    </a-form>
    <div style="margin: 22px 0 -22px 0">
      <a-button @click="prev"> 上一步 </a-button>
      <a-button type="primary" style="margin-left: 8px" @click="createIdentity">
        完成
      </a-button>
    </div>
  </div>
</template>

<script setup>
import Common from '../common/Common';
import { useRouter } from 'vue-router';
import { message } from 'ant-design-vue';
import { saveUpdateAuth } from './configData.js';
import { ref, defineProps, defineEmits, onMounted } from 'vue';

const router = useRouter();
const emit = defineEmits(['getDatas']);
const props = defineProps({
  formData: {
    type: Object,
    default: () => ({}),
  },
  identityInfo: {
    type: Object,
    default: () => ({}),
  },
});
const formRef = ref();
const prevDatas = ref({});
const identitySetting = ref({});

onMounted(() => {
  prevDatas.value = props.identityInfo;
  identitySetting.value = props.formData;
});

// 新建或更新认证源
const createIdentity = () => {
  formRef.value.validate().then(success => {
    const jsonStr = JSON.stringify(identitySetting.value);
    const formData = new FormData();
    formData.append('attribute', jsonStr);
    formData.append('id', prevDatas.value.id);
    formData.append('name', prevDatas.value.name);
    formData.append('no', prevDatas.value.no);
    formData.append('logo', prevDatas.value.file);
    formData.append('active', prevDatas.value.active);
    formData.append('authType', prevDatas.value.authType);
    formData.append('description', prevDatas.value.description);
    saveUpdateAuth(formData).then(
      success => {
        if (success.errorCode === 0) {
          if (!prevDatas.value.id) {
            message.success('新建认证源成功！');
          } else {
            message.success('更新认证源成功！');
          }
          router.push('/desktop/identityManager');
        } else {
          message.error(success.errorMessage);
        }
      },
      error => {
        Common.processException(error);
      },
    );
  });
};

const prev = () => {
  const datas = {
    current: 2,
    formData: identitySetting.value,
  };
  emit('getDatas', datas);
};

</script>

<style scoped>
.proDog-setting > h3 {
  font-size: 14px !important;
  font-weight: 700;
}
:deep(.ant-form-item-control-input-content) {
  display: flex;
  align-items: center;
}
:deep(.ant-form-item-label > label) {
  font-size: 12px !important;
  color: rgba(0, 0, 0, 0.4);
}
.ant-form-item {
  margin-bottom: 4px;
}
.ant-divider-horizontal {
  margin: 0 0 5px 0 !important;
}
</style>