import { getBigQuestionIndex,getPageResiduetHeight } from "./makeBase.js";

function compare(property) {
  return function (a, b) {
    var value1 = a[property];
    var value2 = b[property];
    return value2 - value1;
  };
}
//数据切割  如：选择题

export function cutChooseQuestions(
  questionData,
  pageInfo,
  pageQuestionForUIData,
  questionList,
  isEdit
) {

// debugger
  //竖向
  if(questionData.optionsDirection == 2){
    return cutChooseQuestionsVertical(
      questionData,
      pageInfo,
      pageQuestionForUIData,
      questionList,
      isEdit
    );
  }

  let { mainWidth, mainHeight, studentInfoH, pageCount } = pageInfo; //页面宽度可传
  let cNameW = 9; //选项标题宽度
  let cLabelW =  6.8,
    clabelH = 6.5; //选项高度
  let bigQuestionIndex = 1; //大题号//大题号
  if (questionList.length > 0) {
    bigQuestionIndex = questionData.bigQuestionIndex;
    // eqQuestionIndex = questionList[questionList.length - 1].eqQuestionIndex + 1;
  }
  //初始化所有定位
  let { listItemNum, rectHeight, lyColHeight } = questionData;
  let nowPageNum = 1;
  let residuoRectHeight = getPageResiduetHeight(pageInfo, nowPageNum);
  let nowRowW = 0,
    questionFUIData = [],
    nowY = 0,
    nowRowNum = 0,
    nowRowColNum = 0,
    nowX = 0,
    isFirst = true; //当前行宽;
  let minQuestionHeight = 5 + clabelH * listItemNum + lyColHeight;
  let isNoPage = true;

  for (let i = 0; i < pageQuestionForUIData.length; i++) {
    let item = pageQuestionForUIData[i];
    let nowPageH = getPageResiduetHeight(pageInfo,i+1),
      itemH = 0;

    item.questionForUIData.forEach((it) => {
      let h = it.rectHeight;
      itemH += h;
    });
    let residueH = nowPageH - itemH;
    if ((residueH > minQuestionHeight)&& (!pageQuestionForUIData[i+1] || pageQuestionForUIData[i+1].questionForUIData.length == 0)) {
      isNoPage = false;
      nowPageNum = item.pageIndex;
      residuoRectHeight = residueH;
      break;
    }
  }
  //新页面
  if (isNoPage) {
    nowPageNum = pageQuestionForUIData.length + 1;
    residuoRectHeight =getPageResiduetHeight(pageInfo, nowPageNum);
  }
  let pageNum = nowPageNum;

  //每列宽数组
  let colWidthArr = []; //[5,4,5,4,2]
  let itemAllArr = []; //所有分组数据
  let sumScore = 0;
  questionData.questionList.forEach((item, index) => {
    let num = parseInt(index / listItemNum);
    // item.eqQuestionIndex = eqQuestionIndex + index;
    let numY = (index + 1) % listItemNum; //个数余数
    let w = cNameW + cLabelW * item.selNumber; //宽
    sumScore += Number(item.score);
    item.width = w;
    if (numY > 0 && index + 1 > listItemNum) {
      let idx = num;
      item.colNum = idx; //第几列
      if (!itemAllArr[idx]) {
        itemAllArr[idx] = [];
      }
      itemAllArr[idx].push(item);
    } else {
      if (!itemAllArr[num]) {
        itemAllArr[num] = [];
      }
      item.colNum = num;
      itemAllArr[num].push(item);
    }
  });

  //标题文字  题号+名称+分数  一、填空题（2分）
  let fullTitleName = questionData.title + (!isEdit ? ("（" + sumScore + "分）" ): '');

  //数组加列宽（算出最长的宽）
  itemAllArr.forEach((item) => {
    let maxW = 0;
    if (item.length > 1) {
      let leItem = [...item].sort(compare("width"));
      maxW = leItem[0].width;
    } else {
      maxW = item[0].width;
    }
    colWidthArr.push(maxW.toFixed(2));
  });

  itemAllArr.forEach((item, index) => {
    // 当宽度大于页面内容宽时换行
    nowRowColNum++;
    nowX = nowRowW;
    nowRowW = Number(colWidthArr[index]) + Number(nowRowW);
    //换行时，
    if (nowRowW > mainWidth) {
      nowRowNum++;
      nowX = 0;
      nowY =
        5 +
        clabelH * listItemNum * (nowRowNum + 1) +
        lyColHeight * (nowRowNum + 1);
      nowRowW = Number(colWidthArr[index]);
      questionFUIData[pageNum - 1].rectHeight +=
        clabelH * listItemNum + lyColHeight;
    
      if (nowY > residuoRectHeight) {
        //下一页
        questionFUIData[pageNum - 1].rectHeight =
          questionFUIData[pageNum - 1].rectHeight -
          clabelH * listItemNum -
          lyColHeight;
        questionFUIData[pageNum - 1].residuoRectHeight = 0;
        nowRowNum = 0;
        pageNum++;
        residuoRectHeight = getPageResiduetHeight(pageInfo, pageNum);
      }
    }
    if (!questionFUIData[pageNum - 1]) {
      let rectHh = 2.5 * 2 + clabelH * listItemNum + Number(isFirst ? 7 : 0);
      //最后一行不满
      // if(){}
      questionFUIData[pageNum - 1] = {
        questionFUIData: [],
        bigQuestionIndex,
        optionsDirection: questionData.optionsDirection,
        type: 1,
        title: questionData.title,
        isShowTitle: isFirst,
        pageIndex: pageNum,
        listItemNum: listItemNum,
        rectHeight: rectHh,
        rectWidth: mainWidth,
        rectTop: mainHeight - residuoRectHeight,
        ysRectHeight: rectHh,
        ysRectTop: mainHeight - residuoRectHeight,
        sumScore: sumScore,
        fullTitleName,
      };
      isFirst = false;
    }
    
    item.map((it, idx) => {
      it.rowNum = nowRowNum;
      it.nowRowColNum = nowRowColNum;
      it.x = nowX;
      it.y =
        2.5 +
        clabelH * listItemNum * nowRowNum +
        lyColHeight * nowRowNum +
        clabelH * idx;
      questionFUIData[pageNum - 1].questionFUIData.push(it);
    });
  });

  let questionItem = {
    title: questionData.title,
    fullTitleName: fullTitleName,
    questionScore: questionData.questionScore,
    listItemNum: questionData.listItemNum,
    optionsDirection: questionData.optionsDirection,
    questionNum: questionData.questionNum,
    checkedIsChoice:questionData.checkedIsChoice,
    selectCount: questionData.selectCount,
    type: questionData.type,
    questionList: questionData.questionList,
    sumScore: sumScore,
    questionFUIData: questionFUIData,
    bigQuestionIndex: bigQuestionIndex,
    lyColHeight: questionData.lyColHeight,
    updateNum: bigQuestionIndex + 233,
  };
  console.log("处理后的questionItem",questionItem);
  return questionItem;
  
}
//选择题竖向数据切割
function cutChooseQuestionsVertical(
  questionData,
  pageInfo,
  pageQuestionForUIData,
  questionList,
  isEdit
) {
  let { mainWidth, mainHeight, studentInfoH, pageCount } = pageInfo; //页面宽度可传

  console.log("打印页面高度",mainHeight);
  
  let cLabelW =   7.4,
    clabelH = 5; //选项宽度
  let bigQuestionIndex = getBigQuestionIndex(questionList); //大题号
  
  //初始化所有定位
  let { listItemNum, rectHeight, lyColHeight } = questionData;
  let nowPageNum = 1;
  let residuoRectHeight = getPageResiduetHeight(pageInfo, nowPageNum);
  let questionFUIData = [],
    isFirst = true; //当前行宽;
  let minQuestionHeight = 5 + clabelH * listItemNum + lyColHeight;
  let sumScore = 0;
  

  for (let i = 0; i < pageQuestionForUIData.length; i++) {
    let item = pageQuestionForUIData[i];
    let nowPageH = getPageResiduetHeight(pageInfo,i+1),
      itemH = 0;

    item.questionForUIData.forEach((it) => {
      let h = it.rectHeight
      itemH += h;
    });
    let residueH = nowPageH - itemH;
    if ((residueH > minQuestionHeight)&& (!pageQuestionForUIData[i+1] || pageQuestionForUIData[i+1].questionForUIData.length == 0)) {
      nowPageNum = item.pageIndex;
      residuoRectHeight = residueH;
      break;
    }
  }
  //逻辑梳理 1.判断是否需要分页 2.判断是否需要换行 3.判断是否需要换页

  //一行几个题
  let rowQNum = parseInt((mainWidth-4)/ cLabelW);
  let nowRowNum = 1, //大行数
      nowRowColNum = 0, //列数
      rowMaxOptionNum = 0,
      cumulativeH = 0,
      rowHArr = [0];

  questionData.questionList.forEach((item, index) => {
    sumScore += item.score;
    if(nowRowColNum>rowQNum){
      rowHArr.push( 10 + 5 * rowMaxOptionNum);
      rowMaxOptionNum = 0; //换行清0
      nowRowColNum = 0;
      nowRowNum++;
    }
    nowRowColNum++;
      rowMaxOptionNum = Math.max(rowMaxOptionNum, item.selNumber);
    //最后一行
    if(index == questionData.questionList.length-1){
      rowHArr.push( 5 + 5 * rowMaxOptionNum);
    }
  });

  nowRowColNum = 0;
  nowRowNum = 1;
  rowMaxOptionNum=0;
  let nowRowAllNum = 1; // 当跨页时记录所有换行行数
  let fullTitleName = questionData.title + (!isEdit ? ("（" + sumScore + "分）" ): '');
  questionData.questionList.map((item, index) => {
    if(nowRowColNum>=rowQNum){
      //一行几个 换行
      cumulativeH += rowHArr[nowRowAllNum];
      rowMaxOptionNum = 0; //换行清0
      nowRowColNum = 0;
      nowRowNum++;
      nowRowAllNum++;
      //换页 当下一行高度大于当前剩余高度
      let h = rowHArr[nowRowAllNum]?rowHArr[nowRowAllNum]:0;
      if((cumulativeH+h)+4 + (isFirst ? 7 : 0)  >= residuoRectHeight){
        nowPageNum++;
        questionFUIData[nowPageNum - 2].rectHeight = questionFUIData[nowPageNum - 2].ysRectHeight = cumulativeH + (isFirst ? 7 : 0) + 4;
        isFirst = false;
        cumulativeH=0;
        residuoRectHeight = getPageResiduetHeight(pageInfo, nowPageNum);
        nowRowNum = 1;
        rowMaxOptionNum = 0; //换行清0
        nowRowColNum = 0;
      }
    }
    if(!questionFUIData[nowPageNum - 1]) {
      questionFUIData[nowPageNum - 1] = {
        questionFUIData: [],
        bigQuestionIndex,
        optionsDirection: questionData.optionsDirection,
        type: 1,
        title: questionData.title,
        isShowTitle: isFirst,
        pageIndex: nowPageNum,
        fullTitleName,
        rectTop: mainHeight - residuoRectHeight,
        rectWidth: mainWidth,
        ysRectTop:mainHeight - residuoRectHeight,

      }
    }
    //计算当前行的高 找到最高的那一个几个选项 高度算法 （数字高+选项高）
    item.x = 2+nowRowColNum* cLabelW;
    item.y = 2 + cumulativeH;
    item.rectHeight = 10 + 5 * item.selNumber;
    item.pageIndex = nowPageNum;
    questionFUIData[item.pageIndex - 1].questionFUIData.push(item);
    if(index == questionData.questionList.length-1){
      questionFUIData[nowPageNum - 1].rectHeight = questionFUIData[nowPageNum - 1].ysRectHeight = cumulativeH + rowHArr[rowHArr.length-1] + (isFirst ? 7 : 0) + 4;
    }
    nowRowColNum++;
  });
  
  let questionItem = {
    title: questionData.title,
    fullTitleName: fullTitleName,
    questionScore: questionData.questionScore,
    listItemNum: questionData.listItemNum,
    optionsDirection: questionData.optionsDirection,
    questionNum: questionData.questionNum,
    checkedIsChoice:questionData.checkedIsChoice,
    selectCount: questionData.selectCount,
    type: questionData.type,
    questionList: questionData.questionList,
    sumScore: sumScore,
    questionFUIData: questionFUIData,
    bigQuestionIndex: bigQuestionIndex,
    lyColHeight: questionData.lyColHeight,
    updateNum: bigQuestionIndex + 233,
  };
  return questionItem;
}
//填空题
/*
 *isEdit 编辑类型    1-拖拽改变高度 2-删除 3-编辑
 */
