<!-- 字典项编辑弹窗 -->
<template>
  <ele-modal
    width="30%"
    :visible.sync="addOrEditDialogFlag"
    :close-on-click-modal="false"
    :title="title"
    :before-close="closeDialog"
  >
    <el-form :model="formData" ref="formRef" :rules="rules" label-width="96px">
      <el-form-item label="模板编码:" prop="code">
        <el-input
          clearable
          :disabled="dialogType=='view'"
          v-model="formData.code"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="模板名称:" prop="name">
        <el-input
          clearable
          :disabled="dialogType=='view'"
          v-model="formData.name"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="默认发送人:" prop="nickname">
        <el-input
          clearable
          :disabled="dialogType=='view'"
          v-model.number="formData.nickname"
          placeholder="请输入排序"
        />
      </el-form-item>
      <el-form-item label="模板内容:" prop="content">
        <el-input
          clearable
          type="textarea"
          :disabled="dialogType=='view'"
          v-model="formData.content"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="状态:" prop="status">
        <el-radio :disabled="dialogType=='view'" v-model="formData.status" :label="1">启用</el-radio>
        <el-radio :disabled="dialogType=='view'" v-model="formData.status" :label="0">禁用</el-radio>
      </el-form-item>

      <el-form-item label="备注:" prop="remark">
        <el-input
          clearable
          type="textarea"
          :disabled="dialogType=='view'"
          v-model="formData.remark"
          placeholder="请输入"
        />
      </el-form-item>


    </el-form>
    <template v-slot:footer>
      <el-button type="primary" :loading="loading" @click="save">确认</el-button>
      <el-button @click="closeDialog">返回</el-button>
    </template>
  </ele-modal>
</template>

<script>
import dictMixins from '@/mixins/dictMixins';
import {notifyTemplateGetByIdAPI, notifyTemplateSaveAPI, notifyTemplateUpdateAPI} from "@/api/notifyManage";

export default {
  name: 'addOrEditDialog',
  props: {
    addOrEditDialogFlag: [Boolean],
  },
  mixins: [dictMixins],
  data() {

    return {
      // 表单数据
      formData: {
        code: '',
        name: '',
        nickname: '',
        content: '',
        status: 1,
        remark: '',
      },
      title: null,


      // 表单验证规则
      rules: {
        name: [
          {
            required: true,
            message: '请输入模板名称',
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '请输入模板型编码',
            trigger: 'blur'
          }
        ],
        nickname: [
          {
            required: true,
            message: '请输入默认发送人',
            trigger: 'blur'
          }
        ],
        content: [
          {
            required: true,
            message: '请输入模板内容',
            trigger: 'blur'
          }
        ],
        status: [
          {
            required: true,
            message: '请选择状态',
            trigger: 'change'
          }
        ],

      },
      dialogType: '',
      // 提交状态
      loading: false,
    };
  },
  created() {
  },
  methods: {
    init(type, row = {}) {
      this.title = type == 'add' ? '新增' : '修改'
      this.dialogType = type
      if (type !== 'add') {
        this.getNotifyTemplateInfo(row)
      }
    },
    async getNotifyTemplateInfo(row) {
      this.formData = await notifyTemplateGetByIdAPI(row.id)
    },
    /* 保存编辑 */
    save() {
      this.$refs.formRef.validate(async (valid) => {
        if (!valid) return this.$message.warning('有必填项未填写，请检查');
        this.loading = true;
        const params = {...this.formData};

        const API = this.dialogType == 'add' ? notifyTemplateSaveAPI : notifyTemplateUpdateAPI
        try {
          await API(params)
          this.$message.success('操作成功')
          this.$emit('done')
          this.closeDialog()
        } catch (e) {
          this.loading = false;
        }

      });
    },
    /* 关闭弹窗 */
    closeDialog() {
      this.$emit('update:addOrEditDialogFlag', false)
    }
  },
};
</script>
