<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="微信公众号id" v-bind="validateInfos.wechatId">
            <a-input v-model:value="formData.wechatId" placeholder="请输入微信公众号id" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="v" v-bind="validateInfos.contentV">
            <a-input v-model:value="formData.contentV" placeholder="请输入v" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公众号唯一id" v-bind="validateInfos.msgBiz">
            <a-input v-model:value="formData.msgBiz" placeholder="请输入公众号唯一id" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="创建时间" v-bind="validateInfos.createAt">
		        <a-date-picker placeholder="请选择创建时间"  v-model:value="formData.createAt" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="公众号简介" v-bind="validateInfos.descContent">
	          <j-editor v-model:value="formData.descContent" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="头像" v-bind="validateInfos.heading">
	          <j-image-upload  v-model:value="formData.heading" :disabled="disabled"></j-image-upload>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="最大日期添加数" v-bind="validateInfos.maxDayPubCount">
	          <a-input-number v-model:value="formData.maxDayPubCount" placeholder="请输入最大日期添加数" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="打开历史页时间" v-bind="validateInfos.openHistoryPageAt">
		        <a-date-picker placeholder="请选择打开历史页时间"  v-model:value="formData.openHistoryPageAt" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="标题" v-bind="validateInfos.title">
            <a-input v-model:value="formData.title" placeholder="请输入标题" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="更新时间" v-bind="validateInfos.updatedAt">
		        <a-date-picker placeholder="请选择更新时间"  v-model:value="formData.updatedAt" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="用户名" v-bind="validateInfos.username">
            <a-input v-model:value="formData.username" placeholder="请输入用户名" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="最后添加时间" v-bind="validateInfos.latesPublishAt">
		        <a-date-picker placeholder="请选择最后添加时间"  v-model:value="formData.latesPublishAt" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="微信id" v-bind="validateInfos.wexintId">
            <a-input v-model:value="formData.wexintId" placeholder="请输入微信id" :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 JEditor from '/@/components/Form/src/jeecg/components/JEditor.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../WechatProfiles.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: '',
    wechatId: '',   
    contentV: '',   
    msgBiz: '',   
    createAt: '',   
    descContent: '',   
    heading: '',   
    maxDayPubCount: undefined,
    openHistoryPageAt: '',   
    title: '',   
    updatedAt: '',   
    username: '',   
    latesPublishAt: '',   
    wexintId: '',   
  });
  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 = {
    wechatId: [{ required: true, message: '请输入微信公众号id!'},],
    contentV: [{ required: true, message: '请输入v!'},],
    msgBiz: [{ required: true, message: '请输入公众号唯一id!'},],
    createAt: [{ required: true, message: '请输入创建时间!'},],
    descContent: [{ required: true, message: '请输入公众号简介!'},],
    heading: [{ required: true, message: '请输入头像!'},],
    maxDayPubCount: [{ required: true, message: '请输入最大日期添加数!'},],
    openHistoryPageAt: [{ required: true, message: '请输入打开历史页时间!'},],
    title: [{ required: true, message: '请输入标题!'},],
    updatedAt: [{ required: true, message: '请输入更新时间!'},],
    username: [{ required: true, message: '请输入用户名!'},],
    latesPublishAt: [{ required: true, message: '请输入最后添加时间!'},],
    wexintId: [{ required: true, message: '请输入微信id!'},],
  };
  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>
