<!--
 * @Description: 新增/编辑弹框
 * @Author: Sugar.
 * @Date: 2023-11-24 13:55:00
 * @LastEditors: Sugar.
 * @LastEditTime: 22023-11-24 13:55:00
 * @FilePath: \cattle_webui\src\views\information\informationMr\dialog\AddOrEditDialog.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="title">
          <el-input
            v-model="form.title"
            placeholder="标题"
            clearable
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="排序">
          <el-input
            type="number"
            v-model="form.sort"
            placeholder="排序"
            @change="changePriceAmount('peopleNum')"
            clearable
            style="width: 240px;margin-right: 10px"
          />
        </el-form-item>
        <el-form-item label="封面图" prop="mainImg">
          <el-upload
            ref="upload"
            class="avatar-uploader"
            :action="uploadObj.url"
            :headers="uploadObj.headers"
            :show-file-list="false"
            accept=".jpg,.png"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <div class="avatar" v-if="form.mainImg">
              <el-image
                style="width: 100px; height: 100px"
                :src="form.mainImg"
                fit="cover"
              />
            </div>
            <div class="upload-btn" v-else>
              <i class="el-icon-plus"></i>
              <div class="upload-btn-text">上传照片</div>
            </div>
            <div class="el-upload__tip" slot="tip">只能上传.jpg或.png格式</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="内容简介" prop="infoSnapshot">
          <el-input
            type="textarea"
            :rows="4"
            v-model="form.infoSnapshot"
            placeholder="内容简介"
            clearable
            show-word-limit
            maxlength="34"
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="文章内容：" prop="centent">
          <editor ref="editor" v-model="form.centent" :fileSize="20" :min-height="200" />
        </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/perform/perform";
import Editor from "@/components/Editor";
import { getToken } from "@/utils/auth";
export default {
  name: "addAndEdit",
  props: {
    dict: {
      type: Object,
      default: () => [],
    },
  },
  components: {
    Editor,
  },
  data() {
    return {
      title: "编辑",
      model: "EDIT",
      activeName: '01',
      open: false,
      loading: false,
      form: {
        id: undefined,
        type: "",
        content: "",
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: ["change","blur"] }],
        mainImg: [{ required: true, message: "请上传封面", trigger: ["change","blur"] }],
        infoSnapshot: [{ required: true, message: "请输入内容简介", trigger: ["change","blur"] }],
        centent: [
          { required: true, message: "请输入内容详情", trigger: ["change","blur"] },
        ],
      },
      uploadObj: {
        url: process.env.VUE_APP_UPLOAD_FILE_API + "/upload/single/minio",
        Headers: { Authorization: "Bearer " + getToken() },
      },
    };
  },
  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, 'title', obj.title);
          this.$set(this.form, 'sort', obj.sort);
          this.$set(this.form, 'mainImg', obj.mainImg);
          this.$set(this.form, 'infoSnapshot', obj.infoSnapshot);
          this.$set(this.form, 'centent', obj.centent);
        });
      }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, 'title', '');
      this.$set(this.form, 'sort', '');
      this.$set(this.form, 'mainImg', '');
      this.$set(this.form, 'centent', '');
      this.$set(this.form, 'infoSnapshot', '');
    },
    /**
     * 关闭弹框
     * @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>
