var app = getApp();
const config = require('../../config/config.js')
const util = require('../../utils/util.js')
const Http = require('../../utils/http.js')
Page({
  data: {
    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,
    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: []
  },
  // 选项卡
  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
      })
    }
  },


  bindDateChange1: function (e) {
    this.setData({
      date: e.detail.value,
    })
  },
  bindDateChange2: function (e) {
    this.setData({
      date2: 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}`,
    })
  },
  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;
    that.setData({
      pageIndex: 2,
      date: year + '-' + month,
      date2: year + '-' + month,
    })
    that.getList(null, null, that.data.billTypeValue, null, 1);
    // wx.showLoading({
    //   title: '加载中...',
    // })
    setTimeout(function () {
      wx.hideLoading();
    }, 1500)
  },
  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.notifyList, 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.nearBillList, 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({
          nearBillList: res.data,
        })
      }
    })

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

  },

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

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