var app = getApp();
const config = require('../../config/config.js')
const util = require('../../utils/util.js')
const Http = require('../../utils/HttpBasics.js')
Page({
  data: {
    data: {
      list: []
    }, //数据
    crollTop: 0,
    scrollHeight: 0,
    house_type: 0, //户型
    house_style: 0, //风格
    house_area: 0, //面积
    flagdate: null,
    list: [],
    tabTxt: [{
      title: '全部',
      idss: 0
    },
    {
      title: '日期',
      idss: 1
    },
    {
      title: '类型',
      idss: 2
    },
    {
      title: '状态',
      idss: 3
    },
    {
      title: '来源',
      idss: 4
    }
    ],
    showList: null,
    loading: "",
    tab: [true, true, true, true, true],
    disabled: false, //加载更多按钮状态
    page: 1, //当前页码
    hasMore: false, //加载更多按钮
    moreTxt: '点击加载更多',
    dataNull: true,
    status:'',
    type:'',
    source:'',
    date: "",
    date2: "",
    idss: 0,
    subsidy:'',
    pageNum: 1, //第几页
    height: null,
    billtypes: [{
      name: '自主结算',
      billTypeValue: 1,
      id: 1
    },
    {
      name: '微信分账',
      billTypeValue: 2,
      id: 2
    }
    ],
    statustypes: [
    {
      name: '未结算',
      status: 0,
      id: 0
    },
    {
      name: '已结算',
      status: 1,
      id: 1
    }
    ],
    //结算来源
    auditWayOptions: [
      /* {
          name: "全部",
          value: ''
      }, */
      {
        name: "补贴",
        value: 0,
        id: 0
      },
      {
        name: "卡消费",
        value: 1,
        id: 1
      },
      {
        name: "券核销",
        value: 2,
        id: 2
      },
      {
        name: "砍价核销",
        value: 3,
        id: 3
      },
      {
        name: "拼团核销",
        value: 4,
        id: 4
      },
      {
        name: "收银分账",
        value: 5,
        id: 5
      },
      {
        name: "可配送商品",
        value: 6,
        id: 6
      },
    ],
    starttime: null,
    endtime: null,
    billTypeValue: null
  },
  // 选项卡
  filterTab: function (e) {
    let that = this;
    console.log(e);
    that.setData({
      loading: ""
    })
    that.setData({
      idss: e.target.dataset.idss || e.currentTarget.dataset.idss,
      pageNum: 1
    })
    if (e.currentTarget.dataset.idss===0){
      this.setData({
        source: '',
        type: '',
        status: '',
        date: '',
        date2: ''
      })
      this.getList(1)
      this.getTotleData()
    }
    var data = [true, true, true, true, true],
    index = e.currentTarget.dataset.index;
    data[index] = !that.data.tab[index];
    that.setData({
      tab: data
    })
  },
  bindDateChange1: function (e) {
    if (new Date(e.detail.value).getTime() > new Date(this.data.date2).getTime()){
      tt.showToast({
        title: '抱歉,开始日期不能大于结束日期',
        icon: 'none',
        duration: 2000,
        mask: false
      });
      return;
    }
    this.setData({
      date: e.detail.value,
    })
  },
  bindDateChange2: function (e) {
    if (new Date(e.detail.value).getTime() < new Date(this.data.date).getTime()) {
      tt.showToast({
        title: '抱歉,结束日期不能小于开始日期',
        icon: 'none',
        duration: 2000,
        mask: false
      });
      return;
    }
    this.setData({
      date2: e.detail.value
    })
  },
  search(e){
    console.log(e.currentTarget.dataset.ids)
    let type = e.currentTarget.dataset.ids;
    if(type==0){
      this.setData({
        source:'',
        type:'',
        status:'',
        date:'',
        date2:''
      })
    } else if(type == 1){
      if(!this.data.date){
        tt.showToast({
          title: '抱歉请选择开始日期',
          icon: 'none',
          duration: 2000,
          mask: false
        });
        return;
      } else if (!this.data.date2){
        tt.showToast({
          title: '抱歉请选择结束日期',
          icon: 'none',
          duration: 2000,
          mask: false
        });
        return;
      }
    } else if (type == 2) {
      this.setData({
        type:e.currentTarget.dataset.id
      })
    } else if (type == 3) {
      console.log(e.currentTarget.dataset.id)
      this.setData({
        status: e.currentTarget.dataset.id
      })
    } else if (type == 4) {
      this.setData({
        source: e.currentTarget.dataset.id
      })
    }
    var data = [true, true, true, true, true];
    this.setData({
      tab: data
    })
    this.getList(1)
    this.getTotleData()
  },
  onShow() {
    this.getList(1)
    this.getTotleData()
  },
  formatData(data) {
    let arr = [];
    data.map((item, index) => {
      let a = {};
      a.value = [item];
      a.dateR = item.createDate01;
      let indexSt = 0;
      let haveIf = false;
      console.log(item, this.data.auditWayOptions, 1111)
      console.log(item.sourceStr, item.typeStr, item.statusStr, 222)
      arr.map((item01, index01) => {
        if (item.id == item01.id) {
          indexSt = index01;
          haveIf = true;
        }
      })
      if (haveIf && (new Date(item.createDate)).valueOf() >= (new Date(this.data.date + ' 00:00:00')).valueOf() && (new Date(item.createDate)).valueOf() <= (new Date(this.data.date2 + ' 23:59:59')).valueOf()) {
        arr[indexSt].value.push(item)
      } else if ((new Date(item.createDate)).valueOf() >= (new Date(this.data.date + ' 00:00:00')).valueOf() && (new Date(item.createDate)).valueOf() <= (new Date(this.data.date2 + ' 23:59:59')).valueOf()) {
        arr.push(a)
      }
    })
    this.setData({
      allBillList: arr
    })
    console.log(arr, 888888888888)
  },
  getTotleData(){
    let _this = this;
    Http.get({
      url: config.api.getSubsidySummary,
      data: {
        startdate: this.data.date?this.data.date + " 00:00:00":'',
        enddate: this.data.date2?this.data.date2 + " 23:59:59":'',
        source: this.data.source,
        type: this.data.type,
        status: this.data.status
      }
    })
      .then(res => {
        console.log(res,777)
        _this.setData({
          subsidy: Number(Number(res.data.subsidy)/100).toFixed(2)
        })
        tt.stopPullDownRefresh();
      })
      .catch(err => {
        tt.stopPullDownRefresh();
        tt.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      });
  },
  getList: function (page) {
    let _this = this;
    Http.get({
      url: config.api.getSubsidyList,
      data: {
        pageNum: page,
        pageSize: 10,
        startdate: this.data.date ? this.data.date + " 00:00:00" : '',
        enddate: this.data.date2 ? this.data.date2 + " 23:59:59" : '',
        source: this.data.source,
        type: this.data.type,
        status: this.data.status
      }
    })
      .then(res => {
        tt.stopPullDownRefresh();
        let data = res.data.list;
        let allBillList = [];
        if (page == 1) {
          allBillList = res.data.list;
        } else {
          allBillList = _this.data.allBillList;
        }
        data.map((item, index) => {
          let haveIf = false;
          item.createDate01 = util.formatTime(item.createDate, 'yyyy-MM-dddd hh:mm:ss')
          item.sourceStr = this.data.auditWayOptions.filter(e => e.value == item.source).length > 0 ? this.data.auditWayOptions.filter(e => e.value == item.source)[0].name : '';
          item.typeStr = this.data.billtypes.filter(e => e.id == item.type).length > 0 ? this.data.billtypes.filter(e => e.id == item.type)[0].name : '';
          item.statusStr = item.status===0?'未结算' : "已结算";
          allBillList.map((item02, index02) => {
            if (item02.id == item.id) {
              haveIf = true;
            }
          })
          if (!haveIf) {
            allBillList.push(item)
          }
        })
        console.log(allBillList,444)
        if (res.data.pages <= page) {
          _this.setData({
            allBillList,
            pageNum: res.data.pages - 1,
            content: '已经加载全部数据！'
          })
        } else {
          _this.setData({
            allBillList,
            loading: false,
            content: '小主,我在玩命加载中...'
          })
        }
      })
      .catch(err => {
        tt.stopPullDownRefresh();
        tt.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      });
  },
  onPullDownRefresh: function (e) {
    let that = this;
    that.setData({
      pageNum: 1,
      list: []
    });
    that.getList(1);
    this.getTotleData()
  },
  onReachBottom() {
    var that = this;
    that.data.pageNum++;
    that.setData({
      pageNum: that.data.pageNum,
      loading: true
    });
    that.getList(that.data.pageNum);
    this.getTotleData()
  }
});