add.js 5.4 KB
import React, { Component } from "react";
import { NavLink } from "react-router-dom";
import Questionnair from "@components/reactQuestionnair/src/Questionnair";
import Editor from "@components/reactQuestionnair/src/QuestionnairEditor";
import Answer from "@components/reactQuestionnair/src/QuestionnairAnswer";
import "@components/reactQuestionnair/assets/iconfonts/iconfont.less";
import { UserManageBar } from "@components/Common";

import { formatQuesData, switchEditor } from "@components/reactQuestionnair/utils/utils";
import axios from "@src/axios/index";
import "./health.less";
import { API_SURVEY_MANAGE, API_SURVEY_QURADIO, API_SURVEY_DELETE_BY_ID } from "@src/Api";
import { message } from "antd";


Questionnair.Editor = Editor;
Questionnair.Answer = Answer;

class HealthNoticeAdd extends React.Component {
  state = {
    menuName: "新增告知",
    spinStatus: false,// 是否显示加载中
    editors: [], //可编辑的题目
    surveyId: '',
    
  };
  componentDidMount() {
    let originInfo = this.props.location;
    console.log(this.props.match.params)
    const { params } = this.props.match.params
    let id = this.props.match.params.id
    if (params === 'update') {
      this.setState({ menuName: "修改告知", surveyId: id });
      this.buildSurvey(id)
    } else {
      this.buildSurvey(id)
      this.setState({ menuName: "新增告知", surveyId: id });
    }
  }
  //获取数据,设计健康告知数据buildSurvey
  buildSurvey = (id) => {
    axios.ajax({
      url: API_SURVEY_MANAGE.buildSurvey, //只是修改 发布/保存 的状态
      data: {
        surveyId: id
      }
    }).then(res => {
      console.log(res);
      let editors = formatQuesData(res)
      this.setState({
        questionnairTitle:res.surveyName,
        editors
      })
    })
  }



  onConfirm = (newItem, index, newEditors) => {
    const belongId = this.props.match.params.id
    const { editors } = this.state
    let data = {}
    console.log('newItem', newItem);

    let { type, url } = switchEditor(newItem.type)
    console.log('type', type)
    axios.ajax({
      url,
      data: type === 'input' ? {
        belongId,
        hv: 2,
        completionBackwards: newItem.completionBackwards,
        completionForwards: newItem.completionForwards,
        isRequired: newItem.required ? 1 : 0,  //是否必答
        orderById: newItem.orderById,  //排序
        otherOption: newItem.otherOption ? 1 : 0,
        otherOptionBackwards: newItem.otherOptionBackwards,
        otherOptionForwards: newItem.otherOptionForwards,
        quId: newItem.quId,
        quTag: 1,  //是否是大小题
        quTitle: newItem.title,
        randOrder: 0,  //选项随机排序
        tag: 2   //标记
      } : {
          [type]: newItem.type,
          belongId,  //健康告知id
          hv: 2,   //控制性属性
          isRequired: newItem.required ? 1 : 0,  //是否必答
          orderById: newItem.orderById,  //排序
          otherOption: newItem.otherOption ? 1 : 0,
          otherOptionBackwards: newItem.otherOptionBackwards,
          otherOptionForwards: newItem.otherOptionForwards,
          quId: newItem.quId,
          [type]: [  //单选题/多选/填空
            {
              id: newItem.id,
              isNote: newItem.remark ? 1 : 0,
              optionName: newItem.remarkText,
              orderById: newItem.orderById,  //排序
              optionTitle: JSON.stringify(newItem.options),
            }
          ],
          quTag: 1,  //是否是大小题
          quTitle: newItem.title,
          randOrder: 0,  //选项随机排序
          tag: 2   //标记
        }
    }).then(res => {
      newEditors[index].quId = res.quId
      newEditors[index].id = type !== 'input' ? res[type][0].id : null
      this.setState({
        editors: newEditors
      })

      console.log('this.editors', newEditors)
    })
  }

  //onRemove根据id删除问题
  onRemove = (questionId) => {
    console.log('questionId', questionId)

    axios.ajax({
      url: API_SURVEY_DELETE_BY_ID.deleteQuestionById,
      data: {
        quId: questionId
      }
    }).then(res => {

      console.log('删除成功', res)
    })
  }

  //保存标题
  // onSaveTitle = (qTitle) => {
  //   console.log('tt', qTitle)
  //   this.setState({
  //     surveyName: qTitle
  //   })
  // }

  updateStateById =(surveyName,state)=>{
    console.log('state',state)
    axios.ajax({
      url: API_SURVEY_MANAGE.updateStateById, //只是修改 发布/保存 的状态
      data: {
        surveyId:this.props.match.params.id,
        surveyName,
        state:state ? 1:0
      }
    }).then(res => {
      message.success(state ?'发布成功!': '保存成功!')
    })
  }


  render() {
    const { menuName, surveyId ,questionnairTitle} = this.state;
    let originInfo = this.props.location.state;
    return (
      <div className="roleAdd">
        <div className="nav">
          <NavLink to="/home/healthNotice/manage">健康告知 > </NavLink>
          <span>{menuName}</span>
        </div>

        <Questionnair
          updateStateById= {this.updateStateById}
          surveyId={surveyId}
          questionnairTitle = { questionnairTitle }
          onConfirm={this.onConfirm}
          onRemove={this.onRemove}
          // onSaveTitle={this.onSaveTitle}
          editors={this.state.editors} />
        {/* <Questionnair.Editor editor={editor} activeAnswer = {true}/> */}
      </div>
    );
  }
}

export default HealthNoticeAdd;