/*******************************************************************************
 * Copyright(c) 2022 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：选品
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		于继渤		 2024-1-23			1.00		    选品
 *******************************************************************************/
const mixins = require('@/mixins/index.js')
import Dialog from '@/dist/dialog/dialog.js';
const app = getApp()
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    routeObjName: 'goodsSku',
    dataItem: {},
    choosedGoodsList: [],
    commonService: app.globalData['commonService'],
    inventoryService: app.globalData['inventoryService'],
    typeList: [
      {
        catName: '全部分类'
      }
    ],
    catId: null,
    active: 0,
    popContent:
      [
        { code: 'itemQty', type: 'step', title: '数量', required: true },
        { code: 'priceSale', type: 'number', title: '销售价格', required: true },
        { code: 'whId', name: 'whName', title: '库区', type: 'choose', urlKey: 'openingInventory' },
        { code: 'nonStdCode', title: '商品批次', type: 'str', },
      ],
    // 确定按钮
    buttonList: [
      { name: 'confirm', title: '结算', color: '#CAA977' }
    ],
    contentList: [
      { code: 'need', title: mixins.$t("totalAmount"), type: 'str' },
      { code: 'amount', content: 0, type: 'number' },
      { type: 'count', code: 'count', quantity: 0 }
    ]
  },


  /**
   * @desc : 左侧分类切换
   * @author : 于继渤
   * @date : 2024/1/23 9:16
   */
  changeSidebar(e) {
    console.log(e.detail)
    let index = e.detail
    let typeList = this.data.typeList
    this.setData({
      catId: typeList[index].catId
    })
    this.searchData()
  },


  /**
   * @desc : 设置查询参数
   * @author : 于继渤
   * @date : 2024/1/23 9:16
   */
  setSearchParams(params) {
    params.categoryId = this.data.catId //种类id
    return params
  },
  /**
   * @desc : 查询
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  getData(params) {
    return this.excute(this.data.commonService, this.data.commonService.getGoodsForOrderByPage, params);
  },

  /**
   * @desc : 处理返回数据
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  handleSearchData(tableData) {

    if (tableData && tableData.length > 0) {
      tableData.forEach(res => {
        res.priceSale = res.priceStandard
      })
      this.setData({
        tableData: tableData
      })
    }
  },

  /**
   * @desc : 加载数据
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  loadInit() {
    this.setData({
      billType: this.data.item
    })
  },


  /**
* @desc : 获取种类
* @date : 2024/2/1 15:49
* @author : 于继渤
*/
  getGoodsCategoryNoPage() {
    return this.excute(this.data.commonService, this.data.commonService.getGoodsCategoryNoPage, {}).then(res => {
      console.log('goodsCategoryListBy', res.data.data)
      this.setData({
        typeList: this.data.typeList.concat(res.data.data)
      })
    })
  },
  /**
* @desc : 打开商品
* @date : 2024/2/1 15:49
* @author : 于继渤
*/
  openAddItemInfo(e) {
    console.log(e)
    let item = e.currentTarget.dataset.item
    item.itemQty = 1   //打开 popur 数量默认为1
    this.setData({
      dataItem: JSON.stringify(item),
      showPop: true,
    })
  },
  /**
* @desc : 商品确认事件
* @date : 2024/2/1 15:49
* @author : 于继渤
*/
  editItems(e) {
    console.log('editItems', e)
    let choosedGoodsList = this.data.choosedGoodsList
    let item = e.detail.form
    item.priceSale = Number(item.priceSale)
    //校验销售价格输入是否符合规则
    //销售价格不能低于销售限价
    console.log(item.priceSale, item.priceLimited)
    if (item.priceSale < item.priceLimited) {
      Dialog.alert({
        title: '提示',
        message: '销售价格不能低于销售限价',
      }).then(() => { })
      return
    }
    choosedGoodsList.push(item)
    this.setData({
      choosedGoodsList: choosedGoodsList
    })
    this.calculateParameters()
  },

  /**
  * @desc : 计算数量金额
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  calculateParameters() {
    let contentList = this.data.contentList
    let choosedGoodsList = this.data.choosedGoodsList
    let sumAmt = Number(0)
    let sumQty = Number(0)
    choosedGoodsList.forEach(res => {
      sumAmt += (res.priceStandard * res.itemQty)
      sumQty += res.itemQty
    })
    contentList[1].content = sumAmt
    contentList[2].quantity = sumQty
    this.setData({
      contentList: contentList
    })
  },


  /**
  * @desc : 顶部切换
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  changeTag(e) {
    this.setData({
      active: e.detail.index
    })
  },


  /**
* @desc : 结算按钮
* @date : 2024/2/1 15:49
* @author : 于继渤
*/
  submit() {
    //跳转销售订单
    let that = this
    wx.navigateTo({
      url: that.data.route.saleOrder.url,
      success: function (res) {
        res.eventChannel.emit('params', {
          goodsList: that.data.choosedGoodsList
        })
      }
    })
  },



  /**
* @desc : 初始化
* @date : 2024/2/1 15:49
* @author : 于继渤
*/
  onLoad() {
    this.getGoodsCategoryNoPage()
    let _this = this;
    wx.getSystemInfo({
      success: function (res) {
        _this.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      }
    })
  },
})