const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util.js')
const mixins = require('@/mixins/index.js')
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    // 弹出按钮
    buttonList: [{
      name: 'merge',
      title: mixins.$t("confirm"), //'保存'
    }],
    contentList: [
    //   {
    //   code: 'flag',
    //   title: mixins.$t("allChoose"), //'全选',
    //   type: 'checkbox'
    // },
     {
      code: 'amount',
      content: 0,
      title: mixins.$t("totalAmount"), //'合计金额',
      type: 'number'
    }],
    btnFormData: null,
    returnTotalAmount: 0,
    allChecked: false,
    // 路由
    routeObjName: 'purchase',
    tableData: [],
    chooosepurchaseItemResponseList: [],
    isfirst: true,
  },
  /**
   * @desc :   加载数据源
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  getData(params) {
    return this.excute(this.data.service, this.data.service.selectListByItem, params);
  },


  /**
   * @desc :   父级商品信息选择
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  onListItemChange(e) { 
    let tableData = this.data.tableData
    let index = e.currentTarget.dataset.index
    let chooseList = this.data.chooseList
    if(!tableData[index].checked){ //单选 如果是勾选

      if(chooseList.length>0){// 已经选择了其他单据
        if(tableData[index].purNo ==chooseList[0].purNo ){//相等 说明是一个单据下的不同品 那就直接添加到 chooseList 里
          chooseList  =  tableData[index].purchaseItemResponseList 

        }else{ //不相等 那就说明选择了其他的单据 弹出消息提示
          util.showToast('已经选了单据【'+chooseList.purNo+"】,请先勾掉已选单据");
          return
        } 
      }else{ //没选的话 直接把单据数据赋给已选list
        chooseList  =  tableData[index].purchaseItemResponseList 
      }
    }else{ //本次要勾掉数据 那就删除 已选list 里的数据
      tableData[index].purchaseItemResponseList.forEach(element => {
        chooseList.forEach((item,index) => {
             if(element.itemId == item.itemId){
               chooseList.splice(index, 1);
             }
           });
      }); 
    }
    tableData[index].checked = !tableData[index].checked

    let purchaseItemResponseList = tableData[index].purchaseItemResponseList
    if (purchaseItemResponseList && purchaseItemResponseList.length > 0) {
      purchaseItemResponseList.forEach(res => {
        res.checked = tableData[index].checked
      })
    }

    this.setData({
      tableData: tableData,
      chooseList:chooseList
    })
    this.calculateTotal()
  },
  /**
   * @desc :   子级商品信息选择
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  onChangeItemCheckbox(e) {
    let tableData = this.data.tableData
    let index = e.currentTarget.dataset.index
    let index_ = e.currentTarget.dataset.index_
    let chooseList = this.data.chooseList
    if(!tableData[index].purchaseItemResponseList[index_].checked){ //单选 如果是勾选 
      if(chooseList.length>0){// 已经选择了其他单据
        if(tableData[index].purchaseItemResponseList[index_].purNo ==chooseList[0].purNo ){//相等 说明是一个单据下的不同品 那就直接添加到 chooseList 里
          chooseList.push(tableData[index].purchaseItemResponseList[index_]) 

        }else{ //不相等 那就说明选择了其他的单据 弹出消息提示
          util.showToast('已经选了单据【'+chooseList.purNo+"】,请先勾掉已选单据");
          return
        } 
      }else{ //没选的话 直接把单据数据赋给已选list
         
        chooseList.push(tableData[index].purchaseItemResponseList[index_]) 
      }
    }else{ //本次要勾掉数据 那就删除 已选list 里的数据 
         
        chooseList.forEach((item,itemindex) => {
             if(tableData[index].purchaseItemResponseList[index_].itemId == item.itemId){
               chooseList.splice(itemindex, 1);
             }
           }); 
    } 
    tableData[index].purchaseItemResponseList[index_].checked = !tableData[index].purchaseItemResponseList[index_].checked
    let numberPlus = 0
    let numberReduce = 0 
    tableData[index].purchaseItemResponseList.forEach(element => { //循环出 商品是否全部勾选或者勾掉 从而更改单据的总单的 勾选
      if(element.checked){
        numberPlus++
      }else{
        numberReduce++
      }
    });
   if(numberPlus == tableData[index].purchaseItemResponseList.length ){//选了全部
    tableData[index].checked = true
   }
   if(numberReduce == tableData[index].purchaseItemResponseList.length ){//勾掉全部
    tableData[index].checked = false
   }
    this.setData({
      tableData: tableData,
      chooseList:chooseList
    })

    this.calculateTotal()
  },

  /**
   * @desc :  全退
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  onCheckboxChange(e) {
    let btnFormData = this.data.btnFormData ? JSON.parse(this.data.btnFormData) : ''
    let checked = btnFormData ? btnFormData.flag : false
    let tableData = this.data.tableData
    tableData.forEach(res => {
      res.checked = checked
      if (res.purchaseItemResponseList && res.purchaseItemResponseList.length > 0) {
        res.purchaseItemResponseList.forEach(item => {
          item.checked = checked
        })
      }
    })
    this.setData({
      tableData: tableData
    })
    this.calculateTotal()
  },

  /**
   * @desc :   计算金额/过滤出已选数据
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  calculateTotal() {
    let tableData = this.data.tableData
    let chooosepurchaseItemResponseList = []
    let returnTotalAmount = Number(0)
    //过滤出已选数据
    tableData.forEach(res => {
      if (res.purchaseItemResponseList && res.purchaseItemResponseList.length > 0) {
        res.purchaseItemResponseList.forEach(item => {
          if (item.checked) {
            chooosepurchaseItemResponseList.push(item)
          }
        })
      }
    })
    if (chooosepurchaseItemResponseList && chooosepurchaseItemResponseList.length > 0) {
      chooosepurchaseItemResponseList.forEach(res => {
        returnTotalAmount += (Number(res.itemAmt*res.itemQty))
      })
    }
    let contentList = this.data.contentList
    contentList.forEach(element => {
      if (element.code == 'amount') {
        element.content = returnTotalAmount
      }
    });
    this.setData({
      chooosepurchaseItemResponseList: chooosepurchaseItemResponseList,
      returnTotalAmount: returnTotalAmount,
      contentList: contentList
    })
  },
  /**
   * @desc :   跳转回到新建入库
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  toAdd() {
    let chooosepurchaseItemResponseList = this.data.chooosepurchaseItemResponseList 
    let that = this
    let formData = that.data.formData
    let item = that.data.item 
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.emit('bindData', {
      data: {
        chooosepurchaseItemResponseList: chooosepurchaseItemResponseList
      }
    })
    wx.navigateBack({
      delta: 1
    })

  },
  /**
   * @desc :设置查询参数
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  setSearchParams(params) {
    console.log('ddd', this.data.item);
    let item = this.data.item
    let isfirst = this.data.isfirst
    if (isfirst) { //第一次进入 取传过来的  
      let searchForm = this.data.searchForm ? JSON.parse(this.data.searchForm) : {}
      searchForm.searchText = item.purNo||''
      this.setData({
        chooseList: item.chooseList||[],
        searchForm: JSON.stringify(searchForm),
        searchText: item.purNo ||'',
      })
      params.searchForm = JSON.stringify(searchForm)
      params.searchText = item.purNo
    }
    this.setData({
      isfirst: false
    })  
    return params 
  },
   /**
     * @desc :   处理接口返回数据 (根据入库页面传过来的已选商品 回显)
     * @date : 2024年3月8日
     * @author : 王英杰
     */
    handleSearchData(tableData) { 
      let  chooseList = this.data.chooseList||[]
      if(chooseList.length>0){
        let tableData = this.data.tableData
        let chooseNum = 0
        tableData.forEach(element => {
           if(element.purNo == chooseList[0].purNo){
            element.purchaseItemResponseList.forEach(item => { 
              chooseList.forEach(chooseitem => { 
                if(item.itemId==chooseitem.itemId){
                  item.checked = true
                  chooseNum++
                }
               });
             });
             if(chooseNum ==  element.purchaseItemResponseList.length){
              element.checked = true
             }
           }
        });
        this.setData({
          tableData
        })
        this.calculateTotal()
      }  
    },

})