// 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-02-01",
      "2024-02-29"
    ],
    monthFlag: false,
    monthName: "2024年2月",
    monthOnChoose: 1706749526365,
    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%"
      }
    ],
    tab1TableData: [],
    tab1TopData: [
      {
        "title": "成交额(总额:万)",
        "type": "amount",
        "value": "0.00"
      },
      {
        "title": "成交额(均值:万)",
        "type": "amount",
        "value": "0.00"
      }
    ],
    tab2TableData: [],
    value1: 3, 
  },

  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()
  },

  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),{ userId:app.globalData.user.userId,monthDate: util.formatDayTimeMoth(new Date(this.data.month)),type: 'f_report_s001_index_org_order', cpId: app.globalData.user.ascpId, orgIds: this.data.orgIds ,dateType:this.data.value1}),
    };
    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'])
        })
      } else {
        console.error("获取报表失败", res)
      }
    })
  },
     /**
   * @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()
  },
  //#region 基础内容
  /** 根据月份查出第一天和最后一天 参数是时间戳*/
  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()
  },
  //#endregion
})