<script setup>
import { reactive, ref, onMounted, watch } from "vue";
import { raiInterface, raiSpecial } from "@/api/api.js";
import RichText from "../../../components/richText.vue";

import { ElMessage } from "element-plus";

const props = defineProps({
  initiateVotingDlg: {
    type: Boolean,
    default: false,
  },
  rowForm: {
    type: Object,
    default: {},
  },
});
const $emit = defineEmits(["getDataList"]);

watch(
  () => props.rowForm,
  (newVal) => {
    props.initiateVotingDlg && newVal.QuestionnaireId && getListDetails();
  },
  {
    deep: true,
    immediate: true,
  }
);
// 校验规则
const votingRules = ref({
  type: [{ required: true, message: "请选择活动类型", trigger: "change" }],
  describe: [{ required: true, message: "请输入首段描述文字", trigger: "blur" }],
  date: [{ required: true, message: "请设置投票截止时间", trigger: "change" }],
  select: [{ required: true, message: "请输入最多可选几项", trigger: "change" }],
});

const votingForm = ref({
  type: 8,
  describe: "",
  date: "",
  select: "",
  addThemeList: [
    { QuestionnaireThemeId: 0, ActivityTheme: "" },
    { QuestionnaireThemeId: 0, ActivityTheme: "" },
  ],
});

function closeHandler() {
  votingForm.value = {
    type: 8,
    describe: "",
    date: "",
    select: "",
    addThemeList: [
      { QuestionnaireThemeId: 0, ActivityTheme: "" },
      { QuestionnaireThemeId: 0, ActivityTheme: "" },
    ],
  };
  votingFormRes.value.resetFields();
  $emit("update:initiateVotingDlg", false);
  $emit("update:rowForm", {});
}
// 添加主题
function addThemeHandler() {
  let id = votingForm.value.addThemeList.length;
  votingForm.value.addThemeList.push({ QuestionnaireThemeId: 0, ActivityTheme: "" });
}
function deleteThemeItem(item, index) {
  if (index >= 2) {
    votingForm.value.addThemeList.splice(index, 1);
  }
}

const votingFormRes = ref(null);
// 点击确定
function submitForm() {
  votingFormRes.value.validate(async (valid) => {
    if (valid) {
      const res = await raiInterface.questionnairePreserveAndEdit({
        ActivityTypeId: votingForm.value.type,
        MaxChooseTotal: votingForm.value.select,
        Content: votingForm.value.describe.replace(/<p data-f-id=\"pbf\".*?<\/p>/g, ""),
        EndTime: votingForm.value.date,
        ListTheme: votingForm.value.addThemeList,
        QuestionnaireId: props.rowForm.QuestionnaireId || 0,
      });
      if (res.Ret === 200) {
        $emit("getDataList");
        ElMessage.success("操作成功");
        closeHandler();
      }
    } else {
      console.log("error submit!!");
      return false;
    }
  });
}

async function getListDetails() {
  const res = await raiInterface.questionnaireDetail({
    QuestionnaireId: props.rowForm.QuestionnaireId,
  });
  if (res.Ret === 200) {
    let detail = res.Data.Detail;
    votingForm.value = {
      type: detail.ActivityTypeId,
      describe: detail.Content,
      date: detail.EndTime,
      select: detail.MaxChooseTotal,
      addThemeList: detail.ListTheme,
    };
  }
}

// 获取活动类型

const optionsActivity = ref([]);

async function activityType() {
  const res = await raiInterface.getActivityType({
    IsGetAll: true,
  });
  if (res.Ret === 200) {
    optionsActivity.value = res.Data.List;
  }
}

// 富文本
const froalaConfig = ref({
  height: 150,
  fontSizeDefaultSelection: "16",
  quickInsertEnabled: false,
  theme: "dark", //主题
  placeholderText: "请输入首段描述文字",
  language: "zh_cn",
  events: {
    initialized: function () {
      this.toolbar.hide();
    },
  },
});

onMounted(() => {
  activityType();
});
</script>

<template>
  <div class="container vote-dlg-container">
    <el-dialog
      draggable
      :close-on-click-modal="false"
      :title="props.rowForm.QuestionnaireId ? '编辑' : '发起投票'"
      :modal-append-to-body="false"
      center
      :append-to-body="true"
      v-model="props.initiateVotingDlg"
      :before-close="closeHandler"
      width="800px"
    >
      <div class="vote-dlg-container-theme-survey">
        <el-form :model="votingForm" :rules="votingRules" ref="votingFormRes" 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>
      <template #footer>
        <div 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>
      </template>
    </el-dialog>
  </div>
</template>

<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 {
  width: 100%;
  border-top: 1px solid #cccccc !important;
  border-bottom: 1px solid #cccccc !important;
}
</style>
