/*******************************************************************************
 * Copyright(c) 2022 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：
 *		2.订单开单
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		于继渤		        2022-0-12	            1.00		   	    
 *******************************************************************************/
const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util.js')
const mixins = require('@/mixins/index.js')
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据 
   */
  data: {
    tagList: [{ title: "全部", code: '' }, { title: "待出库", code: ['出库状态-待出库', '出库状态-出库中'] }, { title: "已出库", code: ['出库状态-已出库'] }],
    routeObjName: 'order',
    // 查询条件
    searchContent: [
      { code: 'createtime', title: mixins.$t("pastThirthtyDays"), defaultValue: 5, searchType: Constants.searchType.date },
      { code: 'staff', title: mixins.$t("staffId"), dropType: 'staff' },
      { code: 'org', title: mixins.$t("orgId"), dropType: 'org' },
      { code: 'choose', title: mixins.$t("choose"), searchType: Constants.searchType.pick }
    ],
    // 查询条件-筛选
    pullMenuList: [
      { code: 'outStatus', pullType: 'mSelect', typeName: 'outStatus' },
      { code: 'receiveStatus', pullType: 'mSelect', typeName: 'receiveStatus' },
      { code: 'salesChannel', pullType: 'mSelect', typeName: 'salesChannel' },
      { code: 'customerFrom', pullType: 'mSelect', typeName: 'customerFrom' },
      { code: 'delivery_Time', dataType: 'date' }
    ],

    // 列表区（脚部金额）
    footerAmount: { name: 'sumAmount', title: mixins.$t("totalAmount") },
    // 列表区（脚部信息）
    footerInfo: [{ name: 'sumQuantity', title: mixins.$t("piece"), prefix: mixins.$t("together"), }],
    // 列表区（内容）
    contentList: [
      { name: 'orderNo', title: mixins.$t("saleOrderNo") },
      { name: 'cusPhone', title: mixins.$t("cusPhone") },
      { name: 'addressFull', title: mixins.$t("adddressFull") },
      { name: [{ name: 'orgName', title: '' }, { name: 'staffName', title: mixins.$t("staffId") }], title: mixins.$t("storeInformation") }
    ],

    contentObj: {
      '成交': [{
        name: 'orderNo',
        title: mixins.$t("saleOrderNo")
      }, {
        name: 'customerPhone',
        title: mixins.$t("cusPhone")
      }, {
        name: 'addressFull',
        title: mixins.$t("adddressFull")
      }, {
        name: [{
          name: 'orgName',
          title: ''
        }, {
          name: 'staffName',
          title: mixins.$t("staffId")
        }],
        title: mixins.$t("adddressFull")
      }],
      '未成交': [{
        name: 'orderNo',
        title: mixins.$t("saleOrderNo")
      }, {
        name: 'customerPhone',
        title: mixins.$t("cusPhone")
      }, {
        name: [{
          name: 'orgName',
          title: ''
        }, {
          name: 'staffName',
          title: mixins.$t("staffId")
        }],
        title: mixins.$t("adddressFull")
      }]
    },
    // 弹出按钮
    buttonList: [
      { name: 'saleOutBound', title: mixins.$t("saleOutBound"), formMode: Constants.formMode.other, other: { idKey: 'orderId' } },//出库
      {
        name: 'saleOrderReturn', title: mixins.$t("saleReturn"), formMode: Constants.formMode.other, other: {
          idKey: 'orderId'
        }
      },//退货
      { name: 'collection', title: mixins.$t("cusCollection"), idKey: 'cusId' },//跳转客户收款
      { name: 'merge', title: mixins.$t("printedBill") }, //打印
      { name: 'invalid', title: mixins.$t("invaidOrder") } //作废
    ],
    buttonSaveList: [{ code: 'add', title: mixins.$t("add") }],
    contentSaveList: [{ code: 'need', title: mixins.$t("totalAmount"), type: 'str' }, { code: 'amount', content: 0, type: 'number' }, {
      type: 'count',
      code: 'count',
      bill: 0,
      quantity: 0
    }],
    outStatus: []

  },
  /**
  * @desc :   处理接口返回数据
  * @date : 2024年3月8日
  * @author : 于继渤
  */
  handleSearchData(tableData) {
    tableData.forEach(res => {
      if (res.outStatus !== Constants.outStatus.unOutBound) { //处理状态
        res.statusName = res.outStatusName
      } else {
        res.statusName = res.orderStatusName
      }
    });

    this.setData({
      tableData: tableData
    })
    //计算底部金额
    this.calculateTotalAmount()
  },

  /**
  * @desc :   作废
  * @date : 2024年3月8日
  * @author : 于继渤
  */
  invalid(e) {
    return this.excute(this.data.service, this.data.service.invalid + '/' + e.orderId, {}).then(res => {
      if (res.data.code == 200) {
        wx.showToast({
          title: mixins.$t('invalidSuccessfully'),
          image: '/static/image/success.png',
          duration: 1000
        })
      }
    })
  },


  /**
 * @desc : 切换
 * @date : 2024/2/1 15:49
 * @author : 于继渤
 */
  onChangeTabs(e) {
    let code = e.detail.detail.code
    this.setData({
      outStatus: code,
      pageInfo: {
        currentPage: 1,
        pageSize: 5
      }
    })
    this.searchData()
  },
  /**
* @desc : 设置查询参数
* @date : 2024/2/1 15:49
* @author : 于继渤
*/
  setSearchParams(params) {
    //销售状态
    params.outStatusList = this.data.outStatus == 'all' ? [] : this.data.outStatus
    return params
  },
  /**
     * @desc : 加载数据
     * @author : 于继渤
     * @date : 2024/1/23 9:16
     */
  getData(params) {
    return this.excute(this.data.service, this.data.service.selectByCond, params);
  },

  /**
  * @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.sumAmount
      sumQuantity += res.sumQuantity
    })
    contentSaveList[1].content = sumAmount
    contentSaveList[2].bill = tableData.length
    contentSaveList[2].quantity = sumQuantity
    this.setData({
      contentSaveList: contentSaveList
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

})