<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="600px"
    :visible="visible"
    :confirm-loading="loading"
    :title="`修改内容`"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <!-- 编辑器 -->
    <byte-md-editor
      v-model:value="content"
      :locale="zh_Hans"
      :plugins="plugins"
      uploadImages
      height="300px"
      :editorConfig="{ lineNumbers: true }"
    />
  </ele-modal>
</template>

<script lang="ts" setup>
  import { ref, reactive, watch } from 'vue';
  import { Form, message } from 'ant-design-vue';
  import { assignObject } from 'ele-admin-pro';
  import { updateTenant } from '@/api/oa/tennat';
  import type { Tenant } from '@/api/oa/tennat/model';
  import 'bytemd/dist/index.min.css';
  import 'github-markdown-css/github-markdown-light.css';
  import ByteMdEditor from '@/components/ByteMdEditor/index.vue';
  import highlight from '@bytemd/plugin-highlight';
  // 中文语言文件
  import zh_Hans from 'bytemd/locales/zh_Hans.json';
  // 链接、删除线、复选框、表格等的插件
  import gfm from '@bytemd/plugin-gfm';
  // 插件的中文语言文件
  import zh_HansGfm from '@bytemd/plugin-gfm/locales/zh_Hans.json';
  // 预览界面的样式，这里用的 github 的 markdown 主题
  import 'github-markdown-css/github-markdown-light.css';

  const useForm = Form.useForm;
  // 是否是修改
  const isUpdate = ref(false);
  const props = defineProps<{
    // 弹窗是否打开
    visible: boolean;
    data?: Tenant | null;
    // 修改回显的数据
    field?: string | null;
    content?: string;
    tenantId?: number;
  }>();

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

  // 插件
  const plugins = ref([
    gfm({
      locale: zh_HansGfm
    }),
    highlight()
  ]);

  // 提交状态
  const loading = ref(false);
  const content = ref('');
  // 用户信息
  const form = reactive<Tenant>({
    tenantId: 0,
    comments: ''
  });

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

  const { resetFields, validate } = useForm(form);

  /* 保存编辑 */
  const save = () => {
    validate()
      .then(() => {
        loading.value = true;
        // 判断更新字段
        form.tenantId = props.tenantId;
        if (props.field === 'content') {
          form.comments = content.value;
        }
        if (props.field === 'comments') {
          form.comments = content.value;
        }
        updateTenant(form)
          .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) => {
      content.value = String(props.content);
      console.log(visible);
      if (visible) {
        if (props.data) {
          loading.value = false;
          content.value = String(props.content);
          assignObject(form, props.data);
          isUpdate.value = true;
        } else {
          isUpdate.value = false;
        }
      } else {
        resetFields();
      }
    }
  );
</script>
<style lang="less">
  .tab-pane {
    min-height: 300px;
  }
</style>
