// 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: 1706679819269,
    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": "offerAmount",
        "title": "收款金额(万)",
        "type": "amount",
        "width": "25%"
      },
      {
        "name": "offerCsmCount",
        "title": "报价客户",
        "width": "20%"
      },
      {
        "after": "%",
        "name": "offerRate",
        "title": "报价率",
        "width": "20%"
      }
    ],
    tab1TopData: [
      {
        "title": "报价（总额:万）",
        "type": "amount",
        "value": "12.82"
      },
      {
        "title": "报价额（均值:万）",
        "type": "amount",
        "value": "12.82"
      }
    ],
    tab2Columns: [
      {
        "name": "orgName",
        "title": "门店名称",
        "width": "35%"
      },
      {
        "name": "csmCount",
        "title": "潜在客户",
        "width": "20%"
      },
      {
        "name": "measureCsmCount",
        "title": "量尺",
        "width": "10%"
      },
      {
        "name": "designCsmCount",
        "title": "设计制图",
        "width": "15%"
      },
      {
        "after": "%",
        "name": "orderRate",
        "title": "成交率",
        "width": "20%"
      }
    ],
    tab2TableData: [
      {
        "csmCount": 0,
        "designCsmCount": 1,
        "kpiIndex": 1,
        "measureCsmCount": 0,
        "orderCsmCount": 0,
        "orderRate": 0,
        "orgName": "北海富安居店"
      }
    ],
    value1: 3, 
    tab1TableData: [
      {
        "csmCount": 0,
        "kpiIndex": 1,
        "offerAmount": "12.82",
        "offerCsmCount": 3,
        "offerRate": 0,
        "orgName": "北海富安居店"
      }
    ],
    tab2TopData: [
      {
        "title": "新增潜客",
        "value": 0
      },
      {
        "title": "新增成交",
        "value": 0
      },
      {
        "title": "设计制图",
        "value": 1
      }
    ]
  },
   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()
   },
 
     /**
   * @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()
  },

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


  /** 查询页面展示的所有 */
  getReportData() {
    let params = {
      tab1: Object.assign(this.getBeginDateAndEndDate(this.data.month), { userId:app.globalData.user.userId,type: 'f_report_s001_csm_analysis_offer', cpId: app.globalData.user.ascpId, orgIds: this.data.orgIds,dateType:this.data.value1 }),
      tab2: Object.assign(this.getBeginDateAndEndDate(this.data.month), { userId:app.globalData.user.userId,type: 'f_report_s001_csm_analysis_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.offerAmount/10000) }, { title: '报价额（均值:万）', type: 'amount', value: util.parseFloatFixed2(res.data.data.tab1.avgAmount/10000) }],
          tab1TableData: util.calc10000(res.data.data.tab1.items,['offerAmount']),
          tab2TopData: [{ title: '新增潜客', value: res.data.data.tab2.csmCount }, { title: '新增成交', value: res.data.data.tab2.orderCsmCount }, { title: '设计制图', value: res.data.data.tab2.designCsmCount }],
          tab2TableData: res.data.data.tab2.items
        })
      } else {
        console.error("获取报表失败", res)
      }
    })
  },

  //#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('/', '-')
    }
    console.log(' { beginDate: monthStart, endDate: monthEnd }', { beginDate: monthStart, endDate: monthEnd })
    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
})