export function cutFillBlankQuestions(
  questionData,
  pageInfo,
  pageQuestionForUIData,
  isEdit,
  pageQuestionList
) {
  // debugger
 //纸阅卡逻辑
 if(pageInfo.markType == 2){
  return cutFillBlankQuestionsForPaper(
    questionData,
    pageInfo,
    pageQuestionForUIData,
    isEdit
  );
 }
  let { mainWidth, mainHeight, studentInfoH, pageCount, markType } = pageInfo; //页面信息
  //初始化所有定位
  let { rowExCount, questionList, rowHeight,isOver } = questionData;
  //题不存在即新增题或者编辑本题覆
  // 如果不是新增 则记录原来高度
  let ysRectHeight = 0;
  let oldValueArr = []; //记录旧的值
  if(isEdit){
    ysRectHeight = questionData.rectHeight;
    // questionData.questionFUIData.map(item=>{
    //   oldValueArr.push(item.qValue);
    // })
    let vKeys = Object.keys(questionData).filter(item=>item.indexOf("qValue_")>-1)
      vKeys.map((it)=>{
        oldValueArr.push(questionData[it]);
      })
  }
  let nowPageNum = 1;
  let residuoRectHeight =getPageResiduetHeight(pageInfo, nowPageNum);
  let questionFUIData = [],
    sumScore = 0;
  let minQuestionHeight = 10 + 2 * 2 + rowHeight; //最小高度  标题高+上内边距+下内编剧+一行的高度
  let isNoPage = true;
  for (let i = 0; i < pageQuestionForUIData.length; i++) {
    let item = pageQuestionForUIData[i];
    let nowPageH = getPageResiduetHeight(pageInfo, i+1),
      itemH = 0;

    item.questionForUIData.forEach((it) => {
      let h = it.rectHeight;
      itemH += h;
    });
    let residueH = nowPageH - itemH;
    if ((residueH > minQuestionHeight) && (!pageQuestionForUIData[i+1] || pageQuestionForUIData[i+1].questionForUIData.length == 0)) {
      isNoPage = false;
      nowPageNum = item.pageIndex;
      residuoRectHeight = residueH;
      break;
    }else{
      nowPageNum=item.pageIndex + 1;
    }
  }
  //如果编辑 大题Id不变
  let bigQuestionIndex = questionData.bigQuestionIndex;
  
  //新页面
  if (isNoPage) {
    nowPageNum = pageQuestionForUIData.length + 1;
    residuoRectHeight =getPageResiduetHeight(pageInfo, nowPageNum);
  }
  
  let pageNum = nowPageNum;
  let rowNum = 0,
    itemPaddingTop = 4,
    itemPaddingLeft = 2,
    itemConH = rowHeight,
    isFirst = true,
    rectH = 0,
    nowPageNowRow = 1, //当前页当前行
    nowPageItemIndex = 0, //当前页索引
    itemWidth = (mainWidth - itemPaddingLeft * 2) / rowExCount; //页面看度-俩边间距 % 每行个数

  questionList.forEach((item) => {
    sumScore += Number(item.score);
  });
  let fullTitleName = questionData.title + (!isEdit?"（" + sumScore + "分）":'');

  //选择覆盖 /或新增时
  let qValueArr = {}; //值保存用
  //如果是新增/编辑覆盖
  if(!isEdit || questionData.isOver == 1){

    questionList.map((item, index) => {
      let key = parseInt(nowPageItemIndex / rowExCount);
      let indexB = nowPageItemIndex % rowExCount; //余数
      nowPageItemIndex++;
      //计算item坐标，
      item.x = itemPaddingLeft + indexB * itemWidth;
      item.y = itemPaddingTop + key * itemConH;
      item.rectWidth = itemWidth;
      item.rectHeight = itemConH;
      item.fullTitleName = fullTitleName;
      item.isOver = isOver;
      rowNum = indexB == 0 ? rowNum + 1 : rowNum;
      item.updateNum = item.id;
      //下一页  当前
      if (item.y > (residuoRectHeight - itemConH - 8)) {
        rectH = 0;
        rowNum = 1;
        nowPageItemIndex = 0;
        pageNum++;
        isFirst = false;
        //新页
        residuoRectHeight =getPageResiduetHeight(pageInfo, pageNum);
      }
      if (!questionFUIData[pageNum - 1]) {
        //之前没有
        questionFUIData[pageNum - 1] = {
          questionFUIData: [],
          bigQuestionIndex: bigQuestionIndex,
          type: 2,
          showChooseType: questionData.showChooseType,
          chooseType: questionData.chooseType,
          isOver,
          title: questionData.title,
          fullTitleName,
          isShowTitle: isFirst,
          rowExCount: rowExCount,
          pageIndex: pageNum,
          rectWidth: mainWidth,
          rectTop: mainHeight - residuoRectHeight,
          ysRectTop: mainHeight - residuoRectHeight,
          score: item.score,
          sumScore:questionData.sumScore,//满分  新增
        };
        
        item.pageIndex = pageNum;
      }
      //换行 计算题高度
      if (indexB == 0) {
        questionFUIData[pageNum - 1].ysRectHeight = questionFUIData[
          pageNum - 1
        ].rectHeight = 8 + rowNum * itemConH + (isFirst ? 7 : 0);
      }
      questionFUIData[pageNum - 1].questionFUIData.push(item);
    });

    //保存值 赋值
    questionFUIData = questionFUIData.filter((item) => item != "");
    questionFUIData.map((item, index) => {
      qValueArr['qValue_'+String(index)] = item.qValue = makeFillBlankContent(
        item.questionFUIData,
        item.rectWidth,
        item.rowExCount
      );
    });
    
  }else{
    //其他题变化引起的刷新 / 编辑不覆盖
    //如果不跨页
      if(ysRectHeight <= residuoRectHeight){
        questionFUIData[0] = questionData.questionFUIData[0];
        questionFUIData.map((item) => {
          item.ysRectTop = item.rectTop = mainHeight - residuoRectHeight;
          item.ysRectHeight = item.rectHeight = ysRectHeight;
          item.rectWidth = mainWidth;
          item.fullTitleName = fullTitleName;
          //值
          item.qValue = oldValueArr[0];
          item.pageIndex =pageNum;
        });
      }else{
        // let lastPageIndex = nowPageNum - 1;
        //跨页
        //第一页
        let firstUID = questionData.questionFUIData[0];
        firstUID.ysRectTop = firstUID.rectTop = mainHeight - residuoRectHeight;
        firstUID.rectHeight = firstUID.ysRectHeight = ysRectHeight - residuoRectHeight >= 9?residuoRectHeight:ysRectHeight;
        firstUID.pageIndex = pageNum;
        firstUID.rectWidth = mainWidth;
        firstUID.fullTitleName = fullTitleName;
        firstUID.qValue = oldValueArr[0];
        questionFUIData.push(firstUID);
        //其他页
        if(ysRectHeight - residuoRectHeight > 9){
            //剩余页码
          let pNumArr = getChangeHeightCutItem(
            nowPageNum,
            ysRectHeight - residuoRectHeight,
            pageInfo
          );
          
          pNumArr.forEach((h, index) => {
            let rectT = mainHeight - getPageResiduetHeight(pageInfo,nowPageNum + index + 1);
            let item = {
              pageIndex: nowPageNum + index + 1,
              rectHeight: h,
              rectWidth: mainWidth,
              questionList: questionData.questionList,
              qValue: oldValueArr[index+1],
              qIndex: index+1,
              ysRectHeight: h,
              questionFUIData: [],
              bigQuestionIndex: questionData.bigQuestionIndex,
              type: questionData.type,
              title: questionData.title,
              isShowTitle: false,
              rowExCount: rowExCount,
              rectTop: rectT,
              ysRectTop: rectT,
            };
            //填空题
            // item.qValue = questionData[`qValue_${1+index}`] ? questionData[`qValue_${1+index}`] : "";
            questionFUIData.push(item);
          });
        }
      }
      oldValueArr.map((item,index)=>{
        qValueArr['qValue_'+String(index)] = item;
      })
  }
  
  // 计算题总高度
  let qRectH = questionFUIData.reduce(
    (accumulator, item) => accumulator + item.rectHeight,
    0
  );
  questionFUIData.map((item,index)=>{
    item.qIndex = index;
  })
  let lastItem = questionFUIData[questionFUIData.length - 1];
  let questionItem = {
    title: questionData.title,
    questionScore: questionData.questionScore,
    rowExCount: rowExCount,
    rowHeight: rowHeight,
    rectHeight: qRectH,
    fullTitleName,
    showChooseType: questionData.showChooseType,
    chooseType:questionData.chooseType,
    ...qValueArr,
    listItemNum: questionData.listItemNum,
    optionsDirection: questionData.optionsDirection,
    questionNum: questionData.questionNum,
    type: questionData.type,
    questionList: questionData.questionList,
    sumScore: sumScore,
    questionFUIData: questionFUIData,
    rectWidth: mainWidth,
    bigQuestionIndex: bigQuestionIndex,
    lyColHeight: questionData.lyColHeight,
    lastPageIndex: lastItem.pageIndex,
    lastRectTop: lastItem.rectTop,
    lastRectHeight: lastItem.rectHeight,
    lastQuesIndex: questionFUIData.length - 1,
    isToBottom: questionFUIData.length > 1,
  };
  
  return questionItem;
}

