var app = getApp();
const config = require('../../config/config.js')
const util = require('../../utils/util.js')
const Http = require('../../utils/http.js')
Page({
  data: {
    billTypesObj: {
      name: '',
      id: ''
    },
    data: {
      list: []
    }, //数据
    crollTop: 0,
    scrollHeight: 0,
    house_type: 0, //户型
    house_style: 0, //风格
    house_area: 0, //面积
    flagdate: null,
    list: [],
    typeid: '1',
    tabTxt: [{
      title: '类型',
      idss: 2
    },
    {
      title: '日期',
      idss: 1
    },

    {
      title: '状态',
      idss: 3
    }
    ],
    showList: null,
    loading: "",
    tab: [true, true, true],
    disabled: false, //加载更多按钮状态
    page: 1, //当前页码
    hasMore: false, //加载更多按钮
    moreTxt: '点击加载更多',
    dataNull: true,
    date: null,
    date2: null,
    date3: null,
    date4: null,
    starttime: null,
    endtime: null,
    idss: 0,
    pageIndex: 2, //第几页
    height: null,
    billtypes: [{
      name: '租金',
      billTypeValue: '1',
      id: "1"
    },
    {
      name: '营业管理费',
      billTypeValue: '12',
      id: "12"
    },
    {
      name: '商业管理费',
      billTypeValue: '11',
      id: "11"
    },
    {
      name: '租赁押金',
      billTypeValue: '2',
      id: "2"
    },
    {
      name: '物业费',
      billTypeValue: '3',
      id: "3"
    },
    {
      name: '物业押金',
      billTypeValue: '4',
      id: "4"
    },
    {
      name: '水电空调费',
      billTypeValue: '5',
      id: "5"
    },
    {
      name: '其他费用',
      billTypeValue: '7',
      id: "7"
    },
    {
      name: '其他押金',
      billTypeValue: '8',
      id: "8"
    },

    {
      name: '结算单',
      billTypeValue: '10',
      id: "10"
    },
    ],
    statustypes: [{
      name: '未到期',
      status: "4",
      id: "4"
    },
    {
      name: '待缴',
      status: "2",
      id: '2'
    },
    {
      name: '欠缴',
      status: "1",
      id: "1"
    },
    {
      name: '待清算',
      status: "7",
      id: "7"
    },
    {
      name: '已结清',
      status: "3",
      id: "3"
    },
    ],
    starttime: null,
    endtime: null,
    billTypeValue: '1',
    status: null,
    allBillList: [],
    oweBillList: [],
    nearBillList: [],
    mallBillSum: {},
    mallRent: {},
    mallSales: {},
    billRemark: ''
  },
  // 选项卡
  filterTab: function (e) {
    let that = this;
    console.log(e);
    that.setData({
      loading: ""
    })
    that.setData({
      idss: e.currentTarget.dataset.index,
      pageIndex: 2
    })
    var data = [true, true, true],
      index = e.currentTarget.dataset.index;
    /**
     * index == 0
     * 显示全部
     */
    if (index == 1) {
      data[index] = !that.data.tab[index];
      let date = new Date;
      let year = date.getFullYear();
      var month = '';
      if (date.getMonth() + 1 < 10) {
        var month = '0' + (date.getMonth() + 1);
      } else {
        var month = date.getMonth() + 1;
      }
      that.setData({
        date: year + '-' + month,
        date2: year + '-' + month,
        tab: data
      })
    } else {
      data[index] = !that.data.tab[index];
      that.setData({
        tab: data
      })
    }
  },

  bindPickerChange: function (e) {
    this.setData({
      billTypesObj: this.data.billtypes[e.detail.value]
    })
  },
  bindDateChange1: function (e) {
    this.setData({
      date: e.detail.value,
    })
    this.getmallBillSum()
  },
  bindDateChange2: function (e) {
    this.setData({
      date2: e.detail.value
    })
    this.getmallBillSum()
  },
  bindDateChange3: function (e) {
    this.setData({
      date3: e.detail.value,
    })
    this.getmallSales()
  },
  bindDateChange4: function (e) {
    this.setData({
      date4: e.detail.value
    })
    this.getmallSales()
  },
  endtimeChange: function (e) {
    this.setData({
      endtime: e.detail.value,
    })
  },
  starttimeChange: function (e) {
    this.setData({
      starttime: e.detail.value
    })
  },
  search: function (e) {
    let that = this;
    let billTypeValue = e.target.dataset.billtypevalue;
    let status = e.target.dataset.status;
    if (billTypeValue) {
      this.setData({
        billTypeValue: e.target.dataset.billtypevalue ? e.target.dataset.billtypevalue : ''
      })
    }

    if (e.target.dataset.id) {
      that.setData({
        typeid: e.target.dataset.id
      })
    } else if (e.target.dataset.id1) {
      that.setData({
        typeid1: e.target.dataset.id1
      })
    }
    var data = [true, true, true],
      index = e.currentTarget.dataset.index;
    /**
     * 点击过查询
     */
    if (e.currentTarget.dataset.index == 'dateindex1') {
      data[index] = !that.data.tab[index];
      that.setData({
        tab: data,
        flagdate: "flagdate"
      });
      that.getList(that.data.date, that.data.date2, that.data.billTypeValue, status, 1);
    } else {
      data[index] = !that.data.tab[index];
      that.setData({
        tab: data
      });
      if (that.data.flagdate == 'flagdate') {
        that.getList(that.data.date, that.data.date2, that.data.billTypeValue, status, 1);
      } else {
        that.getList(null, null, that.data.billTypeValue, status, 1);
      }
    }
  },
  goUploading(e) {
    let item = e.currentTarget.dataset.data
    console.log(item)
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/bill/voucher/voucher?id=${id}&billTypeValue=${this.data.billTypeValue}&newPrice=${item.needPay}`,
    })
  },

  /**
   * gotolook点击查看
   */
  gotolook: function (e) {
    var billTypeValue = e.currentTarget.dataset.data.billType;
    let billId = e.currentTarget.dataset.data.billId;
    wx.navigateTo({
      url: `/pages/bill/billdetail/index?billTypeValue=${billTypeValue}&billId=${billId}&flag=mall`,
    })
  },
  gotoDetail: function (e) {
    var name = e.currentTarget.dataset.name;
    let scale = e.currentTarget.dataset.scale;
    wx.navigateTo({
      url: `/pages/bill/querySg/index?name=${name}&scale=${scale}`,
    })
  },
  gotoDetailDate: function (e) {
    wx.navigateTo({
      url: `/pages/bill/querySg/index?date=${this.data.date}&date2=${this.data.date2}`,
    })
  },
  goPay(e) {
    var billTypeValue = e.currentTarget.dataset.data.billType;
    let billId = e.currentTarget.dataset.data.billId;
    wx.navigateTo({
      url: `/pages/bill/pay/index?billTypeValue=${billTypeValue}&billId=${billId}`,
    })
  },
  gotolook02(e) {
    console.log(e, 333)
    let receivePay = e.currentTarget.dataset.data.receivePay;
    let status = e.currentTarget.dataset.data.status;
    let id = e.currentTarget.dataset.data.id;
    var settle_number = e.currentTarget.dataset.data.settle_number;
    var billTypeValue = e.currentTarget.dataset.data.billTypeValue;
    var freeze = e.currentTarget.dataset.data.freeze;
    let owe = e.currentTarget.dataset.data.owe;
    let merchantId = e.currentTarget.dataset.data.merchantId
    wx.navigateTo({
      url: `/pages/bill/billdetail/index?receivePay=${receivePay}&status=${status}&id=${id}&settle_number=${settle_number}&billTypeValue=${billTypeValue}&freeze=${freeze}&owe=${owe}&merchantId=${merchantId}`,
    })
  },
  onShow() {
    let that = this;
    //获取完整的日期
    let date = new Date;
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDay();
    that.setData({
      pageIndex: 2,
      date: year + '-' + month + '-' + '1',
      date2: year + '-' + month + '-' + day,
      date3: year + '-' + month + '-' + '1',
      date4: year + '-' + month + '-' + day,
      starttime: year + '-' + month + '-' + day,
      endtime: year + '-' + month + '-' + day,
    })
    that.getList(null, null, that.data.billTypeValue, null, 1);
    // wx.showLoading({
    //   title: '加载中...',
    // })
    setTimeout(function () {
      wx.hideLoading();
    }, 1500)
  },
  getmallBillSum() {
    Http.getRequest(config.api.mallBillSum, app.globalData.token, '', {
      startTime: this.data.date,
      endTime: this.data.date2
    }, (res) => {
      if (res.data && res.data) {
        this.setData({
          mallBillSum: res.data,
        })
      }
    })
  },
  getmallSales() {
    Http.getRequest(config.api.mallSales, app.globalData.token, '', {
      startTime: this.data.date3,
      endTime: this.data.date4
    }, (res) => {
      if (res.data && res.data) {
        this.setData({
          mallSales: res.data,
        })
      }
    })
  },
  getList: function (starttime, endtime, billTypeValue, status, pageIndex) {
    let that = this;
    that.setData({
      starttime01: starttime || null,
      endtime01: endtime || null,
      billTypeValue01: billTypeValue || null,
      status01: status || null
    })


    Http.getRequest(config.api.menus, app.globalData.token, '', {}, (res) => {
      if (res.data && res.data) {
        let m = []
        res.data.forEach(ele => {
          m.push(ele.permissionId)
        });
        that.setData({
          menus: m,
        })
      }
    })

    Http.getRequest(config.api.mallNotifyList, app.globalData.token, '', {}, (res) => {
      if (res.data && res.data && res.data.length >= 0) {
        res.data.map(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 ") : '';
        })
        that.setData({
          allBillList: res.data,
        })
      }
    })

    Http.getRequest(config.api.mallNearBillHot, app.globalData.token, '', {}, (res) => {
      if (res.data && res.data && res.data.length >= 0) {
        res.data.map(file => {
          file.minBeginTime = file.minBeginTime ? util.formatTime(Number(file.minBeginTime), "yyyy.MM.dd ") : '';
        })
        that.setData({
          nearBillList: res.data,
        })
      }
    })
    Http.getRequest(config.api.mallOweBillHot, app.globalData.token, '', {}, (res) => {
      if (res.data && res.data && res.data.length >= 0) {
        res.data.map(file => {
          file.minBeginTime = file.minBeginTime ? util.formatTime(Number(file.minBeginTime), "yyyy.MM.dd ") : '';
        })
        that.setData({
          oweBillList: res.data,
        })
      }
    })

    this.getmallBillSum()
    Http.getRequest(config.api.mallRent, app.globalData.token, '', {}, (res) => {
      if (res.data && res.data) {
        res.data.notRentTotalRate = (100 - res.data.rentTotalRate).toFixed(2)
        that.setData({
          mallRent: res.data,
        })
      }
    })
    this.getmallSales()

    Http.getRequest(config.api.mallBillDailyTypes, 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: res.data
      })
    })

  },
  inputauditRemark(e) {
    this.setData({
      billRemark: e.detail.value
    })
  },
  confirm() {
    Http.postRequest(config.api.billDailyAdd, app.globalData.token, '', {
      type: this.data.billTypesObj.id,
      starttime: this.data.starttime + ' 00:00:00',
      endtime: this.data.endtime + ' 23:59:59',
      billRemark: this.data.billRemark
    }, (res) => {
      // let arr = []
      // for (let key in res.data) {
      //     let val = res.data[key];
      //     arr.push({id: key, name: val})
      // }
      this.setData({
        billTypes: res.data
      })
    })
  },

  gosearch() {
    wx.navigateTo({
      url: `/pages/bill/querySg/index`,
    })
  },

  goAdd() {
    wx.navigateTo({
      url: `/pages/bill/meter/meter`,
    })
  },

  onReachBottom() {
    let that = this;
    let pageIndex = that.data.pageIndex++;
    that.getList(that.data.starttime01, that.data.endtime01, that.data.billTypeValue01, that.data.status01, pageIndex);
  }
});

// const config = require('../../config/config.js')
// const extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
// let weappId = extConfig.weappId;
// Page({
//   /**
//    * 页面的初始数据
//    */
//   data: {
//     src: config.weapp.dataTowerUrl
//   },

//   /**
//    * 生命周期函数--监听页面加载
//    */
//   onLoad: function(options) {
//     console.log(options);
//     this.setData({
//       openId: options.superopenId,
//       phone: options.phone,
//       weappId: weappId ? weappId : "wx9ff823abeef23b94"
//     })

//   },

//   /**
//    * 生命周期函数--监听页面初次渲染完成
//    */
//   onReady: function() {

//   },

//   /**
//    * 生命周期函数--监听页面显示
//    */
//   onShow: function() {
//     console.log(`${this.data.src}/#/home?openId=${this.data.openId}&phone=${this.data.phone}&from=xiaochengxu&tenantId=${this.data.weappId}`)
//   },

//   /**
//    * 生命周期函数--监听页面隐藏
//    */
//   onHide: function() {

//   },

//   /**
//    * 生命周期函数--监听页面卸载
//    */
//   onUnload: function() {

//   },

//   /**
//    * 页面相关事件处理函数--监听用户下拉动作
//    */
//   onPullDownRefresh: function() {

//   },

//   /**
//    * 页面上拉触底事件的处理函数
//    */
//   onReachBottom: function() {

//   },

//   /**
//    * 用户点击右上角分享
//    */
//   onShareAppMessage: function(res) {
//     let _this = this;
//     return {
//       title: '商管操作台',
//       path: 'pages/index/index',
//       success: function(res) {
//         // 转发成功
//       },
//       fail: function(res) {
//         // 转发失败
//       }
//     }
//   },
// })