<style scoped lang="scss">

</style>
<template>
  <div class="system-dialog-container">
    <el-dialog
        :title="state.dialog.title"
        v-model="state.dialog.isShowDialog"
        width="820px"
        draggable
        destroy-on-close
        :close-on-click-modal="false"
        @close="onClose"
        align-center>
      <el-form
          inline
          :model="state.ruleForm"
          :rules="state.rules"
          ref="formRef"
          size="default"
          label-width="125px"
          class="mt5">
        <el-form-item label="问题" prop="question"  class="w100">
          <el-input
              v-model.trim="state.ruleForm.question"
              placeholder="问题"
              clearable
              class="w100">
          </el-input>
        </el-form-item>

        <el-form-item label="答案" prop="answer"   class="w100">
          <ext-editor ref="faq_editor_ref" v-model="state.ruleForm.answer"></ext-editor>
        </el-form-item>

      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button :loading="state.btnLoading" type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="FaqDialog">
import {nextTick, reactive, onMounted, ref} from 'vue';
import {Msg} from "/@/utils/message";
import {$body, $get} from "/@/utils/request";
import u from '/@/utils/u'
import ExtSelect from "/@/components/form/ExtSelect.vue";
import ExtUpload from "/@/components/form/ExtUpload.vue";
import ExtDatePicker from "/@/components/form/ExtDatePicker.vue";
import ExtDSelect from "/@/components/form/ExtDSelect.vue";
import ExtEditor from "/@/components/form/ExtEditor.vue";


// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const formRef = ref();
const faq_editor_ref = ref();
//定义初始变量，重置使用
const initState = () => ({
  ruleForm: {
    id: null,
    status:1,

  },
  btnLoading: false,
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
    submitTxt: '',
  },
  rules: {
    answer:[u.validator.required],
    question:[u.validator.required],
    // status:[u.validator.required],
  },
})

// 定义变量内容
const state = reactive(initState());


// 打开弹窗
const open = (action: string = 'add', row: any) => {
  state.dialog.title = u.dialog.actions[action].title + "『问题配置』"
  state.dialog.submitTxt = u.dialog.actions[action].btn + "『问题配置』"
  state.dialog.isShowDialog = true;
  if (action !== 'add') {
    loadData(row.id);
  } else {
    state.ruleForm = Object.assign(state.ruleForm, row);
  }
};
// 关闭弹窗
const onClose = () => {
  state.dialog.isShowDialog = false;
  Object.assign(state, initState())
};
// 取消
const onCancel = () => {
  onClose();
};
// 提交
const onSubmit = () => {
  formRef.value.validate((valid:boolean) => {
    if(valid){
      state.btnLoading = true;
      const url = state.ruleForm.id > 0 ? "faq/modify" : "faq/add"
      $body(url, state.ruleForm).then(() => {
        state.btnLoading = false;
        Msg.message('操作成功');
        //console.log('submit!')
        onClose();
        emit('refresh');
      })
    }
  }).catch(() => {
    state.btnLoading = false;
    Msg.message('请先完整填写表单', 'error');
  })
};

const handleFormChange = (formData: any) => {
  //console.log(formData)
}

// 初始化表格数据
const loadData = (id: number) => {
  $get(`faq/detail/${id}`).then((res: any) => {
    state.ruleForm = res;
    nextTick(()=>{

    faq_editor_ref.value?.initText(res.answer);
    })
  })
}

// 暴露变量
defineExpose({
  open
});


</script>