/**
 * 只是UI变化，数据没变
 * UI高度改变 数据处理逻辑  根据高度分割 成各个item ，把选项和数据组装好
 * 先计算高度 看是否需要分页/  分页。将高度分割成各个页面的高  根据高将选项装进去 然后计算选项坐标
*/
export function UIHeightChange(
  questionData,
  pageInfo,
  changeH,
  pageQuestionLIst
) {
  //如果是解答题需要修改小题的高度而不是最后一个高度
  let { mainWidth, mainHeight, studentInfoH, pageCount } = pageInfo; //页面信息
  //初始化所有定位
  let { rowExCount, questionList, rowHeight, questionFUIData } = questionData;
  let lastItemIndex = questionFUIData.length - 1;
  let lastItem = questionFUIData[lastItemIndex];
  // let bigQuestionIndex= questionFUIData[0].bigQuestionIndex;
  //最后一个数据的所在页剩余高度  ？这里？
  let lastPageIndex = lastItem.pageIndex;
  let lastPageResidueH =
    256 -
    (questionFUIData[lastItemIndex].ysRectHeight +
      questionFUIData[lastItemIndex].rectTop);
  // 变化高度小于0
  if (changeH < 0 && questionFUIData.length > 1) {
    // 如果大于最后一页就删除最后一页
    let ch = Math.abs(changeH);
    if (ch > questionFUIData[lastItemIndex].ysRectHeight) {
      questionFUIData.splice(lastItemIndex, 1);
      lastItemIndex--;
    } else {
      // 如果小于最后一页高度
      questionFUIData[lastItemIndex].ysRectHeight += changeH;
      questionFUIData[lastItemIndex].rectHeight =
        questionFUIData[lastItemIndex].ysRectHeight;
    }
  } else if (changeH > lastPageResidueH) {
    //大于0 且 大于当前页剩余高度
    questionFUIData[lastItemIndex].ysRectHeight += lastPageResidueH;
    questionFUIData[lastItemIndex].rectHeight =
      questionFUIData[lastItemIndex].ysRectHeight;
    //跨页
    let pNumArr = getChangeHeightCutItem(
      lastPageIndex,
      changeH - lastPageResidueH,
      pageInfo
    );
    
    pNumArr.forEach((h, index) => {
      let rectT = mainHeight - getPageResiduetHeight(pageInfo,lastPageIndex + index + 1);
      let item = {
        pageIndex: lastPageIndex + index + 1,
        rectHeight: h,
        rectWidth: mainWidth,
        questionList: questionData.questionList,
        qValue: "",
        qIndex: index+1,
        ysRectHeight: h,
        questionFUIData: [],
        bigQuestionIndex: questionFUIData[lastItemIndex].bigQuestionIndex,
        type: questionData.type,
        title: questionData.title,
        isShowTitle: false,
        rowExCount: rowExCount,
        rectTop: rectT,
        ysRectTop: rectT,
      };
      //如果是解答题
      if (questionData.type == 3) {
        item.isShowBorderTop = true;
      }
      //如果英语作文
      if(questionData.type == 4){
        item.lineHeight = questionFUIData[0].lineHeight;
      }
      //填空题
      if(questionData.type == 2){
        item.qValue = questionData[`qValue_${lastItemIndex+1+index}`] ? questionData[`qValue_${lastItemIndex+1+index}`] : "";
      }else{
        item.qValue = questionData.qValue ? questionData.qValue : "";
      }
      questionFUIData.push(item);
    });

    //变化高度 大于0 且不跨页
  } else {
    //不跨页
    questionFUIData[lastItemIndex].ysRectHeight += changeH;
    questionFUIData[lastItemIndex].rectHeight =
      questionFUIData[lastItemIndex].ysRectHeight;
    questionFUIData[lastItemIndex].qValue = questionData.qValue_0
      ? questionData.qValue_0
      : "";
  }
  
  // console.log(questionData,"questionData--=uichange")
  // console.log("cutQeustion-0uiheightchange")
  // 计算题总高度
  let qRectH = questionFUIData.reduce(
    (accumulator, item) => accumulator + item.rectHeight,
    0
  );
  if(questionData.type == 2){
    questionData.rectHeight = qRectH;
  }
  questionData.lastPageIndex = questionFUIData[lastItemIndex].pageIndex;
  return questionData;
}

