<template>
  <div class="container vote-dlg-container">
    <el-dialog
      v-dialogDrag
      :close-on-click-modal="false"
      :title="this.rowForm.QuestionnaireId ? '编辑' : '发起投票'"
      :modal-append-to-body="false"
      center
      :append-to-body="true"
      :visible.sync="initiateVotingDlg"
      :before-close="closeHandler"
      width="800px"
    >
      <div class="vote-dlg-container-theme-survey">
        <el-form :model="votingForm" :rules="votingRules" ref="votingForm" class="demo-ruleForm">
          <el-form-item prop="type">
            <el-select style="width: 100%" v-model="votingForm.type" placeholder="请选择活动类型">
              <el-option v-for="item in optionsActivity" :label="item.ActivityTypeName" :key="item.ActivityTypeId" :value="item.ActivityTypeId"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="describe">
            <div class="fr-wrapper-content-vote-dlg-container">
              <froala :tag="'textarea'" :config="froalaConfig" v-model="votingForm.describe"></froala>
            </div>
          </el-form-item>
          <el-form-item prop="date">
            <el-date-picker v-model="votingForm.date" type="date" placeholder="请设置投票截止时间" value-format="yyyy-MM-dd" style="width: 100%"> </el-date-picker>
          </el-form-item>
          <el-form-item prop="select">
            <el-select v-model="votingForm.select" placeholder="最多可选几项" style="width: 100%" @change="selectChangeHandler">
              <el-option label="1" :value="1"></el-option>
              <el-option label="2" :value="2"></el-option>
              <el-option label="3" :value="3"></el-option>
              <el-option label="4" :value="4"></el-option>
              <el-option label="5" :value="5"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            v-for="(item, index) in votingForm.addThemeList"
            :key="index"
            :prop="'addThemeList.' + index + '.ActivityTheme'"
            :rules="{
              required: true,
              message: '请填写活动主题',
              trigger: 'blur',
            }"
          >
            <div class="add-theme-content">
              <el-input v-model="item.ActivityTheme" placeholder="活动主题" style="width: 100%" clearable></el-input>
              <div class="delete-item-icon" @click="deleteThemeItem(item, index)" v-if="index >= 2">
                <img src="~@/assets/img/icons/delete-Item.png" alt="" />
              </div>
            </div>
          </el-form-item>
          <el-form-item>
            <div class="add-theme-box" @click="addThemeHandler">
              <el-image style="width: 25px; height: 25px; margin-right: 10px" :src="require('@/assets/img/icons/add-img.png')" />
              <span style="cursor: pointer">添加主题</span>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button style="margin-left: 30px" type="primary" plain @click="closeHandler">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { raiInterface, raiSpecial } from "@/api/api.js";
import RichText from "../../../components/richText.vue";
export default {
  name: "",
  components: { RichText },
  props: {
    initiateVotingDlg: {
      type: Boolean,
      default: false,
    },
    rowForm: {
      type: Object,
      default: {},
    },
  },
  watch: {
    rowForm: {
      handler(newVal) {
        this.initiateVotingDlg && newVal.QuestionnaireId && this.getListDetails();
      },
      deep: true,
      immediate: true,
    },
  },
  data() {
    var that = this;
    return {
      froalaConfig: {
        height: 150,
        fontSizeDefaultSelection: "16",
        quickInsertEnabled: false,
        theme: "dark", //主题
        placeholderText: "请输入首段描述文字",
        language: "zh_cn",
        events: {
          initialized: function () {
            that.editor = this;
            that.editor.toolbar.hide();
          },
        },
      },
      votingForm: {
        type: 8,
        describe: "",
        date: "",
        select: "",
        addThemeList: [
          { QuestionnaireThemeId: 0, ActivityTheme: "" },
          { QuestionnaireThemeId: 0, ActivityTheme: "" },
        ],
      },
      votingRules: {
        type: [{ required: true, message: "请选择活动类型", trigger: "change" }],
        describe: [{ required: true, message: "请输入首段描述文字", trigger: "blur" }],
        date: [{ required: true, message: "请设置投票截止时间", trigger: "change" }],
        select: [{ required: true, message: "请输入最多可选几项", trigger: "change" }],
      },
      optionsActivity: [],
    };
  },
  computed: {},
  created() {},
  mounted() {
    this.activityType();
  },
  methods: {
    closeHandler() {
      this.votingForm = {
        type: 8,
        describe: "",
        date: "",
        select: "",
        addThemeList: [
          { QuestionnaireThemeId: 0, ActivityTheme: "" },
          { QuestionnaireThemeId: 0, ActivityTheme: "" },
        ],
      };
      this.$refs.votingForm.resetFields();
      this.$emit("update:initiateVotingDlg", false);
      this.$emit("update:rowForm", {});
    },
    // 添加主题
    addThemeHandler() {
      let id = this.votingForm.addThemeList.length;
      this.votingForm.addThemeList.push({ QuestionnaireThemeId: 0, ActivityTheme: "" });
    },
    deleteThemeItem(item, index) {
      if (index >= 2) {
        this.votingForm.addThemeList.splice(index, 1);
      }
    },
    // 点击确定
    submitForm() {
      this.$refs.votingForm.validate(async (valid) => {
        if (valid) {
          const res = await raiInterface.questionnairePreserveAndEdit({
            ActivityTypeId: this.votingForm.type,
            MaxChooseTotal: this.votingForm.select,
            Content: this.votingForm.describe.replace(/<p data-f-id=\"pbf\".*?<\/p>/g,''),
            EndTime: this.votingForm.date,
            ListTheme: this.votingForm.addThemeList,
            QuestionnaireId: this.rowForm.QuestionnaireId || 0,
          });
          if (res.Ret === 200) {
            this.$parent.getDataList();
            this.$message.success("操作成功");
            this.closeHandler();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 获取活动类型
    async activityType() {
      const res = await raiInterface.getActivityType({
        IsGetAll: true,
      });
      if (res.Ret === 200) {
        this.optionsActivity = res.Data.List;
      }
    },
    async getListDetails() {
      const res = await raiInterface.questionnaireDetail({
        QuestionnaireId: this.rowForm.QuestionnaireId,
      });
      if (res.Ret === 200) {
        let detail = res.Data.Detail;
        this.votingForm = {
          type: detail.ActivityTypeId,
          describe: detail.Content,
          date: detail.EndTime,
          select: detail.MaxChooseTotal,
          addThemeList: detail.ListTheme,
        };
      }
    },
  },
};
</script>
<style scoped lang="scss">
.vote-dlg-container-theme-survey {
  .add-theme-content {
    display: flex;
    align-items: center;
  }
  .delete-item-icon {
    display: flex;
    align-items: center;
    margin-left: 20px;
    cursor: pointer;
    img {
      width: 15px;
      height: 15px;
    }
  }
  .add-theme-box {
    display: flex;
    align-items: center;
  }
}

.fr-wrapper-content-vote-dlg-container {
  border-top: 1px solid #cccccc !important;
  border-bottom: 1px solid #cccccc !important;
}
</style>
