<template>
  <div class="container container-message-dlg">
    <el-dialog width="560px" v-dialogDrag :close-on-click-modal="false" :modal-append-to-body="false" center :visible.sync="messageDialog" title="发送模板消息" :before-close="handleClose">
      <div class="content">
        <div class="annotation">
          <div class="annotation-box" @click="annotationHandler">
            <img style="margin-right: 5px" src="~@/assets/img/icons/annotation_icon.png" alt="" />
            注释
          </div>
        </div>
        <el-form :model="messageForm" :rules="rules" ref="messageFormRef" class="demo-ruleForm">
          <el-form-item prop="sendId">
            <el-select style="width: 100%" v-model="messageForm.sendId" multiple placeholder="请选择发送对象（可多选）" :disabled="typeMessage == '确定行程'">
              <el-option v-for="item in sendOptions" :key="item.Id" :label="item.Name" :value="item.Id"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="activityName">
            <el-input type="textarea" :rows="2" :placeholder="`请输入${this.typeMessage == '确定行程' ? '调研主题' : '活动名称'}（20字内）`" v-model="messageForm.activityName"> </el-input>
          </el-form-item>
          <el-form-item prop="content">
            <el-input type="content" :rows="2" placeholder="请输入变更内容（20字内）" v-model="messageForm.content"> </el-input>
          </el-form-item>
        </el-form>
      </div>
      <el-image
        style="width: 0px; height: 0px"
        src="https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/templet_new.png"
        id="TripImgLink"
        :preview-src-list="['https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/templet_new.png']"
      ></el-image>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmPerson">确定</el-button>
        <el-button @click="handleClose">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { raiInterface } from "@/api/api.js";
export default {
  name: "",
  components: {},
  props: {
    selectionArr: {
      type: Array,
      required: true,
      default: () => [],
    },
    messageDialog: {
      type: Boolean,
      required: true,
      default: false,
    },
    typeMessage: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      previewList: ["https://hzchart.oss-cn-shanghai.aliyuncs.com/cygx/template.png"],
      messageForm: {
        sendId: [7],
        beforeText: "",
        activityName: "",
        content: "",
      },
      rules: {
        sendId: [{ required: true, message: "请选择发送对象", trigger: "change" }],
        activityName: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
        content: [{ required: true, message: "请输入变更内容", trigger: "blur" }],
      },
      defaultSalesProps: {
        multiple: true,
        label: "Name",
        value: "Id",
      },
      sendOptions: [],
    };
  },
  computed: {},
  watch: {
    messageDialog: {
      handler(newValue) {
        console.log(this.selectionArr);
        if (newValue) this.messageForm.activityName = this.selectionArr[0].ActivityName || this.selectionArr[0].ResearchTheme;
        this.activitySendGroupList();
      },
    },
  },
  created() {},
  mounted() {},
  methods: {
    async activitySendGroupList() {
      const res = this.typeMessage == "确定行程" ? await raiInterface.activityTripSendGroupList() : await raiInterface.activitySendGroupList();
      if (res.Ret === 200) {
        this.sendOptions = res.Data;
        this.messageForm.sendId = this.typeMessage == "确定行程" ? [1] : [7];
      }
    },
    // 点击了注释
    annotationHandler() {
      $("#TripImgLink").click();
    },
    handleClickStop() {
      this.$nextTick(() => {
        let domImageView = document.querySelector(".el-image-viewer__mask"); // 获取遮罩层dom
        if (!domImageView) {
          return;
        }
        console.log(domImageView);
        domImageView.addEventListener("click", () => {
          // 点击遮罩层时调用关闭按钮的 click 事件
          document.querySelector(".el-image-viewer__close").click();
        });
      });
    }, // 弹框关闭
    handleClose() {
      this.$refs.messageFormRef.resetFields();
      this.previewList = [];
      this.messageForm = {
        sendId: [7],
        beforeText: "",
        activityName: "",
        content: "",
      };
      this.$emit("update:messageDialog", false);
    },
    // 弹框确定饰件
    confirmPerson() {
      this.$refs["messageFormRef"].validate(async (valid) => {
        if (valid) {
          let params = {
            ActivityIds: this.selectionArr[0].ActivityId + "",
            ResearchTheme: this.selectionArr[0].ResearchTheme || "",
            SendGroup: this.messageForm.sendId.join(","),
            // FirstText: this.messageForm.beforeText,
            ActivityName: this.messageForm.activityName,
            Content: this.messageForm.content,
          };
          const res = this.typeMessage == "确定行程" ? await raiInterface.activityTripSignupTempMsg(params) : await raiInterface.activitySignupTempMsg(params);
          if (res.Ret === 200) {
            this.$message.success("发送成功");
            this.handleClose();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss">
.container-message-dlg {
  .content {
    .annotation {
      display: flex;
      justify-content: flex-end;
      margin-bottom: 20px;
      .annotation-box {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        width: 60px;
        cursor: pointer;
      }
    }
  }
  .el-input {
    width: 100% !important;
  }
  .el-image-viewer__canvas .el-image-viewer__img {
    width: 700px;
    height: 50%;
  }
}
</style>