//Ui变化，问答题 ，数据不变
export function AQUIHeightChange(
  questionData,
  pageInfo,
  changeH,
  changePageIndex,
  eqDisplayIndex
) {
  //如果是解答题需要修改小题的高度而不是最后一个高度
  let { mainWidth, mainHeight, studentInfoH, pageCount } = pageInfo; //页面信息
  let smallQuestionIndex = questionData.questionList.findIndex((item) => {
    return item.eqDisplayIndex == eqDisplayIndex;
  });
  //初始化所有定位
  // let smallQuestionItem = ;
  let { rowExCount, questionList, rowHeight, questionFUIData } = questionData;

  let nowSmallQUI = questionFUIData.map((item) => {
    return item.eqDisplayIndex == eqDisplayIndex;
  });
  let lastItemIndex = nowSmallQUI.length - 1;
  let lastItem = nowSmallQUI[lastItemIndex];
  //最后一个数据的所在页剩余高度
  let lastPageIndex = lastItem.pageIndex;
  let lastPageResidueH =
    256 -
    (questionFUIData[lastItemIndex].ysRectHeight +
      questionFUIData[lastItemIndex].rectTop);
  if (changeH > lastPageResidueH) {
    questionFUIData[lastItemIndex].ysRectHeight += lastPageResidueH;
    questionFUIData[lastItemIndex].rectHeight =
      questionFUIData[lastItemIndex].ysRectHeight;

    //跨页
    let pNumArr = getChangeHeightCutItem(
      lastPageIndex,
      changeH - lastPageResidueH,
      pageInfo
    );
    pNumArr.forEach((h, index) => {
      let rectT =
        (lastPageIndex + index + 1) % pageCount == 1 ? studentInfoH : 0;
      let item = {
        pageIndex: lastPageIndex + index + 1,
        rectHeight: h,
        rectWidth: mainWidth,
        qValue: questionData.qValue,
        ysRectHeight: h,
        questionFUIData: [],
        bigQuestionIndex: questionFUIData[lastItemIndex].bigQuestionIndex,
        type: questionData.type,
        title: questionData.title,
        isShowTitle: false,
        rowExCount: rowExCount,
        rectTop: rectT,
        ysRectTop: rectT,
      };
      //如果是解答题
      if (questionData.type == 3) {
        item.isShowBorderTop = true;
      }
      if (index == 0) {
        item.qValue = questionData.qValue ? questionData.qValue : "";
      }
      questionFUIData.push(item);
    });
  } else {
    //不跨页
    questionFUIData[lastItemIndex].ysRectHeight += changeH;
    questionFUIData[lastItemIndex].rectHeight =
      questionFUIData[lastItemIndex].ysRectHeight;
    questionFUIData[lastItemIndex].qValue = questionData.qValue
      ? questionData.qValue
      : "";
  }
  console.log(questionData, "questionData--=uichange");
  // console.log("cutQeustion-0uiheightchange")

  return questionData;
}
//通过当前页码，当前题高度 ， 返回分割后的所有高度

/**
 * @param {*} pageIndex
 * @param {*} cutHeight
 * @param {*} pageInfo
 * @param {*} qType  1:单选  2:填空 3：问答  4:作文英 5:作文汉
 * @returns
 */
function getChangeHeightCutItem(pageIndex, cutHeight, pageInfo, qType) {
  // let { mainHeight, pageCount, studentInfoH } = pageInfo;
  let cutHeightArr = [];
  // let minH = qType == 5 ? 9 : 0;
  function cut(pIndex, h) {
    let flag = false;
    if(h<9){
      flag = true;
    }
    // if (qType == 5 && h < 9) {
    //   flag = true;
    // } else if (qType == 3 && h < 9) {
    //   flag = true;
    // }else if (qType == 4 && h < 9) {
    //   flag = true;
    // }
    if (flag) return;
    let currentPageH =  getPageResiduetHeight(pageInfo, pIndex);
      // pIndex % pageCount == 1 || pageCount == 1
      //   ? mainHeight - studentInfoH
      //   : 256;
    let residueH = h - currentPageH;
    if (residueH > 0) {
      cutHeightArr.push(currentPageH);
      cut(pIndex + 1, residueH);
    } else {
      cutHeightArr.push(h);
    }
  }
  cut(pageIndex, cutHeight);
  return cutHeightArr;
}

