var app = getApp();
const config = require('../../config/config.js')
const util = require('../../utils/util.js')
const Http = require('../../utils/HttpBasics.js')
const format = require('../../utils/format.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,
    statustypes: [
        {
          name: '审核中',
          status: 0,
          id: 0
        },
        {
          name: '审核通过',
          status: 1,
          id: 1
        },
        {
          name: '审核驳回',
          status: 2,
          id:2
        },
        {
          name: '已失效',
          status: 3,
          id:3
        }
    ],
    starttime: null,
    endtime: null,
    billTypeValue: null,
    paramData: null,
    topData: null,
    tabList:[
      {
        label:'订单录入',
        value:'1'
      },
      {
        label:'VIP卡绑定',
        value:'2'
      },
      {
        label:'绑卡记录',
        value:'3'
      }
    ],
    active: '1',
    formData:{
      "activityId": null, //活动id
      "phone": "",
      "name": "",
      "address": "",
      "vipNum": ""
    },
    rule: {
      "phone": [{
        required: true,
        message: "请填写客户电话"
      },
      {
        type: 'phone',
        message: "请输入正确的手机号码"
      }],
      "name": [{
          required: true,
          message: "请填写客户姓名"
        }
      ],
      "address": [{
        required: true,
        message: "请填写客户地址"
      }],
      "vipNum": [{
          required: true,
          message: "请填写会员卡号"
        }
      ]
    },
    searchActive: '1',
    searchVal:'',
    allCardList:[] //绑卡记录
  },
   /**
   * 生命周期函数--监听页面加载
   */
   onLoad: function (options) {
    this.setData({
      paramData:options
    });
  },
  tabChange: function(e){
    this.setData({
      active: e.target.dataset.index,
      searchVal:'',
      pageNum: 1
    })
    if(e.target.dataset.index!=='2') this.getList(1)
  },
  searchTabChange:function(e){
    this.setData({
      searchActive:  e.target.dataset.index,
      searchVal:''
    })
    this.getList(1)
  },
  inputChange(e) {
    this.setData({
      formData: Object.assign(this.data.formData, {
        [e.target.dataset.key]: e.detail.value
      })
    })
  },
  searchTnputChange(e) {
    this.setData({
      searchVal: e.detail.value
    })
  },
  searchVipData(){
    this.getList(1)
  },
  /**
   * 绑定会员
   */
  submitForm() {
    if (!format.validateForm(this.data.formData, this.data.rule)) {
      return
    }
    Http.post({
      url: config.api.legionUserSaveOrUpdate,
      data: {
        ...this.data.formData,
        activityId: this.data.paramData.id
      }
    }).then(res => {
      wx.showToast({
        title: '绑定成功！',
        icon: "none"
      })
    }).catch(err => {
      wx.showToast({
        title: err.message ? err.message : err.data,
        icon: "none"
      })
    })
  },
  // 选项卡
  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()) {
      wx.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()) {
      wx.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) {
        wx.showToast({
          title: '抱歉请选择开始日期',
          icon: 'none',
          duration: 2000,
          mask: false
        });
        return;
      } else if (!this.data.date2) {
        wx.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.legionOrderStatistics,
      data: {
        createDateBegin: this.data.date ? this.data.date + " 00:00:00" : '',
        createDateEnd: this.data.date2 ? this.data.date2 + " 23:59:59" : '',
        auditStatus: this.data.status,
        activityId: this.data.paramData.id
      }
    })
      .then(res => {
        _this.setData({
            topData: res.data
        })
        wx.stopPullDownRefresh();
      })
      .catch(err => {
        wx.stopPullDownRefresh();
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      });
  },
  getList: function(page){
    if(this.data.active === '1'){
      this.getLegionOrderList(page)
    }else{
      this.getLegionUserList(page)
    }
  },
  getLegionOrderList: function (page) {
    let _this = this;
    Http.get({
      url: config.api.legionOrderList,
      data: {
        pageNum: page,
        pageSize: 10,
        createDateBegin: this.data.date ? this.data.date + " 00:00:00" : '',
        createDateEnd: this.data.date2 ? this.data.date2 + " 23:59:59" : '',
        auditStatus: this.data.status,
        activityId: this.data.paramData.id
      }
    })
      .then(res => {
        wx.stopPullDownRefresh();
        let data = res.data.list;
        let allBillList = [];
        if (page == 1) {
          allBillList = res.data.list;
        } else {
          allBillList = _this.data.allBillList;
        }
        allBillList.map((item, index) => {
          let haveIf = false;
          item.startDate = util.formatTime(item.createDate, 'yyyy-MM-dddd hh:mm:ss')
          item.statusStr = this.data.statustypes.find(e => e.status === item.auditStatus)?.name
        })
        if (res.data.pages <= page) {
          _this.setData({
            allBillList,
            pageNum: res.data.pages - 1,
            content: '已经加载全部数据！'
          })
        } else {
          _this.setData({
            allBillList,
            loading: false,
            content: '小主,我在玩命加载中...'
          })
        }
      })
      .catch(err => {
        wx.stopPullDownRefresh();
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      });
  },
  getLegionUserList: function (page) {
    let _this = this;
    Http.get({
      url: config.api.legionUserList,
      data: {
        pageNum: page,
        pageSize: 10,
        phone: this.data.searchActive === '1'?this.data.searchVal:'',
        vipNum: this.data.searchActive === '2'?this.data.searchVal:'',
        activityId: this.data.paramData.id
      }
    })
      .then(res => {
        wx.stopPullDownRefresh();
        let data = res.data.list;
        let allCardList = [];
        if (page == 1) {
          allCardList = res.data.list;
        } else {
          allCardList = _this.data.allCardList;
        }
        allCardList.map((item, index) => {
          let haveIf = false;
          item.startDate = util.formatTime(item.createDate, 'yyyy-MM-dddd hh:mm:ss')
          item.statusStr = this.data.statustypes.find(e => e.status === item.auditStatus)?.name
        })
        if (res.data.pages <= page) {
          _this.setData({
            allCardList,
            pageNum: res.data.pages - 1,
            content: '已经加载全部数据！'
          })
        } else {
          _this.setData({
            allCardList,
            loading: false,
            content: '小主,我在玩命加载中...'
          })
        }
      })
      .catch(err => {
        wx.stopPullDownRefresh();
        wx.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()
  },
  viewDetail(e) {
    let id = e.currentTarget.dataset.id 
    let flag = e.currentTarget.dataset.flag 
    wx.navigateTo({
        url: `/pages/legionOrderDetail/index?id=${id}&auditStatus=${flag}`,
        })
  },
  submit() {
    wx.navigateTo({
        url: `/pages/legionOrderDetail/index?tag=add&activityId=${this.data.paramData.id}`,
    })
  }
});