const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util.js')
const mixins = require('@/mixins/index.js')
const app = getApp()
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    isfirst: true,
     // 系统参数
     flgHandleSetting: app.globalData.setting[Constants.settingCode.IVT001] == "true" ? true : false,
     tagList: [{ title: mixins.$t("all"), code: '' }, { title: mixins.$t("inBoundingIntoStatusName"), code: mixins.$t("inBoundingIntoStatus")  }, { title: mixins.$t("inBoundedIntoStatusName"), code: mixins.$t("inBoundedIntoStatus")  }],
    // 查询条件
    searchContent: [{ code: 'createtime', title: mixins.$t("pastThirthtyDays"), defaultValue: 5, searchType: Constants.searchType.date }, { code: 'choose', title: mixins.$t("choose"), searchType: Constants.searchType.pick }],
    // 查询条件-筛选
    pullMenuList: [

      { code: 'purchaseStatus', pullType: 'mSelect', typeName: 'purchaseStatus' },
      { code: 'contactName', dataType: '' },
      { code: 'deliveryPhone', dataType: '' },
      { code: 'addressFull', dataType: '' },
      { code: 'makingTime', dataType: 'date' }, { code: 'deliveryTime', dataType: 'date' }],
    // 列表区（脚部金额）
    footerAmount: { name: 'returnAmt', title: mixins.$t("totalAmount") },
    // 列表区（脚部信息）
    footerInfo: [{ name: 'returnQty', title: mixins.$t("piece"), prefix: mixins.$t("together"), }],
    intoStatus:null,
    // 列表区（内容）
    contentList: [
      { name: 'intoNo', title: mixins.$t("saleOrderNo") },
      {
        name: [{ name: 'contactName', title: '' },
        { name: 'contactPhone', title: '' }], title: mixins.$t("returnInfo")
      },
      { name: 'returnAddress', title: mixins.$t("returnAddress") },
      {
        name: [{ name: 'orgName', title: '' },
        { name: 'staffName', title: mixins.$t("staffId") }], title: mixins.$t("storeInformation")
      }],

    // 弹出按钮
    buttonList: [{
      name: 'editInto',
      title: mixins.$t('edit'),
    }, { name: 'invalid', title: mixins.$t("invaidOrder") }],
    // 路由
    // routeObjName: 'inbound',
    routeObjName: 'inboundReturn',
    tableData: [],
    active: 0,
    tabsActive: 0,
    buttonSaveList: [{ code: 'add', title: mixins.$t("add") }],
    contentSaveList: [{ code: 'amount', content: 0, type: 'number', title: mixins.$t("totalAmount") }, {
      type: 'count',
      code: 'count',
      bill: 0,
      quantity: 0
    }],
  },
  /**
   * @desc :   三个小点打开
   * @date : 2024年3月8日
   * @author : 于继渤 
   */
  toPoint(e) {
    let item = e.detail.item
    let buttonList = null
    if (!item.flgValid) { //作废
      buttonList = [{
        name: 'printedBill',
        title: mixins.$t("printedBill")
      }, ] //打印
    } else {
      // 弹出按钮
      buttonList = [{
        name: 'editInto',
        title: mixins.$t('edit'),
      }, { name: 'invalid', title: mixins.$t("invaidOrder") },{
        name: 'printedBill',
        title: mixins.$t("printedBill")
      }, ]
    }
    this.setData({
      // 弹出按钮
      buttonList: buttonList
    })
  },
  /**
 * @desc : 跳转明细
 * @date : 2024/2/1 15:49
 * @author : 于继渤
 */
  toDetail(e) {
    console.log(e)
    let item = e.detail.item
    wx.navigateTo({
      url: this.data.route.detail.url,
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据 TODO 测试例子url是写死的，实际中，需要从route中读取
        res.eventChannel.emit('params', { id: item.intoId, formMode: Constants.formMode.edit, item: item })
      }
    })
  },

  editInto(e) {
   
    let item = e
    console.log('跳转编辑formMode',e)
    wx.navigateTo({
      url: this.data.route.edit.url,
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据 TODO 测试例子url是写死的，实际中，需要从route中读取
        res.eventChannel.emit('params', { id: item.intoId, formMode: Constants.formMode.edit, item: item })
      }
    })
  },


  /**
 * @desc : 切换
 * @date : 2024/2/1 15:49
 * @author : 于继渤
 */
onChangeTabs(e) {
  let code = e.detail.detail.code
  console.log('onChangeTabs',e)
  this.setData({
    intoStatus: code,
  })
  this.searchData()
},
  /**
   * @desc : 设置查询参数
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  setSearchParams(params) {
    let tabsActive = this.data.tabsActive
    params.intoType = '入库类型-采退出库'
    // if (tabsActive == 0) { //出库状态
    //   params.intoStatus = ''
    // } else if (tabsActive == 1) {
    //   params.intoStatus = mixins.$t("inBoundingIntoStatus")
    // } else if (tabsActive == 2) {
    //   params.intoStatus = mixins.$t("inBoundedIntoStatus")
    // }
    params.intoStatus = this.data.intoStatus == 'all' ? '' : this.data.intoStatus
    if (this.data.flgHandleSetting) {
      params.intoStatus = mixins.$t("inBoundedIntoStatus") 
    }
    //采购订单明细 跳转到采购退货  
    let isfirst = this.data.isfirst
    let purNo = this.data.item ? this.data.item.purNo : null
    if (isfirst && purNo) { //第一次进入 取传过来的  
      let searchForm = this.data.searchForm ? JSON.parse(this.data.searchForm) : {}
      searchForm.searchText = purNo || ''
      this.setData({
        searchForm: JSON.stringify(searchForm),
        searchText: purNo || '',
      })
      params.searchForm = JSON.stringify(searchForm)
      params.searchText = purNo
    }
    this.setData({
      isfirst: false
    })

    return params
  },




  /**
   * @desc :   加载数据源
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  getData(params) {
    return this.excute(this.data.service, this.data.service.selectByCond, params);
  },

  /**
  * @desc :   处理接口返回数据
  * @date : 2024年3月8日
  * @author : 于继渤
  */
  handleSearchData(tableData) {
    //计算底部金额
    this.calculateTotalAmount()
  },

  /**
  * @desc :   计算底部合计金额等
  * @date : 2024年4月8日
  * @author : 于继渤
  */
  calculateTotalAmount() {
    let tableData = this.data.tableData
    let contentSaveList = this.data.contentSaveList
    let sumAmount = Number(0)
    let sumQuantity = Number(0)
    tableData.forEach(res => {
      sumAmount += res.returnAmt
      sumQuantity += res.returnQty
    })
    contentSaveList[0].content = sumAmount
    contentSaveList[1].bill = tableData.length
    contentSaveList[1].quantity = sumQuantity * -1
    this.setData({
      contentSaveList: contentSaveList
    })

  },
  /**
  * @desc :   作废
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  invalid(e) {

    this.setData({
      loading: true
    })
    return this.excute(this.data.service, this.data.service.cancel, {
      intoId: e.intoId
    }).then(res => {
      if (res.data.code == Constants.SUCESS_CODE) {
        wx.showToast({
          title: mixins.$t('invalidSuccessfully'),
          image: '/static/image/success.png',
          duration: 1000
        })
        // 查询
        this.searchData();
      }
      this.setData({
        loading: false
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },


})