import * as echarts from '../../../pages/ec-canvas/echarts.min';
import { getOpData } from '../echarts-data.js'
const app = getApp()
const Constants = require('../../../utils/Constants.js');
const util = require('../../../utils/util.js')
const mixins = require('@/mixins/index.js')

Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    service: app.globalData['reportService'],
    // 查询条件
    searchContent: [{
      code: 'makeTime',
      title: mixins.$t('currentMonth'),
      defaultValue: 3,
      searchType: Constants.searchType.date
    }],
    loading: false,
    ec: {},
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 报表查询数据
    this.getSaleProfit()
  },


  /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
  },

  onRefresh() {
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading()
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    //报表查询数据
    this.getSaleProfit()
  },





  /** 去详细 */
  toDetail(e) {
    console.log(e, '/package-business-analysis/pages/profit-report/' + e.currentTarget.dataset.url)
    wx.navigateTo({
      url: '/package-business-analysis/pages/profit-report/' + e.currentTarget.dataset.url
    })
  },


  onShow() {
    this.echartsProfitDate = this.selectComponent("#echartsProfitDate")
    this.echartsProfitStaff = this.selectComponent("#echartsProfitStaff")
    this.echartsProfitOrg = this.selectComponent("#echartsProfitOrg")
    this.echartsProfitSku = this.selectComponent("#echartsProfitSku")
  },

  /** 设置echarts */
  setEcharts(name, { data, labelKey, valueKey, type }) {
    this[name].init((canvas, width, height, dpr) => {
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr
      });
      canvas.setChart(chart);
      chart.setOption(getOpData(data, labelKey, valueKey, type));
      return chart;
    });
  },


  /**
   * 销售毛利点击按日或按月查询
   */
  clickProfitDate(e) {
    this.setData({
      byDate: e.currentTarget.dataset.value
    })
    this.getSaleProfit()
  },

  /** 查询所有报表信息 */
  /** 查询页面展示的所有 */
  getSaleProfit() {
    this.setData({
      loading: true
    })
    this.excute(this.data.service, this.data.service.getSaleProfit, Object.assign({
      byDate: this.data.byDate
    }, JSON.parse(this.data.searchForm))).then(res => {
      if (res.data.code == Constants.SUCESS_CODE) {
        this.setData({
          profit: res.data.data.profit,
        })

        /** 图形重新渲染 */
        setTimeout(() => {
          this.setEcharts('echartsProfitDate', { data: res.data.data.dateProfit, labelKey: 'accDate', valueKey: 'profit', type: 1 })
          this.setEcharts('echartsProfitOrg', { data: res.data.data.orgProfit, labelKey: 'orgName', valueKey: 'profit', type: 3 })
          this.setEcharts('echartsProfitStaff', { data: res.data.data.staffProfit, labelKey: 'staffName', valueKey: 'profit', type: 3 })
          this.setEcharts('echartsProfitSku', { data: res.data.data.skuProfit, labelKey: 'skuName', valueKey: 'profit', type: 3 })
        }, 1000)

      }
      this.setData({
        loading: false
      })
    })
  },

   /**
    * 显示tip
    */
    showTip(e) {
      let item = e.currentTarget.dataset.item
      if (item) {
        util.showToast(item);
      }
    },
})

