import { parseTime } from './ruoyi'

/**
 * 表格时间格式化
 */
export function formatDate(cellValue, type) {
  if (cellValue == null || cellValue == "") return "";
  var date = new Date(cellValue)
  var year = date.getFullYear()
  var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  var time = ''
  if( type == 1 ){
    time = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
  }else{
    time = year + '-' + month + '-' + day
  }
  return time
}

export function isEmpty(str){
  return str == undefined || str == ''
}

/**
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time, option) {
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000
  } else {
    time = +time
  }
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return (
      d.getMonth() +
      1 +
      '月' +
      d.getDate() +
      '日' +
      d.getHours() +
      '时' +
      d.getMinutes() +
      '分'
    )
  }
}

/**
 * @param {string} url
 * @returns {Object}
 */
export function getQueryObject(url) {
  url = url == null ? window.location.href : url
  const search = url.substring(url.lastIndexOf('?') + 1)
  const obj = {}
  const reg = /([^?&=]+)=([^?&=]*)/g
  search.replace(reg, (rs, $1, $2) => {
    const name = decodeURIComponent($1)
    let val = decodeURIComponent($2)
    val = String(val)
    obj[name] = val
    return rs
  })
  return obj
}

/**
 * @param {string} input value
 * @returns {number} output value
 */
export function byteLength(str) {
  // returns the byte length of an utf8 string
  let s = str.length
  for (var i = str.length - 1; i >= 0; i--) {
    const code = str.charCodeAt(i)
    if (code > 0x7f && code <= 0x7ff) s++
    else if (code > 0x7ff && code <= 0xffff) s += 2
    if (code >= 0xDC00 && code <= 0xDFFF) i--
  }
  return s
}

/**
 * @param {Array} actual
 * @returns {Array}
 */
export function cleanArray(actual) {
  const newArray = []
  for (let i = 0; i < actual.length; i++) {
    if (actual[i]) {
      newArray.push(actual[i])
    }
  }
  return newArray
}

/**
 * @param {Object} json
 * @returns {Array}
 */
export function param(json) {
  if (!json) return ''
  return cleanArray(
    Object.keys(json).map(key => {
      if (json[key] === undefined) return ''
      return encodeURIComponent(key) + '=' + encodeURIComponent(json[key])
    })
  ).join('&')
}

/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
  const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  if (!search) {
    return {}
  }
  const obj = {}
  const searchArr = search.split('&')
  searchArr.forEach(v => {
    const index = v.indexOf('=')
    if (index !== -1) {
      const name = v.substring(0, index)
      const val = v.substring(index + 1, v.length)
      obj[name] = val
    }
  })
  return obj
}

/**
 * @param {string} val
 * @returns {string}
 */
export function html2Text(val) {
  const div = document.createElement('div')
  div.innerHTML = val
  return div.textContent || div.innerText
}

/**
 * Merges two objects, giving the last one precedence
 * @param {Object} target
 * @param {(Object|Array)} source
 * @returns {Object}
 */
export function objectMerge(target, source) {
  if (typeof target !== 'object') {
    target = {}
  }
  if (Array.isArray(source)) {
    return source.slice()
  }
  Object.keys(source).forEach(property => {
    const sourceProperty = source[property]
    if (typeof sourceProperty === 'object') {
      target[property] = objectMerge(target[property], sourceProperty)
    } else {
      target[property] = sourceProperty
    }
  })
  return target
}

/**
 * @param {HTMLElement} element
 * @param {string} className
 */
export function toggleClass(element, className) {
  if (!element || !className) {
    return
  }
  let classString = element.className
  const nameIndex = classString.indexOf(className)
  if (nameIndex === -1) {
    classString += '' + className
  } else {
    classString =
      classString.substr(0, nameIndex) +
      classString.substr(nameIndex + className.length)
  }
  element.className = classString
}

/**
 * @param {string} type
 * @returns {Date}
 */
export function getTime(type) {
  if (type === 'start') {
    return new Date().getTime() - 3600 * 1000 * 24 * 90
  } else {
    return new Date(new Date().toDateString())
  }
}

/**
 * @param {Function} func
 * @param {number} wait
 * @param {boolean} immediate
 * @return {*}
 */
export function debounce(func, wait, immediate) {
  let timeout, args, context, timestamp, result

  const later = function() {
    // 据上一次触发时间间隔
    const last = +new Date() - timestamp

    // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
    if (last < wait && last > 0) {
      timeout = setTimeout(later, wait - last)
    } else {
      timeout = null
      // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
      if (!immediate) {
        result = func.apply(context, args)
        if (!timeout) context = args = null
      }
    }
  }

  return function(...args) {
    context = this
    timestamp = +new Date()
    const callNow = immediate && !timeout
    // 如果延时不存在，重新设定延时
    if (!timeout) timeout = setTimeout(later, wait)
    if (callNow) {
      result = func.apply(context, args)
      context = args = null
    }

    return result
  }
}

/**
 * This is just a simple version of deep copy
 * Has a lot of edge cases bug
 * If you want to use a perfect deep copy, use lodash's _.cloneDeep
 * @param {Object} source
 * @returns {Object}
 */
export function deepClone(source) {
  if (!source && typeof source !== 'object') {
    throw new Error('error arguments', 'deepClone')
  }
  const targetObj = source.constructor === Array ? [] : {}
  Object.keys(source).forEach(keys => {
    if (source[keys] && typeof source[keys] === 'object') {
      targetObj[keys] = deepClone(source[keys])
    } else {
      targetObj[keys] = source[keys]
    }
  })
  return targetObj
}

