<!--
 * @Description: 新增/编辑弹框
 * @Author: Sugar.
 * @Date: 2023-11-24 13:55:00
 * @LastEditors: Sugar.
 * @LastEditTime: 2023-11-24 13:55:00
 * @FilePath: \cattle_webui\src\views\performMr\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="name">
          <el-input
            v-model="form.name"
            placeholder="主办方名你"
            clearable
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="法人" prop="corporationName">
          <el-input
            v-model="form.corporationName"
            placeholder="法人"
            clearable
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="负责人" prop="contactName">
          <el-input
            v-model="form.contactName"
            placeholder="负责人"
            clearable
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="负责人联系电话" prop="contactMobile">
          <el-input
            v-model="form.contactMobile"
            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 { saveAndEdit, getSelectById } from "@/api/performMr/performMr";
import Editor from "@/components/Editor";
import { getToken } from "@/utils/auth";
export default {
  name: "addAndEdit",
  props: {
    dict: {
      type: Object,
      default: () => [],
    },
  },
  components: {
    Editor,
  },
  data() {
    var isPhone = (rule, value, callback) => {
      let reg = /^1[23456789]\d{9}$/;
      let reg1 = /^(?:(?:\d{3}-)?\d{8}|^(?:\d{4}-)?\d{7,8})(?:-\d+)?$/;
      if (!reg.test(value)) {
        if(reg1.test(value)){
          callback();
        }
        callback(new Error('请输入正确开票电话'));
      } else {
        callback();
      }
    };
    return {
      title: "编辑",
      model: "EDIT",
      open: false,
      loading: false,
      form: {
        id: undefined,
      },
      rules: {
        name: [{ required: true, message: "请输入主办方名你", trigger: "blur" }],
        corporationName: [{ required: true, message: "请输入法人名你", trigger: "blur" }],
        contactName: [{ required: true, message: "请输入负责人名你", trigger: "blur" }],
        contactMobile: [
          { required: true, message: "请输入负责人联系电话", trigger: "blur" },
          { validator: isPhone, trigger: '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;
      if (obj){
        this.title = "编辑";
        this.getSelectByIdApi(obj);
      }else{
        this.title = "新增";
        this.$nextTick(() => {
          this.$refs["form"].clearValidate();
        });
      }
    },
    /** 获取详情 */
    getSelectByIdApi(row) {
      const id = row.id
      getSelectById(id).then(response => {
        let obj = response.data;
        this.$set(this.form, 'id', obj.id);
        this.$set(this.form, 'name', obj.name);
        this.$set(this.form, 'corporationName', obj.corporationName);
        this.$set(this.form, 'contactName', obj.contactName);
        this.$set(this.form, 'contactMobile', obj.contactMobile);
      });
    },
    /**
     * 保存
     * @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.$refs["form"].resetFields();
      this.form.id = undefined;
    },
    /**
     * 关闭弹框
     * @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>
