
import {fixNumber,mmToPx} from "@/utils/common";
//无密封线页面基础信息
const pageSettingData = {
  "A4":{
    stuNumPosiMaxX:171.3, //学号最右时x坐标
  },
  "A32":{
    stuNumPosiMaxX:196.3, //学号最右时x坐标
    leftColumnWidth: 0, //左侧栏宽度
  },
  "A33":{
    stuNumPosiMaxX:0, //学号最右时x坐标 暂无
  }
}
//有密封线页面基础信息
const pageHasSafeLineData = {
  "A4":{
    stuNumPosiMaxX:171.3, //学号最右时x坐标
  },
  "A32":{
    stuNumPosiMaxX:196.3, //学号最右时x坐标
  },
  "A33":{
    stuNumPosiMaxX:0, //学号最右时x坐标 暂无
  }
}

//工具函数 A32 纸张转换坐标
/*
 * @param {*} x
 * @param {*} pageIndex 当前栏
 */
function getX(x,pageIndex){
  //如果是A32
  let maxX = pageSettingData[pageInfo.styType].stuNumPosiMaxX;
  return fixNumber(maxX - x);
}
export function transferDataToTemplate(cardData){
    console.log("cardData",cardData);
    //如果是黑块/俩栏/双页打印 
    // 定位点喝页码的坐标 单位 mm
    let {pageInfo,pageQuestionForUIData} = cardData;
    let templateData = {}, pageItem = {};
    pageItem.object_name = "",
    pageItem.upload_name = "",
    pageItem.cardType = pageInfo.styType == 'A4'?1:2;
    pageQuestionForUIData.forEach((item,pageIndex)=>{
      //定位点和页码定位信息
      
      //如果偶数页面并且是A32布局 左侧页面宽度为
      let leftPageW = 0,
      leftAndWidth = 0; //右侧x坐标需要加左侧宽度
      let isEvenPage = (item.pageIndex % 2 == 0 && pageInfo.styType == "A32") ? true : false; //是否A32试卷第二页
      if(isEvenPage){
        leftPageW = pageQuestionForUIData[pageIndex-1].rectWidth;
        leftAndWidth = leftPageW - 8.7 + item.mainRectLeft;
      }
      if(item.pageNum == 1){
        if(!pageItem.position){
          pageItem.position = {
            "width": 5.418,
            "height": 3.55,
            "page":0,
            "setpoint":[],
            "page_start_x":78.3, 
            "page_start_y": 273,
            "page_width":3.5,
            "blank_width":2,
            "left_block_width":2,
            "left_block_height":4,
          };
        }
          // 四个黑块先去掉
        // if(item.QRData1){
          
        //   //四个角坐标 定位区
        //   pageItem.position.setpoint[0] = {
        //     x:item.QRData1.rectLeft,
        //     y:8,
        //   };
        // }
      
        // // 定位点3
        // if(item.QRData2){
        //   pageItem.position.setpoint[2]= {
        //     x:item.QRData2.rectLeft,
        //     y:284,
        //   };
        // }
        
        
        // // 定位点2
        // if(item.QRData3){
        //   pageItem.position.setpoint[1]= {
        //     x:item.QRData3.rectLeft + leftPageW,
        //     y:8,
        //   };
        // }
        // // 定位点4
        // if(item.QRData4){
        //   pageItem.position.setpoint[3]= {
        //     x:item.QRData4.rectLeft + leftPageW,
        //     y:284,
        //   };
        // }
        //如果双面印刷
        
        //卡号填涂区 条形码/数字 考号区
        //1:贴条码2:涂卡号
        
        let stuMaxX = (Number(item.mainRectLeft)+ Number(item.mainWidth)-8.7)
        
        if(pageInfo.idCardType == 1&&item.pageIndex == 1){
          pageItem.barcode={
            "set_barcode":"",
            "start_x":fixNumber(stuMaxX-64), //132.3
            "start_y":24.8,
            "width":64,
            "height":28
          }
          //缺考
          pageItem.quekao = {
            "width":4,
            "height":2.7,
            "start_x":stuMaxX-9.45,  //stuMaxX-9.6
            "start_y":57.3
          }
        }
        if(pageInfo.idCardType == 2&&item.pageIndex == 1){
          //缺考
          pageItem.quekao = {
            "width":4,
            "height":2.7,
            "start_x":108.5, 
            "start_y":70.75
          }
          pageItem.kaohao={
            "width":8*pageInfo.idCardNumberLen,
            "height":58.5,
            "num":pageInfo.idCardNumberLen,
            "set_kaohao":"",
            "start_x": fixNumber(stuMaxX-8*pageInfo.idCardNumberLen),
            "start_y": 17.8
          }
        }
      }
      
      //题
      let {questionForUIData} = item;
      
      //选择题
      if(questionForUIData && questionForUIData.length > 0){
        console.log("questionForUIData",questionForUIData);
        questionForUIData.forEach(question=>{
          if(question.type == 1){
            console.log("question",question);
            console.log("item.mainRectLeft",item);
            console.log("question",question);
            console.log("fixNumber(isEvenPage?leftAndWidth:(item.mainRectLeft-8.7)",fixNumber(isEvenPage?leftAndWidth:(item.mainRectLeft-8.7)));
            console.log('fixNumber方法前数据',isEvenPage?leftAndWidth:(item.mainRectLeft-8.7));

            let qDataItem = {
              "name":question.bigQuestionIndex, 
              "type":1,    
              "position":{
                "x":fixNumber(isEvenPage?leftAndWidth:(item.mainRectLeft-8.7)), //在第二栏时 8.7mm 是第一个黑块到左侧边缘的距离
                "y":fixNumber(question.rectTop + 18), //rectTop  已经包含（58.5+7）58.5mm  准考证号高度  +7mm选择题标题高度  +18mm  答题卡总标题的高度  后面的8是顶部黑块到顶部的高度
                // "y":fixNumber(72.6-8), //选择题区域上边框距离顶部72.6第一个黑块距离顶部8mm
                // "w":Number(item.mainWidth),
                w:Number(item.mainWidth),//宽度
                "h":question.rectHeight - (question.isShowTitle?7:0)
              },
              "page":item.pageNum,
              "width":3.97,
              "height":3.17,
              "blank_x":2.63,
              "blank_y":3.33,
              "group":[]
            };
            let qGroupItem = {};
            let groupNum = question.listItemNum;
            question.questionFUIData.forEach((qItem,index)=>{
              let itemIdx = index%groupNum;//当前组索引;
              //每组第一个时
              if(index%groupNum == 0){
                qGroupItem = {
                  "name":`group_${index/groupNum}`, //组名称
                  "title":[],
                  "sign":[],//选项类型 0:单选 1:多选
                  "num":[],
                  "start_x":qItem.x+9,
                  "start_y":fixNumber(qItem.y + 0.915)
                }
              }
              qGroupItem.title[itemIdx]=qItem.eqDisplayIndex;
              qGroupItem.sign[itemIdx]=qItem.questiontype==2?1:0;
              qGroupItem.num[itemIdx]=qItem.selNumber;
              if((itemIdx == (groupNum-1)) || index == (question.questionFUIData.length-1)){
                qDataItem.group.push(qGroupItem)
              }
            })
            if(!pageItem.qustionData){
              pageItem.qustionData = [];
            }
            console.log("打印qDataItem",qDataItem);
            pageItem.qustionData.push(qDataItem);
          }else if(question.showChooseType && question.isShowTitle){
            let chooseType = question.chooseType;
            //选做题
            let qDataItem = {
              "name":question.bigQuestionIndex,
              "bigQuestionIndex":question.bigQuestionIndex,
              "type":question.type,
              "page":item.pageNum,
              "position":[
                {
                  "index":1,
                  "start_x":getChooseAreaPosi(isEvenPage,leftAndWidth,item, chooseType == 3?2:1), //在第二栏时
                  "start_y":fixNumber(question.rectTop + 20 - 8 +2.22), 
                  "width":4.6,
                  "height":2.56
                },{
                  "index":2,
                  "start_x":getChooseAreaPosi(isEvenPage,leftAndWidth,item,chooseType == 3?1:0), //在第二栏时
                  "start_y":fixNumber(question.rectTop + 20 - 8 +2.22), 
                  "width":4.6,
                  "height":2.56
                },
                
              ]
            }
            if(chooseType == 3){
              qDataItem.position.push({
                "index":3,
                "start_x":getChooseAreaPosi(isEvenPage,leftAndWidth,item,0), //在第二栏时
                "start_y":fixNumber(question.rectTop + 20 - 8 + 2.22), 
                "width":4.6,
                "height":2.56
              })
            }
            if(!pageItem.xuanzuo){
              pageItem.xuanzuo = [];
            }
            pageItem.xuanzuo.push(qDataItem);
          }
        })
      }
    })
    
    return pageItem;
}
function getChooseAreaPosi(isEvenPage,leftAndWidth,item,num){
  
  return fixNumber(isEvenPage?leftAndWidth:(Number(item.mainRectLeft)+Number(item.mainWidth)-8.7-42.5-num*18-8.8))
}
