import wangeditor from "../components/wangEditor.vue";
import question from "../components/question.vue";
import { questionname } from "@/api/system/taskExecuteRecord";

import message from "../components/message.vue";
import scripts from "../components/script.vue";
import propaganda from "../components/propaganda.vue";
import officialAccount from "../components/officialAccount.vue";
import miniProgram from "../components/miniProgram.vue";
import { getAgencytype } from "@/api/system/agency";
import Scriptpreview from ".././components/Scriptpreviewopen.vue";
import Editorxj from "../Editorxj/index.vue";
import {
  getPropaganda,
} from "@/api/manage/propaganda";
import questionopennew from "../components/questionopennew.vue";

import {
  selectTaskTypeList,
  taskPartitionList,
  taskStatusDictList,
  specialDiseaseNode,
  triggerCondition,
  selectSpecialDisease,
  updateRouteCheckStatus,
  list,
  triggerConditionList,
  getList,
} from "@/api/system/specialDiseaseNode";
import { faLess } from "@fortawesome/free-brands-svg-icons";
import { faL } from "@fortawesome/free-solid-svg-icons";

export default {
  components: {
    questionopennew,
    wangeditor,
    Editorxj,
    question,
    propaganda,
    message,
    scripts,
    officialAccount,
    miniProgram,
    Scriptpreview
  },
  name: "specialDiseaseNode",
  data() {
    return {
      specialDiseaseNodeId: '',
      taskPartitionDictId: '',
      lookNodeContent: false,
      lookquestionname: false,
      lookquestionlist: false,//问卷
      imageScriptpreviews: false,//宣教
      formview: {},
      lookitemnew: {},
      baseUrl: process.env.VUE_APP_BASE_API,
      showquestion: false,
      value: 1,
      ConditionValue: [],
      dictDataType: "",
      openlist: {
        suitRange: "",
        routeId: "",
        triggerConditionList: [],
      },
      open: false,
      show3: false,
      activeNames: ["1"],
      routeform: {
        routeCheckStatus: null,
        routeCheckRemark: "",
      },
      optionslist: [],
      // 包含
      optionstriggerConditionOperator: [], // 将这部分选项的
      optionslistS: [],
      optionslistSname: [],
      // 诊断
      optionsname: [],
      routeName: "",
      updata: [],
      updatalists: [],
      updatalist: [],
      // triggerConditionList: [{}],
      form: {
        specialDiseaseNodeId: "",
        nodeContent: "",
        templateId: "",
        templateName: "",
        taskType: "",
        taskTypeName: "",
        taskSubdivision: "",
        taskSubdivisionName: "",
        taskStatus: "",
        secondClassifyDescribe: "",
        executionTime: "",
        appletPushSign: "0",
        officialPushSign: "0",
        messagePushSign: "0",
        taskNodeType: "",
      },
      itemindex: 0,
      listindex: 0,
      totalNumber: 0,
      agreeNumber: 0,
      value: "",
      input: "",
      phoneNodeContent: '',
      //任务类型
      selectTaskTypeList: [],
      //任务状态
      taskStatusDictList: [],
      //任务细分
      taskPartitionList: [],
      routeId: "",
      options: [],
      lists: [],
      dialogVisible: false,
      parentDictCodelist: [],
      parentDictCode: "",
      itemlist: {
        specialDiseaseRouteId: "",
        // suitRange: "",
        routeName: "",
        specialDiseaseNodeList: [],
      },
      updataname: "",
      rules: {
        routeCheckStatus: [
          { required: true, message: "请选择节点审核状态", trigger: "change" },
        ],
      },
      taskNodeTypeList: [
        {
          name: '电话外呼',
          code: 'PHONE_OUTBOUND',
        },
        {
          name: '问卷量表',
          code: 'QUESTIONNAIRE_SCALE',
        },
        {
          name: '宣教文案',
          code: 'PROPAGANDA_ARTICLE',
        },
        {
          name: '文字提醒',
          code: 'TEXT_REMIND',
        }
      ],
    };
  },

  created() {
    if (this.$route.query) {
      this.$set(this.form, "specialDiseaseNodeId", this.$route.query.id);
      this.$set(this.openlist, "suitRange", this.$route.query.suitRange);
      this.openlist.routeId = this.$route.query.id;
      this.itemlist.routeName = this.$route.query.routeName
    }
    this.taskinfo();
    this.infolistname();
    this.infolists();
    this.infolistoptionslistS();
    this.infolistMESSAGE();
    this.infolistword();
    // this.optiononditionOperator();
    this.optiononditionOperators();
  },
  beforeDestroy() {
    console.log(111111111)
    localStorage.removeItem('wangeditorlist')
  },
  watch: {
    "form.phoneMessageRemind": {
      handler(newValue, oldValue) {
        if (newValue == "NOT_SEND_MESSAGE") {
          this.form.phoneMessageTemplateId = "";
          this.form.phoneMessageTemplateName = "";
        }
      },
      deep: true,
    },

  },
  methods: {
    // 短信提醒
    infolistword() {
      var dictType = "text_message_remind";
      getAgencytype(dictType).then((res) => {
        this.optionslist = res.data;
        // this.taskinfo();
      });
    },

    // 重播次数
    infolistMESSAGE() {
      var dictType = "redial_times";
      getAgencytype(dictType).then((res) => {
        this.optionslistSname = res.data;
        this.taskinfo();
      });
    },
    // 弹框确定
    upload() {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      this.openlist.triggerConditionList.forEach((e) => {
        e.routeId = this.$route.query.id;
        e.routeName = this.$route.query.routeName;
        delete e.optionstriggerConditionOperator

      });
      delete this.openlist.triggerConditionList.optionstriggerConditionOperator
      loading.close();
      // return;
      triggerCondition(this.openlist).then((res) => {
        if (res.code == 200) {
          this.$notify({
            type: "success",
            title: "提示",
            message: "修改触发条件成功",
          });
          this.itemlist.routeName = res.data;
        }
        this.infolist();
        // this.handlelist();
        this.openfalse();
      });
    },
    // 关闭弹框
    openfalse() {
      this.openlist.triggerConditionList = [
        {
          routeId: "",
          routeName: "",
          triggerConditionName: "",
          triggerConditionOperator: "",
          triggerConditionValue: "",

          triggerConditionOperatorName: "",
          dictDataType: "STRING",

          triggerLogic: "AND",
          triggerLogicName: "",
        },
      ];
      this.open = false;
      this.resetForm("openlist");
    },
    // 批量新增添加属性
    addtriggerCondition() {
      this.idd++;
      var obj = {
        routeId: "",
        routeName: "",
        triggerConditionName: "",
        triggerConditionOperator: "",
        triggerConditionValue: "",

        triggerConditionOperatorName: "",
        dictDataType: "STRING",
        triggerLogic: "AND",
        triggerLogicName: "",
      };

      if (this.openlist.triggerConditionList.length == 3) {
        this.$message.error("至多仅支持两种属性");
      } else {
        this.openlist.triggerConditionList.push(obj);
      }
    },
    // 删除属性
    delitemlistname(index, item) {
      this.openlist.triggerConditionList.splice(index, 1);
    },
    //取消
    handleClose() {
      this.dialogVisible = false;
      this.routeform = {
        routeCheckStatus: "",
        routeCheckRemark: "",
      };
      this.resetForm("routeform");
    },
    //审核
    auditing() {
      this.$refs["routeform"].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: "审核中",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)",
          });
          if (!this.form.routeNodeDay) {
            this.$message.error("请输入节点时间段天数");
            return loading.close();
          }
          if (!this.form.routeNodeName) {
            this.$message.error("请选择节点时间段");
            return loading.close();
          }
          if (!this.form.taskType) {
            this.$message.error("请选择任务类型");
            return loading.close();
          }
          if (!this.form.taskSubdivision) {
            this.$message.error("请选择任务细分");
            return loading.close();
          }
          if (!this.form.taskStatus) {
            this.$message.error("请选择任务状态");
            return loading.close();
          }
          updateRouteCheckStatus({
            routeCheckStatus: this.routeform.routeCheckStatus,
            id: this.form.specialDiseaseNodeId,
            routeCheckRemark: this.routeform.routeCheckRemark,
            routeCheckPerson: JSON.parse(localStorage.getItem("user")).nickName,
          }).then((res) => {
            loading.close();
            if (res.data == 1) {
              this.info();
              this.handleClose();
              this.$modal.msgSuccess("审核成功!");
              this.listindex = 0
              this.itemindex = 0
            }
          });
        }
      });
    },
    // 保存触发条件
    addlistname() {
      this.show3 = !this.show3;
    },
    // 适用范围
    changeoptions(e) {
      this.parentDictCode = this.options.find(
        (el) => el.dictValue == e
      )?.dictCode;
      this.changelisy();
    },
    changelisy() {
      list(this.parentDictCode).then((res) => {
        this.parentDictCodelist = res.rows;
      });
    },

    // 回显
    info() {
      const loading = this.$loading({
        lock: true,
        text: "数据加载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      this.lists = [];
      if (this.$route.query) {
        this.$set(this.updata, "suitRange", this.$route.query.suitRange);
        this.parentDictCode = this.options.find(
          (el) => el.dictValue == this.updata.suitRange
        )?.dictCode;
        setTimeout(() => {
          this.changelisy();
        }, 3000);
        this.itemlist.specialDiseaseRouteId = this.$route.query.id;
        selectSpecialDisease(this.$route.query.id).then((res) => {
          this.agreeNumber = res.data.agreeNumber;
          this.totalNumber = res.data.totalNumber;
          res.data.specialDiseaseNodeList.forEach((e) => {
            // if (e.taskNodeType == 'PHONE_OUTBOUND') {
            //   e.taskSubdivisionName = e.taskSubdivisionName + '(电话外呼)'
            // } else if (e.taskNodeType == 'QUESTIONNAIRE_SCALE') {
            //   e.taskSubdivisionName = e.taskSubdivisionName + '(问卷量表)'
            // } else if (e.taskNodeType == 'PROPAGANDA_ARTICLE') {
            //   e.taskSubdivisionName = e.taskSubdivisionName + '(宣教文案)'
            // } else if (e.taskNodeType == 'TEXT_REMIND') {
            //   e.taskSubdivisionName = e.taskSubdivisionName + '(文字提醒)'
            // }
            if (e.taskNodeType == 'PHONE_OUTBOUND' && e.flowScheme) {
              this.phoneNodeContent = e.flowScheme
            }
            if (e.messagePushSign) {
              e.messagePushSign = "" + e.messagePushSign;
            }
            if (e.officialPushSign) {
              e.officialPushSign = "" + e.officialPushSign;
            }
            if (e.appletPushSign) {
              e.appletPushSign = "" + e.appletPushSign;
            }
            if (e.phonePushSign) {
              e.phonePushSign = "" + e.phonePushSign;
            }
          });
          this.lists = this.handleData(
            res.data.specialDiseaseNodeList,
            "routeNodeDay",
            "routeNodeName"
          );
          this.form = this.lists[0].list[0];
          // 宣教模板回显
          if (this.form.taskNodeType == "PROPAGANDA_ARTICLE" && this.form.templateId) {
            getPropaganda(this.form.templateId).then((response) => {
              console.log(response.data, 'data')
              this.formview = response.data;
            });
          } else
            if (this.form.taskNodeType == "QUESTIONNAIRE_SCALE" || this.form.taskNodeType == "PHONE_OUTBOUND" && this.form.templateId) {
              // 问卷模板回显
              questionname(this.form.templateId).then((res) => {
                this.lookitemnew = res.data;
              });
            }
          if (this.form.taskType) {
            this.changeTaskType(this.form.taskType, this.form.taskSubdivision);
          }
          loading.close();
        });
      } else {
        this.lists.push({
          routeNodeName: "",
          routeNodeDay: "",
          list: [
            {
              nodeContent: "<p></p>",
              templateId: "",
              templateName: "",
              taskType: "",
              taskSubdivision: "",
              taskSubdivisionName: "",
              taskStatus: "",
              secondClassifyDescribe: "",
              executionTime: "",
              appletPushSign: "0",
              officialPushSign: "0",
              messagePushSign: "0",
              messagePreview: "",
              officialRemindContent: "",
              taskNodeType: "",
              appletRemindContent: "",
              appletPromptDescription: "",
            },
          ],
        });
        this.form = this.lists[0].list[0];
        loading.close();
      }
    },
    handleData(list, key, keytwo) {
      //得到数据的主键列表
      let keysArr = list.map((item) => {
        return {
          routeNodeDay: item[key],
          routeNodeName: item[keytwo],
        };
      });
      let array = keysArr.filter(
        (item, index, arr) =>
          index ===
          arr.findIndex(
            (ite) =>
              ite.routeNodeDay == item.routeNodeDay &&
              ite.routeNodeName == item.routeNodeName
          )
      );

      let keys = [...new Set(array)];
      let newList = keys.map((item) => {
        return {
          //这里写新的 数据结构 如下是整体复制
          routeNodeDay: item[key],
          routeNodeName: item[keytwo],
          list: list.filter(
            (i) => i[key] == item[key] && i[keytwo] == item[keytwo]
          ),
        };
      });
      return newList;
    },

    // 且或
    infolistoptionslistS() {
      var dictType = "trigger_logic";
      getAgencytype(dictType).then((res) => {
        this.optionslistS = res.data;
      });
    },
    // 诊断
    optiononditionOperators() {
      var dictType = "trigger_condition_name";
      getAgencytype(dictType).then((res) => {
        this.optionsname = res.data;
      });
    },
    // 包含
    optiononditionOperator() {
      var dictType = "trigger_condition_operator";
      getAgencytype(dictType).then((res) => {
        this.openlist.triggerConditionList.optionstriggerConditionOperator = res.data;

      });
    },


    // 包含
    changetriggerLogic(e, index) {
      // this.openlist.triggerConditionList[index].triggerConditionOperatorName =
      //   this.openlist.triggerConditionList[index].optionstriggerConditionOperator.find(
      //     (el) => el.dictValue == e
      //   ).dictLabel;
      this.openlist.triggerConditionList[index].triggerConditionValue = null;
    },

    // 诊断
    changetriggerConditionName(e, index) {
      var dictType = e;
      getAgencytype(dictType).then((res) => {
        this.ConditionValue = res.data;
      });
      this.openlist.triggerConditionList[index].triggerConditionName =
        this.optionsname.find((el) => el.dictValue == e).dictLabel;
      this.openlist.triggerConditionList[index].triggerConditionValue = null;
      this.openlist.triggerConditionList[index].triggerConditionOperator = null
      this.dictDataType = this.optionsname.find(
        (el) => el.dictValue == e
      ).dictDataType;
      this.handlelist(index)
      this.openlist.triggerConditionList[index].dictDataType =
        this.optionsname.find((el) => el.dictValue == e).dictDataType;
    },

    // 包含或等号接口
    handlelist(index) {
      var dictType = "trigger_condition_operator";
      // this.openlist.triggerConditionList[index].optionstriggerConditionOperator=[]
      getList(dictType, this.dictDataType).then((res) => {
        this.$set(this.openlist.triggerConditionList[index], 'optionstriggerConditionOperator', res.data)
        this.$forceUpdate();
      });
    },
    // 且或
    changetriggerLogicsname(e, index) {
      this.openlist.triggerConditionList[index].triggerLogic = e;
      this.openlist.triggerConditionList[index].triggerLogicName =
        this.optionslistS.find((el) => el.dictValue == e).dictLabel;
    },
    // 适用范围
    infolists() {
      var dictType = "suit_range";
      getAgencytype(dictType).then((res) => {
        this.options = res.data;
      });
      this.infolist();
    },
    // 展示触发条件
    infolist() {
      this.routeId = this.$route.query.id;
      triggerConditionList(this.routeId).then((res) => {
        this.openlist.triggerConditionList = res.data;
        res.data.forEach((p) => {
          this.optiononditionOperators();
          p.dictDataType = this.optionsname.find(
            (el) => el.dictValue == p.triggerConditionCode
          )?.dictDataType;
          var dictType = p.triggerConditionCode;
          getAgencytype(dictType).then((res) => {
            this.ConditionValue = res.data;
            this.optiononditionOperator();
          });
        });
        if (res.data.length == 1) {
          // 回显一条时
          this.updata = res.data[0];
          this.updata.triggerConditionOperatorName =
            this.optionstriggerConditionOperator.find(
              (el) => el.dictValue == this.updata.triggerConditionOperator
            )?.dictLabel;
          this.updatalist = [];
        } else {
          // 回显两条或者三条
          this.updata = res.data[0];
          this.updata.triggerConditionOperatorName =
            this.optionstriggerConditionOperator.find(
              (el) => el.dictValue == this.updata.triggerConditionOperator
            )?.dictLabel;
          res.data.splice(0, 1);
          this.updatalist = res.data;
          this.updatalist.forEach((e) => {
            e.triggerConditionOperatorName =
              this.optionstriggerConditionOperator.find(
                (el) => el.dictValue == e.triggerConditionOperator
              )?.dictLabel;
          });
        }
        if (this.open == true) {
          this.$set(this.openlist, "suitRange", this.$route.query.suitRange);
        } else {
          this.$set(this.updata, "suitRange", this.openlist.suitRange);
        }
      });
    },
    // 编辑接口回显
    infolistname() {
      this.routeId = this.$route.query.id;
      triggerConditionList(this.routeId).then((res) => {
        this.openlist.triggerConditionList = res.data;
        this.openlist.triggerConditionList.forEach((o) => {
          this.optiononditionOperators();
          o.dictDataType = this.optionsname.find(
            (el) => el.dictValue == o.triggerConditionCode
          )?.dictDataType;
          var dictType = "trigger_condition_operator";
          getList(dictType, o.dictDataType).then((res) => {
            this.$set(o, 'optionstriggerConditionOperator', res.data)
            this.$forceUpdate()
          });
          var dictType = o.triggerConditionCode;
          getAgencytype(dictType).then((res) => {
            this.ConditionValue = res.data;
          });
        });
        this.$set(this.updata, "suitRange", this.openlist.suitRange);
      });
    },
    // 编辑
    edit() {
      this.open = true;
      this.infolistname();

      // this.openfalse();
    },
    //wangeditor传值
    onNodeContent(item) {
      console.log(item, 'items')
      let wangeditorlist = []
      if (localStorage.getItem('wangeditorlist')) {
        wangeditorlist = JSON.parse(localStorage.getItem('wangeditorlist'))
        wangeditorlist = wangeditorlist.filter(e => e.taskSubdivision != item.taskSubdivision && e.specialDiseaseNodeId != item.specialDiseaseNodeId)
      }
      wangeditorlist.push(item)
      // console.log(wangeditorlist, 'wangeditorlist')
      localStorage.setItem('wangeditorlist', JSON.stringify(wangeditorlist))
      if (item.nodeContent) {
        // console.log(555)
        this.form.nodeContent = item.nodeContent;
        // console.log(this.form.nodeContent, 'this.form.nodeContentthis.form.nodeContentthis.form.nodeContent')
        var abc = new RegExp("<br>", "g");
        var bbb = this.form.nodeContent.replace(abc, "");
        this.form.nodeContent = bbb;

      }
    },
    //小程序传值
    miniProgramtemplate(item) {
      this.form.appletTemplateId = item.templateId;
      this.form.appletTemplateName = item.templateName;
      this.form.appletRemindContent = item.templateContent;
    },
    // 话术
    messageontemplateword(item) {
      this.form.phoneTemplateId = item.templateId;
      this.form.phoneTemplateName = item.templateName;
      this.phoneNodeContent = item.phoneNodeContent;
    },
    // 预览话术图
    look() {
      this.lookNodeContent = true
    },
    // 预览问卷
    lookquestion() {
      this.lookquestionname = true
    },
    // 宣教预览
    imagepreviews() {
      this.imageScriptpreviews = true
    },
    // 问卷库模板选择
    looklist() {
      this.lookquestionlist = true
    },
    // 切换人工电话和自动外呼
    changephoneDialMethod(e) {
      // console.log(e)
      if (e == 'AI') {
        // this.form.templateId=''
        // 清空问卷id和名字
        this.form.phoneTemplateId = ''
        this.form.phoneTemplateName = ''
        // 清空话术和问卷
        this.phoneNodeContent = ''
        this.lookitemnew = ''
      } else if (e == 'COMMON') {
        // 清空问卷id话术id和对应名字
        this.form.templateId = ''
        this.form.templateName = ''
        this.form.phoneTemplateId = ''
        this.form.phoneTemplateName = ''
        // 清空话术和问卷
        this.phoneNodeContent = ''
        this.lookitemnew = ''
      }
    },
    //短信传值
    messageontemplate(item) {
      this.form.messageTemplateId = item.templateId;
      this.form.messageTemplateName = item.templateName;
      this.form.messagePreview = item.templateContent;
    },
    // 短信
    messageontemplateMESSAGE(item) {
      this.form.phoneMessageTemplateId = item.templateId;
      this.form.phoneMessageTemplateName = item.templateName;
      this.form.messagePreview = item.templateContent;
    },
    //公众号传值
    officialAccountontemplate(item) {
      this.form.officialTemplateId = item.templateId;
      this.form.officialTemplateName = item.templateName;
      this.form.officialRemindContent = item.templateContent;
    },
    // 暂存
    TemporaryStorage() {
      if (
        this.form.taskNodeType != "QUESTIONNAIRE_SCALE" &&
        this.form.taskNodeType != "PROPAGANDA_ARTICLE" &&
        this.form.taskNodeType != 'PHONE_OUTBOUND'
      ) {
        this.$refs.wangeditor.emit();
      }
      this.$confirm(
        "确认暂存时将过滤掉部分内容未填写完成的子节点,是否确认暂存？",
        "提示",
        {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
        }
      ).then(() => {
        const loading = this.$loading({
          lock: true,
          text: "暂存中",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)",
        });
        this.itemlist.specialDiseaseNodeList = [];
        this.lists.forEach((e) => {
          e.list.length > 0
            ? e.list.forEach((el) => {
              el.routeNodeDay = e.routeNodeDay;
              el.routeNodeName = e.routeNodeName;
              if (el.phoneDialMethod == 'COMMON' && el.templateId) {
                el.phonePushSign = "1";
                el.taskNodeType = 'QUESTIONNAIRE_SCALE'
              } else if (el.phoneDialMethod == 'COMMON' && el.phoneTemplateId) {
                el.phonePushSign = "1";
                el.taskNodeType = 'PHONE_OUTBOUND'

              } else if (el.phoneDialMethod == "AI" && el.phoneTemplateId) {
                el.phonePushSign = "1";
                el.taskNodeType = "PHONE_OUTBOUND";
              }
              this.itemlist.specialDiseaseNodeList.push(el);
            })
            : "";
        });
        // this.updata.specialDiseaseNodeList =
        //   this.updata.specialDiseaseNodeList.filter(
        //     (e) =>
        //       !e.routeNodeDay &&
        //       !e.routeNodeName &&
        //       !e.taskType &&
        //       !e.taskSubdivision &&
        //       !e.taskStatus
        //   );
        // return

        specialDiseaseNode(this.itemlist).then((res) => {
          let wangeditorlist = []
          if (localStorage.getItem('wangeditorlist')) {
            wangeditorlist = JSON.parse(localStorage.getItem('wangeditorlist'))
            wangeditorlist = wangeditorlist.filter(e => e.specialDiseaseNodeId == this.form.specialDiseaseNodeId)
            console.log(wangeditorlist, 'wangeditorlist')
            localStorage.setItem('wangeditorlist', JSON.stringify(wangeditorlist))
          }
          this.info();
          loading.close();
          this.$modal.msgSuccess("暂存成功!");
          this.listindex = 0
          this.itemindex = 0
        });
        setTimeout(() => {
          loading.close();
        }, 3000);
      });
    },
    //问卷传值
    questionontemplate(item) {
      // console.log(item, 'iten')
      this.form.templateId = item.templateId;
      this.form.templateName = item.templateName;
      this.lookitemnew = item.lookitemnew
    },
    //宣教传值
    propagandaontemplate(item) {
      // console.log(item, 'item')
      this.form.templateId = item.templateId;
      this.form.templateName = item.templateName;
      // if(item){
      this.formview = item.formview

    },
    onemit() {
      if (
        this.form.taskNodeType != "QUESTIONNAIRE_SCALE" &&
        this.form.taskNodeType != "PROPAGANDA_ARTICLE" &&
        this.form.taskNodeType != "PHONE_OUTBOUND"
      ) {
        this.$refs.wangeditor.emit();
      }
    },
    // 点击
    async bottomclickevent(uitem, index, uindex) {
      await this.onemit();
      const loading = this.$loading({
        lock: true,
        text: "加载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      this.listindex = index;
      this.itemindex = uindex;
      if (
        uitem.taskNodeType == "PROPAGANDA_ARTICLE" &&
        uitem.templateId
      ) {
        await getPropaganda(uitem.templateId).then((response) => {
          this.formview = response.data;
        });
      } else if (
        uitem.taskNodeType == "QUESTIONNAIRE_SCALE" &&
        uitem.templateId
      ) {
        await questionname(uitem.templateId).then((res) => {
          this.lookitemnew = res.data;
        });
      } else if (
        uitem.taskNodeType == "PHONE_OUTBOUND" &&
        uitem.flowScheme
      ) {
        this.phoneNodeContent = uitem.flowScheme;
      }
      this.taskPartitionList = [];
      if (uitem.taskType) {
        this.changeTaskType(uitem.taskType, uitem.taskSubdivision);
      }
      this.form = uitem;
      setTimeout(() => {
        loading.close();
      }, 1500);
    },

    handleStep() {
      this.active = 2;
    },
    //任务类型 任务状态
    taskinfo() {
      selectTaskTypeList().then((res) => {
        this.selectTaskTypeList = res.data;
        this.info();
      });
    },
    //任务类型
    changeTaskType(code, taskSubdivision) {
      this.taskPartitionDictId = ''
      let id = this.selectTaskTypeList?.find((e) => e.taskTypeCode == code)?.id;
      this.form.taskTypeName = this.selectTaskTypeList?.find(
        (e) => e.taskTypeCode == code
      )?.taskTypeName;
      taskPartitionList(id).then((res) => {
        // res.data?.forEach(e => {
        //   if (e.taskNodeType == 'PHONE_OUTBOUND') {
        //     e.taskPartitionName = e.taskPartitionName + '(电话外呼)'
        //   } else if (e.taskNodeType == 'QUESTIONNAIRE_SCALE') {
        //     e.taskPartitionName = e.taskPartitionName + '(问卷量表)'
        //   } else if (e.taskNodeType == 'PROPAGANDA_ARTICLE') {
        //     e.taskPartitionName = e.taskPartitionName + '(宣教文案)'
        //   } else if (e.taskNodeType == 'TEXT_REMIND') {
        //     e.taskPartitionName = e.taskPartitionName + '(文字提醒)'
        //   }
        // })
        this.taskPartitionList = res.data;
        this.form.taskSubdivision = "";
        this.form.taskSubdivisionName = "";
        this.form.taskNodeType = "";
        this.taskStatusDictList = [];
        if (taskSubdivision) {
          this.form.taskSubdivision = taskSubdivision;
          this.changetaskSubdivision(taskSubdivision, 1);
        }
      });
    },
    //点击任务细分
    changetaskSubdivision(code, type) {
      if (
        this.form.taskNodeType != "QUESTIONNAIRE_SCALE" &&
        this.form.taskNodeType != "PROPAGANDA_ARTICLE" &&
        this.form.taskNodeType != "PHONE_OUTBOUND"
      ) {
        this.$refs.wangeditor.emit();
      }
      setTimeout(() => {
        const loading = this.$loading({
          lock: true,
          text: "加载中",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)",
        });
        if (!type) {
          this.form.taskStatus = ''
        }
        this.form.taskSubdivisionName = this.taskPartitionList?.find(
          (e) => e.taskPartitionCode == code
        )?.taskPartitionName;
        let id = this.taskPartitionList?.find(
          (e) => e.taskPartitionCode == code
        )?.id;
        console.log(id, 'id')
        taskStatusDictList(id).then((res) => {
          this.taskStatusDictList = res.data;
        });
        this.taskPartitionDictId = id
        this.form.secondClassifyDescribe = this.taskPartitionList?.find(
          (e) => e.taskPartitionCode == code
        )?.secondClassifyDescribe;
        this.form.executionTime = this.taskPartitionList?.find(
          (e) => e.taskPartitionCode == code
        )?.executionTime;
        this.taskPartitionList.forEach((el) => {
          if (code == el.taskPartitionCode) {
            this.form.executionTime = el.executionTime;
            this.form.taskNodeType = el.taskNodeType;
            if (
              this.form.taskNodeType == "PHONE_OUTBOUND" ||
              this.form.taskNodeType == "PROPAGANDA_ARTICLE" ||
              this.form.taskNodeType == "QUESTIONNAIRE_SCALE"
            ) {
              this.form.nodeContent = `<p></p>`;
            }
            if (!type) {
              this.form.templateId = "";
              this.form.templateName = "";
            }
          }
          let wangeditorlist = []
          if (localStorage.getItem('wangeditorlist')) {
            wangeditorlist = JSON.parse(localStorage.getItem('wangeditorlist'))
            this.form.nodeContent = '<p></p>'
            this.form.nodeContent = wangeditorlist.find(e => e.taskSubdivision == this.form.taskSubdivision && e.specialDiseaseNodeId == this.form.specialDiseaseNodeId)?.nodeContent
            // console.log(this.form.nodeContent, 'this.form.nodeContent8858585')
          }
        });
        loading.close();

      }, 500);


      console.log(this.form)
    },
    // 添加管理路径节点
    addlist() {
      // this.taskPartitionDictId = ''
      list(this.parentDictCode).then((res) => {
        // this.parentDictCodelist = res.rows;
        res.rows.forEach((e) => {
          this.lists.forEach((el) => {
            el.routeNodeName = e.dictValue;
          });
        });
      });
      this.lists.push({
        routeNodeName: "",
        routeNodeDay: "",
        list: [
          {
            nodeContent: "<p></p>",
            templateId: "",
            templateName: "",
            taskType: "",
            officialRemindContent: "",
            taskSubdivision: "",
            taskSubdivisionName: "",
            appletRemindContent: "",
            appletPromptDescription: "",
            taskStatus: "",
            secondClassifyDescribe: "",
            executionTime: "",
            appletPushSign: "0",
            officialPushSign: "0",
            messagePushSign: "0",
            messagePreview: "",
            taskNodeType: "",
          },
        ],
      });
      // },
    },
    // 添加节点
    additem(item) {
      // this.taskPartitionDictId = ''

      item.list.push({
        nodeContent: "<p></p>",
        templateId: "",
        templateName: "",
        taskType: "",
        officialRemindContent: "",
        taskSubdivision: "",
        taskSubdivisionName: "",
        appletRemindContent: "",
        appletPromptDescription: "",
        taskStatus: "",
        secondClassifyDescribe: "",
        executionTime: "",
        appletPushSign: "0",
        officialPushSign: "0",
        messagePushSign: "0",
        messagePreview: "",
        taskNodeType: "",
      });
    },
    // 删除节点
    delitemh(item, index) {
      item.list.splice(this.itemindex, 1);
      if (item.list.length == 0) {
        this.lists.splice(index, 1);
      }
    },
  },
};
// </script>
