/*******************************************************************************
 * Copyright(c) 2024 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：
 *		2.功能描述：以销定采查询方法
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		王英杰		  	     2024-1-19       			1.00		       	新建
 *******************************************************************************/
const mixins = require('@/mixins/index.js')
const common = require('@/utils/common.js')
const util = require('@/utils/util.js')
const Constants = require('@/utils/Constants.js');
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
      // 查询条件
      searchContent: [
        { code: 'makeTime', title:  mixins.$t('pastThirthtyDays'), defaultValue: 5, searchType: Constants.searchType.date }, 
        { code: 'org', title: mixins.$t('orgId'), dropType: 'org' },
        { code: 'choose', title: mixins.$t('choose'), searchType: Constants.searchType.pick }
      ],
          // 查询条件-筛选
    pullMenuList: [
      { code: 'cusFrom', pullType: 'mSelect', typeName: 'cusFrom' },
      { code: 'followCount', dataType: 'number' },
      { code: 'nextFollowTime', dataType: 'date' }
    ],
    selectTotallength: 0,
    selectlength: 0,
  
    // 弹出按钮
    buttonList: null,
    contentList: [{
      code: 'flag',
      title:  mixins.$t('allChoose'),//'全选',
      type: 'checkbox'
    }, {
      code: 'count',
      type: 'count',
      bill: 2,
      quantity: 3
    }],
    btnFormData: null,
    // 路由
    routeObjName: 'order',
  },


  /**
   * @desc : 查询 purSaleListBy
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  getData(params) {
    return this.excute(this.data.service, this.data.service.purSaleListBy, params);
  },
  /**
   * @desc : 展开 显示更多商品
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  collapseFlagChange(e) {
    let index = e.currentTarget.dataset.index
    let list = this.data.tableData
    list[index].collapseFlag = !list[index].collapseFlag
    this.setData({
      tableData: list
    })
  },
   /**
   * @desc : 处理选择完事后得数量合计
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  selectTotallength(list) { 
    let chooseList = []
    // let countindex = this.data.contentList.filter(it => it.code == 'count');
    // contentList[countindex].bill
    // contentList[countindex].quantity 
    list.forEach(res => {
      if (res.orderItemResponseList && res.orderItemResponseList.length > 0) {
        let tempList = res.orderItemResponseList.filter(itt => {
          return itt.checked
        })
        chooseList = chooseList.concat(tempList)
      }
    })
    if (chooseList.length > 0) {
      let selectlength = Number(0)
      let selectTotallength = Number(0)
      chooseList.forEach(item => {
        selectlength += Number(item.itemQty)
        selectTotallength += Number(item.itemAmt * item.itemQty)
      })
      this.setData({
        selectlength: selectlength,
        selectTotallength: common.toThousandCents(selectTotallength)
      })
    } else {
      this.setData({
        selectlength: 0,
        selectTotallength: 0
      })
    }


  },
  /**
   * @desc : 整个信息复选框
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  onListChange(e) {
    console.log(e)
    let index = e.currentTarget.dataset.index
    let list = this.data.tableData
    list[index].checked = !list[index].checked
    if (list[index].orderItemResponseList && list[index].orderItemResponseList.length > 0) {
      list[index].orderItemResponseList.forEach(res => {
        res.checked = list[index].checked
      })
    }
    this.selectTotallength(list)
    this.setData({
      tableData: list
    })
  },

  /**
   * @desc : 商品复选框
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  onListItemChange(e) {
    let index_ = e.currentTarget.dataset.index_
    let index = e.currentTarget.dataset.index
    let list = this.data.tableData
    let orderItemResponseList = list[index].orderItemResponseList
    orderItemResponseList[index_].checked = !orderItemResponseList[index_].checked
    this.selectTotallength(list)
    this.setData({
      tableData: list
    })
  },


  /**
   * @desc : 全选
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  onListAllChange(e) {
    console.log(e)
    this.setData({
      checkedAllFlag: e.detail
    })
    let list = this.data.tableData
    if (list && list.length > 0) {
      list.forEach(res => {
        res.checked = e.detail
        res.orderItemResponseList = res.orderItemResponseList.map(item => ({
          ...item,
          checked: e.detail
        }));
      })
    }
    let goodsListLength = list.filter(itt => {
      return itt.checked
    })

    this.setData({
      tableData: list,
      selectlength: e.detail ? goodsListLength.length : 0, //单数
    })
  },
  /**
   * @desc : 按钮点击事件回调
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  open(e) {
   let that = this
    let code = e.detail.name
    if (code == "cancel") { //取消点击事件
      wx.navigateBack({
        delta: 1
      })
    }
    if (code == "save") { //确定点击事件  如果是从采购订单过来的 就应该返回去  如果不是 要打开采购订单新建页
      let  list = this.data.tableData
      let chooseList = []
      list.forEach(res => {
        if (res.orderItemResponseList && res.orderItemResponseList.length > 0) {
          let tempList = res.orderItemResponseList.filter(itt => {
            return itt.checked
          })
          chooseList = chooseList.concat(tempList)
        }
      })
      if (!(chooseList && chooseList.length > 0)) {
        //"请至少选择一件商品" ,
        util.showToast(mixins.$t('pleaseChoose'));
      }
      if (that.data.isSourceOrderFlag) {
        const eventChannel = that.getOpenerEventChannel();
        eventChannel.emit('bindData', {
          data: {
            choooseItemList: chooseList
          }
        })
        wx.navigateBack({
          delta: 1
        })
      }else{ //打开采购订单新建 并把品 传过去 
          
        wx.navigateTo({
          url: that.data.route.openPurchase.url,   
          success: function (res) { 
            res.eventChannel.emit('params', {choooseItemList: chooseList ,isSourceOrderFlag:that.data.isSourceOrderFlag})
          }
        })
      }
    }

  },
  /**
   * @desc : 页面初始化加载方法
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  loadInit() {
    // 获取页面栈
    const pages = getCurrentPages();
    const prevPage = pages[pages.length - 2]; // 上一个页面
    // 判断上一个页面是哪个页面   true 就是 从采购订单进入的  false则是 从其他页面进来的
    this.setData({
      isSourceOrderFlag: (prevPage.route == 'package-purchase/pages/purchase-order/add/add')
    })
    if (this.data.isSourceOrderFlag) {
      this.setData({
        // 弹出按钮
        buttonList: [{
          code: "cancel",
          plain: true,
          title:  mixins.$t('cancel'),//'取消'
        }, {
          code: "save",
          title: mixins.$t('confirm'),//'确定'
        }],
      })
    } else {
      this.setData({
        // 弹出按钮
        buttonList: [{
          code: "save",
          title: mixins.$t('confirm'),//'确定'
        }],
      })
    }
  },

})