/**
 * @param {Array} arr
 * @returns {Array}
 */
export function uniqueArr(arr) {
  return Array.from(new Set(arr))
}

/**
 * @returns {string}
 */
export function createUniqueString() {
  const timestamp = +new Date() + ''
  const randomNum = parseInt((1 + Math.random()) * 65536) + ''
  return (+(randomNum + timestamp)).toString(32)
}

/**
 * Check if an element has a class
 * @param {HTMLElement} elm
 * @param {string} cls
 * @returns {boolean}
 */
export function hasClass(ele, cls) {
  return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'))
}

/**
 * Add class to element
 * @param {HTMLElement} elm
 * @param {string} cls
 */
export function addClass(ele, cls) {
  if (!hasClass(ele, cls)) ele.className += ' ' + cls
}

/**
 * Remove class from element
 * @param {HTMLElement} elm
 * @param {string} cls
 */
export function removeClass(ele, cls) {
  if (hasClass(ele, cls)) {
    const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)')
    ele.className = ele.className.replace(reg, ' ')
  }
}

export function makeMap(str, expectsLowerCase) {
  const map = Object.create(null)
  const list = str.split(',')
  for (let i = 0; i < list.length; i++) {
    map[list[i]] = true
  }
  return expectsLowerCase
    ? val => map[val.toLowerCase()]
    : val => map[val]
}

export const exportDefault = 'export default '

export const beautifierConf = {
  html: {
    indent_size: '2',
    indent_char: ' ',
    max_preserve_newlines: '-1',
    preserve_newlines: false,
    keep_array_indentation: false,
    break_chained_methods: false,
    indent_scripts: 'separate',
    brace_style: 'end-expand',
    space_before_conditional: true,
    unescape_strings: false,
    jslint_happy: false,
    end_with_newline: true,
    wrap_line_length: '110',
    indent_inner_html: true,
    comma_first: false,
    e4x: true,
    indent_empty_lines: true
  },
  js: {
    indent_size: '2',
    indent_char: ' ',
    max_preserve_newlines: '-1',
    preserve_newlines: false,
    keep_array_indentation: false,
    break_chained_methods: false,
    indent_scripts: 'normal',
    brace_style: 'end-expand',
    space_before_conditional: true,
    unescape_strings: false,
    jslint_happy: true,
    end_with_newline: true,
    wrap_line_length: '110',
    indent_inner_html: true,
    comma_first: false,
    e4x: true,
    indent_empty_lines: true
  }
}

// 首字母大小
export function titleCase(str) {
  return str.replace(/( |^)[a-z]/g, L => L.toUpperCase())
}

// 下划转驼峰
export function camelCase(str) {
  return str.replace(/_[a-z]/g, str1 => str1.substr(-1).toUpperCase())
}

export function isNumberStr(str) {
  return /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g.test(str)
}

export function getUrlParam(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
  var r = window.location.search.substr(1).match(reg); //匹配目标参数
  if (r != null) return unescape(r[2]);
  return null; //返回参数值
}

function addDynamicText(width, content) {
  // 创建一个临时元素来计算文本高度
  var tempDiv = document.createElement("div");
  tempDiv.style.position = "absolute";
  tempDiv.style.left = "-1000px";
  tempDiv.style.top = "-1000px";
  tempDiv.style.width = width + "mm"; // 使用与LODOP相同的单位
  tempDiv.style.fontFamily = "Arial"; // 使用与打印相同的字体
  tempDiv.style.fontSize = "14px"; // 使用与打印相同的字号
  tempDiv.style.lineHeight = "1.2"; // 使用与打印相同的行高
  tempDiv.innerHTML = content;
  
  document.body.appendChild(tempDiv);
  var height = tempDiv.offsetHeight;
  document.body.removeChild(tempDiv);
  
  // 转换为毫米（假设LODOP使用毫米为单位）
  var heightInMM = height * 0.264583; // 1px ≈ 0.264583mm
  return height
}