//解答题新增/编辑
export function cutAnwserQuestionAdd(
  questionData,
  pageInfo,
  pageQuestionForUIData,
  pageQuestionList,
  isEdit
) {
  let { mainWidth, mainHeight, studentInfoH, pageCount } = pageInfo; //页面信息
  console.log("打印解答题处理前的数据",questionData);
  //初始化所有定位
  let { questionList } = questionData;
  // let lastItemIndex = questionForUIData[questionForUIData.length-1];
  let nowPageNum = 1;
  let residuoRectHeight =getPageResiduetHeight(pageInfo, nowPageNum);

  //是否用旧值
  let isUseOldValue = isEdit && !questionData.isOver;
  let questionFUIData = [],
    sumScore = 0;
  let minQuestionHeight = 23; //最小高度  标题高+上内边距+下内编剧+一行的高度
  let isNoPage = true;
  let eqQuestionIndex = 1; //小题号
  //大题号
  let bigQuestionIndex = 1;
  if (pageQuestionList.length > 0) {
    bigQuestionIndex = questionData.bigQuestionIndex
      ? questionData.bigQuestionIndex
      : pageQuestionList[pageQuestionList.length - 1].bigQuestionIndex + 1;
    eqQuestionIndex =
      pageQuestionList[pageQuestionList.length - 1].eqQuestionIndex + 1;
  }
  questionList.forEach((it) => {
    sumScore += Number(it.score);
  });
  let fullTitleName = questionData.title + (!isEdit?"（" + sumScore + "分）":'');
  // if(isEdit){
  //   let t1 =  questionData.title.split("（")[0]
  //   fullTitleName =
  // }else{
  //   fullTitleName = 
  // }
  for (let i = 0; i < pageQuestionForUIData.length; i++) {
    let item = pageQuestionForUIData[i];
    let nowPageH = getPageResiduetHeight(pageInfo,i+1),
      itemH = 0;

    item.questionForUIData.forEach((it) => {
      let h = it.rectHeight;
      itemH += h;
    });
    let residueH = nowPageH - itemH;
    if ((residueH > minQuestionHeight) && (!pageQuestionForUIData[i+1] || pageQuestionForUIData[i+1].questionForUIData.length == 0)) {
      isNoPage = false;
      nowPageNum = item.pageIndex;
      residuoRectHeight = residueH;
      break;
    }else{
      nowPageNum=item.pageIndex + 1;
    }
  }

  //新页面
  if (isNoPage) {
    nowPageNum = pageQuestionForUIData.length + 1;
    residuoRectHeight =getPageResiduetHeight(pageInfo, nowPageNum);
  }
  questionList.map((ysItem, index) => {
    let item = { ...ysItem };
    //旧的值
    let oldQValueArr = [];
    if (isUseOldValue) {
      let vKeys = Object.keys(ysItem).filter(item=>item.indexOf("qValue_")>-1)
      vKeys.map((it)=>{
        oldQValueArr.push(ysItem[it]);
      })
      // questionData.questionFUIData.forEach((item) => {
      //     if(!oldQValueArr[item.id]){
      //       oldQValueArr[item.id] = []
      //     }
      //     oldQValueArr[item.id].push(item.qValue)
      // });
    
    }
    item.sumScore = sumScore;
    item.fullTitleName = fullTitleName;
    if (index == 0) {
      item.isFirst = true;
      item.rectHeight = Number(item.rectHeight) + 7;

      item.isShowTitle = true;
      item.isShowBorderTop = true;
    } else {
      item.rectHeight = Number(item.rectHeight);
      item.isShowTitle = false;
      item.isShowBorderTop = false;
    }
    console.log("打印item",item);
    //小题内容
    if (isUseOldValue) {
      item.qValue = oldQValueArr[0]?oldQValueArr[0]:"";
      
    } else {
      ysItem.qValue_0 =
        item.qValue = `<p>${item.name}.(${item.score}分)</p>`;
    }
    item.rectLeft = 0;
    item.qIndex = 0; //当前ui在当前题中排序
    //高度大于当前页剩余高度时换页
    if (item.rectHeight > residuoRectHeight+9) {
      let isFirstPageFull = false;
      if(residuoRectHeight>9){
        item.ysRectTop = item.rectTop = mainHeight - residuoRectHeight;
        item.pageIndex = nowPageNum;
        item.bigQuestionIndex = bigQuestionIndex;
        item.type = questionData.type;
        item.title = questionData.title;
        item.rectWidth = mainWidth;
        item.itemIndex = index;
        item.showChooseType = questionData.showChooseType;
        item.chooseType = questionData.chooseType;
        item.sumScore=questionData.questionScore;
        item.score = questionData.questionScore;
        questionFUIData.push(item);
      }else{
        residuoRectHeight = 0;
        isFirstPageFull = true;
        questionFUIData[questionFUIData.length-1].rectHeight+=residuoRectHeight;
      }
      //当前页
      let syHeight = item.rectHeight - residuoRectHeight;
      item.rectHeight = residuoRectHeight;
      item.ysRectHeight = residuoRectHeight;
      let heightArr = getChangeHeightCutItem(
        nowPageNum + 1,
        syHeight,
        pageInfo,
        3
      );
      heightArr.forEach((it, idx) => {
        // 当前页码
        nowPageNum++;
        let qIndex = isFirstPageFull?idx:idx+1;
        let rectT = mainHeight - getPageResiduetHeight(pageInfo,nowPageNum);
        let cutItem = {
          rectTop: rectT,
          ysRectTop: rectT,
          showChooseType:questionData.showChooseType,
          chooseType:questionData.chooseType,
          pageIndex: nowPageNum,
          bigQuestionIndex,
          eqQuestionIndex,
          qIndex,
          eqDisplayIndex: item.eqDisplayIndex,
          type: questionData.type,
          title: questionData.title,
          isShowTitle: false,
          rectWidth: mainWidth,
          itemIndex: index,
          sumScore:questionData.questionScore,
          score:questionData.questionScore,
          rectHeight: it,
          ysRectHeight: it,
          isShowBorderTop: true,
        };
        if(isUseOldValue){
          cutItem.qValue = oldQValueArr[qIndex]? oldQValueArr[qIndex]:'';
        }else{
          cutItem.qValue = ysItem[`qValue_${qIndex}`] = item.qValue;
        }
        questionFUIData.push(cutItem);
        //剩余高度 = 当前页高度-当前题高度
        let nowPaageH = getPageResiduetHeight(pageInfo, nowPageNum);
        residuoRectHeight = nowPaageH - it;
      });
    } else {
      if(item.rectHeight > residuoRectHeight){
        //剩余高度>0 小于 9
        item.rectHeight = residuoRectHeight;
      }
      item.ysRectTop = item.rectTop = mainHeight - residuoRectHeight;
      item.rectLeft = 0;
      item.pageIndex = nowPageNum;
      item.bigQuestionIndex = bigQuestionIndex;
      item.eqQuestionIndex = eqQuestionIndex;
      item.type = questionData.type;
      item.title = questionData.title;
      item.rectWidth = mainWidth;
      item.ysRectHeight = item.rectHeight;
      item.itemIndex = index;
      residuoRectHeight -= item.rectHeight;
      item.showChooseType = questionData.showChooseType;
      item.chooseType = questionData.chooseType;
      item.sumScore=questionData.questionScore;
      item.score = questionData.questionScore;
      questionFUIData.push(item);
    }
    if (questionList.length > 1) {
      eqQuestionIndex++;
    }

    //如果是新增/覆盖 则使用新的否则使用旧的qValue

    // if (isEdit && !questionData.isOver) {
    //   questionFUIData.map((it) => {
    //     it.
    //   });
    // }
  });

  let lastItem = questionFUIData[questionFUIData.length - 1];
  let questionItem = {
    title: questionData.title,
    questionScore: questionData.questionScore,
    questionNum: questionData.questionNum,
    showChooseType: questionData.showChooseType,
    chooseType: questionData.chooseType,
    type: questionData.type,
    questionList: questionData.questionList,
    sumScore: sumScore,
    questionFUIData: questionFUIData,
    bigQuestionIndex: bigQuestionIndex,
    lastPageIndex: lastItem.pageIndex,
    lastRectTop: lastItem.rectTop,
    lastRectHeight: lastItem.rectHeight,
    lastQuesIndex: questionFUIData.length - 1,
  };
  console.log("解答题处理后的数据",questionItem);
  return questionItem;
}
// 作文英
export function cutArticleEnData(
  questionData,
  pageInfo,
  pageQuestionForUIData,
  isEdit
) {
  let { mainWidth, mainHeight, studentInfoH, pageCount } = pageInfo; //页面信息
  let { isToBottom, questionForUIData } = pageQuestionForUIData;
  //初始化所有定位
  let { questionList } = questionData;
  // let lastItemIndex = questionForUIData[questionForUIData.length-1];
  let nowPageNum = 1;
  let residuoRectHeight =getPageResiduetHeight(pageInfo,nowPageNum);
  let questionFUIData = [],
    sumScore = 0;
  let minQuestionHeight = 15; //最小高度  标题高+上内边距+下内编剧+一行的高度
  let isNoPage = true;
  //大题号
  let bigQuestionIndex = 1;
  for (let i = 0; i < pageQuestionForUIData.length; i++) {
    let item = pageQuestionForUIData[i];
    let nowPageH = getPageResiduetHeight(pageInfo,i+1),
      itemH = 0;

    item.questionForUIData.forEach((it) => {
      let h = it.rectHeight;
      itemH += h;
      bigQuestionIndex = it.bigQuestionIndex + 1;
    });
    let residueH = nowPageH - itemH;
    if ((residueH > minQuestionHeight) && (!pageQuestionForUIData[i+1] || pageQuestionForUIData[i+1].questionForUIData.length == 0)) {
      isNoPage = false;
      nowPageNum = item.pageIndex;
      residuoRectHeight = residueH;
      break;
    }
  }
  //如果编辑 大题Id不变
  bigQuestionIndex = questionData.bigQuestionIndex
    ? questionData.bigQuestionIndex
    : bigQuestionIndex;
  //新页面
  if (isNoPage) {
    nowPageNum = pageQuestionForUIData.length + 1;
    residuoRectHeight = getPageResiduetHeight(pageInfo,nowPageNum)
    
  }
  //总分
  sumScore = questionData.questionScore;
  let fullTitleName = questionData.title + (!isEdit ? ("（" + sumScore + "分）" ): '');
  let item = JSON.parse(JSON.stringify(questionData));
  item.sumScore = sumScore;
  item.fullTitleName = fullTitleName;
  item.isFirst = true;
  item.isShowTitle = true;
  item.rectLeft = 0;
  //高度大于当前页剩余高度时换页
  if (item.rectHeight > residuoRectHeight) {
    item.rectTop = item.ysRectTop = mainHeight - residuoRectHeight;
    item.pageIndex = nowPageNum;
    item.bigQuestionIndex = bigQuestionIndex;
    item.type = questionData.type;
    item.title = questionData.title;
    item.rectWidth = mainWidth;
    item.itemIndex = 0;
    item.showChooseType = questionData.showChooseType;
    item.chooseType = questionData.chooseType;
    item.isShowTitle = true;
    item.isShowBorderTop = true;
    questionFUIData.push(item);

    //当前页
    let syHeight = item.rectHeight - residuoRectHeight;
    item.rectHeight = residuoRectHeight;
    item.ysRectHeight = residuoRectHeight;

    let heightArr = getChangeHeightCutItem(nowPageNum + 1, syHeight, pageInfo);
    heightArr.forEach((it, idx) => {
      let rectT =mainHeight - getPageResiduetHeight(pageInfo,nowPageNum + 1 + idx);
      questionFUIData.push({
        rectTop: rectT,
        ysRectTop: rectT,
        pageIndex: nowPageNum + 1 + idx,
        bigQuestionIndex,
        lineHeight:questionData.lineHeight,
        showChooseType: questionData.showChooseType,
        chooseType: questionData.chooseType,
        type: questionData.type,
        title: questionData.title,
        isShowTitle: false,
        rectWidth: mainWidth,
        itemIndex: 0,
        questionList: questionData.questionList,
        rectHeight: it,
        ysRectHeight: it,
        isShowBorderTop: true,
      });
    });
  } else {
    item.rectTop = item.ysRectTop = mainHeight - residuoRectHeight;
    item.rectLeft = 0;
    item.pageIndex = nowPageNum;
    item.bigQuestionIndex = bigQuestionIndex;
    item.type = questionData.type;
    item.title = questionData.title;
    item.rectWidth = mainWidth;
    item.showChooseType = questionData.showChooseType;
    item.chooseType = questionData.chooseType;
    item.ysRectHeight = item.rectHeight;
    item.itemIndex = 0;
    item.isShowBorderTop = true;
    residuoRectHeight -= item.rectHeight;
    questionFUIData.push(item);
  }
  questionData.bigQuestionIndex = bigQuestionIndex;
  
  let lastItem = questionFUIData[questionFUIData.length - 1];
  let questionItem = {
    ...questionData,
    questionFUIData: questionFUIData,
    showChooseType: questionData.showChooseType,
    chooseType: questionData.chooseType,
    sumScore:questionData.questionList[0].score,
    bigQuestionIndex: bigQuestionIndex,
    lastPageIndex: lastItem.pageIndex,
    lastRectTop: lastItem.rectTop,
    lastRectHeight: lastItem.rectHeight,
    lastQuesIndex: questionFUIData.length - 1,
  };
  return questionItem;
}
//语文作文数据切割

