<!--
 * @Description: 新增/编辑弹框
 * @Author: Sugar.
 * @Date: 2023-11-24 13:55:00
 * @LastEditors: gcz
 * @LastEditTime: 2024-03-12 14:02:11
 * @FilePath: \great_webui\src\views\system\smsMgr\dialog\addAndEdit.vue
 * @Copyright: Copyright (c) 2016~2023 by Sugar., All Rights Reserved.
-->
<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="700px"
    append-to-body
    :close-on-click-modal="false"
    @close="cancel"
  >
    <div class="dialog">
      <el-form :model="form" ref="form" :rules="rules" label-width="120px">
        <el-form-item label="模板名称" prop="templateName" required>
          <el-input
            v-model="form.templateName"
            placeholder="模板名称"
            clearable
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="模板编码" prop="templateCode">
          <el-input
            v-model="form.templateCode"
            placeholder="模板编码"
            clearable
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="模板类型" prop="templateIdent">
          <el-select v-model="form.templateIdent" placeholder="请选择模板类型">
                <el-option
                  v-for="dict in dict.type.template_ident"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
        </el-form-item>
        <el-form-item label="模板内容" prop="templateContent">
          <el-input
            type="textarea"
            :rows="2"
            v-model="form.templateContent"
            placeholder="模板内容"
            clearable
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="可变参数数量" prop="parameterNumber">
          <el-input
            v-model="form.parameterNumber"
            placeholder="可变参数数量"
            clearable
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="签名名称" prop="signName">
          <el-input
            v-model="form.signName"
            placeholder="签名名称"
            clearable
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="签名编码" prop="signNo">
          <el-input
            v-model="form.signNo"
            placeholder="签名编码"
            clearable
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="备注" >
          <el-input
            v-model="form.remark"
            placeholder="备注"
            clearable
            style="width: 100%;"
          />
        </el-form-item>

        
        
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button
        type="primary"
        @click="submitForm"
        v-loading.fullscreen.lock="loading"
        element-loading-text="提交中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <span v-if="loading">提交中...</span>
        <span v-else>保存</span>
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
// import { updateNoticeMgr } from "@/api/system/noticeMgr";
import { saveAndEdit } from "@/api/smsMgr/smsMgr";
// import { getToken } from "@/utils/auth";
// import { log } from 'mathjs';
export default {
  name: "addAndEdit",
  props: {
    dict: {
      type: Object,
      default: () => [],
    },
  },
  components: {
    
  },
  data() {
    return {
      title: "编辑",
      model: "EDIT",
      activeName: '01',
      open: false,
      loading: false,
      form: {
        id: undefined,
      },
      rules: {
        templateName: [{ required: true, message: "请输入模板名称", trigger: ["change","blur"] }],
        templateCode: [{ required: true, message: "模板编码", trigger: ["change","blur"] }],
        templateIdent: [{ required: true, message: "请选择模板类型", trigger: ["change","blur"] }],
        templateContent: [{ required: true, message: "请输入模板内容", trigger: ["change","blur"] }],
        parameterNumber: [
          { required: true, message: "请输入可变参数数量", trigger: ["change","blur"] },
        ],
        signName	: [{ required: true, message: "请输入签名名称", trigger: ["change","blur"] }],
        signNo: [{ required: true, message: "请输入签名编码", trigger: ["change","blur"] }],
        remark: [{ required: true, message: "请输入备注", trigger: ["change","blur"] }],
      },
    };
  },
  methods: {
    /**
     * 打开弹框
     * @date 2023-11-22
     * @param {any} obj
     * @returns {any}
     */
    openDialog(title, obj) {
      this.open = true;
      this.reset();
      if (obj){
        this.title = "编辑模版";
        this.$nextTick(() => {
          this.$set(this.form, 'id', obj.id);
          this.$set(this.form, 'templateName', obj.templateName);
          this.$set(this.form, 'templateCode', obj.templateCode);
          this.$set(this.form, 'templateIdent', obj.templateIdent);
          this.$set(this.form, 'templateContent', obj.templateContent);
          this.$set(this.form, 'parameterNumber', obj.parameterNumber);
          this.$set(this.form, 'signName', obj.signName);
          this.$set(this.form, 'signNo', obj.signNo);
          this.$set(this.form, 'remark', obj.remark);
        });
      }else{
        this.title = "新增模版";
        this.$nextTick(() => {
          this.$refs["form"].clearValidate();
        });
      }
    },
    changePriceAmount(key) {
      if(this.form[key] * 1 < 0){
        this.$message.error("输入需大于或等于0!");
        this.$set(this.form, key, '');
        return false
      }
    },
    /**
     * 保存
     * @date 2023-11-22
     * @returns {any}
     */
    submitForm() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          try {
            this.loading = true;
            const { code } = await saveAndEdit({ ...this.form });
            if (code === 200) {
              this.$message.success("操作成功!");
              this.$emit("getList");
              this.cancel();
            }
          } catch (error) {
          } finally {
            this.loading = false;
          }
        }
      });
    },
    /**
     * 重置
     * @date 2023-11-22
     * @returns {any}
     */
    reset() {
      this.$set(this.form, 'id', '');
      this.$set(this.form, 'templateName', '');
      this.$set(this.form, 'templateCode', '');
      this.$set(this.form, 'templateIdent', '');
      this.$set(this.form, 'templateContent', '');
      this.$set(this.form, 'parameterNumber', '');
      this.$set(this.form, 'signName', '');
      this.$set(this.form, 'signNo', '');
      this.$set(this.form, 'remark', '');
    },
    /**
     * 关闭弹框
     * @date 2023-11-22
     * @returns {any}
     */
    cancel() {
      this.reset();
      this.open = false;
    },
    /**
     * 上传成功
     * @date 2023-11-22
     * @param {any} res
     * @returns {any}
     */
    handleAvatarSuccess(res) {
      if (res.code === 200) {
        // this.form.mainImg = res?.data?.url;
        this.$set(this.form, 'mainImg', res?.data?.url)
      }
    },
    /**
     * 上传文件之前之前
     * @date 2023-11-22
     * @param {any} file
     * @returns {any}
     */
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || "image/png";
      if (!isJPG) {
        this.$message.error("上传头像图片只能是jpg或png格式!");
      }
      return isJPG;
    },
  },
};
</script>

<style lang="scss" scoped>
.dialog {
  padding: 0 30px;
  max-height: 65vh;
  overflow-y: auto;
}
.dialog {
  padding: 0 30px;
  .upload-btn {
    width: 100px;
    height: 100px;
    background-color: #fbfdff;
    border: dashed 1px #c0ccda;
    border-radius: 5px;
    i {
      font-size: 30px;
      margin-top: 20px;
    }
    &-text {
      margin-top: -10px;
    }
  }
  .avatar {
    cursor: pointer;
  }
}
</style>