// 充值收据
export function exchangeClothPrinter(LODOP, data, lodopName) {
  LODOP.PRINT_INIT(`充值收据`);
  // 设置打印模板
  LODOP.SET_PRINT_PAGESIZE(3, 800, 45, '');
  // 设置打印内容
  LODOP.ADD_PRINT_TEXT(30, 95, 150, 20, `一七生活 ${data.storeName}`);
  LODOP.ADD_PRINT_TEXT(55, 135, 50, 20, '充值单');
   // 开始绘制虚线
  LODOP.ADD_PRINT_LINE(80, 10, 80, 295, 1, 1);
  LODOP.ADD_PRINT_TEXT(90, 10, 200, 20, `订单号：${data.orderNo}`);
  LODOP.ADD_PRINT_TEXT(110, 10, 300, 20, `打印日期：${data.createTime}`);
  LODOP.ADD_PRINT_LINE(130, 10, 130, 295, 1, 1);
  let type = ''
  if (data.rechargeType == '3') {
    type = '自定义'
  }
  if (data.rechargeType == '0') {
    type = '套餐'
  }
  if (data.rechargeType == '1') {
    type = '福利金'
  }
  LODOP.ADD_PRINT_TEXT(140, 10, 300, 20, `充值类型：${type}`);
  LODOP.ADD_PRINT_TEXT(160, 10, 300, 20, `充值金额：${data.rechargeAmount || 0}；赠送：${data.giveAmount || 0}；福利：${data.welfareAmount || 0}；`);
  LODOP.ADD_PRINT_TEXT(180, 10, 300, 20, `支付金额：${data.payAmount}`);
  let payType = ''
  if (data.rechargeType == '1') {
    type = `(福利)${data.welfareAmount}`
  }else if(data.payType == '0'){
    payType = '微信 '
  }else if(data.payType == '1'){
    payType = '支付宝 '
  }else if(data.payType == '2'){
    payType = '现金'
  }else if(data.payType == '9'){
     payType = `(现金)${data.cashPayAmount} `
    if (data.aliPayAmount) {
      payType += `(支付宝)${data.aliPayAmount}`
    }
    if (data.wxPayAmount) {
      payType += `(微信)${data.wxPayAmount}`
    }
  }
  LODOP.ADD_PRINT_TEXT(200, 10, 300, 20, `支付方式：${payType}`);
  LODOP.ADD_PRINT_TEXT(220, 10, 300, 20, `订单备注：${data.remark?data.remark:''}`);
  LODOP.ADD_PRINT_LINE(240, 10, 240, 295, 1, 1);
  LODOP.ADD_PRINT_TEXT(250, 10, 300, 20, `会员金额：${data.memberBeforeBalance || 0}`);
  LODOP.ADD_PRINT_TEXT(270, 10, 300, 20, `本次充值：${data.memberChangeAmount || 0}元`);
  LODOP.ADD_PRINT_TEXT(290, 10, 300, 20, `会员余额：${(data.memberAfterBalance || 0)}元`);
  LODOP.ADD_PRINT_LINE(310, 10, 310, 295, 1, 1);
  LODOP.ADD_PRINT_TEXT(320, 10, 300, 20, `会员电话：${data.appUserInfoVO.contactPhone || ''}`);
  LODOP.ADD_PRINT_TEXT(340, 10, 300, 20, `会员姓名：${data.appUserInfoVO.contactName || ''}`);
  let address = `会员地址：${data.appUserInfoVO.province || ''}${ data.appUserInfoVO.city|| ''}${ data.appUserInfoVO.area|| ''}${ data.appUserInfoVO.address|| ''}${ data.appUserInfoVO.addressDetail|| ''}`
  LODOP.ADD_PRINT_TEXT(360, 10, 300, 20, address);

  LODOP.ADD_PRINT_TEXT(400, 10, 300, 20, `联系电话：${data.sysOrg.contactPhone}`);
  LODOP.ADD_PRINT_TEXT(420, 10, 300, 20, `本店地址：${data.sysOrg.address || ''}`);
  LODOP.ADD_PRINT_TEXT(460, 10, 300, 20, `客户签名：`);
  LODOP.ADD_PRINT_LINE(500, 10, 500, 295, 1, 1);
  LODOP.ADD_PRINT_TEXT(510, 50, 300, 20, `请仔细阅读店内张贴的取衣凭证注意事项`);
  LODOP.ADD_PRINT_LINE(530, 10, 530, 295, 1, 1);
  if (LODOP.SET_PRINTER_INDEXA(lodopName))
  LODOP.PRINT(); //直接打印
}

