// pages/addReel/addReel.js
const Http = require('../../utils/HttpBasics.js')
const config = require('../../config/config.js')
const util = require('../../utils/util.js')
Page({
  getSearchsText(e) {
    this.setData({
      searchsText: e.detail.value
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    clsIndex: 0,
    searchsText: '',
    status: -1, //卷状态
    list: [], //数据列表
    putApplyStatus: 0, //查询是否审批状态 -1表示未审批 2 表示以审批,
    targetAd: "",
    channel: 0, //已上架按钮判断条件 1是已上架
    pageNum: 1,
    statueList: [{
      name: '已上架',
      action: 'getShelfList',
      cls: 'save'
    },
    {
      name: '已作废',
      action: 'getcancellation',
      cls: 'cancellation',
    },
    {
      name: '已审批',
      action: 'getexamine',
      cls: 'examine',
    },
    {
      name: '未审批',
      action: 'getnoExamine',
      cls: 'noExamine',
    }
    ],
    noUpdata: true,//add是否能更改
  },
  addReel() {
    this.setData({
      noUpdata: true,
    })
    wx.navigateTo({
      url: '/pages/upload/upload'
    })
  },
  /**
   * 已上架
   */
  getShelfList() {
    this.setData({
      status: 0,
      channel: 1,
      pageNum: 1,
      targetAd: '',
      clsIndex: 0,
      noUpdata: false,
    })
    let getdata = {
      title: this.data.searchsText,
      status: this.data.status, // -1表示全部
      sourceType: 2,
      pageNum: 1,
      pageSize: 10,
      // type: 6,//6表示通用卷 
      noUpdata: false,
    }
    Http.get({
      url: config.api.getShelf,
      data: getdata
    }).then(res => {
      const {
        code,
        data
      } = res
      if (code == 200) {
        this.setData({
          list: data.list
        })
      } else {
        console.log("查询出错")
        return
      }

    })
  },
  /**
   *查询卷
   */
  search() {
    wx.showLoading({
      title: '加载中',
    })
    let getdata = {
      title: this.data.searchsText,
      status: this.data.status, // -1表示全部
      sourceType: 2,
      pageNum: this.data.pageNum,
      pageSize: 10,
      type: 6, //6表示通用卷 
      targetAd: this.data.targetAd,
      noUpdata: false,
    }
    if (this.putApplyStatus != 0) {
      getdata.putApplyStatus = this.data.putApplyStatus
    }
    Http.get({
      url: config.api.getCouponlist,
      data: getdata
    }).then(res => {
      const {
        code,
        data
      } = res
      if (code == 200) {
        if (this.data.pageNum == 1) {
          wx.hideLoading();
          this.setData({
            list: data.list
          })
        } else {
          if (data) {
            let tempList = []
            tempList.push(...this.data.list, ...data.list)
            console.log(this.data.list, tempList)
            wx.hideLoading();
            this.setData({
              list: tempList
            })

          } else {
            wx.hideLoading();
            return
          }
        }

      } else {
        console.log("查询出错")
        return
      }

    })

  },
  /**
   * 查全部
   */
  getAll() {
    this.setData({
      status: -1,
      putApplyStatus: "",
      targetAd: '',
      channel: '',
      pageNum: 1,
    })
    this.search()
  },
  /**
   * 查询已作废
   */
  getcancellation() {
    this.setData({
      status: 1,
      putApplyStatus: "",
      targetAd: '',
      channel: '',
      pageNum: 1,
      clsIndex: 1,
      noUpdata: false,
    })
    this.search()
  },
  /**
   * 查询以审批
   */
  getexamine() {
    this.setData({
      status: 0,
      putApplyStatus: 2,
      targetAd: '1',
      channel: '',
      pageNum: 1,
      clsIndex: 2,
      noUpdata: false,
    })
    this.search()
  },
  /**
   * 查询未审批
   */
  getnoExamine() {
    this.setData({
      status: 0,
      putApplyStatus: -1,
      targetAd: '',
      channel: '',
      pageNum: 1,
      clsIndex: 3,
      noUpdata: true,
    })
    this.search()
  },
  /**
   * 跳转到卷详情
   */
  goParticulars(e) {
    const data = e.currentTarget.dataset.data
    let postData = {
      title: data.title,
      type: "6",
      priceStr: data.priceStr,
      coverImg: data.coverImg,
      salePriceStr: data.salePriceStr,
      inventory: data.inventory,
      useLimitQuantity: data.useLimitQuantity,
      validStartDate: data.validStartDate,
      validEndDate: data.validStartDate,
      remark: data.remark,
      subTitle: '', //不用管管副标题 B端没用
      sendType: "1",
      remainInventory: data.remainInventory, //剩余库存
      validType: data.validType, //1是时间类型 2是领取后多少天有效
      numDays: data.validType == 2 ? data.validDays : '',
      validStartDate: data.validType == 1 ? util.formatTime(data.validStartDate, 'yyyy-MM-dddd') : '请选择时间',
      validEndDate: data.validType == 1 ? util.formatTime(data.validEndDate, 'yyyy-MM-dddd') : '请选择时间',
      status: data.status,
      id: data.id,
      couponId: data.couponId ? data.couponId : "",
      autoRefund: data.autoRefund,
      putApplyStatus: data.putApplyStatus, //0未提交审批 1待审批 2审批成功 3审批驳回
      channel: this.data.channel,
      noUpdata: this.data.noUpdata,
      useLimitRule: data.useLimitRule,
      coverPicture: data.coverPicture,
      detailPicture: data.detailPicture
    }
    console.log(postData, "postData")
    const tmepdata = JSON.stringify(postData)
    wx.setStorageSync('tmepdata', tmepdata)
    wx.navigateTo({
      url: '/pages/addReel/addReel',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      pageNum: 1
    })
    // this.search()
    this.getShelfList()
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(this.data.clsIndex, "???")
    switch (this.data.clsIndex) {
      case 0: this.getShelfList(); break;
      case 1: this.getcancellation(); break;
      case 2: this.getexamine(); break;
      case 3: this.getnoExamine(); break;
    }
  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      pageNum: this.data.pageNum + 1
    })
    this.search()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})