/*******************************************************************************
 * Copyright(c) 2024 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：
 *		2.功能描述：表单-（开单）
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		周兴		  	2024-1-30    1.00		   	  新建
 *******************************************************************************/
const common = require('@/utils/common');
const mixins = require('@/mixins/index.js')
const util = require('@/utils/util.js')
const Constants = require('@/utils/Constants');
const chooseLocation = requirePlugin('chooseLocation');
const config = require('@/config/config.js')
import Dialog from '@/dist/dialog/dialog.js';
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 区域
    cardList: {
      type: Array,
      value: [],
      observer: function (newVal) {
        if (newVal) {
          let filters = newVal.filter(it => it.name == 'items')
          if (filters && filters.length > 0) {
            this.setData({
              itemCard: {
                ...filters[0]
              }
            })
          }
        }
      }
    },
    // 内容
    contentObj: {
      type: Object,
    },
    // 点击那个日期显示哪个日期的时间戳
    dataTime: {
      type: Number,
    },
    // 最小日期
    minDate: {
      type: Number,
    },
    // 开单组件类型(sale,pur)
    type: {
      type: String,
    },
    // 外部传入控制的选商品所有标识的集合
    controlFlags: {
      type: Object, // controlFlags 是一个对象  
      value: {}, // 默认值是一个空对象  
    },
    // 类型 sale; basic
    popType: {
      type: String,
      value: 'sale'
    },
    // 路由名称
    routeObjName: {
      type: String
    },
    titleTagCol: {
      type: String,
      value: 'titleTag'
    },
    titleCol: {
      type: String,
      value: 'title'
    },
    iconCol: {
      type: String,
      value: 'iconThumPath'
    },
    pupPriceCol: {
      type: String,
      value: 'priceCol'
    },
    priceTailCol: {
      type: String,
      value: 'priceTailCol'
    },

    // 价格列
    priceCol: {
      type: String,
      value: 'priceSale'
    },
    // 数量列
    quantityCol: {
      type: String,
      value: 'itemQuantity'
    },
    // 数量列
    descCol: {
      type: String,
      value: 'desc'
    },
    // pop底部描述列
    subCol: {
      type: String,
      value: 'desc'
    },
    // 价格标题
    priceTitle: {
      type: String,
    },
    sumStandardCol: {
      type: String,
      value: 'sumStandard'
    },
    discountStandardCol: {
      type: String,
      value: 'discountStandard'
    },
    sumQuantityCol: {
      type: String,
      value: 'sumQuantity'
    },
    wholeOrderDiscountCol: {
      type: String,
      value: 'wholeOrderDiscountCol'
    },
    sumAmountCol: {
      type: String,
      value: 'sumAmount'
    },
    discountAmountCol: {
      type: String,
      value: 'discountAmountCol'
    },
    swipeDisabled: {
      type: String,
    },
    sumAmountColFlag: {
      type: Boolean,
      value: true
    },
    reverseCalculationFlag: {
      type: Boolean,
      value: false
    },
    isDetailFlag: {
      type: Boolean,
      value: false
    },
    // 可用收款列
    canUseCollectCol: {
      type: String,
      value: 'canUseCollect'
    },
    // 收款列
    useCollectCol: {
      type: String,
      value: 'useCollect'
    },
    // 弹框的内容
    popContent: {
      type: Array,
    },
    popupTopObj: {
      type: Array,
    },
    // 是否只读
    readonly: {
      type: Boolean,
    },
    titpsFlag: { //商品右上角信息
      type: Boolean,
      value: false
    },
    titpsItemColorFlagKey: {
      type: String,
    },
    titpsLabel: {
      type: String
    },
    stepperFlag: { //步进器 标识
      type: Boolean,
    },
    quantityMin: { //步进器 最小值
      type: String,
    },
    quantityMax: {
      type: String
    },
    showPopFlag: { //弹窗标识  true 不弹窗
      type: Boolean,
    },
    staff: {
      type: String, //主从业务员
    },
    org: {
      type: String, //主从业务部门
    },
    saleStaffAndOrgFlag: {
      type: Boolean,
      value: true
    },
    cardContentList: { //商品明细下的选单
      type: Array,
    },
    quantityRedCol: { //例  可售量显示字段 key
      type: String,
    },
    quantityRedColFlagKey: { // 显示隐藏整行的key   商品明细控制
      type: String
    },
    quantityRedTitle: { //右侧显示文字
      type: String,
      value: '可售量：'
    },
    goodsTagText: { //自定义tag
      type: String,
      value: ''
    },
    // 表名，用于上传附件
    table: {
      type: String,
    },
    // 删除时是否进行校验
    delValidFlag: {
      type: Boolean,
    },
    // 显示盈亏数量 和盈亏原因
    lossReasonFlag: {
      type: Boolean,
    },
    // 对象取值列
    objCol: {
      type: String
    },
    // 对象列名
    objLabel: {
      type: String
    },
    // 对象类型(客户customer；供应商supplier)
    objType: {
      type: String
    },
    // 对象标题
    objTitle: {
      type: String,
      value: 'cusName'
    },
    // 对象副标题
    objSubTitle: {
      type: String,
      value: 'cusPhone'
    },
    // 金额标题
    amountTitle: {
      type: String,
      value: '应收金额'
    },
    // 金额列
    amountCol: {
      type: String,
      value: 'receivableResidue'
    },
    // 对象Id列
    objIdCol: {
      type: String,
      value: 'cusId'
    },
    chooseText: {
      type: String,
    },
    goodsTagColor: { //标签颜色
      type: String,
    }, 
    // 结果集
    value: {
      type: String,
      observer: function (newVal) {
        if (newVal) {
          this.setData({
            form: JSON.parse(newVal) || {}
          })
        } else {
          this.setData({
            form: {}
          })
          // 清空明细的合计
          if (this.data.itemCard) {
            let cardList = this.data.cardList
            let index = cardList.findIndex(it => it.name == 'items');
            // 先删除掉
            cardList = cardList.filter(it => it.name != 'items');
            // 再插入
            cardList.splice(index, 0, {
              ...this.data.itemCard
            })
            // console.log('rrr1',filters);
            // if(filters && filters.length > 0){
            //   filters[0] = {...this.data.itemCard}
            // }
            this.setData({
              cardList: cardList
            })
          }
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    // 系统版本
    gradeCode: null,
    // 是否显示pop
    show: false,
    showDate: false,
    showPop: false,

    $t: app.globalData.lang,
    routeUrl: app.globalData['routeUrl'],
    item: null,
    index: null,
    dataItem: null,
    dataIndex: null,
    card: null,
    itemCard: null, // 记录明细信息，清空时使用
    key: null,
    // 下拉类型
    dropType: null,
    dropCode: null,
    dropName: null,
    itemName: 'goodsList',
    deleteName: 'deleteList',
    selectCoordinationUrl: '/package-base-select/pages/select-choose-coordination/select-choose-coordination',
    selectGoodsUrl: null,
    chooseVisible: true, // 用于联动控制选商品按钮
    maxSize: Constants.uploadFileConfig.maxSize,
    imageUrl: config.image_url + '/static/img/',
    // popContent:
    //   [{ code: 'quantity', type: 'step', title: '数量', required: true }, { code: 'pricePromotion', type: 'number', title: '销售价格', required: true }, { code: 'warehouseId', name: 'warehouseName', type: 'drop', required: true, dropType: 'warehouse' , required: true}, { code: 'color', type: 'str', title: '批次号' , required: true}, { code: 'giftFlag', type: 'checkbox', title: '赠品标识' }],
    form: {},
    popChangePrice: [{
      code: 'priceSale',
      type: 'number',
      title: mixins.$t("priceSale"),
      required: true,
      center: 'right',
      readonly: true
    },
    {
      code: 'factPrice',
      type: 'number',
      title: mixins.$t("factPrice"),
      required: true,
      readonly: false
    },
    {
      code: 'factAmt',
      type: 'number',
      title: mixins.$t("factAmt"),
      required: true,
      readonly: false
    },
  ],
  },
  lifetimes: {
    attached: function () {
      // 获取系统版本
      this.setData({
        gradeCode: app.globalData.company.gradeCode
      })
      // 销售订单
      if (this.data.type == Constants.billType.sale || this.data.type == Constants.billType.saleOut ||
        this.data.type == Constants.billType.pur || this.data.type == Constants.billType.purInto || this.data.type == Constants.billType.saleOutReturn ||
        this.data.type == Constants.billType.out || this.data.type == Constants.billType.purReturn) {
        this.setData({
          selectGoodsUrl: '/package-base-select/pages/select-goods/select-goods'
        })
      }

      // 处理contentObj
      this.handleContentObj();
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
      // 清空地址信息
      chooseLocation.setLocation(null);
    },
  },
  pageLifetimes: {
    /**
     * @desc :  生命周期函数--监听页面显示
     * @author : 周兴
     * @date : 2024/2/19 12:16
     */
    show: function () {
      // 设置地址
      this.setAddress();
      // 是否弹出询问框
      this.checkDialog();
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * @desc   : 改变后是否弹出询问
     * @author : 周兴
     * @date   : 2024/1/26 11:46
     */
    checkDialog() {
      let item = this.data.item;
      let originalData = this.data.originalData;
      let form = this.data.form
      let itemName = this.data.itemName

      if (item && item.clearItemFlag && form[itemName] && form[itemName].length > 0 &&
        originalData && originalData.id != form[item.code]) {
        Dialog.confirm({
          message: '是否清空商品明细？',
        }).then(() => {
          form[itemName] = []
          originalData.id = form[item.code]
          originalData.name = form[item.name]
          this.setData({
            originalData: originalData,
            form: form,
            value: JSON.stringify(form)
          })
        }).catch(() => {
          setTimeout(() => {
            form[item.code] = originalData.id
            form[item.name] = originalData.name
            this.setData({
              form: form,
              value: JSON.stringify(form)
            })
          }, 500)
        })
      }
    },
    /**
     * @desc   : 处理ContentObj数据
     * @author : 周兴
     * @date   : 2024/1/26 11:46
     */
    handleContentObj(card) {
      let contentObj = this.data.contentObj;
      let cardList = this.data.cardList;
      let form = this.data.form;
      let items = form[this.data.itemName]
      if (cardList && cardList.length > 0 && contentObj) {
        if (card) {
          cardList = cardList.filter(it => it.name == card.name);
        }
        cardList.forEach((it, index) => {
          // 不是明细
          if (it.name != 'items') {
            if (it.displayNum && contentObj[it.name].length > it.displayNum) {
              for (let i = it.displayNum; i < contentObj[it.name].length; i++) {
                // 如果折叠就把超过的部分隐藏
                if (it.showMore) {
                  contentObj[it.name][i].show = true;
                } else {
                  contentObj[it.name][i].show = false;
                }
              }
            }
          } else {
            // 商品明细
            if (it.displayNum && items && items.length > it.displayNum) {
              for (let i = it.displayNum; i < items.length; i++) {
                // 如果折叠就把超过的部分隐藏//todo
                // console.log('it',it.showMore,items[i]);
                if (it.showMore) {
                  items[i].show = true;
                } else {
                  items[i].show = false;
                }
              }
            }
          }
        })
        this.setData({
          contentObj: contentObj,
          form: form,
          value: JSON.stringify(form)
        })
      }
    },
    /**
     * @desc   : 展开收缩
     * @author : 周兴
     * @date   : 2024/1/26 11:46
     */
    expand(e) {
      let card = e.currentTarget.dataset.card
      let index = e.currentTarget.dataset.index
      let cardList = this.data.cardList
      if (cardList[index]) {
        cardList[index].showMore = !cardList[index].showMore
      }
      this.setData({
        cardList: cardList
      })
      // 处理contentObj
      this.handleContentObj(card);
    },
    /**
     * @desc   : 打开Pop进行修改
     * @author : 周兴
     * @date   : 2024/1/26 11:46
     */
    openChoosedItemInfo(e) {
      if (this.data.showPopFlag) {
        return;
      }
      let ds = e.currentTarget.dataset;
      let popContent = this.data.popContent
      console.log('openChoosedItemInfo', e);
      if (ds.item.flgSubUnit) {
        ds.item.calculationFormula = ds.item.packBox + ds.item.unitName + '= 1' + ds.item.subUnitName
        ds.item.packageBox = ds.item['box'] + ds.item.subUnitName + ds.item['piece'] + ds.item.unitName
      }
      if (this.data.type == Constants.billType.pur) { //采购订单 采购数量 
        ds.item.sumPrice = ds.item['itemQty'] * ds.item.pricePur
      }
      if (this.data.type == Constants.billType.purInto) { //采购入库 
        ds.item.sumPrice = ds.item['itemQty'] * ds.item.priceInto
      }
      if (this.data.type == Constants.billType.out) {
        ds.item.sumPrice = ds.item['itemQty'] * ds.item.priceOut
      }
      if (this.data.type == Constants.billType.saleOut) {
        ds.item.sumPrice = ds.item['outingQty'] * ds.item.priceOut
        // let index_discount = popContent.findIndex(element => element.code == 'discount');
        // let index_discAmt = popContent.findIndex(element => element.code == 'discAmt');
        // if (ds.item['priceOut'] && ds.item['priceOut'] > 0) { //销售单价小于0 折扣和优惠 都不可输入
        //   popContent[index_discount].readonly = false
        //   popContent[index_discAmt].readonly = false
        // } else {
        //   popContent[index_discount].readonly = true
        //   popContent[index_discAmt].readonly = true
        // }
      }
      if (this.data.type == Constants.billType.sale) {
        ds.item.sumPrice = ds.item['itemQty'] * ds.item.priceSale
        let index_discount = popContent.findIndex(element => element.code == 'discount');
        let index_discAmt = popContent.findIndex(element => element.code == 'discAmt');
        if (ds.item['priceSale'] && ds.item['priceSale'] > 0) { //销售单价小于0 折扣和优惠 都不可输入
          popContent[index_discount].readonly = false
        } else {
          popContent[index_discount].readonly = true
        }
      }
      if (this.data.type == Constants.billType.inventory) {
        ds.item.sumPrice = ds.item['outingQty'] * ds.item.priceOut
      }
      if (this.data.type == Constants.billType.saleOutReturn) { //销售退货的时候 打开商品的pup 就要计算箱片 
        ds.item.sumPrice = ds.item['outingQty'] * ds.item.priceOut
      }
      this.setInitPopData()
      //没有skuid 是外协商品 
      let popupTopObj_sku = null
      let popContent_sku = null
      let popContent_ = util.copyObj(popContent)
      if (ds.item.skuId) { //正常品走
        popupTopObj_sku = this.data.popupTopObj
        popContent_sku = popContent_
      } else { //外协品走
        //  全-免费版-销售出库-新建、编辑、详情-外协品-抽屉不展示库存量、仓库字段
        popupTopObj_sku = []
        let index = popContent_.findIndex(element => element.code == 'whId');
        if (index !== -1) {
          // 找到了满足条件的元素，index 是它的索引  
          popContent_sku = popContent_
          popContent_sku.splice(index, 1)
        } else {
          popContent_sku = popContent_
        }
      }
      console.log("popContent_sku", popContent_sku);
      this.setData({
        popContent_sku: popContent_sku,
        popupTopObj_sku: popupTopObj_sku,
        dataIndex: ds.index,
        dataItem: JSON.stringify(ds.item),
        showPop: true,
      })

      this.triggerEvent("openChoosedItemInfo", {
        item: ds.item,
        index: ds.index
      })
    },
    /**
     * @desc   : dk-number-input 的点击事件外漏
     * @author : 王英杰
     * @date   : 2024/1/26 11:46
     */
    openPopDkNumberInput(e) {
      console.log("openDkNumberInput-pop组件111", e);
      let key = e.detail.key
      if (key == "priceSale" || key == "factPrice") {
        let dataItem = this.data.dataItem
        this.setData({
          showChangePrice: true,
          dataItemChangePrice:dataItem
        })
      }
    },
    /**
     * @desc   : 再次打开箱片的弹框
     * @author : 周兴
     * @date   : 2024/1/26 11:46
     */
    openPackageBox(e) {
      console.log("openPackageBox-", e);
      let dataItem = JSON.parse(this.data.dataItem)


      //采购入库 销售退货
      if (this.data.type == Constants.billType.sale ||
        this.data.type == Constants.billType.purReturn ||
        this.data.type == Constants.billType.pur ||
        this.data.type == Constants.billType.purInto ||
        this.data.type == Constants.billType.saleOutReturn ||
        this.data.type == Constants.billType.out ||
        this.data.type == Constants.billType.saleOut ||
        this.data.type == Constants.billType.inventory
      ) {
        this.setData({
          popContentBox: [{
              code: 'box',
              type: 'step',
              title: dataItem.subUnitName,
              required: true
            },
            {
              code: 'piece',
              type: 'step',
              title: dataItem.unitName,
              required: true
            },
          ],
        })
      }
      this.setData({
        showPopBox: true,
      })
    },
    /**
     * @desc   : 关闭Pop后修改数据
     * @author : 周兴
     * @date   : 2024/1/26 11:46
     */
    editItems(data) {
      let form = this.data.form;
      let itemName = this.data.itemName;
      if (form[itemName]) {
        form[itemName][this.data.dataIndex] = data.detail.form;
      }
      //计算金额
      if (this.data.type == Constants.billType.sale) {
        form = this.setGoodsAmount(form);
      }
      this.setData({
        form: form,
        value: JSON.stringify(form)
      })
      if (this.data.type == Constants.billType.saleOut) {
        this.setGoodsOutAmount()
      }
      if (this.data.type == Constants.billType.saleOutReturn) {
        this.setGoodsOutReturnAmount()
      }
      // 外露关闭pop后事件
      this.triggerEvent("editItems", {
        data: form
      })
    },
    /**
     * @desc : 商品确认事件--箱片的确认
     * @date : 2024/2/1 15:49
     * @author : 于继渤
     */
    editItemsBox(e) {
      let dataItem = JSON.parse(this.data.dataItem)
      if (this.data.type == Constants.billType.sale ||
        this.data.type == Constants.billType.pur ||
        this.data.type == Constants.billType.out ||
        this.data.type == Constants.billType.purInto ||
        this.data.type == Constants.billType.inventory
      ) {
        let itemQty = Number(dataItem['box']) * Number(dataItem['packBox']) + Number(dataItem['piece'])
        //如果设有做大值 判断  箱片的反算数量 大于 数量最大值
        if (dataItem["itemQty_max"] && itemQty > dataItem["itemQty_max"]) {
          //那就吧 把最大值的数量换算 箱片赋值箱片
          dataItem['box'] = parseInt(dataItem['itemQty'] / dataItem['packBox'])
          dataItem['piece'] = Number(dataItem['itemQty'] % dataItem['packBox'])
          dataItem.packageBox = dataItem['box'] + dataItem.subUnitName + dataItem['piece'] + dataItem.unitName
          this.setData({
            dataItem: JSON.stringify(dataItem)
          })
          util.showToast('超出最大数量');
          return
        }
        //数量等于 包乘以包装数 加上片数 
        dataItem['itemQty'] = Number(dataItem['box']) * Number(dataItem['packBox']) + Number(dataItem['piece'])
        dataItem.packageBox = dataItem['box'] + dataItem.subUnitName + dataItem['piece'] + dataItem.unitName
        if (this.data.type == Constants.billType.pur) { // 采购订单
          dataItem.sumPrice = dataItem['itemQty'] * dataItem.pricePur
        }
        if (this.data.type == Constants.billType.purInto) { //采购入库
          dataItem.sumPrice = dataItem['itemQty'] * dataItem.priceInto
        }
        if (this.data.type == Constants.billType.out) { //销售退货
          dataItem.sumPrice = dataItem['itemQty'] * dataItem.priceOut
        }
        if (this.data.type == Constants.billType.sale) { //销售订单
          dataItem.sumPrice = dataItem['itemQty'] * dataItem.priceSale
          dataItem.factAmt = dataItem['itemQty'] * dataItem.factPrice
        }
      }
      if (this.data.type == Constants.billType.saleOutReturn) {

        let itemQty = (dataItem['box'] * dataItem['packBox'] + dataItem['piece'])
        let itemQty_min = dataItem.outingQty_min ? Math.abs(dataItem.outingQty_min) : ''; //取绝对值 (因为是负值 即最小值的绝对值 为最大值)
        //如果设有做大值 判断  箱片的反算数量 大于 数量最大值  
        if (itemQty_min && itemQty > itemQty_min) {
          //那就吧 把最大值的数量换算 箱片赋值箱片
          let outingQty = Math.abs(dataItem.outingQty); //取绝对值
          dataItem['box'] = parseInt(outingQty / dataItem['packBox'])
          dataItem['piece'] = Number(outingQty % dataItem['packBox'])
          dataItem.packageBox = dataItem['box'] + dataItem.subUnitName + dataItem['piece'] + dataItem.unitName
          this.setData({
            dataItem: JSON.stringify(dataItem)
          })
          util.showToast('超出最大数量');
          return
        }

        //数量等于 包乘以包装数 加上片数   
        dataItem['outingQty'] = -1 * (dataItem['box'] * dataItem['packBox'] + dataItem['piece'])
        dataItem.packageBox = dataItem['box'] + dataItem.subUnitName + dataItem['piece'] + dataItem.unitName
        dataItem.sumPrice = dataItem.priceOut * dataItem.itemQty
      }
      if (this.data.type == Constants.billType.inventory) {
        //数量等于 包乘以包装数 加上片数 
        dataItem['outingQty'] = dataItem['box'] * dataItem['packBox'] + dataItem['piece']
        dataItem.packageBox = dataItem['box'] + dataItem.subUnitName + dataItem['piece'] + dataItem.unitName
        dataItem.sumPrice = dataItem['outingQty'] * dataItem.priceOut
        dataItem.factAmt = dataItem['outingQty'] * dataItem.factPrice
      }
      if (this.data.type == Constants.billType.saleOut) {
        //数量等于 包乘以包装数 加上片数 
        dataItem['outingQty'] = dataItem['box'] * dataItem['packBox'] + dataItem['piece']
        dataItem.packageBox = dataItem['box'] + dataItem.subUnitName + dataItem['piece'] + dataItem.unitName
        dataItem.sumPrice = dataItem['outingQty'] * dataItem.priceOut
        dataItem.factAmt = dataItem['outingQty'] * dataItem.factPrice
      }

      this.setData({
        dataItem: JSON.stringify(dataItem)
      })
    },
        /**
     * @desc : 商品确认事件-修改价格
     * @date : 2024/2/1 15:49
     * @author : 于继渤
     */
    editItemsChangePrice(e) {
      let dataItem = this.data.dataItemChangePrice
      this.setData({
        dataItem:dataItem
      })
    },
    /**
     * @desc :商品明细数量加减事件
     * @date : 2024/2/1 15:49
     * @author : 于继渤
     */
    onChangeStepper(e) {
      this.triggerEvent("onStepper", {
        index: e.currentTarget.dataset.index,
        item: e.detail
      })
    },
    /**
     * @desc   : 打开下拉/反算页面
     * @author : 周兴
     * @date   : 2024/1/26 11:46
     */
    openReverseCalculationPop() {
      this.triggerEvent("openReverseCalculationPop", {})
    },
    /**
     * @desc   : 打开下拉/跳转选择页面
     * @author : 周兴
     * @date   : 2024/1/26 11:46
     */
    open(e) {
      // 只读 
      let item = e.currentTarget.dataset;
      if (this.data.readonly) {
        return;
      }

      // 明细 只读240320
      if (item.item.readonlyItem) {
        return;
      }
      let that = this
      if (item.item.type == 'drop') {
        this.setData({
          show: true,
          dropCode: item.key,
          dropName: item.name,
          dropType: item.type,
          index: item.index,
          card: item.card
        })
      } else if (item.item.type == 'mutidrop') {
        this.setData({
          mutishow: true,
          dropCode: item.key,
          dropName: item.name,
          dropType: item.type,
          index: item.index,
          card: item.card
        })
      } else if (item.item.type == 'choose') {
        if (item.item.readonlyReturn && !item.item.chooseVisible) {
          return
        }
        that.setData({
          changeObj: item.item.changeObj,
        })
        //判断是否存在此key的值
        if (item.item.titpsKey && !that.data.form[item.item.titpsKey]) {
          // 是否弹出询问框
          Dialog.confirm({
            message: item.item.titpsMessage, //提示信息
          }).then(() => {}).catch(() => {})
          return
        }
        //控制校验提示删除明细
        if (this.data.form[this.data.itemName] && this.data.form[this.data.itemName].length > 0 && this.data.chooseVisible && item.item.chooseVisible) {
          // 是否弹出询问框
          Dialog.confirm({
            message: '请先删除商品明细中的商品', //提示信息
          }).then(() => {}).catch(() => {})
          return
        }

        // 跳转链接
        if (item.item.urlKey) {
          this.setData({
            index: item.index,
            card: item.card,
            item: item.item,
            key: item.key
          })
          let route = this.data.routeUrl[this.data.routeObjName][item.item.urlKey]
          let url = route.url
          if (url) {
            wx.navigateTo({
              url: url,
              events: {
                // 回调后
                bindData: function (data) {
                  //处理明细 给明细赋值  例如选仓库回调赋值
                  if (item.flag) {
                    let form = that.data.form
                    form[that.data.itemName][item.index][item.item.code] = data.data.id
                    form[that.data.itemName][item.index][item.item.name] = data.data.name
                    that.setData({
                      form: form
                    })
                    // 如果有校验信息就清除掉
                    let card = that.data.card;
                    let index = that.data.index;
                    let contentObj = that.data.contentObj;
                    if (card && contentObj[card][index]?.errMsg) {
                      contentObj[card][index].errMsg = undefined;
                      that.setData({
                        contentObj: contentObj
                      })
                    }
                  }
                  // 如果需要询问，那么需要记录原始的值
                  if (item.item.clearItemFlag) {
                    that.setData({
                      originalData: {
                        id: that.data.form[item.key],
                        name: that.data.form[item.name]
                      }
                    })
                  }

                  that.triggerEvent("chooseData", {
                    code: item.key,
                    data: data,
                    index: item.index
                  })
                  // 如果需要联动控制选商品按钮
                  if (item.item.controlChooseFlag) {
                    that.setData({
                      chooseVisible: false
                    })
                  }
                  let contentObj = that.data.contentObj;
                  if (item.card && contentObj[item.card][item.index]?.errMsg && data) {
                    contentObj[item.card][item.index].errMsg = undefined;
                    that.setData({
                      contentObj: contentObj
                    })
                  }
                }
              },
              success: function (res) {
                let itemData = item.item
                if (route.primaryKey) {
                  itemData.id = that.data.form[route.primaryKey]
                  console.log('route', route, that.data.form[route.primaryKey])
                }

                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('params', {
                  item: itemData,
                  route: route
                })
              }
            })
          }
        }
      } else if (item.item.type == 'selectIcon') {
        // 跳转链接
        if (item.item.urlKey) {
          let url = this.data.routeUrl[this.data.routeObjName][item.item.urlKey].url
          // TODO
          // url = '/package-base6-select/pages/select-customers/select-customers'
          if (url) {
            wx.navigateTo({
              url: url,
              events: {
                // 回调后
                bindData: function (data) {
                  that.triggerEvent("chooseData", {
                    code: item.key,
                    data: data
                  })
                  // 如果需要联动控制选商品按钮
                  if (item.item.controlChooseFlag) {
                    that.setData({
                      chooseVisible: false
                    })
                  }
                }
              },
              success: function (res) {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('params', {
                  data: item.item.urlKeyParam
                })
              }
            })
          }
        }
      }
    },
    /**
     * @desc   : 打开选择主从业务员/业务部门组件
     * @author : 周兴
     * @date   : 2024/2/20 11:46
     */
    openMulti(e) {
      if (this.data.readonly) {
        return;
      }
      console.log('openMulti', this.data.form)
      let type = e.currentTarget.dataset.type
      let _this = this
      // 选择主从业务员
      if (type == 'staff') {
        wx.navigateTo({
          url: '/package-sales/pages/choose-sale-staff/choose-sale-staff',
          events: {
            // 回调后
            bindData: function (data) {
              _this.triggerEvent("chooseData", {
                code: type,
                data: data
              })
            }
          },
          success: function (res) {
            // 通过eventChannel向被打开页面传送数据
            res.eventChannel.emit('params', {
              id: _this.data.form.staff.value,
              formMode: 'index'
            })
          }
        })
        // 选择主从业务部门
      } else if (type == 'org') {
        wx.navigateTo({
          url: '/package-sales/pages/choose-sale-org/choose-sale-org',
          events: {
            // 回调后
            bindData: function (data) {
              _this.triggerEvent("chooseData", {
                code: type,
                data: data
              })
            }
          },
          success: function (res) {
            // 通过eventChannel向被打开页面传送数据
            res.eventChannel.emit('params', {
              id: _this.data.form.org.value,
              formMode: 'index'
            })
          }
        })
      }
    },
    /**
     * @desc   : 非空校验
     * @author : 周兴
     * @date   : 2024/2/20 11:46
     */
    validData() {
      let flag = true;
      let cardList = this.data.cardList;
      if (!cardList || cardList.length == 0) {
        return flag;
      }
      let form = this.data.form;
      let contentObj = this.data.contentObj;
      for (let card of cardList) {
        if (contentObj[card.name]) {
          // 明细
          if (card.name == 'items') {
            if (!form[this.data.itemName] || form[this.data.itemName].length == 0) {
              util.showToast('商品明细不能为空')
              return false;
            }
            contentObj[card.name].forEach(item => {
              let title = this.$t(item.title ? item.title : item.code)
              // 如果必须输入，就要进行判断
              if (item.required && form[this.data.itemName] && form[this.data.itemName].length > 0) {
                form[this.data.itemName].forEach(t => {
                  if (!t[item.code]) {
                    t[item.code + '_errMsg'] = title + '不为空'
                    flag = false;
                  }

                })
              }
            })
          } else {
            contentObj[card.name].forEach(item => {
              let title = this.$t(item.title ? item.title : item.code)
              // 如果必须输入，就要进行判断
              if (item.required && !form[item.code]) {
                item.errMsg = title + '不为空'
                flag = false;
              }

              if (item.type == 'phone' && item.required && form[item.code]) {
                // 联系电话格式错误
                var myreg = Constants.Mobile_Phone_Number_Regular_Expression
                if (!myreg.test(form[item.code])) {
                  item.errMsg = title + '格式错误'
                  flag = false;
                }
              }
            })
          }
        }
      }
      this.setData({
        contentObj: contentObj
      })
      return flag;
    },
    /**
     * @desc   : 展开页签
     * @author : 周兴
     * @date   : 2024/2/20 11:46
     */
    expandCard(e) {
      let cardList = this.data.cardList
      let index = e.currentTarget.dataset.index
      if (cardList[index]) {
        // 收缩的时候需要展示信息
        let expand = cardList[index].expand
        cardList[index].info = undefined
        if (expand) {
          let form = this.data.form
          let infoCols = e.currentTarget.dataset.item.infoCols
          if (infoCols && infoCols.length > 0) {
            let info = ''
            infoCols.forEach(it => {
              if (form[it]) {
                info += form[it] + '/'
              }
            })
            if (info.length > 0) {
              info = info.substring(0, info.length - 1);
              cardList[index].info = info
            }
          }
        }
        cardList[index].expand = !expand
        this.setData({
          cardList: cardList
        })
      }
    },
    /**
     * @desc   : 打开日期pop
     * @author : 周兴
     * @date   : 2024/1/26 11:46
     */
    openDatePop(e) {
      // 只读
      if (this.data.readonly) {
        return;
      }
      let item = e.currentTarget.dataset;
      this.setData({
        showDate: true,
        index: item.index,
        key: item.key,
        card: item.card
      })
    },
    /**
     * @desc   : 关闭日期pop
     * @author : 周兴
     * @date   : 2024/1/26 11:46
     */
    cancelDatePop() {
      this.setData({
        showDate: false
      })
    },
    /**
     * @desc   : 选择日期
     * @author : 周兴
     * @date   : 2024/1/26 11:46
     */
    chooseDate(e) {
      let form = this.data.form;
      let index = this.data.index;
      let card = this.data.card;
      let code = this.data.key;
      form[code + '_time'] = e.detail
      form[code] = util.toDateStr(e.detail);
      let contentObj = this.data.contentObj;
      if (card && contentObj[card][index]?.errMsg && e.detail) {
        contentObj[card][index].errMsg = undefined;
      }
      this.setData({
        value: JSON.stringify(form),
        form: form,
        contentObj: contentObj,
        showDate: false
      })
    },
    /**
     * @desc   : 打开选择商品明细界面
     * @author : 周兴
     * @date   : 2024/1/26 11:46
     */
    openChooseItems(e) {
      if (this.data.selectGoodsUrl) {
        this.setData({
          item: null
        })
        let _this = this
        wx.navigateTo({
          url: this.data.selectGoodsUrl,
          events: {
            // 回调后
            bindData: function (data) {
              // 设置商品明细controlChooseFlag
              console.log('bindDatachoosed', data);
              _this.setGoods({
                data: data
              });
              // 选品后，是否需要控制选单只读
              _this.handleChooseByItem(_this, true);
            }
          },
          success: function (res) {
            // 通过eventChannel向被打开页面传送数据
            res.eventChannel.emit('params', {
              choose: true,
              item: e.currentTarget.dataset.type ? e.currentTarget.dataset.type : _this.data.type,
              popContent: _this.data.popContent,
              controlFlags: _this.data.controlFlags
            })
          }
        })
      }
    },

    openChooseCoordinationItems(e) {
      let _this = this
      wx.navigateTo({
        url: this.data.selectCoordinationUrl,
        events: {
          // 回调后
          bindData: function (data) {
            // // 设置商品明细controlChooseFlag
            _this.setGoods({
              data: data
            });
          }
        },
        success: function (res) {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('params', {
            // choose: true,
            item: e.currentTarget.dataset.type ? e.currentTarget.dataset.type : _this.data.type,
            // popContent: _this.data.popContent,
            // controlFlags: _this.data.controlFlags
          })
        }
      })
    },



    /**
     * @desc   : 选择数据
     * @author : 周兴
     * @date   : 2024/1/26 11:46
     */
    chooseData(e) {
      let contentObj = this.data.contentObj;
      let form = this.data.form
      if (e.detail.ids) {
        form[this.data.dropCode + 's'] = e.detail.ids;
        form[this.data.dropName + 's'] = e.detail.names;
      } else {
        form[this.data.dropCode] = e.detail.value;
        form[this.data.dropName] = e.detail.text;
      }
      let card = this.data.card;
      // 选择后清空校验提示信息
      if (card && contentObj[card][this.data.index]?.errMsg && e.detail) {
        contentObj[card][this.data.index].errMsg = undefined;
      }
      this.setData({
        value: JSON.stringify(form),
        form: form,
        contentObj: contentObj
      })
      // 如果处理id，name还需要赋值其他，需要在页面事件中自行处理
      this.triggerEvent("chooseData", {
        code: this.data.dropCode,
        item: e.detail.item
      })
    },
    /**
     * @desc   : 修改str 文本变化
     * @author : 王英杰
     * @date   : 2024/2/2 11:46
     */
    changeDetailField(e) {
      console.log("changeDetailField", e);
      let dataItem = JSON.parse(this.data.dataItem)
      dataItem[e.detail.key] = e.detail.name
      this.setData({
        dataItem: JSON.stringify(dataItem)
      })
    },
    /**
     * @desc   : 修改输入框的值
     * @author : 周兴
     * @date   : 2024/2/2 11:46
     */
    changeField(e) {
      let ds = e.currentTarget.dataset
      let key = ds.key
      let item = ds.item
      let form = this.data.form
      if (e.detail) {
        form[key] = e.detail;
      } else {
        delete form[key]
      }
      let contentObj = this.data.contentObj;
      // 输入值后清空校验提示信息
      if (item && item.errMsg && e.detail) {
        contentObj[ds.card][ds.index].errMsg = undefined;
      }
      this.setData({
        form: form,
        contentObj: contentObj
      })
    },
    /**
     * @desc   : 失去焦点，把值传出
     * @author : 周兴
     * @date   : 2024/2/2 11:46
     */
    blurField(e) {
      let ds = e.currentTarget.dataset
      this.setData({
        value: JSON.stringify(this.data.form)
      })
      this.triggerEvent("changeField", {
        code: ds.key
      })
    },
    /**
     * @desc   : 清空输入框的值
     * @author : 周兴
     * @date   : 2024/2/2 11:46
     */
    clearField(e) {
      // 只读
      if (this.data.readonly) {
        return;
      }
      let ds = e.currentTarget.dataset
      let key = ds.key
      let form = this.data.form
      form[key] = undefined
      this.setData({
        value: JSON.stringify(form),
        form: form
      })
      // 如果要清除其他值，需要调用
      this.triggerEvent("clear", {
        code: ds.key
      })
    },
    /**
     * @desc   : 清空地址的值
     * @author : 周兴
     * @date   : 2024/2/2 11:46
     */
    clearAddress(e) {
      // 明细 只读只读240320
      if (this.data.readonly) {
        return;
      }
      let form = this.data.form
      delete form['address']
      delete form['addressName'];
      delete form['addressFull'];
      delete form['addressGcj02'];
      delete form['addressArea'];
      this.setData({
        value: JSON.stringify(form),
        form: form
      })
      // 如果要清除其他值，需要调用
      this.triggerEvent("clear", {
        code: ds.key
      })
    },
    /**
     * @desc   : 修改合计金额，反算明细商品
     * @author : 周兴
     * @date   : 2024/5/22 11:46
     */
    changeAmount(e) {
      let index = e.currentTarget.dataset.index
      let sumAmountCol = this.data.sumAmountCol
      let cardList = this.data.cardList
      if (cardList && cardList[index]) {
        cardList[index][sumAmountCol] = e.detail.value
      }
      this.setData({
        cardList: cardList
      })
      // this.reCalcItemsAmount();
    },
    /**
     * @desc   : 修改合计金额，反算明细商品
     * @author : 周兴
     * @date   : 2024/5/22 11:46
     */
    reCalcItemsAmount(amount) {
      let form = this.data.form
      let itemName = this.data.itemName
      let itemList = form[itemName]
      let type = this.data.type
      if (itemList && itemList.length > 0) {
        // 单条明细金额字段
        let pupPriceCol = this.data.pupPriceCol;
        let priceCol = this.data.priceCol;
        let quantityCol = this.data.quantityCol;
        let flag = false;
        let sum = 0;
        // 判断是否所有明细的金额都是0,金额都为0 ，按照数量金额分摊
        let filters = itemList.filter(it => it[pupPriceCol])
        if (!filters || filters.length == 0) {
          // 所有的金额都为0
          flag = true;
          // 获取所有数量的汇总
          sum = itemList.sum(quantityCol)
        } else {
          // 获取所有金额的汇总
          sum = itemList.sum(pupPriceCol)
        }
        itemList.forEach((it, index) => {
          console.log('it', it);
          // 按照数量金额分
          if (flag) {
            it[pupPriceCol] = (it[quantityCol] / sum * amount).toFixed(2)
          } else {
            // 如果按照金额分，金额为0的不分
            if (it[pupPriceCol]) {
              it[pupPriceCol] = (it[pupPriceCol] / sum * amount).toFixed(2)
            }
          }
          it[priceCol] = (parseFloat(it[pupPriceCol]) / it[quantityCol]).toFixed(2)
        })
        this.setData({
          form: form
        })
      }
    },
    /**
     * @desc   : 清空日期的值
     * @author : 周兴
     * @date   : 2024/2/2 11:46
     */
    clearDate(e) {
      let ds = e.currentTarget.dataset
      let key = ds.key
      let form = this.data.form
      form[key] = undefined
      form[key + '_time'] = undefined
      this.setData({
        value: JSON.stringify(form),
        form: form
      })
    },
    /**
     * @desc   : 清空选单的值
     * @author : 周兴
     * @date   : 2024/2/2 11:46
     */
    clearChoose(e) {
      let ds = e.currentTarget.dataset
      let key = ds.key
      let name = ds.name
      let item = ds.item
      // 明细 只读只读240320
      if (this.data.readonly) {
        return;
      }
      // 明细 只读只读240320
      if (item.readonlyItem) {
        return;
      }
      let form = this.data.form
      form[key] = undefined
      form[name] = undefined
      this.setData({
        value: JSON.stringify(form),
        form: form
      })
      // 如果需要联动控制选商品按钮
      if (item && item.controlChooseFlag) {
        this.setData({
          chooseVisible: true
        })
      }
      // 如果要清除其他值，需要调用
      this.triggerEvent("clear", {
        code: ds.key
      })
    },
    /**
     * @desc   : 修改数值输入框的值
     * @author : 周兴
     * @date   : 2024/2/2 11:46
     */
    changeNumberField(e) {
      let ds = e.currentTarget.dataset
      let key = ds.key
      let item = ds.item
      let form = this.data.form
      form[key] = e.detail.value
      let contentObj = this.data.contentObj;
      // 输入值后清空校验提示信息
      if (item.errMsg && e.detail) {
        contentObj[ds.card][ds.index].errMsg = undefined;
      }
      this.setData({
        value: JSON.stringify(form),
        form: form,
        contentObj: contentObj
      })
    },
    /**
     * @desc   : 修改数值输入框的值 离焦事件
     * @author : 于继渤
     * @date   : 2024/2/2 11:46
     */
    changeNumberBlur(e) {
      this.triggerEvent("changeNumberBlur", e.detail)
    },
    /**
     * @desc   : 修改电话的值
     * @author : 周兴
     * @date   : 2024/2/2 11:46
     */
    changePhone(e) {
      let ds = e.currentTarget.dataset;
      let key = ds.key;
      let index = ds.index;
      let card = ds.card;
      let form = this.data.form
      form[key] = e.detail.detail
      let contentObj = this.data.contentObj;
      if (card && contentObj[card][index]?.errMsg && e.detail.detail) {
        contentObj[card][index].errMsg = undefined;
      }
      this.setData({
        value: JSON.stringify(form),
        form: form,
        contentObj: contentObj
      })
    },
    /**
     * @desc   : 给电话赋值错误信息
     * @author : 周兴
     * @date   : 2024/2/20 11:46
     */
    changePhoneblur(e) {
      let ds = e.currentTarget.dataset
      let errMsg = e.detail.errMsg;
      let card = ds.card;
      let index = ds.index;
      let contentObj = this.data.contentObj;
      if (errMsg && card && contentObj[card][index]) {
        contentObj[card][index].errMsg = errMsg;
        this.setData({
          errMsg: errMsg
        })
      }
      this.triggerEvent("changePhoneblur", ds)
    },
    /**
     * @desc   : 打开选择对象的页面
     * @author : 周兴
     * @date   : 2024/1/26 11:46
     */
    openObjPop() {
      let _this = this
      if (_this.data.readonly) {
        return
      }
      // 选择客户
      if (this.data.objType == Constants.objType.customer) {
        wx.navigateTo({
          url: '/package-base-select/pages/select-customers/select-customers',
          events: {
            // 回调后
            bindData: function (data) {
              let form = _this.data.form
              let objIdCol = _this.data.objIdCol;
              // 赋值对象Id
              if (objIdCol && data[objIdCol]) {
                form[objIdCol] = data[objIdCol]
              }
              // 客户欠款
              if (data['receivableResidue'] && data['receiptResidue']) {
                data['cusDebt'] = data['receivableResidue'] - data['receiptResidue']
              }
              form.objInfo = data;
              _this.setData({
                form: form,
                value: JSON.stringify(form)
              })
              _this.triggerEvent("chooseObjTypeData", {
                data: form,
              })
            }
          },
          success: function (res) {
            // 通过eventChannel向被打开页面传送数据
            res.eventChannel.emit('params', {
              data: 'test'
            })
          }
        })
        // 选择供应商
      } else if (this.data.objType == Constants.objType.supplier) {
        wx.navigateTo({
          // url: '/package-base-select/pages/select-suppliers/select-suppliers',
          url: '/package-base-select/pages/select-data/select-data',
          events: {
            // 回调后
            bindData: function (data) {
              let dataitem = data.data.item
              let form = _this.data.form
              let objIdCol = _this.data.objIdCol;
              // 赋值对象Id
              if (objIdCol && dataitem[objIdCol]) {
                form[objIdCol] = dataitem[objIdCol]
              }
              form.objInfo = dataitem;
              _this.setData({
                form: form,
                value: JSON.stringify(form)
              })
              _this.triggerEvent("chooseObjTypeData", {
                data: form,
              })
            }
          },
          success: function (res) {
            // 通过eventChannel向被打开页面传送数据
            let item = {}
            let route = _this.data.routeUrl[_this.data.routeObjName];
            res.eventChannel.emit('params', {
              item: item,
              route: route['chooseSupplier']
            })
          }
        })
      }
    },
    /**
     * @desc : 小程序地图调用
     * @author : 周兴
     * @date : 2024/2/18 18:16
     */
    onClickWxchartAddress(e) {
      // 只读
      if (this.data.readonly) {
        return;
      }
      // 跳转新建地图实例
      common.onClickWxchartAddress();
      let item = e.currentTarget.dataset;
      this.setData({
        index: item.index,
        card: item.card
      })
    },
    /**
     * @desc   : 改变选择框内容
     * @author : 周兴
     * @date   : 2024/1/26 11:46
     */
    onChange(e) {
      // 只读
      if (this.data.readonly) {
        return;
      }
      let key = e.currentTarget.dataset.key
      let checkFlag = e.currentTarget.dataset.value
      let form = this.data.form
      form[key] = !checkFlag
      this.setData({
        value: JSON.stringify(form),
        form: form
      })
    },
    /**
     * @desc : 设置地址
     * @author : 周兴
     * @date : 2024/1/19
     */
    setAddress() {
      const location = chooseLocation.getLocation();
      console.log('loc', location, this.data.form);
      let form = this.data.form
      if (location) {
        form['address'] = location;
        form['addressName'] = location.name;
        form['addressFull'] = form['addressFull'] ? form['addressFull'] : location.address + location.name;
        form['addressGcj02'] = {
          latitude: location.latitude,
          longitude: location.longitude
        };
        form['addressArea'] = {
          province: location.province,
          city: location.city,
          district: location.district == undefined || !location.district ? location.city : location.district
        }
        // 如果有校验信息就清除掉
        let card = this.data.card;
        let index = this.data.index;
        let contentObj = this.data.contentObj;
        if (card && contentObj[card][index]?.errMsg) {
          contentObj[card][index].errMsg = undefined;
          this.setData({
            contentObj: contentObj
          })
        }
      }
      this.setData({
        value: JSON.stringify(form),
        form: form
      })
    },

    /**
     * @desc : 设置商品
     * @author : 周兴
     * @date : 2024/1/19
     */
    setGoods(e) {
      if (e.data) {
        let form = this.data.form
        let goodsList = form[this.data.itemName] || []
        e.data.choosedGoodsList.forEach(it => {
          goodsList.push(it)
        })
        form[this.data.itemName] = goodsList
        // 设置商品总单的金额信息 
        if (this.data.type == Constants.billType.sale) {
          form = this.setGoodsAmount(form);
        }
        this.setData({
          form: form,
          value: JSON.stringify(form)
        })
        if (this.data.type == Constants.billType.saleOut) { //销售出库 
          this.setGoodsOutList(e.data)
        }
        if (this.data.type == Constants.billType.purReturn) { //采购退货
          this.setReturnGoodsAmount(e.data);
        }
        if (this.data.type == Constants.billType.saleOutReturn) { //采购退货
          this.setGoodsOutReturnList(e.data);
        }

        if (this.data.type == Constants.billType.pur) {
          // this.setPurGoodsAmount(e.data);
        }
        console.log('jm3', this.data.form);
        // 设置是否进行收缩
        let card = this.data.cardList.filter(it => it.name == 'items');
        this.handleContentObj(card[0])
        console.log('jm2', this.data.form);
        // 外露setGoods方法
        this.triggerEvent("setGoods", {
          data: goodsList
        })
      }
    },
    /**
     * @desc : 删除商品明细
     * @author : 周兴
     * @date : 2024/2/22 18:16
     */
    onCloseDel(e) {
      let deleteFlag = true;
      let item = e.currentTarget.dataset.item
      // 判断删除前是否需要判断
      if (this.data.delValidFlag) {
        // 行不允许删除
        if (item.deleteFlag == false) {
          deleteFlag = false;
        }
      }
      // 不允许删除给出提示信息
      if (!deleteFlag) {
        util.showToast(item.message ? item.message : '不满足删除条件');
      } else {
        const {
          instance
        } = e.detail;
        let index = e.currentTarget.dataset.index
        // 删除明细
        this.delItem(index);
        let goodList = this.data.form[this.data.itemName]
        if (!goodList || goodList.length == 0) {
          // 还原选择组件
          this.handleChooseByItem(this, false);
        }
        instance.close();
      }
      // 外露setGoods方法
      this.triggerEvent("onCloseDel", {

      })

    },
    /**
     * @desc : 根据选品控制来源单
     * @author : 周兴
     * @date : 2024/2/22 18:16
     */
    handleChooseByItem(_this, readonlyReturn) {
      let cardList = _this.data.cardList
      let filters = cardList.filter(it => it.name == 'items');
      let contentObj = _this.data.contentObj;
      if (filters && filters.length > 0 && filters[0].controls && filters[0].controls.length > 0) {
        filters[0].controls.forEach(it => {
          let filter = contentObj[it.card].filter(t => t.code == it.code);
          if (filter && filter.length > 0) {
            filter[0].readonlyReturn = readonlyReturn;
          }
        })
        _this.setData({
          contentObj: contentObj
        })
      }
    },

    setFlgGift(code, flag, key) {
      this.selectComponent('#dkPopup').setFlgGift(code, flag, key)
    },

    /**
     * @desc : 删除商品明细
     * @author : 周兴
     * @date : 2024/2/22 18:16
     */
    delItem(index) {
      let form = this.data.form
      let goodsList = form[this.data.itemName] || []
      let deleteList = form[this.data.deleteName] || []
      // 把删除的数据记录下来
      if (goodsList[index]) {
        deleteList.push(goodsList[index])
      }
      goodsList.splice(index, 1)
      form[this.data.itemName] = goodsList
      form[this.data.deleteName] = deleteList
      //计算
      if (this.data.type == Constants.billType.sale) {
        form = this.setGoodsAmount(form);
      }
      this.setData({
        form: form,
        value: JSON.stringify(form)
      })
      if (this.data.type == Constants.billType.saleOut) {
        this.setGoodsOutAmount()
      }
      if (this.data.type == Constants.billType.saleOutReturn) {
        this.setGoodsOutReturnAmount()
      }

      // 外露setGoods方法
      this.triggerEvent("delItem", {
        data: goodsList
      })
    },
    /**
     * @desc : 设置商品明细金额相关信息  采购订单专用
     * @author : 周兴
     * @date : 2024/1/19
     */
    setPurGoodsAmount(data) {
      let form = this.data.form
      let cardList_ = this.data.cardList
      let goodsList = form[this.data.itemName]
      let sumAmountCol = this.data.sumAmountCol

      goodsList.forEach(element => { //循环出商品总共价格
        element.itemAmt = element.pricePurchase
        element.intoingAmt = element.pricePurchase
        element.intoingQty = element.itemQty
        element.pricePur = element.pricePurchase
        element.priceInto = element.pricePurchase
        element.sumAmountCol = element.pricePurchase * element.itemQty
      })
      cardList_.forEach(card => { //循环出商品总共价格
        if (card.name === 'items') {
          card.itemAmt = goodsList ? goodsList.sum("pricePurchase") : 0
          card.itemQty = goodsList ? goodsList.sum("itemQty") : 0
          card.intoAmt = 0
          card.intoQty = 0
          card[sumAmountCol] = goodsList ? goodsList.sum("sumAmountCol") : 0
          //合计金额
        }
      })
      form[this.data.itemName] = goodsList
      this.setData({
        cardList: cardList_,
        form: form,
        value: JSON.stringify(form)
      })
    },
    /**
     * @desc : 设置商品明细金额相关信息 TODO 
     * @author : 周兴
     * @date : 2024/1/19
     */
    setGoodsAmount(formdata) {
      let cardList = this.data.cardList
      let form = formdata
      console.log('form2', form);
      let index = cardList.findIndex(it => it.name == 'items');
      if (form.goodsList && form.goodsList.length > 0) {
        let sumStandard = Number(0)
        let sumAmount = Number(0)
        let factAmt = Number(0)
        let sumQuantity = Number(0)
        form.goodsList.forEach(item => {
          //标价合计 =  商品标价 x  商品数量
          item.priceStd = item.priceStd ? item.priceStd : (item.priceStandard ? item.priceStandard : 0) //标价
          item.amtStd = Number(item.priceStd * item.itemQty) //合计标价金额
          item.priceSale = item.priceSale ? item.priceSale : item.priceStandard
          item.factPrice = item.factPrice ? item.factPrice : item.priceSale
          item.discount = item.discount ? item.discount : (item.factPrice == 0 ? 0 : 100)
          item.discAmt = item.discAmt ? item.discAmt : 0
          if (item.flgGift) { //赠品
            item.priceSale = Number(0)
            item.factPrice = Number(0)
            item.discount = Number(0)
            item.discAmt = Number(0)
          }
          item.itemAmt = Number(item.priceSale) * Number(item.itemQty) //商品金额
          item.sumPrice = Number(item.priceSale) * Number(item.itemQty) //商品金额
          item.priceDiscount = item.priceStd ? Number((Number(item.priceSale) / Number(item.priceStd)) * 100).toFixed(2) : 100 //售价折扣  
          item.factAmt = Number(item.factPrice) * Number(item.itemQty)
          sumQuantity += Number(item.itemQty) //数量
          sumAmount += Number(item.priceSale) * Number(item.itemQty) //没有售价默认取标价
          factAmt += Number(item.factAmt)
          sumStandard += Number((item.priceStd ? item.priceStd : item.priceStandard)) * Number(item.itemQty)
        })
        //标价合计
        cardList[index].sumStandard = sumStandard
        //销售折扣 (合计金额/标价合计)
        cardList[index].saleDiscount = sumStandard ? Number((sumAmount / sumStandard) * 100).toFixed(2) : 100
        //合计金额
        cardList[index].sumAmount = sumAmount
        cardList[index].sumQuantity = sumQuantity
        //TODO   需要获取当前客户可用预存
        cardList[index].canUseCollect = 0 //可用
        cardList[index].useCollect = 0 //使用 
        form.factAmt = factAmt
        form.sumStandard = sumStandard
        form.saleDiscount = cardList[index].saleDiscount
        form.sumAmount = sumAmount
        form.canUseCollect = 0
        form.useCollect = 0
        form.sumQuantity = sumQuantity
        form.discount = 100 //整单折扣
        form.discAmt = 0 //优惠金额
      } else {
        cardList[index].sumStandard = 0
        cardList[index].saleDiscount = 0
        cardList[index].sumAmount = 0
        cardList[index].canUseCollect = 0
        cardList[index].useCollect = 0

        form.sumStandard = 0
        form.saleDiscount = 0
        form.sumAmount = 0
        form.canUseCollect = 0
        form.useCollect = 0
        form.factAmt = 0
        form.discount = 100 //整单折扣
        form.discAmt = 0 //优惠金额
      }
      console.log('form1', form);
      this.setData({
        cardList: cardList,
      })
      return form
    },
    /**
     * @desc : 设置商品明细金额相关信息 销售出库
     * @author : 于继渤
     * @date : 2024/1/19
     */
    setGoodsOutAmount() {
      let cardList = this.data.cardList
      let form = this.data.form
      let index = cardList.findIndex(it => it.name == 'items');
      if (form.goodsList && form.goodsList.length > 0) {
        let sumAmount = Number(0)
        let factAmt = Number(0)
        let sumQuantity = Number(0)
        let sumStandard = Number(0)
        form.goodsList.forEach(item => {
          //标价合计 =  商品标价 x  商品数量
          item.priceStd = item.priceStandard //标价
          item.outingAmt = item.priceOut * item.outingQty
          sumQuantity += item.outingQty //数量
          if (item.flgGift) { //赠品
            item.priceOut = Number(0)
            item.factPrice = Number(0)
            item.discount = Number(0)
            item.discAmt = Number(0)
          }
          item.discount = item.discount ? item.discount : Number(0)
          item.discAmt = item.discAmt ? item.discAmt : Number(0)
          sumAmount += Number(item.priceOut) * Number(item.outingQty) //销售金额
          factAmt += Number(item.factPrice) * Number(item.outingQty) //实际金额
          sumStandard += (item.priceStd ? item.priceStd : item.priceStandard) * item.outingQty

        })
        //标价合计
        cardList[index].sumStandard = sumStandard
        //销售折扣 (合计金额/标价合计)
        cardList[index].saleDiscount = sumStandard ? Number((sumAmount / sumStandard) * 100).toFixed(2) : 100

        //TODO   需要获取当前客户可用预存
        cardList[index].canUseCollect = 0 //可用
        cardList[index].useCollect = 0 //使用
        form.sumStandard = sumStandard
        form.saleDiscount = cardList[index].saleDiscount
        //合计金额
        cardList[index].sumAmount = sumAmount
        cardList[index].sumQuantity = sumQuantity
        form.sumAmount = sumAmount
        form.factAmt = factAmt
        form.sumQuantity = sumQuantity
        form.outingAmt = sumAmount
        form.outingQty = sumQuantity
      }

      this.setData({
        cardList: cardList,
        form: form
      })
    },
    /**
     * @desc : 设置商品明细金额相关信息 销售出库
     * @author : 于继渤
     * @date : 2024/1/19
     */
    setGoodsOutReturnAmount() {
      let cardList = this.data.cardList
      let form = this.data.form
      let index = cardList.findIndex(it => it.name == 'items');
      if (form.goodsList && form.goodsList.length > 0) {
        let sumAmount = Number(0)
        let sumQuantity = Number(0)
        form.goodsList.forEach(item => {
          //标价合计 =  商品标价 x  商品数量
          item.priceStd = item.priceStandard //标价
          item.outingQty = item.outingQty ? item.outingQty : item.itemQty * -1
          item.outingAmt = item.priceOut * item.outingQty
          item.priceOut = item.priceOut
          sumQuantity += Number(item.outingQty) //数量
          sumAmount += item.priceOut * item.outingQty //没有售价默认取标价
        })

        //合计金额
        cardList[index].sumAmount = sumAmount
        cardList[index].sumQuantity = sumQuantity
        form.sumAmount = sumAmount
        form.sumQuantity = sumQuantity
        form.outingAmt = sumAmount
        form.outingQty = sumQuantity
      } else {
        let sumAmount = Number(0)
        let sumQuantity = Number(0)
        //合计金额
        cardList[index].sumAmount = sumAmount
        cardList[index].sumQuantity = sumQuantity
        form.sumAmount = sumAmount
        form.sumQuantity = sumQuantity
        form.outingAmt = sumAmount
        form.outingQty = sumQuantity
      }
      this.setData({
        cardList: cardList,
        form: form
      })
    },
    /**
     * @desc :  销售出库设置选品参数
     * @author : 于继渤
     * @date : 2024/1/19
     */
    setGoodsOutList(data) {
      let form = this.data.form
      if (form.goodsList && form.goodsList.length > 0) {
        form.goodsList.forEach(item => {
          item.priceOut = item.priceOut ? item.priceOut : item.priceStandard
          item.factPrice = item.priceOut
          item.factAmt = Number(item.factPrice) * Number(item.itemQty)
          item.outingAmt = Number(item.priceOut) * Number(item.itemQty)
          item.outingQty = item.itemQty
          item.discount = item.factPrice == 0 ? 0 : 100
          item.discAmt = 0
          item['outingQty_min'] = 1
          delete item['priceLimited']
          delete item['pricePurchase']
          delete item['priceWholesale']
        })
      }
      this.setData({
        form: form
      })
      this.setGoodsOutAmount()
    },
    /**
     * @desc : 数量改变事件
     * @date : 2024/2/1 15:49
     * @author : 于继渤
     */
    changeStep(e) {
      let key = e.detail.key
      let value = e.detail.value
      let dataItem = JSON.parse(this.data.dataItem)
      dataItem[key] = value
      if (isNaN(Number(value))) { //value是字符串类型的数字 故不能用 (typeof myVar === 'number'来判断
        dataItem['box'] = 0
        dataItem['piece'] = 0
        dataItem.packageBox = undefined
        dataItem.sumPrice = 0
        this.setData({
          dataItem: JSON.stringify(dataItem)
        })
        return
      }
      if (this.data.type == Constants.billType.sale ||
        this.data.type == Constants.billType.pur ||
        this.data.type == Constants.billType.purInto) {
        //箱片的取值逻辑： 箱 =(数量/包装）取整，片= （数量%包装）取余数
        if (key == 'itemQty') {
          dataItem['itemQty'] = value
          dataItem['box'] = parseInt(value / dataItem.packBox)
          dataItem['piece'] = parseFloat(Number(value % dataItem.packBox).toFixed(dataItem.decimalPlaces));
          dataItem.packageBox = dataItem['box'] + dataItem.subUnitName + dataItem['piece'] + dataItem.unitName
          if (this.data.type == Constants.billType.pur) { //采购订单 ，单价*数量，实时变化
            dataItem.sumPrice = dataItem['itemQty'] * dataItem.pricePur
          }
          if (this.data.type == Constants.billType.purInto) { //采购入库
            dataItem.sumPrice = dataItem['itemQty'] * dataItem.priceInto
          }
          if (this.data.type == Constants.billType.sale) { //订单
            dataItem.sumPrice = dataItem['itemQty'] * dataItem.priceSale
            dataItem.factAmt = dataItem['itemQty'] * dataItem.factPrice
          }
        }

      } else if (this.data.type == Constants.billType.inventory) {
        //箱片的取值逻辑： 箱 =(数量/包装）取整，片= （数量%包装）取余数
        if (key == 'outingQty') {
          dataItem['outingQty'] = value
          dataItem['box'] = parseInt(value / dataItem.packBox)
          dataItem['piece'] = parseFloat(Number(value % dataItem.packBox).toFixed(dataItem.decimalPlaces));
          dataItem.packageBox = dataItem['box'] + dataItem.subUnitName + dataItem['piece'] + dataItem.unitName
        }

      } else if (this.data.type == Constants.billType.saleOut) {
        //箱片的取值逻辑： 箱 =(数量/包装）取整，片= （数量%包装）取余数
        if (key == 'outingQty') {
          dataItem['outingQty'] = value
          dataItem['box'] = parseInt(value / dataItem.packBox)
          dataItem['piece'] = parseFloat(Number(value % dataItem.packBox).toFixed(dataItem.decimalPlaces));
          dataItem.packageBox = dataItem['box'] + dataItem.subUnitName + dataItem['piece'] + dataItem.unitName
          dataItem.sumPrice = dataItem['outingQty'] * dataItem.priceOut
          dataItem.factAmt = dataItem['outingQty'] * dataItem.factPrice
        }

      } else if (this.data.type == Constants.billType.saleOutReturn) {
        if (key == 'outingQty') {
          console.log('outingQty', -Math.abs(value));
          dataItem['outingQty'] = -Math.abs(value)
          // 使用 Math.abs() 函数获取 outingQty 的绝对值  
          let outingQty = Math.abs(dataItem.outingQty);
          dataItem['box'] = parseInt(outingQty / dataItem.packBox)
          dataItem['piece'] = parseFloat(Number(outingQty % dataItem.packBox).toFixed(dataItem.decimalPlaces));
          dataItem.packageBox = dataItem['box'] + dataItem.subUnitName + dataItem['piece'] + dataItem.unitName
          dataItem.sumPrice = dataItem['outingQty'] * dataItem.priceOut
        }
      }
      this.setData({
        dataItem: JSON.stringify(dataItem)
      })
    },
    /**
     * @desc   : 修改数值离开框的值
     * @author : 周兴
     * @date   : 2024/2/2 11:46
     */
    blurNumberField(e) {
      console.log("blurNumberField", e);
      let dataItem = JSON.parse(this.data.dataItem)
      let key = e.detail.key
      // 采购订单
      if (this.data.type == Constants.billType.pur) {
        dataItem.pricePur = e.detail.value
        dataItem.sumPrice = dataItem['itemQty'] * dataItem.pricePur
      }
      if (this.data.type == Constants.billType.purInto) { //采购入库
        dataItem.priceInto = e.detail.value
        dataItem.sumPrice = dataItem['itemQty'] * dataItem.priceInto
      }
      if (this.data.type == Constants.billType.saleOutReturn ||
        this.data.type == Constants.billType.out ||
        this.data.type == Constants.billType.inventory

      ) { //销售退货
        dataItem.priceOut = e.detail.value
        dataItem.sumPrice = dataItem['itemQty'] * dataItem.priceOut
      }
      if (this.data.type == Constants.billType.saleOutReturn ||
        this.data.type == Constants.billType.inventory) {
        if (key == "priceOut") {
          dataItem.priceOut = e.detail.value
          dataItem.sumPrice = dataItem['outingQty'] * dataItem.priceOut
        }
      }
      if (this.data.type == Constants.billType.saleOut) {
        if (key == "priceOut") {
          dataItem.priceOut = e.detail.value
          dataItem.sumPrice = dataItem['outingQty'] * dataItem.priceOut
        }
      }
      if (this.data.type == Constants.billType.sale) {
        //实际合计金额=合计金额*折扣  
        if (key == "discount") { //销售价格是0的时候  折扣锁死 不让改
          dataItem.discount = e.detail.value
          dataItem.factPrice = Number(dataItem.priceSale) * Number((Number(dataItem.discount) / 100)) //实际单价  = 销售单价*折扣 
          dataItem.factAmt = dataItem.factPrice * dataItem['itemQty'] //实际销售金额= 实际销售单价*数量
        }
      }
      this.setData({
        dataItem: JSON.stringify(dataItem)
      })
    },
    /**
     * @desc   : 销售订单价格反算的时候 明细弹出pop修改价格点击再弹pop的离焦事件
     * @author : 王英杰
     * @date   : 2024/2/2 11:46
     */
    blurChangePriceNumberField(e) {
      let dataItem = JSON.parse(this.data.dataItemChangePrice)
      let key = e.detail.key
      // 使用 Math.round() 方法进行四舍五入取整 
      if (key == "factPrice") { //改实际单价  
        dataItem.factPrice = e.detail.value //实际单价
        dataItem.factAmt = dataItem.factPrice * dataItem['itemQty'] //实际金额=实际单价*数量 
        dataItem.discount = Math.round(dataItem.priceSale ? (Number(dataItem.factPrice) / Number(dataItem.priceSale) * 100) : 100) //明细折扣 = 实际单价 /售价 
      } 
      if (key == "factAmt") { // 改实际金额
        dataItem.factAmt = e.detail.value //实际金额
        dataItem.factPrice = Math.round( (Number(dataItem.factAmt) / Number(dataItem.itemQty) * 100) )/100 //明细单价 = 金额 /数量
        dataItem.discount =dataItem.priceSale ? Math.round( (Number(dataItem.factPrice) / Number(dataItem.priceSale) * 100) ) : 100 //明细折扣 = 实际单价 /售价 (对浮点数乘以100（将小数点右移两位），然后使用Math.round()进行四舍五入，最后再除以100（将小数点左移两位）来得到结果)
      }
      this.setData({
        dataItemChangePrice: JSON.stringify(dataItem)
      })

    },
    /**
     * @desc :  销售出库退货设置选品参数
     * @author : 于继渤
     * @date : 2024/1/19
     */
    setGoodsOutReturnList(data) {
      let form = this.data.form
      if (form.goodsList && form.goodsList.length > 0) {
        form.goodsList.forEach(item => {
          item.priceOut = item.priceOut ? item.priceOut : item.priceStandard
          item.outQty = item.itemQty
          item.outingQty = item.outingQty
          item.outAmt = item.priceOut * item.itemQty
          // item.outingAmt = item.priceOut * item.itemQty
          // item['outingQty_min'] = item.itemQty * -1
          console.log('3333333333');
          item['outingQty_max'] = Number(0)
          delete item['priceLimited']
          delete item['pricePurchase']
          delete item['priceWholesale']
        })
      }
      this.setData({
        form: form
      })
      this.setGoodsOutReturnAmount()
    },
    /**
     * @desc   : 显示tip
     * @author : 周兴
     * @date   : 2024/4/25 11:46
     */
    showTip(e) {
      let item = e.currentTarget.dataset.item
      if (item.tip) {
        util.showToast(item.tip);
      }
    },
    /**
     * @desc : 设置商品明细金额相关信息 TODO 
     * @author : 周兴
     * @date : 2024/1/19
     */
    setReturnGoodsAmount(data) {
      let cardList = this.data.cardList
      let form = this.data.form
      let index = cardList.findIndex(it => it.name == 'items');
      if (form.goodsList && form.goodsList.length > 0) {
        let sumReturnAmt = Number(0) //合计金额
        let sumReturnQty = Number(0)
        form.goodsList.forEach(item => {
          item.priceReturn = item.priceStandard * -1
          item.priceInto = item.priceStandard
          item.itemAmt = Number(item.priceReturn * item.itemQty)
          item.itemQtyMax = -1
          if (item.itemId) { //选商品过来的 不校验  选单据校验
            item.itemQtyMin = item.usableQty * -1
          } else {
            item.itemQtyMin = -99999999999
          }
          // sumReturnAmt += item.priceReturn * item.itemQty
          // sumReturnQty += item.itemQty
          item.itemQty = item.itemQty * -1
        })
        // form.sumReturnQty = sumReturnQty
        // form.sumReturnAmt = sumReturnAmt
        // cardList[index].sumReturnAmt = sumReturnAmt
      }
      console.log('form', form)
      this.setData({
        cardList: cardList,
        form: form
      })
    },
    /**
     * @desc : 上传附件超出大小
     * @author : 周兴
     * @date : 2024/3/27 16:16
     */
    overSize(e) {
      util.showToast('文件超出' + Constants.uploadFileConfig.maxSizeMB);
    },
    /**
     * @desc : 删除图片
     * @author : 周兴
     * @date : 2024/4/29 18:16
     */
    deleteImg(e) {
      let form = this.data.form;
      let index = e.detail.index;
      let arr = form['annexPaths']
      arr.splice(index, 1);
      this.setData({
        form: form,
        value: JSON.stringify(form)
      })
    },
    changeCheckBox(e) {
      this.triggerEvent("changeCheckBox", {
        code: e.detail.code,
        flag: e.detail.flag
      })
    },
    /**
     * @desc : 上传附件
     * @author : 周兴
     * @date : 2024/3/27 16:16
     */
    afterRead(e) {
      // this.setData({
      //     uploadFileFlag:true
      // })
      const _this = this;
      const {
        file
      } = e.detail;
      // 配置存放的文件夹 公司code - 表名 - 年月 -- 日
      let folder = app.globalData.company.cpCode + '/' + this.data.table + '/' + new Date().toYearMonth2() + '/' + new Date().toDay()
      let url = config.upload_url;
      url = url.replace('mdm-server', 'mdm-server' + '-' + app.globalData.company.svcCode.replace('_', '-'))
      file.forEach(res => {
        // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
        wx.uploadFile({
          url: url,
          filePath: res.path,
          name: 'file',
          formData: {
            'folder': folder
          },
          header: {
            'Authorization': 'Bearer ' + app.globalData.token
          },
          success(res) {
            let data = JSON.parse(res.data)
            if (data.code === 200) {
              _this.setData({
                uploadFileFlag: false
              })
              let form = _this.data.form
              let annexPaths = form['annexPaths'] || []
              data.data.url = config.image_url + data.data.path
              annexPaths.push(data.data)
              form.annexPaths = annexPaths;
              _this.setData({
                form: form,
                value: JSON.stringify(form)
              })
            }
          },
        })
      })
    },
    customersChoose() {
      this.triggerEvent("customersChoose", {})
    },
    /**
     * @desc : 加载数据
     * @date : 2024/2/1 15:49
     * @author : 姜永辉
     */
    setInitPopData() {

      //销售订单 //采购订单 库存调整
      if (this.data.type == Constants.billType.sale || this.data.type == Constants.billType.pur || this.data.type == Constants.billType.out) {
        this.setData({
          popContentBox: [{
              code: 'box',
              type: 'step',
              title: mixins.$t("goodsBox"),
              required: true
            },
            {
              code: 'piece',
              type: 'step',
              title: mixins.$t("goodsPiece"),
              required: true
            },
          ],

        })
      }
      //销售出库
      if (this.data.type == Constants.billType.saleOut) {
        this.setData({
          popContentBox: [{
              code: 'box',
              type: 'step',
              title: mixins.$t("goodsBox"),
              required: true
            },
            {
              code: 'piece',
              type: 'step',
              title: mixins.$t("goodsPiece"),
              required: true
            },
          ],

        })
      }
      //销售退货
      if (this.data.type == Constants.billType.saleOutReturn) {
        this.setData({

        })
      }
      //采购退货
      if (this.data.type == Constants.billType.purReturn) {
        this.setData({

        })
      }


      //采购入库 销售退货
      if (this.data.type == Constants.billType.purInto || this.data.type == Constants.billType.saleOutReturn) {
        this.setData({
          popContentBox: [{
              code: 'box',
              type: 'step',
              title: mixins.$t("goodsBox"),
              required: true
            },
            {
              code: 'piece',
              type: 'step',
              title: mixins.$t("goodsPiece"),
              required: true
            },
          ],

        })
      }


    },
    /**
     * @desc : 获取语言的方法
     * @author : 周兴
     * @date : 2024/1/19
     */
    $t(name) {
      if (this.data.$t[name]) {
        return this.data.$t[name];
      } else {
        return name;
      }
    },
  },
})