//收衣明细收据
export function collectClothPrinter(LODOP, data, lodopName) {
  // 创建Lodop打印对象
  // var LODOP = getLodop();
  LODOP.PRINT_INIT(`收衣明细收据${data.contactName?'-'+data.contactName:''}`);

  // 设置打印模板
  LODOP.SET_PRINT_PAGESIZE(3, 800, 45, '');
  //这里3表示纵向打印且纸高“按内容的高度”；800表示纸宽80mm；45表示页底空白4.5mm

  // 设置打印内容
  LODOP.ADD_PRINT_TEXT(30, 95, 150, 20, `一七生活 ${data.orgName || data.sysOrg.name}`);
  LODOP.ADD_PRINT_TEXT(55, 135, 50, 20, '收衣单');

  // 开始绘制虚线
  LODOP.ADD_PRINT_LINE(80, 10, 80, 295, 1, 1);

  LODOP.ADD_PRINT_TEXT(90, 80, 200, 20, `订单号：${data.orderNo}`);

  //  绘制条码
  LODOP.ADD_PRINT_BARCODE(110, 30, 250, 80, '128B', data.orderNo);
  LODOP.SET_PRINT_STYLEA(0,"ShowBarText",0);

  LODOP.ADD_PRINT_TEXT(200, 50, 300, 20, `收衣日期：${data.createTime?data.createTime:'--'}`);
  
  LODOP.ADD_PRINT_TEXT(230, 50, 300, 20, `预计取衣时间：${ data.planSendToUserTime}`);

  LODOP.ADD_PRINT_LINE(255, 10, 255, 295, 1, 1);

  LODOP.ADD_PRINT_TEXT(265, 20, 400, 20, '编号       衣物         价格       瑕疵');

  LODOP.ADD_PRINT_LINE(285, 10, 285, 295, 1, 1);

  const top = 305;
  let height = top
  for(var i = 0; i < data.orderClothItemVOS.length; i++) {
    let item = data.orderClothItemVOS[i];
    let _name = ''
    item.orderClothColors.forEach(col_n=>{
      _name += '-'+col_n.clothColorName
    })
    let Craft = ''
    if (item.orderClothCrafts) {
      item.orderClothCrafts.forEach(item_n => {
        Craft += ('|' + item_n.clothCraftName)
      })
    }
    _name = item.clothItemName+_name + ('-' + item.clothSpeedName) + Craft
    let _flaws = ''
    item.orderClothFlaws.forEach(item_n=>{
      _flaws += item_n.clothFlawName+';'
    })

    LODOP.ADD_PRINT_TEXT(height, 20, 50, 20, i+1);
    LODOP.ADD_PRINT_TEXT(height, 45, 110, 20, _name);
    LODOP.ADD_PRINT_TEXT(height, 165, 40, 20, `${item.totalPrice}元`);
    LODOP.ADD_PRINT_TEXT(height, 200, 110, "100%", _flaws);
    LODOP.SET_PRINT_STYLEA(0, "Autowrap", 1);
    let h1 = addDynamicText(30, _name)
    let h2 = addDynamicText(30, _flaws)
    var actualHeight = h1 > h2 ? h1 : h2
    height += actualHeight
  }

  // const height = top + (data.orderClothDetails.length+1)*20;
  // 合计
  LODOP.ADD_PRINT_TEXT(height + 15, 10, 150, 20, `合计：${data.orderClothCount}件`);
  LODOP.ADD_PRINT_TEXT(height + 15, 160, 150, 20, `总金额：${data.orderAmount}元`);

  LODOP.ADD_PRINT_LINE(height + 35, 10, height + 35, 295, 1, 1);
  LODOP.ADD_PRINT_TEXT(height + 45, 10, 150, 20, `优惠金额：${(data.activityDiscountAmount || 0) + (data.cashDeductAmount || 0) + (data.couponDeductAmount || 0)}元`);
  LODOP.ADD_PRINT_TEXT(height + 45, 160, 150, 20, `实付金额：${(data.payAmount || 0)}元`);
  LODOP.ADD_PRINT_TEXT(height + 65, 10, 150, 20, `优惠券：${(data.couponDeductAmount || 0)}元`);
  LODOP.ADD_PRINT_TEXT(height + 65, 160, 150, 20, `折扣活动：${(data.activityDiscountAmount || 0)}元`);
  LODOP.ADD_PRINT_TEXT(height + 85, 10, 150, 20, `现金券：${data.cashDeductAmount || 0}元`);
  LODOP.ADD_PRINT_LINE(height + 105, 10, height + 105, 295, 1, 1);

  //金额
  let payTypename = '';
  if (data.payTimeType == 1 && data.payStatus == 0) {
    payTypename = '取衣付款 '
  } else if(data.payType == '0'){
    payTypename = '微信 '
  }else if(data.payType == '1'){
    payTypename = '支付宝 '
  }else if(data.payType == '2'){
    payTypename = '现金 '
  }else if(data.payType == '3'){
    payTypename = '余额 '
  }else if(data.payType == '8'){
    payTypename = '组合支付 '
  }



  LODOP.ADD_PRINT_TEXT(height + 115, 10, 150, 20, `付款方式：${payTypename}`);
  LODOP.ADD_PRINT_TEXT(height + 115, 160, 150, 20, `会员金额：${data.memberBeforeBalance?data.memberBeforeBalance+'元':'0元'}`);
  LODOP.ADD_PRINT_TEXT(height + 135, 10, 150, 20, `本次扣款：${data.memberChangeAmount?data.memberChangeAmount+'元':'0元'}`);
  LODOP.ADD_PRINT_TEXT(height + 135, 160, 150, 20, `会员余额：${data.memberAfterBalance?data.memberAfterBalance +'元':'0元'}`);
  let contactName = ''
  let n = 0
  for(let i = 0; i < (data.appUserInfoVO.contactName || data.appUserInfoVO.nickName || data.realName).length; i++) {
    n++
    if (i < 9) {
      contactName += (data.appUserInfoVO.contactName || data.appUserInfoVO.nickName || data.realName)[i]
    }
  }
  if (n >= 9) contactName += '...'
  LODOP.ADD_PRINT_TEXT(height + 155, 10, 150, 20, `客户：${contactName}`);
  LODOP.ADD_PRINT_TEXT(height + 155, 160, 180, 20, `客户电话：${data.appUserInfoVO.contactPhone || data.appUserInfoVO.phoneNumber}`);
  let address = `客户地址：${data.appUserInfoVO.province || ''}${ data.appUserInfoVO.city|| ''}${ data.appUserInfoVO.area|| ''}${ data.appUserInfoVO.address|| ''}${ data.appUserInfoVO.addressDetail|| ''}`
  LODOP.ADD_PRINT_TEXT(height + 175, 10, 300, 20, address);
  var userHeight = addDynamicText(90, address)
  height += userHeight

  LODOP.ADD_PRINT_LINE(height + 180, 10, height + 180, 295, 1, 1);
  console.log(data.sysOrg)
  LODOP.ADD_PRINT_TEXT(height + 190, 10, 300, 20, `本店地址：${data.sysOrg.address || ''}`);
  var sysHeight = addDynamicText(90, `本店地址：${data.sysOrg.address || ''}`)
  height += sysHeight
  LODOP.ADD_PRINT_TEXT(height + 200, 10, 300, 20, `联系电话：${data.sysOrg.contactPhone}`);

  // LODOP.ADD_PRINT_TEXT(height + 210, 10, 180, 20, `营业时间：${data.sysOrg.businessStartTime?data.sysOrg.businessStartTime:''}-${data.sysOrg.businessEndTime?data.sysOrg.businessEndTime:''}`);
  // LODOP.ADD_PRINT_TEXT(height + 210, 200, 120, 20, `员工：${data.createBy?data.createBy:''}`);

  LODOP.ADD_PRINT_LINE(height + 220, 10, height + 220, 295, 1, 1);
  LODOP.ADD_PRINT_TEXT(height + 230, 10, 300, 20, `订单备注：${data.remark?data.remark:''}`);
  var remarkHeight = addDynamicText(90, `订单备注：${data.remark?data.remark:''}`)
  height += remarkHeight
  LODOP.ADD_PRINT_TEXT(height + 250, 10, 300, 50, `客户签名：`);
  LODOP.ADD_PRINT_LINE(height + 290, 10, height + 290, 295, 1, 1);
  LODOP.ADD_PRINT_TEXT(height + 300, 50, 300, 20, `请仔细阅读店内张贴的取衣凭证注意事项`);
  LODOP.ADD_PRINT_LINE(height + 320, 10, height + 320, 295, 1, 1);
  console.log("AAA!23", lodopName, this.LODOP.SET_PRINTER_INDEXA(lodopName))
  if (this.LODOP.SET_PRINTER_INDEXA(lodopName))
    console.log("AAA!2223")
  // this.LODOP.PREVIEW(); //打印预览
  this.LODOP.PRINT(); //直接打印
}

