<!-- 编辑弹窗 -->
<template>
  <ele-modal
    :width="1000"
    :visible="visible"
    :maskClosable="false"
    :maxable="true"
    :title="'处理'"
    :body-style="{ paddingBottom: '28px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="styleResponsive ? { md: 2, sm: 5, xs: 24 } : { flex: '90px' }"
      :wrapper-col="
        styleResponsive ? { md: 21, sm: 19, xs: 24 } : { flex: '1' }
      "
    >
      <a-form-item label="备注">
        <a-textarea
          :rows="4"
          :maxlength="500"
          placeholder="请输入备注"
          v-model:value="form.comments"
        />
      </a-form-item>
      <a-form-item label="状态" name="status">
        <a-radio-group v-model:value="form.status">
          <a-radio :value="1">已处理</a-radio>
          <a-radio :value="0">待处理</a-radio>
        </a-radio-group>
      </a-form-item>
    </a-form>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { message } from 'ant-design-vue';
  import { addFormRecord, updateFormRecord } from '@/api/cms/form-record';
  import { FormRecord } from '@/api/cms/form-record/model';
  import { useThemeStore } from '@/store/modules/theme';
  import { storeToRefs } from 'pinia';
  import { FormInstance } from 'ant-design-vue/es/form';
  import { ItemType } from 'ele-admin-pro/es/ele-image-upload/types';
  import useFormData from '@/utils/use-form-data';

  // 是否是修改
  const isUpdate = ref(false);
  // 是否开启响应式布局
  const themeStore = useThemeStore();
  const { styleResponsive } = storeToRefs(themeStore);
  // 编辑器内容，双向绑定
  const content = ref<any>('');

  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    // 修改回显的数据
    data?: FormRecord | null;
  }>();

  const emit = defineEmits<{
    (e: 'done'): void;
    (e: 'update:visible', visible: boolean): void;
  }>();

  // 提交状态
  const loading = ref(false);
  // 已上传数据
  const images = ref<ItemType[]>([]);
  // 表格选中数据
  const formRef = ref<FormInstance | null>(null);

  // 表单数据
  const { form, resetFields, assignFields } = useFormData<FormRecord>({
    formRecordId: undefined,
    formId: undefined,
    status: 0,
    comments: ''
  });

  /* 更新visible */
  const updateVisible = (value: boolean) => {
    emit('update:visible', value);
  };

  // 表单验证规则
  const rules = reactive({
    name: [
      {
        required: true,
        type: 'string',
        message: '请填写页面名称',
        trigger: 'blur'
      }
    ],
    path: [
      {
        required: true,
        type: 'string',
        message: '请填写路由地址',
        trigger: 'blur'
      }
    ],
    component: [
      {
        required: true,
        type: 'string',
        message: '请填写组件路径',
        trigger: 'blur'
      }
    ]
  });

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        const formData = {
          ...form,
          formData: JSON.stringify(form.formData)
        };
        const saveOrUpdate = isUpdate.value ? updateFormRecord : addFormRecord;
        saveOrUpdate(formData)
          .then((msg) => {
            loading.value = false;
            message.success(msg);
            updateVisible(false);
            emit('done');
          })
          .catch((e) => {
            loading.value = false;
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        if (props.data) {
          assignFields(props.data);
          isUpdate.value = true;
          form.status = 1;
        } else {
          isUpdate.value = false;
          content.value = '';
        }
      } else {
        resetFields();
        formRef.value?.clearValidate();
      }
    },
    { immediate: true }
  );
</script>
<style lang="less"></style>
