/*******************************************************************************
 * 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')
import Dialog from '@/dist/dialog/dialog.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: 'deliveryDate',
        dataType: 'date'
      },
      {
        code: 'viewVoidedDocuments',
        dataType: 'checkbox'
      },
    ],
    // 列表区（脚部金额）
    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: 'editInto',
        title: mixins.$t('edit')
      },
      {
        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: 'printedBill',
        title: mixins.$t("printedBill")
      }, //打印
      {
        name: 'invalid',
        title: mixins.$t("invaidOrder")
      } //作废
    ],
    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
    }],
    primaryKey: 'orderId'
  },
  /**
   * @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: '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: 'printedBill',
          title: mixins.$t("printedBill")
        }, //打印
        {
          name: 'invalid',
          title: mixins.$t("invaidOrder")
        } //作废
      ]
    }
    this.setData({
      // 弹出按钮
      buttonList: buttonList
    })
  },

    /**
  * @desc :   跳转客户详情
  * @date :   2024年5月27日
  * @author : 姜永辉
  */
 toTitle(e) {
  let cusId = e.detail.item.cusId
  wx.navigateTo({
    url: this.data.route.detailCustomer.url,
    success: function (res) {
      // 通过eventChannel向被打开页面传送数据 TODO 测试例子url是写死的，实际中，需要从route中读取
      res.eventChannel.emit('params', { id: cusId, formMode: Constants.formMode.edit })
    }
  })
},
  /**
   * @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) {
    this.setData({
      loading: true
    })
    return this.excute(this.data.service, this.data.service.invalid + '/' + e.orderId, {}).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
      })
    })
  },

  /**
   * @desc :   销售出库方法提示校验
   * @date : 2024年3月8日
   * @author : 于继渤
   */
  isAllowedSaleOutBound(e) {
    let result = ''
    //判断出库量是否小于订单量
    if (e.outQty == e.sumQuantity) {
      result = '可出库量不足'
    }
    return result
  },
  /**
   * @desc :   销售出库退货方法提示校验
   * @date : 2024年3月8日
   * @author : 于继渤
   */
  isAllowedSaleOrderReturn(e) {
    console.log(e)
    let result = ''
    //判断退货量是否小于订单量
    if (e.outQty == 0) {
      result = '可退货量不足'
    }
    return result
  },



  /**
   * @desc : 切换
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  onChangeTabs(e) {
    let code = e.detail.detail.code
    this.setData({
      outStatus: code,
    })
    this.searchData()
  },
  /**
   * @desc : 设置查询参数
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  setSearchParams(params) {
    if (!params.outStatusList) {
      params.outStatusList = this.data.outStatus == 'all' ? [] : this.data.outStatus
    }
    params.cusId = this.data.id ? this.data.id : null
    params.viewVoidedDocuments = params.viewVoidedDocuments ? true : false
    delete params['deliveryDate']
    return params
  },
  /**
   * @desc : 加载数据
   * @author : 于继渤
   * @date : 2024/1/23 9:16
   */
  getData(params) {
    return this.excute(this.data.service, this.data.service.selectByCond, params);
  },
  /**
   * @desc : 编辑
   * @author : 于继渤
   * @date : 2024/1/23 9:16
   */
  editInto(e) {
    let item = e
    wx.navigateTo({
      url: this.data.route.add.url,
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据 TODO 测试例子url是写死的，实际中，需要从route中读取
        res.eventChannel.emit('params', {
          id: item.orderId,
          formMode: Constants.formMode.edit,
          item: item
        })
      }
    })
  },
  /**
   * @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[0].content = sumAmount
    contentSaveList[1].bill = tableData.length
    contentSaveList[1].quantity = sumQuantity
    this.setData({
      contentSaveList: contentSaveList
    })

  },

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

  },

})