//取衣明细收据
export function sendClothPrinter(LODOP, data, lodopName) {
  // 创建Lodop打印对象
  // var LODOP = getLodop();
  LODOP.PRINT_INIT(`取衣明细收据${data.contactName?'-'+data.contactName:''}`);

  // 设置打印模板
  LODOP.SET_PRINT_PAGESIZE(3, 800, 45, '');
  //这里3表示纵向打印且纸高“按内容的高度”；800表示纸宽80mm；45表示页底空白4.5mm

  // 设置打印内容
  LODOP.ADD_PRINT_TEXT(30, 95, 150, 20, `一七生活 ${data.sysOrg.name}`);
  LODOP.ADD_PRINT_TEXT(55, 135, 50, 20, '取衣单');

  // 开始绘制虚线
  LODOP.ADD_PRINT_LINE(80, 10, 80, 295, 1, 1);

  LODOP.ADD_PRINT_TEXT(90, 80, 200, 20, `订单号：${data.orderNo}`);

  //  绘制条码
  LODOP.ADD_PRINT_BARCODE(110, 30, 250, 80, '128B', data.orderNo);
  LODOP.SET_PRINT_STYLEA(0,"ShowBarText",0);

  LODOP.ADD_PRINT_TEXT(200, 50, 300, 20, `取衣时间：${data.sendToUserTime?data.sendToUserTime:'--'}`);

  LODOP.ADD_PRINT_LINE(225, 10, 225, 295, 1, 1);

  let _number = 0;
  for(var i = 0; i < data.orderClothItemVOS.length; i++) {
    if( data.orderClothItemVOS[i].flowStatus == 5 ){
      _number = _number + 1;
    }
  }

  LODOP.ADD_PRINT_TEXT(235, 10, 150, 20, `衣服件数：${data.orderClothCount}`);
  LODOP.ADD_PRINT_TEXT(235, 160, 150, 20, `已取件数：${_number}`);

  LODOP.ADD_PRINT_LINE(260, 10, 260, 295, 1, 1);

  //金额
  let payTypename = '';
  if(data.payType == '0'){
    payTypename = '微信 '
  }else if(data.payType == '1'){
    payTypename = '支付宝 '
  }else if(data.payType == '2'){
    payTypename = '现金 '
  }else if(data.payType == '3'){
    payTypename = '余额 '
  } else if(data.payType == '8'){
    payTypename = '组合支付 '
  } 

  LODOP.ADD_PRINT_TEXT(270, 10, 150, 20, `付款方式：${payTypename}`);
  LODOP.ADD_PRINT_TEXT(270, 160, 150, 20, `会员金额：${data.memberPrice?data.memberPrice+data.payAmount:0}元`);
  LODOP.ADD_PRINT_TEXT(300, 10, 150, 20, `本次扣款：${data.payAmount?data.payAmount : 0}元`);
  LODOP.ADD_PRINT_TEXT(300, 160, 150, 20, `会员余额：${data.memberPrice?data.memberPrice:0}元`);
  let contactName = ''
  let n = 0
  for(let i = 0; i < (data.contactName).length; i++) {
    n++
    if (i < 9) {
      contactName += (data.contactName)[i]
    }
  }
  if (n >= 9) contactName += '...'
  LODOP.ADD_PRINT_TEXT(330, 10, 150, 20, `客户：${contactName}`);
  LODOP.ADD_PRINT_TEXT(330, 160, 180, 20, `客户电话：${data.contactPhone}`);
  LODOP.ADD_PRINT_TEXT(360, 10, 300, 20, `客户地址：${data.province || ''}${data.city || ''}${data.area || ''}${data.address || ''}${data.addressDetail || ''}`);

  LODOP.ADD_PRINT_LINE(385, 10, 385, 295, 1, 1);
  LODOP.ADD_PRINT_TEXT(395, 10, 300, 20, `本店地址：${data.sysOrg.address}${data.sysOrg.addressDetail}`);
  LODOP.ADD_PRINT_TEXT(425, 10, 300, 20, `联系电话：${data.sysOrg.contactPhone}`);

  LODOP.ADD_PRINT_TEXT(455, 10, 180, 20, `营业时间：${data.sysOrg.time?data.sysOrg.time:''}`);
  LODOP.ADD_PRINT_TEXT(455, 200, 120, 20, `员工：${data.sysOrg.user?data.sysOrg.user:''}`);

  LODOP.ADD_PRINT_LINE(480, 10, 480, 295, 1, 1);
  LODOP.ADD_PRINT_TEXT(490, 10, 300, 20, `订单备注：${data.remark?data.remark:''}`);
  LODOP.ADD_PRINT_TEXT(530, 10, 300, 50, `客户签名：`);
  LODOP.ADD_PRINT_LINE(560, 10, 560, 295, 1, 1);
  LODOP.ADD_PRINT_TEXT(570, 50, 300, 20, `请仔细阅读店内张贴的取衣凭证注意事项`);
  LODOP.ADD_PRINT_LINE(590, 10, 590, 295, 1, 1);

  if (this.LODOP.SET_PRINTER_INDEXA(lodopName))
  // this.LODOP.PREVIEW(); //打印预览
  this.LODOP.PRINT(); //直接打印
}

