// 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: 1706679819268,
    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": "repayAmount",
        "title": "收款金额(万)",
        "type": "amount",
        "width": "25%"
      },
      {
        "ifNull": "-",
        "name": "kpiRepayAmount",
        "title": "任务目标(万)",
        "type": "amount",
        "width": "20%"
      },
      {
        "after": "%",
        "name": "kpiRepayRate",
        "title": "完成率",
        "width": "20%"
      }
    ],
    tab1TableData: [
      {
        "kpiIndex": 1,
        "kpiRepayAmount": null,
        "kpiRepayRate": 0,
        "orgName": "北海富安居店",
        "repayAmount": "17.96"
      }
    ],
    tab1TopData: [
      {
        "title": "收款额（总额:万）",
        "type": "amount",
        "value": "17.96"
      },
      {
        "title": "收款额（均值:万）",
        "type": "amount",
        "value": "17.96"
      }
    ],
    tab2Columns: [
      {
        "name": "orgName",
        "title": "门店名称",
        "width": "35%"
      },
      {
        "name": "receivable",
        "title": "欠款额(万)",
        "type": "amount",
        "width": "25%"
      },
      {
        "name": "orderAmount",
        "title": "成交额(万)",
        "type": "amount",
        "width": "25%"
      },
      {
        "name": "csmCount",
        "title": "欠款户数",
        "width": "15%"
      }
    ],
    tab2TableData: [
      {
        "csmCount": 12,
        "kpiIndex": 1,
        "orderAmount": "98.54",
        "orgName": "北海富安居店",
        "receivable": "80.26"
      }
    ],
    tab2TopData: [
      {
        "title": "欠款额（总额:万）",
        "type": "amount",
        "value": "80.26"
      },
      {
        "title": "欠款额（均值:万）",
        "type": "amount",
        "value": "80.26"
      }
    ],
    tabs: [
      "收款任务",
      "欠款情况"
    ],
    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()
  },
  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 }
  },

   /**
   * @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_index_org_repay',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_repay_debt',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,['kpiRepayAmount','repayAmount']),
          tab2TopData:[{title:'欠款额（总额:万）',type:'amount',value:util.parseFloatFixed2(res.data.data.tab2.receivable/10000)},{title:'欠款额（均值:万）',type:'amount',value:util.parseFloatFixed2(res.data.data.tab2.avgAmount/10000)}],
          tab2TableData:util.calc10000(res.data.data.tab2.items,['orderAmount','receivable'])
        })
        
      } else {
        console.error("获取报表失败", res)
      }
    })
  },

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

})