var app = getApp();
const config = require('../../../config/config.js')
const util = require('../../../utils/util.js')
const Http = require('../../../utils/http.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    array: ['美国', '中国', '巴西', '日本'],
    billTypesObj: {
      name: '',
      id: ''
    },
    billScalesObj: {
      name: '',
      id: ''
    },
    billtypes: [
      { id: "1", name: "租金" },
      { id: "2", name: "物业费" },
      { id: "3", name: "日常(水电空调)费" },
      { id: "4", name: "其他费用" },
      { id: "5", name: "其他押金" },
      { id: "11", name: "商业管理费" },
      { id: "12", name: "运营管理费" },
      { id: "13", name: "租赁押金" },
      { id: "21", name: "物业押金" }
    ],
    statustypes: [],
    date: '',
    date2: '',
    list: []
  },
  onLoad: function (options) {
    // let date = new Date;
    // let year = date.getFullYear();
    // let month = date.getMonth() + 1;
    // this.setData({
    //   date: year + '-' + month,
    //   date2: year + '-' + month,
    // })
    let that = this;
    that.setData({
      billScalesObj: {
        name: options.name,
        id: options.scale
      },
      date: options.date,
      date2: options.date2
    })
    this.getData()
    this.search()
  },
  bindDateChange1: function (e) {
    this.setData({
      date: e.detail.value,
    })
  },
  bindDateChange2: function (e) {
    this.setData({
      date2: e.detail.value
    })
  },
  bindPickerChange: function (e) {
    this.setData({
      billTypesObj: this.data.billtypes[e.detail.value]
    })
  },
  bindPickerChange2: function (e) {
    this.setData({
      billScalesObj: this.data.billScales[e.detail.value]
    })
  },
  getData() {
    Http.getRequest(config.api.billScales, app.globalData.token, '', {}, (res) => {
      let arr = []
      for (let key in res.data) {
        let val = res.data[key];
        arr.push({ id: key, name: val })
      }
      this.setData({
        billScales: arr
      })
    })
    Http.getRequest(config.api.billTypes, app.globalData.token, '', {}, (res) => {
      let arr = []
      for (let key in res.data) {
        let val = res.data[key];
        arr.push({ id: key, name: val })
      }
      this.setData({
        billTypes: arr
      })
    })

  },
  search() {
    Http.getRequest(config.api.mallListBill, app.globalData.token, '', {
      billTypeValue: this.data.billTypesObj.id,
      starttime: this.data.date,
      endtime: this.data.date2,
      onlyOweOrNear: this.data.billScalesObj.id,
      pageNum: 1,
      pageSize: 1000,
    }, (res) => {
      if (res.code === 200) {
        res.data.list.forEach(file => {
          file.starttime = file.starttime ? util.formatTime(Number(file.starttime), "yyyy.MM.dd ") : '';
          file.endtime = file.starttime ? util.formatTime(Number(file.endtime), "yyyy.MM.dd ") : '';
        })
        this.setData({
          list: res.data.list
        })
      }
    })
  },
  /**
  * gotolook点击查看
  */
  gotolook: function (e) {
    var billTypeValue = e.currentTarget.dataset.data.billType;
    let billId = e.currentTarget.dataset.data.id;
    wx.navigateTo({
      url: `/pages/bill/billdetail/index?billTypeValue=${billTypeValue}&billId=${billId}`,
    })
  },
  goBack() {
    wx.navigateBack({
      delta: 1
    })
  }
})