//洗衣明细收据
export function laundryPrinter(LODOP, data, lodopName) {
  console.log(LODOP, data, lodopName)
  // 创建Lodop打印对象
  // var LODOP = getLodop();
  LODOP.PRINT_INIT('洗衣明细收据');

  // 设置打印模板
  LODOP.SET_PRINT_PAGESIZE(1, 1010, 160, '');
  //这里3表示纵向打印且纸高“按内容的高度”；800表示纸宽80mm；45表示页底空白4.5mm

  // 设置打印内容
  LODOP.ADD_PRINT_TEXT(30, 95, 150, 20, `一七生活 ${data.sysOrg?data.sysOrg.name:''}`);
  LODOP.ADD_PRINT_TEXT(55, 135, 50, 20, data.clothOrderType==1?'取衣单':'收衣单');

  // 开始绘制虚线
  LODOP.ADD_PRINT_LINE(80, 10, 80, 295, 1, 1);

  LODOP.ADD_PRINT_TEXT(90, 80, 200, 20, `订单号：${data.orderNo}`);

  //  绘制条码
  LODOP.ADD_PRINT_BARCODE(110, 30, 250, 80, '128B', data.orderNo);
  LODOP.SET_PRINT_STYLEA(0,"ShowBarText",0);

  LODOP.ADD_PRINT_TEXT(200, 50, 300, 20, `取衣时间：${data.sendToUserTime?data.sendToUserTime:'--'}`);

  LODOP.ADD_PRINT_LINE(225, 10, 225, 295, 1, 1);

  LODOP.ADD_PRINT_TEXT(235, 10, 150, 20, `衣服件数：${data.orderClothCount}`);
  LODOP.ADD_PRINT_TEXT(235, 160, 150, 20, `取衣件数：${data.orderClothCount}`);

  LODOP.ADD_PRINT_LINE(260, 10, 260, 295, 1, 1);

  //金额
  let payTypename = '';
  if(data.payType == '0'){
    payTypename = '微信 '
  }else if(data.payType == '1'){
    payTypename = '支付宝 '
  }else if(data.payType == '2'){
    payTypename = '现金 '
  }else if(data.payType == '3'){
    payTypename = '余额 '
  }else if(data.payType == '8'){
    payTypename = '组合支付 '
  }

  LODOP.ADD_PRINT_TEXT(270, 10, 150, 20, `付款方式：${payTypename}`);
  LODOP.ADD_PRINT_TEXT(270, 160, 150, 20, `会员金额：${data.memberPrice?data.memberPrice:''}元`);
  LODOP.ADD_PRINT_TEXT(300, 10, 150, 20, `本次扣款：${data.thisPrice?data.thisPrice:''}元`);
  LODOP.ADD_PRINT_TEXT(300, 160, 150, 20, `会员余额：${data.totalPrice?data.totalPrice-data.thisPrice:''}元`);

  LODOP.ADD_PRINT_TEXT(330, 10, 150, 20, `客户：${data.contactName?data.contactName:''}`);
  LODOP.ADD_PRINT_TEXT(330, 160, 180, 20, `客户电话：${data.contactPhone?data.contactPhone:''}`);
  LODOP.ADD_PRINT_TEXT(360, 10, 300, 20, `客户地址：${data.province || ''}${data.city || ''}${data.area || ''}${data.address || ''}${data.addressDetail || ''}`);

  LODOP.ADD_PRINT_LINE(385, 10, 385, 295, 1, 1);
  LODOP.ADD_PRINT_TEXT(395, 10, 300, 20, `本店地址：${data.sysOrg.address || ''}${data.sysOrg.addressDetail || ''}`);
  LODOP.ADD_PRINT_TEXT(425, 10, 300, 20, `联系电话：${data.sysOrg.contactPhone?data.sysOrg.contactPhone:''}`);

  LODOP.ADD_PRINT_TEXT(455, 10, 180, 20, `营业时间：${data.sysOrg.time?data.sysOrg.time:''}`);
  LODOP.ADD_PRINT_TEXT(455, 200, 120, 20, `员工：${data.sysOrg?data.sysOrg.user:''}`);

  LODOP.ADD_PRINT_LINE(480, 10, 480, 295, 1, 1);
  LODOP.ADD_PRINT_TEXT(490, 10, 300, 20, `订单备注：${data.remark?data.remark:''}`);
  LODOP.ADD_PRINT_TEXT(530, 10, 300, 50, `客户签名：`);

  // 打印预览
  // LODOP.PREVIEW();

  if (this.LODOP.SET_PRINTER_INDEXA(lodopName))
  this.LODOP.PRINT(); //直接打印
}

