<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="24">
          <a-form-item label="微博用户昵称" v-bind="validateInfos.screenName">
            <a-input v-model:value="formData.screenName" placeholder="请输入微博用户昵称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="微博用户性别" v-bind="validateInfos.gender">
            <a-input v-model:value="formData.gender" placeholder="请输入微博用户性别" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="微博数" v-bind="validateInfos.statusesCount">
	          <a-input-number v-model:value="formData.statusesCount" placeholder="请输入微博数" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="粉丝数" v-bind="validateInfos.followersCount">
	          <a-input-number v-model:value="formData.followersCount" placeholder="请输入粉丝数" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="关注数" v-bind="validateInfos.followCount">
	          <a-input-number v-model:value="formData.followCount" placeholder="请输入关注数" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="注册时间" v-bind="validateInfos.registrationTime">
            <a-input v-model:value="formData.registrationTime" placeholder="请输入注册时间" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="阳光信用" v-bind="validateInfos.sunshine">
            <a-input v-model:value="formData.sunshine" placeholder="请输入阳光信用" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="生日" v-bind="validateInfos.birthday">
            <a-input v-model:value="formData.birthday" placeholder="请输入生日" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="所在地" v-bind="validateInfos.location">
            <a-input v-model:value="formData.location" placeholder="请输入所在地" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="教育经历" v-bind="validateInfos.education">
            <a-input v-model:value="formData.education" placeholder="请输入教育经历" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公司" v-bind="validateInfos.company">
            <a-input v-model:value="formData.company" placeholder="请输入公司" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="微博简介" v-bind="validateInfos.description">
            <a-input v-model:value="formData.description" placeholder="请输入微博简介" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="微博主页" v-bind="validateInfos.profileUrl">
            <a-input v-model:value="formData.profileUrl" placeholder="请输入微博主页" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="微博头像url" v-bind="validateInfos.profileImageUrl">
	          <j-image-upload  v-model:value="formData.profileImageUrl" :disabled="disabled"></j-image-upload>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="微博高清头像url" v-bind="validateInfos.avatarHd">
	          <j-image-upload  v-model:value="formData.avatarHd" :disabled="disabled"></j-image-upload>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="微博等级" v-bind="validateInfos.urank">
	          <a-input-number v-model:value="formData.urank" placeholder="请输入微博等级" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="微博会员等级" v-bind="validateInfos.mbrank">
	          <a-input-number v-model:value="formData.mbrank" placeholder="请输入微博会员等级" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="微博是否认证" v-bind="validateInfos.verified">
	          <a-input-number v-model:value="formData.verified" placeholder="请输入微博是否认证" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="微博认证类型" v-bind="validateInfos.verifiedType">
	          <a-input-number v-model:value="formData.verifiedType" placeholder="请输入微博认证类型" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="微博认证信息" v-bind="validateInfos.verifiedReason">
            <a-input v-model:value="formData.verifiedReason" placeholder="请输入微博认证信息" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../WeiboUser.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    screenName: '',   
    gender: '',   
    statusesCount: undefined,
    followersCount: undefined,
    followCount: undefined,
    registrationTime: '',   
    sunshine: '',   
    birthday: '',   
    location: '',   
    education: '',   
    company: '',   
    description: '',   
    profileUrl: '',   
    profileImageUrl: '',   
    avatarHd: '',   
    urank: undefined,
    mbrank: undefined,
    verified: undefined,
    verifiedType: undefined,
    verifiedReason: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    screenName: [{ required: true, message: '请输入微博用户昵称!'},],
    gender: [{ required: true, message: '请输入微博用户性别!'},],
    statusesCount: [{ required: true, message: '请输入微博数!'},],
    followersCount: [{ required: true, message: '请输入粉丝数!'},],
    followCount: [{ required: true, message: '请输入关注数!'},],
    registrationTime: [{ required: true, message: '请输入注册时间!'},],
    sunshine: [{ required: true, message: '请输入阳光信用!'},],
    birthday: [{ required: true, message: '请输入生日!'},],
    location: [{ required: true, message: '请输入所在地!'},],
    education: [{ required: true, message: '请输入教育经历!'},],
    company: [{ required: true, message: '请输入公司!'},],
    description: [{ required: true, message: '请输入微博简介!'},],
    profileUrl: [{ required: true, message: '请输入微博主页!'},],
    profileImageUrl: [{ required: true, message: '请输入微博头像url!'},],
    avatarHd: [{ required: true, message: '请输入微博高清头像url!'},],
    urank: [{ required: true, message: '请输入微博等级!'},],
    mbrank: [{ required: true, message: '请输入微博会员等级!'},],
    verified: [{ required: true, message: '请输入微博是否认证!'},],
    verifiedType: [{ required: true, message: '请输入微博认证类型!'},],
    verifiedReason: [{ required: true, message: '请输入微博认证信息!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    min-height: 500px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