export function cutArticleCnData(
  questionData,
  pageInfo,
  pageQuestionForUIData,
  isEdit
) {
  let { mainWidth, mainHeight, studentInfoH, pageCount } = pageInfo; //页面信息
  let { questionForUIData } = pageQuestionForUIData;

  //初始化所有定位
  let { questionList } = questionData;
  // let lastItemIndex = questionForUIData[questionForUIData.length-1];
  let nowPageNum = 1;
  let residuoRectHeight =getPageResiduetHeight(pageInfo, nowPageNum);
  let questionFUIData = [];
  let minQuestionHeight = 15; //最小高度  标题高+上内边距+下内编剧+一行的高度
  let isNoPage = true;
  let { fontNumber, cellSize, title, questionScore } = questionData;

  let cellMargin = 2; //上下间距
  let nowNum = 0;
  //大题号
  let bigQuestionIndex = 1;
  for (let i = 0; i < pageQuestionForUIData.length; i++) {
    let item = pageQuestionForUIData[i];
    let nowPageH = getPageResiduetHeight(pageInfo,i+1),
      itemH = 0;

    item.questionForUIData.forEach((it) => {
      let h = it.rectHeight;
      itemH += h;
    });
    let residueH = nowPageH - itemH;
    if (residueH > minQuestionHeight) {
      isNoPage = false;
      nowPageNum = item.pageIndex;
      residuoRectHeight = residueH;
      break;
    }
  }
  //如果编辑 大题Id不变
  bigQuestionIndex = questionData.bigQuestionIndex;
  //新页面
  if (isNoPage) {
    nowPageNum = pageQuestionForUIData.length + 1;
    residuoRectHeight =getPageResiduetHeight(pageInfo, nowPageNum);
  }
  let fullTitleName = questionData.title + (!isEdit ? "（" + questionScore + "分）" : '');
  questionList.forEach((item, index) => {
    if (index == 0) {
      item.isFirst = true;
      item.isShowTitle = true;
      item.introForEditor  = questionData.introForEditor;
    }
    item.rectLeft = 0;
    //高度大于当前页剩余高度时换页
    if (item.rectHeight > residuoRectHeight) {
      item.rectTop = item.ysRectTop = mainHeight - residuoRectHeight;
      item.pageIndex = nowPageNum;
      item.bigQuestionIndex = bigQuestionIndex;
      item.type = questionData.type;
      item.title = questionData.title;
      item.fullTitleName = fullTitleName;
      item.showChooseType = questionData.showChooseType;
      item.chooseType = questionData.chooseType;
      item.rectWidth = mainWidth;
      item.itemIndex = index;
      item.isShowTitle = true;
      item.isShowBorderTop = true;
      
      let cellArr = [],
        key = 0,
        nowRowNum = 0,
        rowAllNum = parseInt((residuoRectHeight - 7 - 20 - 4) / (cellSize + 2))+2, //当前页多少行  增加了2行
        rowCellNumber = parseInt((mainWidth - 4) / cellSize); //每行多少个
      let leftPadding = (mainWidth - rowCellNumber * cellSize) / 2;
      //一共几个
      let nowPageCellNum = rowCellNumber * rowAllNum;

      for (let i = 0; i < nowPageCellNum; i++) {
        key = parseInt(i / rowCellNumber);
        nowRowNum = i % rowCellNumber;
        nowNum += 1;
        let item = {
          x: leftPadding + cellSize * nowRowNum,
          y: key * (cellSize + cellMargin),
          indexTxt: nowNum % 100 == 0 ? nowNum : "",
          isRowEnd:
            nowRowNum == rowCellNumber - 1 || i == fontNumber - 1
              ? true
              : false,
        };
        cellArr.push(item);
      }
      item.cellList = cellArr;
      questionFUIData.push(item);

      //当前页
      let syHeight = item.rectHeight - residuoRectHeight;
      item.rectHeight = residuoRectHeight;
      item.ysRectHeight = residuoRectHeight;

      let heightArr = getChangeHeightCutItem(
        nowPageNum + 1,
        syHeight,
        pageInfo,
        5
      );
      
      heightArr.forEach((it, idx) => {
        let rectT = mainHeight - getPageResiduetHeight(pageInfo,nowPageNum + idx + 1)
        //   (nowPageNum + idx + 1) % pageCount == 1 || pageCount == 1
        //     ? studentInfoH
        //     : 0;
        let cellArr = [],
          key = 0,
          nowRowNum = 0,
          rowAllNum = parseInt((it - 2 - 4.5) / (cellSize + 2)), //当前页多少行
          rowCellNumber = parseInt((mainWidth - 4) / cellSize); //每行多少个
        let leftPadding = (mainWidth - rowCellNumber * cellSize) / 2;
        //一共几个
        let nowPageCellNum = rowCellNumber * rowAllNum;

        for (let i = 0; i < nowPageCellNum; i++) {
          key = parseInt(i / rowCellNumber);
          nowRowNum = i % rowCellNumber;
          nowNum += 1;
          let item = {
            x: leftPadding + cellSize * nowRowNum,
            y: key * (cellSize + cellMargin),
            indexTxt: nowNum % 100 == 0 ? nowNum : "",
            isRowEnd:
              nowRowNum == rowCellNumber - 1 || i == fontNumber - 1
                ? true
                : false,
          };
          cellArr.push(item);
        }
        questionFUIData.push({
          rectTop: rectT,
          ysRectTop: rectT,
          pageIndex: nowPageNum + 1 + idx,
          bigQuestionIndex,
          type: questionData.type,
          title: questionData.title,
          fullTitleName: fullTitleName,
          showChooseType: questionData.showChooseType,
          chooseType: questionData.chooseType,
          isShowTitle: false,
          cellSize: questionData.cellSize,
          cellList: cellArr,
          rectWidth: mainWidth,
          itemIndex: index,
          rectHeight: it,
          ysRectHeight: it,
          isShowBorderTop: true,
        });
      });
    } 
    else {
      //不翻页情况

      item.rectTop = item.ysRectTop = mainHeight - residuoRectHeight;
      item.rectLeft = 0;
      item.pageIndex = nowPageNum;
      item.bigQuestionIndex = bigQuestionIndex;
      item.type = questionData.type;
      item.title = questionData.title;
      item.fullTitleName = fullTitleName;
      item.rectWidth = mainWidth;
      item.showChooseType = questionData.showChooseType;
      item.chooseType = questionData.chooseType;
      // item.rectHeight = item.rectHeight;
      item.ysRectHeight = item.rectHeight;
      item.itemIndex = index;
      item.isShowBorderTop = true;
      // residuoRectHeight -= item.rectHeight;
      let cellArr = [],
        key = 0,
        nowRowNum = 0,
        rowAllNum = parseInt((item.rectHeight - 7 - 20 - 2) / (cellSize + 2)), //当前页多少行
        rowCellNumber = parseInt((mainWidth - 4) / cellSize); //每行多少个
      let leftPadding = (mainWidth - rowCellNumber * cellSize) / 2;
      //一共几个
      let nowPageCellNum = rowCellNumber * rowAllNum;
      for (let i = 0; i < nowPageCellNum; i++) {
        key = parseInt(i / rowCellNumber);
        nowRowNum = i % rowCellNumber;
        nowNum += 1;
        let item = {
          x: leftPadding + cellSize * nowRowNum,
          y: key * (cellSize + cellMargin),
          indexTxt: nowNum % 100 == 0 ? nowNum : "",
          isRowEnd:
            nowRowNum == rowCellNumber - 1 || i == fontNumber - 1
              ? true
              : false,
        };
        cellArr.push(item);
      }
      item.cellList = cellArr;
      questionFUIData.push(item);
    }
  });
  let lastItem = questionFUIData[questionFUIData.length - 1];
  let questionItem = {
    fullTitleName,
    ...questionData,
    sumScore: questionData.questionScore,
    questionFUIData: questionFUIData,
    bigQuestionIndex: bigQuestionIndex,
    showChooseType: questionData.showChooseType,
    chooseType: questionData.chooseType,
    lastPageIndex: lastItem.pageIndex,
    lastRectTop: lastItem.rectTop,
    lastRectHeight: lastItem.rectHeight,
    lastQuesIndex: questionFUIData.length - 1,
  };
  return questionItem;
}
//非作答区数据切割
export function cutNoAnwserAreaData(
  questionData,
  pageInfo,
  pageQuestionForUIData,
  isEdit
) {
  let { mainWidth, mainHeight, studentInfoH, pageCount } = pageInfo; //页面信息
  let { isToBottom, questionForUIData } = pageQuestionForUIData;
  //初始化所有定位
  let { questionList } = questionData;
  // let lastItemIndex = questionForUIData[questionForUIData.length-1];
  let nowPageNum = 1;
  let residuoRectHeight = getPageResiduetHeight(pageInfo, nowPageNum);
  let questionFUIData = [];
  let minQuestionHeight = 15; //最小高度  标题高+上内边距+下内编剧+一行的高度
  let isNoPage = true;
  //大题号
  //如果已经存在 isEdit 为true 记录高度 分割
  for (let i = 0; i < pageQuestionForUIData.length; i++) {
    let item = pageQuestionForUIData[i];
    let nowPageH = getPageResiduetHeight(pageInfo,i+1),
      itemH = 0;

    item.questionForUIData.forEach((it) => {
      let h = it.rectHeight
      itemH += h;
    });
    let residueH = nowPageH - itemH;
    if ((residueH > minQuestionHeight) && (!pageQuestionForUIData[i+1] || pageQuestionForUIData[i+1].questionForUIData.length == 0)){
      isNoPage = false;
      nowPageNum = item.pageIndex;
      residuoRectHeight = residueH;
      break;
    }
  }
  let bigQuestionIndex = questionData.bigQuestionIndex;
  //新页面
  if (isNoPage) {
    nowPageNum = pageQuestionForUIData.length + 1;
    residuoRectHeight = getPageResiduetHeight(pageInfo, nowPageNum);
  }
  questionList.forEach((itemq, index) => {
    let item = JSON.parse(JSON.stringify(itemq));
    if (index == 0) {
      item.isFirst = true;
      item.isShowTitle = true;
    }

    item.rectLeft = 0;
    //高度大于当前页剩余高度时换页
    if (item.rectHeight > residuoRectHeight) {
      item.rectTop = mainHeight - residuoRectHeight;
      item.pageIndex = nowPageNum;
      item.bigQuestionIndex = bigQuestionIndex;
      item.type = questionData.type;
      item.rectWidth = mainWidth;
      item.itemIndex = index;
      questionFUIData.push(item);
      //当前页
      let syHeight = item.rectHeight - residuoRectHeight;
      item.rectHeight = residuoRectHeight;
      item.ysRectHeight = residuoRectHeight;

      let heightArr = getChangeHeightCutItem(
        nowPageNum + 1,
        syHeight,
        pageInfo
      );
      heightArr.forEach((it, idx) => {
        let rectT = mainHeight - getPageResiduetHeight(pageInfo, nowPageNum + idx+1);
          // (nowPageNum + idx + 1) % pageCount == 1 || pageCount == 1
          //   ? studentInfoH
          //   : 0;
        questionFUIData.push({
          rectTop: rectT,
          pageIndex: nowPageNum + 1 + idx,
          bigQuestionIndex,
          type: questionData.type,
          rectWidth: mainWidth,
          itemIndex: index,
          rectHeight: it,
          ysRectHeight: it,
          isShowBorderTop: true,
        });
      });
    } else {
      item.rectTop = mainHeight - residuoRectHeight;
      item.rectLeft = 0;
      item.pageIndex = nowPageNum;
      item.bigQuestionIndex = bigQuestionIndex;
      item.type = questionData.type;
      item.title = questionData.title;
      item.rectWidth = mainWidth;
      item.ysRectHeight = item.rectHeight;
      item.itemIndex = index;
      residuoRectHeight -= item.rectHeight;
      questionFUIData.push(item);
    }
  });

  let lastItem = questionFUIData[questionFUIData.length - 1];
  let questionItem = {
    type: questionData.type,
    questionList: questionData.questionList,
    questionFUIData: questionFUIData,
    bigQuestionIndex: bigQuestionIndex,
    lastPageIndex: lastItem.pageIndex,
    lastRectTop: lastItem.rectTop,
    lastRectHeight: lastItem.rectHeight,
    lastQuesIndex: questionFUIData.length - 1,
  };

  return questionItem;
}