//衣服条码
export function barCodePrinter(LODOP, data, lodopName){

  // 设置打印模板
  LODOP.SET_PRINT_PAGESIZE(3, 1016, 45, '');
  //这里3表示纵向打印且纸高“按内容的高度”；800表示纸宽80mm；45表示页底空白4.5mm
  console.log(data)
  let _flawsHeight = 0
  const top = 20;
  for(var i = 0; i < data.orderClothItemVOS.length; i++) {
    let item = data.orderClothItemVOS[i];
    let _name = ''
    item.orderClothColors.forEach(col_n=>{
      _name += '-'+col_n.clothColorName
    })
    _name = item.clothItemName+_name
    let _flaws = ''
    let n = 0
    item.orderClothFlaws.forEach(item_n=>{
      if (n < 10) {
        _flaws += item_n.clothFlawName+';'
      } 
      n++
    })
    if (n >= 10) {
      _flaws += '...'
    }
    let junInfo = ''
    if (item.orderClothAdjuncts) {
      item.orderClothAdjuncts.forEach(item_n=>{
        junInfo += `${item_n.adjunctName}`+';'
      })
    }

    let Craft = ''
    if (item.orderClothCrafts) {
      item.orderClothCrafts.forEach(item_n => {
        Craft += ('|' + item_n.clothCraftName)
      })
    }

    let h = i * 110 + _flawsHeight
    
    console.log(_flawsHeight)
    //  绘制条码
    LODOP.ADD_PRINT_BARCODE(top + h, 10, 180, 40, '128A', item.washCode);
    LODOP.SET_PRINT_STYLEA(0,"FontSize",5);
    LODOP.ADD_PRINT_TEXT(top + 50 + h, 10, 180, 20, `${_name}`);

    // 
    LODOP.ADD_PRINT_TEXT(top + h - 10, 200, 700, 20, item.createTime);
    LODOP.ADD_PRINT_TEXT(top + 10 + h, 200, 700, 20, `1件（￥${item.payPrice}） `);

    // 
    LODOP.ADD_PRINT_TEXT(top + 30 + h, 200, 100, 15, `${data.id}-${i+1}`);
    LODOP.ADD_PRINT_TEXT(top + 30 + h, 250, 100, 20, data.sysOrg.name);
    LODOP.ADD_PRINT_TEXT(top + 50 + h, 200, 400, 15, `备注：${item.clothSpeedName}${Craft}`);
    LODOP.ADD_PRINT_TEXT(top + 70 + h, 200, 400, 15, `附件：${junInfo}`);
    LODOP.ADD_PRINT_TEXT(top + 90 + h, 10, 460, 20, _flaws);
    _flawsHeight = addDynamicText(100, `${_flaws || ''}`)
    
  }

  console.log("AAA123")
  if (this.LODOP.SET_PRINTER_INDEXA(lodopName))
    console.log("AAA123")

  // this.LODOP.PREVIEW(); //打印预览
  this.LODOP.PRINT(); //直接打印
}

//标签打印
export function labelPrinter(LODOP, data, lodopName) {
  // console.log(LODOP, data, lodopName)

  LODOP.PRINT_INIT(`${data.orgName}${data.clothItemName?'-'+data.clothItemName:''}`);

  // 设置打印模板
  LODOP.SET_PRINT_PAGESIZE(2, 600, 400, '');
  // LODOP.SET_PRINT_PAGESIZE(1, 600, 400, '');
  //这里3表示纵向打印且纸高“按内容的高度”；800表示纸宽80mm；45表示页底空白4.5mm

  // 设置打印内容
  LODOP.ADD_PRINT_TEXT(10, 5, 120, 20, `${data.orgName?data.orgName:''}`);

  let _name = ''
  data.orderClothColors.forEach(col_n=>{
    _name += '-'+col_n.clothColorName
  })
  let _flaws = ''
  data.orderClothFlaws.forEach(item_n=>{
    _flaws += item_n.clothFlawName+';'
  })

  LODOP.ADD_PRINT_TEXT(25, 5, 150, 20, `${data.clothItemName}${_name?'-'+_name:''}${_flaws?'-'+_flaws:''}`);
  LODOP.ADD_PRINT_TEXT(60, 5, 150, 20, '附件');

  const top = 80;
  if( data.orderClothAdjuncts ){
    for(var i = 0; i < data.orderClothAdjuncts.length; i++) {
      let item = data.orderClothAdjuncts[i];
      if( i>1 ){
        LODOP.ADD_PRINT_TEXT(top + i * 15, 5, 150, 10, `……`);
        break
      }
      LODOP.ADD_PRINT_TEXT(top + i * 15, 5, 150, 15, `${item.adjunctName}*${item.num}`);
    }
  }

  //  绘制条码
  LODOP.ADD_PRINT_BARCODE(125, 8, 150, 80, '128Auto', data.washCode);
  LODOP.SET_PRINT_STYLEA(0,"FontSize",5);

  // this.LODOP.PREVIEW(); //打印预览

  if (this.LODOP.SET_PRINTER_INDEXA(lodopName))
  // this.LODOP.PREVIEW(); //打印预览
  this.LODOP.PRINT(); //直接打印
}

