// package5/pages/report/detail/detail.js

const api = require('../../../utils/api.js');
const util = require('../../../utils/util.js')
const app = getApp()
const Constants = require('../../../utils/Constants.js');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    createTimeType: {
      "text": "本月",
      "value": 3
    },
    month: [
      "2024-01-01",
      "2024-01-31"
    ],
    monthFlag: false,
    monthName: "2024年1月",
    monthOnChoose: 1706679819276,
    option1: [
      {
        "text": "本日",
        "value": 1
      },
      {
        "text": "本周",
        "value": 2
      },
      {
        "text": "本月",
        "value": 3
      },
      {
        "text": "本年",
        "value": 5
      },
      {
        "text": "自定义",
        "value": 0
      }
    ],
    orgFlag: false,
    orgIds: [],
    orgNames: null,
    tab1Columns: [
      {
        "name": "orgName",
        "title": "门店名称",
        "width": "35%"
      },
      {
        "name": "orderAmount",
        "title": "成交金额(万)",
        "type": "amount",
        "width": "25%"
      },
      {
        "ifNull": "-",
        "name": "kpiOrderAmount",
        "title": "任务目标(万)",
        "type": "amount",
        "width": "20%"
      },
      {
        "after": "%",
        "name": "kpiOrderRate",
        "title": "完成率",
        "width": "20%"
      }
    ],
    tab1TopData: [
      {
        "title": "成交额（总额:万）",
        "type": "amount",
        "value": "98.17"
      },
      {
        "title": "成交额（均值:万）",
        "type": "amount",
        "value": "98.17"
      }
    ],
    tab2Columns: [
      {
        "name": "orgName",
        "title": "门店名称",
        "width": "30%"
      },
      {
        "name": "rejectAmount",
        "title": "退货额(万)",
        "type": "amount",
        "width": "25%"
      },
      {
        "name": "outAmount",
        "title": "出库额(万)",
        "type": "amount",
        "width": "25%"
      },
      {
        "after": "%",
        "name": "kpiRate",
        "title": "退货占率",
        "width": "20%"
      }
    ],
    tab2TableData: [
      {
        "kpiIndex": 1,
        "kpiRate": 4.63,
        "orgName": "北海富安居店",
        "outAmount": "6.48",
        "rejectAmount": "0.30"
      }
    ],
    tab3Columns: [
      {
        "name": "orgName",
        "title": "门店名称",
        "width": "30%"
      },
      {
        "name": "csmRate",
        "title": "客单价(万)",
        "type": "amount",
        "width": "25%"
      },
      {
        "name": "sumAmount",
        "title": "成交金额(万)",
        "type": "amount",
        "width": "25%"
      },
      {
        "name": "csmCount",
        "title": "成交户数",
        "width": "20%"
      }
    ],
    tab3TableData: [
      {
        "csmCount": 13,
        "csmRate": "7.55",
        "kpiIndex": 1,
        "orgName": "北海富安居店",
        "sumAmount": "98.17"
      }
    ],
    tab3TopData: [
      {
        "title": "客户单价（均值:万）",
        "type": "amount",
        "value": "7.55"
      },
      {
        "title": "成交户数",
        "value": 13
      }
    ],
    tab4Columns: [
      {
        "name": "kpiName",
        "title": "促销套餐(Top5)",
        "width": "30%"
      },
      {
        "name": "sumAmount",
        "title": "成交金额(万)",
        "type": "amount",
        "width": "25%"
      },
      {
        "name": "sumQty",
        "title": "成交数量",
        "width": "25%"
      },
      {
        "name": "csmCount",
        "title": "成交户数",
        "width": "20%"
      }
    ],
    tab4Columns2: [
      {
        "name": "kpiName",
        "title": "促销单品(Top5)",
        "width": "30%"
      },
      {
        "name": "sumAmount",
        "title": "成交金额(万)",
        "type": "amount",
        "width": "25%"
      },
      {
        "name": "sumQty",
        "title": "成交数量",
        "width": "25%"
      },
      {
        "name": "csmCount",
        "title": "成交户数",
        "width": "20%"
      }
    ],
    tab4TopData: [
      {
        "title": "活动成交(万）",
        "type": "amount",
        "value": "64.04"
      },
      {
        "title": "促销套餐(万)",
        "type": "amount",
        "value": "55.34"
      },
      {
        "title": "单品促销(万)",
        "type": "amount",
        "value": "8.70"
      }
    ],
    tab4_1TableData: [
      {
        "csmCount": 6,
        "kpiIndex": 1,
        "kpiName": "1115新活动",
        "sumAmount": "38.39",
        "sumQty": 18
      },
      {
        "csmCount": 2,
        "kpiIndex": 2,
        "kpiName": "套餐相同商品",
        "sumAmount": "14.00",
        "sumQty": 4
      },
      {
        "csmCount": 3,
        "kpiIndex": 3,
        "kpiName": "零售套餐001",
        "sumAmount": "2.95",
        "sumQty": 3
      }
    ],
    tab4_2TableData: [
      {
        "csmCount": 11,
        "kpiIndex": 1,
        "kpiName": "智能盖板HE920",
        "sumAmount": "8.70",
        "sumQty": 35
      }
    ],
    tab5Columns: [
      {
        "name": "orgName",
        "title": "门店名称",
        "width": "30%"
      },
      {
        "name": "sumAmount",
        "title": "定+智品类(万)",
        "type": "amount",
        "width": "25%"
      },
      {
        "name": "dzAmount",
        "title": "定制品(万)",
        "type": "amount",
        "width": "25%"
      },
      {
        "name": "znAmount",
        "title": "智能坐便(万)",
        "type": "amount",
        "width": "20%"
      }
    ],
    tab5TableData: [
      {
        "dzAmount": 0,
        "kpiIndex": 1,
        "orgName": "北海富安居店",
        "sumAmount": "26.55",
        "znAmount": "26.55"
      }
    ],
    tab5TopData: [
      {
        "title": "定+智品类(万)",
        "type": "amount",
        "value": "26.55"
      },
      {
        "title": "定制品(万）",
        "type": "amount",
        "value": "0.00"
      },
      {
        "title": "智能单品(万）",
        "type": "amount",
        "value": "26.55"
      }
    ],
    tab5_2Columns: [
      {
        "name": "orgName",
        "title": "门店名称",
        "width": "30%"
      },
      {
        "name": "sumAmount",
        "title": "定制品(万)",
        "type": "amount",
        "width": "25%"
      },
      {
        "name": "cabinetAmount",
        "title": "定制柜(万)",
        "type": "amount",
        "width": "25%"
      },
      {
        "name": "glassAmount",
        "title": "定制玻璃(万)",
        "type": "amount",
        "width": "20%"
      }
    ],
    tab5_2TableData: [],
    tab5_2TopData: [
      {
        "title": "定制品(万)",
        "type": "amount",
        "value": "0.00"
      },
      {
        "title": "定制柜(万）",
        "type": "amount",
        "value": "0.00"
      },
      {
        "title": "定制玻璃(万）",
        "type": "amount",
        "value": "0.00"
      }
    ],
    tabs: [
      "销售任务",
      "退货情况",
      "客单价",
      "活动效果",
      "定+智品类"
    ],
    value1: 3, 
    tab1TableData: [
      {
        "kpiIndex": 1,
        "kpiOrderAmount": null,
        "kpiOrderRate": 0,
        "orderAmount": "98.17",
        "orgName": "北海富安居店"
      }
    ],
    tab2TopData: [
      {
        "title": "退货额（总额:万）",
        "type": "amount",
        "value": "0.30"
      },
      {
        "title": "退货额（均值:万）",
        "type": "amount",
        "value": "0.30"
      }
    ]
  },

  dateClick(e) {
    if (e.detail === 0) {
      this.selectComponent("#month-components").toggle(true)
      this.dropdowncommit({ currentTarget: { dataset: { type: 'month' } } })
    } else {
      this.getReportData()
    }
  },
  onConfirmChooseDate(e) {
    this.setData({
      month: this.data.monthOnChoose,
      monthName: this.formatDate(this.data.monthOnChoose)
    })
    this.setData({
      monthFlag: false
    })
    let option1 = this.data.option1
    option1[option1.length - 1].text = this.data.monthName
    this.setData({
      option1: option1
    })
    this.selectComponent("#month-components").toggle(false)
    this.getReportData()
  },

  /**
  * @desc : 日期组件后的回调
  * @author : 于继渤
  * @date : 2022/5/24 12:16
  */
  dateCommit(e) {
    let dateType = e.detail.dateType
    if (dateType === 'accountdate') {
      if (e.detail.dateDefault.text === '自定义') {
        this.setData({
          value1: 0,
        })
      }
      if (e.detail.dateDefault.text === '本月') {
        this.setData({
          value1: 3,
        })
      }
      let data = e.detail.date
      console.log(e)
      this.setData({
        month: data,
        createTimeType: e.detail.dateDefault
      })
      this.selectComponent('#create-time-type').toggle();
    }
    this.getReportData()
  },

  onCloseChooseDate(e) {
    this.setData({
      // monthOnChoose: this.data.month
      value1: 3,

    })
    this.setData({
      monthFlag: false
    })
    this.selectComponent("#month-components").toggle()
    this.getReportData()
  },

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


  /** 查询页面展示的所有 */
  getReportData() {
    let params = {
      tab1: Object.assign(this.getBeginDateAndEndDate(this.data.month), { dateType: this.data.value1, userId: app.globalData.user.userId, type: 'f_report_s001_index_org_order', cpId: app.globalData.user.ascpId, orgIds: this.data.orgIds }),
      tab2: Object.assign(this.getBeginDateAndEndDate(this.data.month), { dateType: this.data.value1, userId: app.globalData.user.userId, type: 'f_report_s001_order_analysis_reject', cpId: app.globalData.user.ascpId, orgIds: this.data.orgIds }),
      tab3: Object.assign(this.getBeginDateAndEndDate(this.data.month), { dateType: this.data.value1, userId: app.globalData.user.userId, type: 'f_report_s001_order_analysis_csm', cpId: app.globalData.user.ascpId, orgIds: this.data.orgIds }),
      tab4: Object.assign(this.getBeginDateAndEndDate(this.data.month), { dateType: this.data.value1, userId: app.globalData.user.userId, type: 'f_report_s001_order_analysis_activity', cpId: app.globalData.user.ascpId, orgIds: this.data.orgIds }),
      tab5: Object.assign(this.getBeginDateAndEndDate(this.data.month), { dateType: this.data.value1, userId: app.globalData.user.userId, type: 'f_report_s001_order_analysis_specs', cpId: app.globalData.user.ascpId, orgIds: this.data.orgIds }),
    };
    api.request(Constants.WX_REPORT + 'select_report_batch', 'POST', params).then(res => {
      if (res.data.code === 200) {
        this.setData({
          tab1TopData: [{ title: '成交额（总额:万）', type: 'amount', value: util.parseFloatFixed2(res.data.data.tab1.sumAmount / 10000) }, { title: '成交额（均值:万）', type: 'amount', value: util.parseFloatFixed2(res.data.data.tab1.avgAmount / 10000) }],
          tab1TableData: util.calc10000(res.data.data.tab1.items, ['kpiOrderAmount', 'orderAmount']),

          tab2TopData: [{ title: '退货额（总额:万）', type: 'amount', value: util.parseFloatFixed2(res.data.data.tab2.sumAmount / 10000) }, { title: '退货额（均值:万）', type: 'amount', value: util.parseFloatFixed2(res.data.data.tab2.avgAmount / 10000) }],
          tab2TableData: util.calc10000(res.data.data.tab2.items, ['rejectAmount', 'outAmount']),

          tab3TopData: [{ title: '客户单价（均值:万）', type: 'amount', value: util.parseFloatFixed2(res.data.data.tab3.avgAmount / 10000) }, { title: '成交户数', value: res.data.data.tab3.csmCount }],
          tab3TableData: util.calc10000(res.data.data.tab3.items, ['sumAmount', 'csmRate']),

          tab4TopData: [{ title: '活动成交(万）', type: 'amount', value: util.parseFloatFixed2(res.data.data.tab4.sumAmount / 10000) }, { title: '促销套餐(万)', type: 'amount', value: util.parseFloatFixed2(res.data.data.tab4.comAmount / 10000) }, { title: '单品促销(万)', type: 'amount', value: util.parseFloatFixed2(res.data.data.tab4.pomAmount / 10000) }],
          tab4_1TableData: util.calc10000(res.data.data.tab4.comItems, ['sumAmount']),
          tab4_2TableData: util.calc10000(res.data.data.tab4.pomItems, ['sumAmount']),
          tab5TopData: [{ title: '定+智品类(万)', type: 'amount', value: util.parseFloatFixed2(res.data.data.tab5.sumAmount / 10000) }, { title: '定制品(万）', type: 'amount', value: util.parseFloatFixed2(res.data.data.tab5.dzAmount / 10000) },
          { title: '智能单品(万）', type: 'amount', value: util.parseFloatFixed2(res.data.data.tab5.znAmount / 10000) }],
          tab5TableData: util.calc10000(res.data.data.tab5.items, ['dzAmount', 'znAmount', 'sumAmount'])
          ,
          tab5_2TopData: [{ title: '定制品(万)', type: 'amount', value: util.parseFloatFixed2(res.data.data.tab5.dzAmount / 10000) }, { title: '定制柜(万）', type: 'amount', value: util.parseFloatFixed2(res.data.data.tab5.cabinetAmount / 10000) },
          { title: '定制玻璃(万）', type: 'amount', value: util.parseFloatFixed2(res.data.data.tab5.glassAmount / 10000) }],
          tab5_2TableData: util.calc10000(res.data.data.tab5.dzItems, ['cabinetAmount', 'glassAmount', 'sumAmount'])
        })

      } else {
        console.error("获取报表失败", res)
      }
    })
  },
  /** 根据月份查出第一天和最后一天 参数是时间戳*/
  getBeginDateAndEndDate(timestamp) {
    let month = this.data.month
    let monthStart = month[0] ? month[0] : ''
    let monthEnd = month[1] ? month[1] : ''
    if (month && month[0] && (month[0].indexOf('/') != -1)) {
      monthStart = month[0].replaceAll('/', '-')
    }
    if (month && month[1] && (month[1].indexOf('/') != -1)) {
      monthEnd = month[1].replaceAll('/', '-')
    }
    return { beginDate: monthStart, endDate: monthEnd }
  },
  formatDate(value) {
    var time = new Date(value);
    var year = time.getFullYear();
    var month = time.getMonth() + 1;
    return year + "年" + month + "月"
  },
  onInput(e) {
    this.setData({
      monthOnChoose: e.detail
    })
  },


  dropdowncommit(e) {
    this.setData({
      [e.currentTarget.dataset.type + 'Flag']: true
    })
  },

  /** 下拉多选关闭 */
  muticloseAll(e) {
    this.setData({
      orgFlag: false
    })
    this.selectComponent("#org-components").toggle()
  },

  /** 下拉多选确定 */
  mutiSearchAll(e) {
    this.setData({
      orgIds: e.detail.idList,
      orgNames: e.detail.names,
      orgFlag: false
    })
    this.selectComponent("#org-components").toggle(false)
    this.getReportData()
  },

})