//填空题的内容区生成 网阅卡
export function makeFillBlankContent(questionFUIData, rectWidth, rowExCount) {
  // console.log(this.fillBlankItemData,'this.fillBlankItemData');
  let arr = [];

  let blankLineCell = ""; //12px  //计算
  let rowHasBlankNum = parseInt((115 / 190) * rectWidth); // 190 宽 112个空白符  190/112  每行多少个半角空白符号
  // 每个题空格个数
  let rowCount = parseInt((rowHasBlankNum - rowExCount * 5) / rowExCount);
  for (let i = 0; i < rowCount; i++) {
    blankLineCell += "<u>&ensp;</u>";
  }
  // console.log(blankLineCell);
  questionFUIData.forEach((item, index) => {
    let key = parseInt(index / rowExCount);
    let indexB = index % rowExCount;
    let exNumberTxt =
      (indexB == 0 ? "" : "&ensp;&ensp;") +
      String(item.name) +
      (String(item.name).length > 1 ? "&ensp;" : "&ensp;&ensp;");

    let it = `<span style="font-family: 宋体, STSong, SimSun;height: 10mm;display:inline-block;">
                <span style="font-size: 12px; text-align: center;  margin-top: 5mm; height: 10mm;">${exNumberTxt}</span><u style="height: 9.5mm; border-bottom: 0.1mm solid black;">${blankLineCell}</u></span>`;
    if (!arr[key]) {
      arr[key] = "";
    }
    arr[key] += it;
  });
  console.log(arr, "arr");
  let str = "";
  let conH = 10;
  arr.map((item) => {
    conH += 9;
    let it = `<p>${item}</p>`;
    str += it;
  });

  return str;
}
export function makeFillBlankContents(questionFUIData, rectWidth, groupNumber) {

  console.log("questionFUIData",questionFUIData,rectWidth,groupNumber);
  let questionNumber=questionFUIData.length;
  let totalWidth=718;
  let startNumber=getBigQuestionIndex(questionFUIData);
  
  console.log(startNumber)
  // console.log(this.fillBlankItemData,'this.fillBlankItemData');
  // 计算每行填空题的宽度
  const fillWidth=7;//一个空格的宽度 大概 7.46  默认7px吧
  let fillBankWidth = ((totalWidth - 40)/groupNumber) - 15 - 15; // 40 两边边距的宽度 15 为小题占用的宽度 15为小题之间间隔的距离
  let count = parseInt(fillBankWidth / fillWidth); // 计算一个填空题&nbsp;的数量
  let content = '';
  let questionList = GetQuestionList(groupNumber,questionNumber,startNumber);
  questionList.forEach(row => {
    let rowStr = '<p style="text-align: left;">';//编辑器内容靠左
    row.list.forEach(item => {
      let fillBankArr = item + '.<u>';//题号
      for (let i = 0; i < count; i++) {
        fillBankArr += '&nbsp;';
      }
      fillBankArr += '</u>';
      rowStr += fillBankArr + '&nbsp;&nbsp;';
    });
    rowStr += '</p>';
    content += rowStr;
  });
  return content;//返回填空题的内容
}
 //获取根据每行显示个数 groupNumber  填空题的数量 questionNumber,起始题号startNumber
 function GetQuestionList(groupNumber,questionNumber,startNumber) {
  let questionList = [];
  let row=Math.ceil(questionNumber / groupNumber);//行数
  for (let i = 0; i < row; i++) 
  {
    let list=[];
    let endNumber=startNumber+questionNumber;
    for(let j=0;j<groupNumber;j++)
    {
      let num=i*groupNumber+j+startNumber;
      if(num<endNumber)
      {
        list.push(num);
      }
    }
    questionList.push({
      row: i + 1,
      list: list
    });
  }
  return questionList;
}