//商品小票
export function goodsPrinter(LODOP, data, lodopName) {
  // 创建Lodop打印对象
  // var LODOP = getLodop();
  LODOP.PRINT_INIT(`商品订单${data.contactName?'-'+data.contactName:''}`);

  // 设置打印模板
  LODOP.SET_PRINT_PAGESIZE(3, 800, 45, '');
  //这里3表示纵向打印且纸高“按内容的高度”；800表示纸宽80mm；45表示页底空白4.5mm

  // 设置打印内容
  if (data.sysOrg) {
    LODOP.ADD_PRINT_TEXT(30, 95, 150, 20, `一七生活 ${data.sysOrg ? data.sysOrg.name : '---'}`);
  } else {
    LODOP.ADD_PRINT_TEXT(30, 135, 150, 20, `一七生活`);

  }
  LODOP.ADD_PRINT_TEXT(55, 135, 100, 20, '商品订单');

  // 开始绘制虚线
  LODOP.ADD_PRINT_LINE(80, 10, 80, 295, 1, 1);

  LODOP.ADD_PRINT_TEXT(90, 80, 250, 20, `订单号：${data.orderNo}`);

  //  绘制条码
  LODOP.ADD_PRINT_BARCODE(110, 30, 250, 80, '128B', data.orderNo);
  LODOP.SET_PRINT_STYLEA(0,"ShowBarText",0);

  LODOP.ADD_PRINT_TEXT(200, 50, 300, 20, `打印日期：${data.newTime?data.newTime:'--'}`);

  LODOP.ADD_PRINT_LINE(230, 10, 230, 295, 1, 1);
  //255
  LODOP.ADD_PRINT_TEXT(240, 20, 400, 20, '编号        衣物                价格');

  LODOP.ADD_PRINT_LINE(260, 10, 260, 295, 1, 1);

  const top = 280;
  for(var i = 0; i < data.orderGoodsSkuList.length; i++) {
    let item = data.orderGoodsSkuList[i];
    let skuName = ''
    if (item.skuName) {
      let skuNames = JSON.parse(item.skuName)
      skuName = skuNames.join('/')
    }
    LODOP.ADD_PRINT_TEXT(top + i * 30, 20, 50, 20, i+1);
    LODOP.ADD_PRINT_TEXT(top + i * 30, 45, 110, 20, item.goodsName);
    LODOP.ADD_PRINT_TEXT(top + i * 30, 165, 150, 20, `${item.salePrice}元*${item.buyNum}  ${skuName}`);
  }

  const height = top + (data.orderGoodsSkuList.length+1)*25;
  // 合计
  LODOP.ADD_PRINT_TEXT(height + 15, 10, 150, 20, `合计：${data.orderGoodsSkuList.length}件`);

  // let payTypeData = ['微信','支付宝','现金','余额']

  // LODOP.ADD_PRINT_TEXT(height + 35, 10, 300, 20, `付款方式：${payTypeData[data.payType]}`);

  //35
  LODOP.ADD_PRINT_LINE(height + 35, 10, height + 35, 295, 1, 1);

  //金额
  let payTypename = '';
  let thisPrice = ''
  if(data.payType == '0'){
    payTypename = '微信 '
  }else if(data.payType == '1'){
    payTypename = '支付宝 '
  }else if(data.payType == '2'){
    payTypename = '现金 '
  }else if(data.payType == '3'){
    payTypename = '余额 '
    thisPrice = data.payAmount
  }else if(data.payType == '8'){
    payTypename = '组合支付 '
  }

  LODOP.ADD_PRINT_TEXT(height + 45, 10, 150, 20, `总金额：${data.totalPrice}元`);
  //缺每一种方式的具体金额以及使用优惠券金额
  LODOP.ADD_PRINT_TEXT(height + 65, 10, 150, 20, `付款方式：${payTypename}`);

  LODOP.ADD_PRINT_LINE(height + 85, 10, height + 85, 295, 1, 1);

  LODOP.ADD_PRINT_TEXT(height + 95, 10, 300, 20, `会员金额：${data.memberBeforeBalance+'元'}`);
  LODOP.ADD_PRINT_TEXT(height + 115, 10, 300, 20, `本次扣款：${data.memberChangeAmount+'元'}`);
  LODOP.ADD_PRINT_TEXT(height + 135, 10, 300, 20, `会员余额：${data.memberAfterBalance}元`);

  LODOP.ADD_PRINT_LINE(height + 155, 10, height + 155, 295, 1, 1);

  LODOP.ADD_PRINT_TEXT(height + 165, 10, 300, 20, `客户：${data.appUserInfoVO.realName}`);
  LODOP.ADD_PRINT_TEXT(height + 185, 10, 300, 20, `客户电话：${data.appUserInfoVO.phoneNumber}`);
  let address = `客户地址：${data.appUserInfoVO.province || ''}${ data.appUserInfoVO.city|| ''}${ data.appUserInfoVO.area|| ''}${ data.appUserInfoVO.address|| ''}${ data.appUserInfoVO.addressDetail|| ''}`
  LODOP.ADD_PRINT_TEXT(height + 205, 10, 300, 20, address);
  if (data.sysOrg) {
    LODOP.ADD_PRINT_TEXT(height + 240, 10, 300, 20, `本店地址：${data.sysOrg.address?data.sysOrg.address+data.sysOrg.addressDetail:''}`);
    LODOP.ADD_PRINT_TEXT(height + 260, 10, 300, 20, `联系电话：${data.sysOrg.contactPhone}`);
  
    LODOP.ADD_PRINT_TEXT(height + 280, 10, 300, 20, `营业时间：${data.sysOrg.time?data.sysOrg.time:''}`);
    LODOP.ADD_PRINT_TEXT(height + 300, 10, 300, 20, `员工：${data.sysOrg.user?data.sysOrg.user:''}`);
  
    LODOP.ADD_PRINT_LINE(height + 320, 10, height + 320, 295, 1, 1);
    LODOP.ADD_PRINT_TEXT(height + 330, 10, 300, 50, `客户签名：`);
  } else {
    LODOP.ADD_PRINT_TEXT(height + 240, 10, 300, 50, `客户签名：`);
  }
   

  if (this.LODOP.SET_PRINTER_INDEXA(lodopName))
  // this.LODOP.PREVIEW(); //打印预览
  this.LODOP.PRINT(); //直接打印
}



