<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"
        align-center>
      <el-form
          inline
          :model="state.ruleForm"
          :rules="rules"
          label-position="top"
          ref="formRef"
          size="default"
          label-width="100px"
          class="mt5">
        <el-form-item label="反馈标题" prop="title" class="w100">
          <el-input
              v-model="state.ruleForm.title"
              placeholder="反馈标题"
              clearable
              readonly
              class="w100">
          </el-input>
        </el-form-item>
        <el-form-item label="纠错类型" prop="type">
          <ext-d-label type="Feedback.type" :model-value="state.ruleForm.type"></ext-d-label>
        </el-form-item>
        <el-form-item label="提交时间" prop="submitTime">
          {{u.fmt.fmtDateTime(state.ruleForm.submitTime)}}
        </el-form-item>
        <el-form-item label="反馈人" prop="submitUserId">
          {{state.ruleForm.submitUserId}}
        </el-form-item>
        <el-form-item label="附件列表" prop="attachList" class="w100">
          <ext-image :src-list="state.attachList"></ext-image>
<!--          <el-input
              v-model="state.ruleForm.attachList"
              placeholder="附件列表"
              clearable
              class="wd350">
          </el-input>-->
        </el-form-item>
        <el-form-item label="问题描述" prop="content" class="w100">
          <el-input
              v-model="state.ruleForm.content"
              placeholder="问题描述"
              clearable
              type="textarea"
              readonly
              class="wd350">
          </el-input>
        </el-form-item>
        <el-form-item label="回复内容" prop="replyContent" class="w100">
          <el-input
              v-model="state.ruleForm.replyContent"
              placeholder="请填写回复内容"
              clearable
              type="textarea"
              class="wd350">
          </el-input>
        </el-form-item>
        <el-form-item label="回复时间" prop="replyTime" v-if="state.ruleForm.replyTime">
          <el-input
              v-model="state.ruleForm.replyTime"
              placeholder="回复时间"
              clearable
              class="wd350">
          </el-input>
        </el-form-item>
        <el-form-item label="回复人" prop="replyUserId"  v-if="state.ruleForm.replyTime">
          <el-input
              v-model="state.ruleForm.replyUserId"
              placeholder="回复人"
              clearable
              class="wd350">
          </el-input>
        </el-form-item>
<!--        <el-form-item label="状态" prop="status">
          <el-input
              v-model="state.ruleForm.status"
              placeholder="状态"
              clearable
              class="wd350">
          </el-input>
        </el-form-item>-->


      </el-form>

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

<script setup lang="ts" name="FeedbackDialog">
import {defineAsyncComponent, reactive, onMounted, ref} from 'vue';
import {Msg} from "/@/utils/message";
import {$body, $get} from "/@/utils/request";
import u from '/@/utils/u'
import ExtImage from "/@/components/form/ExtImage.vue";
import ExtDLabel from "/@/components/form/ExtDLabel.vue";


// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const formRef = ref();
//定义初始变量，重置使用
const initState = () => ({
  ruleForm: {
    id: null
  },
  btnLoading: false,
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
    submitTxt: '',
  },
  rules: {},
  attachList:[]
})

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


// 打开弹窗
const open = (action: string = 'add', row: any) => {
  state.dialog.title =  "回复『纠错反馈』"
  state.dialog.submitTxt =  "回复『纠错反馈』"
  state.dialog.type=action;
  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((v: boolean) => {
    if (v) {
      state.btnLoading = true;
      const url = !!state.ruleForm.id  ? "feedback/modify" : "feedback/add"
      $body(url, state.ruleForm).then(() => {
        state.btnLoading = false;
        Msg.message('操作成功');
        //console.log('submit!')
        onClose();
        emit('refresh');
      })
    } else {
      state.btnLoading = false;
      Msg.message('请先完整填写表单', 'error');
    }
  })
};

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

// 初始化数据
const loadData = (id: number) => {
  $get(`feedback/detail/${id}`).then((res: any) => {
    state.ruleForm = res;
    state.attachList = JSON.parse(res.attachList).map(k=>k.url)
  })
}

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


</script>