//纸阅卡 填空题
function cutFillBlankQuestionsForPaper(
  questionData,
  pageInfo,
  pageQuestionForUIData,
  isEdit
){
  // 如果不是新增 则记录原来高度
  let ysRectHeight = 0;
  if(isEdit){
    ysRectHeight = questionData.rectHeight;
  }

  let { mainWidth, mainHeight, studentInfoH, pageCount } = pageInfo; //页面信息
  //初始化所有定位
  let { rowExCount, questionList, rowHeight,isOver,bigQuestionIndex} = questionData;
  let nowPageNum = 1;
  let residuoRectHeight = getPageResiduetHeight(pageInfo,nowPageNum);
  let questionFUIData = [],
    sumScore = 0;
  let minQuestionHeight = 10 + 2 * 2 + rowHeight; //最小高度  标题高+上内边距+下内编剧+一行的高度
  let isNoPage = true;
  for (let i = 0; i < pageQuestionForUIData.length; i++) {
    let item = pageQuestionForUIData[i];
    let nowPageH = getPageResiduetHeight(pageInfo,i+1),
      itemH = 0;

    item.questionForUIData.forEach((it) => {
      itemH += it.rectHeight;
    });
    let residueH = nowPageH - itemH;
    if (residueH > minQuestionHeight) {
      isNoPage = false;
      nowPageNum = item.pageIndex;
      residuoRectHeight = residueH;
      break;
    }
  }
  //新页面
  if (isNoPage) {
    nowPageNum = pageQuestionForUIData.length + 1;
    residuoRectHeight =getPageResiduetHeight(pageInfo, nowPageNum);
  }
  let pageNum = nowPageNum; //当前页码
  let itemPaddingTop = 4,
    itemPaddingLeft = 2,
    isFirst = true,
    itemWidth = (mainWidth - itemPaddingLeft * 2 - 2*rowExCount) / rowExCount, //页面看度-俩边间距 % 每行个数
    nowRowWidth = 0, //当前行小题占用了的宽度
    nowRowNum = 0; //行数 第几行了
    // residuoRectHeight = residuoRectHeight-7-4*2;//内容区最高高度  当前页剩余高度-标题高度-上间距-下间距
    // let pageRectHArr = []; //未来使用页面的初始高度 做多3页

  questionList.forEach((item) => {
    sumScore += Number(item.score);
  });
  let fullTitleName = questionData.title + (!isEdit ? ("（" + sumScore + "分）" ): '');
  questionList.map((item, index) => {
    //用到原来的宽度
      
    if(isEdit && !isOver){
      item.rectWidth = item.rectWidth?(item.rectWidth>(mainWidth-6)?(mainWidth-6):item.rectWidth):itemWidth;
      item.rectHeight = item.rectHeight?item.rectHeight:rowHeight;
      //换行
      if((mainWidth - nowRowWidth)<item.rectWidth){
        nowRowNum++;
        nowRowWidth=0;
        residuoRectHeight -= item.rectHeight;
      }
      //换页
      //当前页剩余高度 
      if(residuoRectHeight< item.rectHeight+4+7){
        pageNum++;
        nowRowWidth=0;
        nowRowNum = 0;//当前页当前行归零
        isFirst = false;
        //新页面剩余高度
        residuoRectHeight = getPageResiduetHeight(pageInfo, pageNum);
      }
      item.x = itemPaddingLeft +nowRowWidth;
      item.y = item.rectHeight*nowRowNum + itemPaddingTop;
      item.pageNum = pageNum;
      
      
    }else{
      //新增
      //占用的宽
      item.rectWidth = itemWidth;
      item.rectHeight = rowHeight;
      //换行
      if((mainWidth - nowRowWidth)<itemWidth){
        nowRowNum++;
        nowRowWidth=0;
        residuoRectHeight -= item.rectHeight;
      }
      //跨页
      //当前行数*行高 > 当前页剩余高度
      if(residuoRectHeight< item.rectHeight+4+7){
        pageNum++;
        nowRowWidth=0;
        nowRowNum = 0;//当前页当前行归零
        //新页面剩余高度
        residuoRectHeight = getPageResiduetHeight(pageInfo, pageNum);
        //标题不显示
        isFirst = false;
      }
      item.x = itemPaddingLeft +nowRowWidth;
      item.y = rowHeight*nowRowNum + itemPaddingTop;
      item.pageIndex = pageNum;
      
    }
    item.fullTitleName = fullTitleName;
    item.isOver = isOver;
    item.updateNum = item.id;
    if (!questionFUIData[pageNum - 1]) {
      //之前没有
      questionFUIData[pageNum - 1] = {
        questionFUIData: [],
        bigQuestionIndex: bigQuestionIndex,
        type: 2,
        isOver,
        title: questionData.title,
        fullTitleName,
        isShowTitle: isFirst,
        rowExCount: rowExCount,
        pageIndex: pageNum,
        rectWidth: mainWidth,
        rectTop: mainHeight - residuoRectHeight,
        ysRectTop: mainHeight - residuoRectHeight,
        score: item.score,
      };
    }
    //换行 计算题高度
    //当占用宽度为0时 换行时 计算高度
    if (nowRowWidth == 0) {
      questionFUIData[pageNum - 1].ysRectHeight = questionFUIData[
        pageNum - 1
      ].rectHeight = 8 + (nowRowNum+1) * rowHeight + (isFirst ? 7 : 0);
    }
    //当前行占用宽度
    nowRowWidth = item.x+item.rectWidth;
    questionFUIData[pageNum - 1].questionFUIData.push(item);
  });
  // 计算题总高度
  let qRectH = questionFUIData.reduce(
    (accumulator, item) => accumulator + item.rectHeight,
    0
  );
  
  questionFUIData = questionFUIData.filter((item) => item != "");
  let lastItem = questionFUIData[questionFUIData.length - 1];
  
  let questionItem = {
    title: questionData.title,
    questionScore: questionData.questionScore,
    rowExCount: rowExCount,
    rowHeight: rowHeight,
    rectHeight: qRectH,
    fullTitleName,
    listItemNum: questionData.listItemNum,
    optionsDirection: questionData.optionsDirection,
    questionNum: questionData.questionNum,
    type: questionData.type,
    questionList: questionData.questionList,
    sumScore: sumScore,
    questionFUIData: questionFUIData,
    rectWidth: mainWidth,
    bigQuestionIndex: bigQuestionIndex,
    lyColHeight: questionData.lyColHeight,
    lastPageIndex: lastItem.pageIndex,
    lastRectTop: lastItem.rectTop,
    lastRectHeight: lastItem.rectHeight,
    lastQuesIndex: questionFUIData.length - 1,
    isToBottom: questionFUIData.length > 1,
  };
  return questionItem;
}