
var app = getApp()
var that;
Page({
  data: {
    date: app.globalData.month,
    activeIndex: 1,
    page: 1,
    showZp: false,//显示sx选择
    parameter1: { //我指派的参数
      pt_id: 0,//类型
      status: 'all',//状态
      sort: 'publish',//排序
    },
    parameter2: { //我的悬赏
      pt_id: 0,
      order_by: 'create_time',
    },
    list: []
  },
  onLoad(e) {
    if (e.index) {
      this.setData({
        activeIndex: e.index,
      })
    }
    that = this;
    dd.setNavigationBar({ title: "我发布的任务" });
  },

  //显示选择员工
  selectSx() {
    if (this.data.activeIndex == 3) {
      app.globalData.showToast("重复任务暂不支持筛选")
      return
    }
    this.setData({ showZp: true })
  },
  //关闭弹窗
  onClose() {
    this.setData({
      showZp: false,
    })
  },
  //选择筛选
  onConfirmSx(data) {
    if (this.data.activeIndex == 1) {
      this.setData({
        sxObj: data,
        page: 1,
        parameter1: { //我指派的参数
          pt_id: data[0],//类型
          status: data[1],//状态
          sort: data[2],//排序
        }
      })
    } else {
      this.setData({
        sxObj: data,
        page: 1,
        parameter2: { //我指派的参数
          pt_id: data[0],//类型
          order_by: data[2] == 'publish' ? 'create_time' : 'expire_time',
        }
      })
    }
    this.setData({list:[]})
    this.getData(this.data.activeIndex);
  },
  onShow() {
    if (this.data.page == 1) {
      dd.hideLoading();
      this.setData({list:[]})
      that.getData(that.data.activeIndex);
    }
  },
    //过滤List,通常用于删除了某条数据，刷新列表
  filtrationList(index,id) {
    if (index == 1) {
      this.getToDay()
    } else if(index==2&&this.data.page!=1){
      var list=this.data.list;
      for (let i in list) {
        if (list[i].id == id) {
          list.splice(i, 1)
        }
      }
      this.setData({ list:list})
    }
  },
  getData(index, is) {
    var urlS = ['api/integral/work/list/publisher', 'api/integral/task/publish/list', 'api/integral/schedule/list'];
    var data;
    if (index == 1) {
      var parameter1 = this.data.parameter1;
      data = {
        page: that.data.page,
        page_size: 20,
        source_type: 1,
        pt_id: parameter1.pt_id,//类型
        status: parameter1.status,//状态
        sort: parameter1.sort,//排序
      }
    } else if (index == 2) {
      var parameter2 = this.data.parameter2;
      data = {
        page: that.data.page,
        page_size: 20,
        status: 'all',
        source_type: 1,
        pt_id: parameter2.pt_id,//类型
        order_by: parameter2.order_by,//类型
      }
    } else {
      data = {
        page: that.data.page,
        page_size: 20,
      }
    }
    app.$get(urlS[index - 1], data).then((res) => {
      var data = res.data.data.list;
      var list = that.data.list;
      data.forEach(element => {
        if (element.pt_id) {
          element.pt_name = app.getTypesItem(element.pt_id).name
        }
      });
      if (is) {
        if (data.length < 20) {
          this.setData({
            list: list.concat(data),
            isData: true
          })
        } else {
          this.setData({
            list: list.concat(data)
          })
        }
      } else {
        this.setData({
          list: data,
          isData: false
        })
      }
    })
  },
  activeItem(e) {
    var index = e.target.dataset.index;
    this.setData({
      activeIndex: index,
      page: 1,
      list: [],
      sxObj: [],
      parameter1: { //我指派的参数
        pt_id: 0,//类型
        status: 'all',//状态
        sort: 'publish',//排序
      },
      parameter2: { //我的悬赏
        pt_id: 0,
        order_by: 'create_time',
      }
    })
    this.getData(index);
  },
  openDetail(e) {
    var index = e.target.dataset.index;
    var id = e.target.dataset.item.id;
    if (that.data.activeIndex == 1) {
      dd.navigateTo({
        url: '../workDetail/workDetail?id=' + id
      })
    } else if (that.data.activeIndex == 2) {
      dd.navigateTo({
        url: '../taskDetail/taskDetail?id=' + id
      })
    } else {
      dd.navigateTo({
        url: '../repetitive_tasks_detail/repetitive_tasks_detail?id=' + id
      })
    }
  },
  //上啦加载
  onScrollToLower() {
    if (!that.data.isData) {
      this.setData({ page: ++that.data.page })
      this.getData(that.data.activeIndex, true);
    }
  },
});
