// packageD/auction/auctioneer-manage/good_manage/good_manage.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    delbox: false,
    id: wx.getStorageSync("yz_uid"),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getStatus();
    this.getData();
  },
  sdel(e) {
    let ind = e.currentTarget.dataset.ind;
    var list = this.data.datas;
    console.log(!list[ind].edit_active);
    list[ind].edit_active = !list[ind].edit_active;
    this.setData({
      datas: list,
    });
  },
  gobianji(e) {
    console.log(e.currentTarget.dataset.good);
    let goodid = e.currentTarget.dataset.good;
    wx.navigateTo({
      url:
        "/packageD/auction/auctioneer-publish/publish_first/publish_first?good=" +
        goodid,
    });
  },
  // 上架
  toUp(e) {
    var that = this;
    var goodid = e.currentTarget.dataset.good;
    var sta = e.currentTarget.dataset.status;
    var json = {
      art: "upperLower",
      goods_id: goodid,
      status: sta == 0 ? 1 : 0,
    };
    let urlStr = app.getNetAddresss("plugin.auction.api.index.getAuctionData");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.data.upperLower,
            icon: "none",
            duration: 1000,
          });
          that.getSearch();
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  commodityDelete(e) {
    var that = this;
    let goodid = e.currentTarget.dataset.good;
    let urlStr = app.getNetAddresss("plugin.auction.api.index.getAuctionData");
    let json = {
      goods_id: goodid,
      art: "delGoods",
    };
    wx.showModal({
      title: "提示",
      content: "此操作将永久删除该拍品, 是否继续? ",
      success(res) {
        if (res.confirm) {
          app._getNetWork({
            url: urlStr,
            data: json,
            success: function (resdata) {
              var res = resdata.data;
              if (res.result == 1) {
                wx.showToast({
                  title: "拍品删除成功",
                  icon: "none",
                  duration: 1000,
                });
                that.getSearch();
              } else {
                wx.showToast({
                  title: res.msg,
                  icon: "none",
                  duration: 1000,
                });
              }
            },
            fail: function (res) {
              console.log(res);
            },
          });
        } else if (res.cancel) {
          console.log("用户点击取消");
        }
      },
    });
  },
  emptyEditActive() {
    for (var i = 0; i < this.data.datas.length; i++) {
      this.data.datas[i].edit_active = false;
    }
    this.setData({
      datas: this.data.datas,
    });
  },
  itemClick(e) {
    console.log(e);
    this.setData({
      active: e.detail.index,
    });
    this.getSearch();
  },
  goFabu() {
    console.log(this.data.uncertified_status);
    if (this.data.renzhen_status == "已认证") {
      this.getNum();
      return;
    }
    if (this.data.uncertified_status == "2") {
      wx.showToast({
        title: "未认证会员不允许发布拍品",
        icon: "none",
        duration: 1000,
      });
    } else {
      this.getNum();
    }
  },
  // 拍卖发布数量验证
  getNum() {
    var that = this;
    let urlStr = app.getNetAddresss("plugin.auction.api.index.getAuctionData");
    let json = {
      art: "judgeNum",
    };
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.navigateTo({
            url:
              "/packageD/auction/auctioneer-publish/publish_first/publish_first",
          });
        } else {
          that.getverifyGoods();
          // wx.showToast({
          //   title: res.msg,
          //   icon: "none",
          //   duration: 1000,
          //   success(){
          //     setTimeout(() => {
          //       that.getverifyGoods()
          //     }, 1000);
          //   }
          // })
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  // 查看是否认证
  getverifyGoods() {
    var that = this;
    let json = {
      art: "judgeNum2",
    };
    let urlStr = app.getNetAddresss("plugin.auction.api.index.getAuctionData");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(that.data.renzhen_status);
          if (that.data.renzhen_status == "未认证") {
            wx.navigateTo({
              url:
                "/packageD/auction/auction-apply/auction-apply/auction-apply",
            });
          } else if (that.data.renzhen_status == "认证中") {
            // 认证中去编辑申请
            wx.navigateTo({
              url:
                "/packageD/auction/auction-apply/auction-apply/auction-apply?info=get",
            });
          }
        } else {
          console.log(res, "需要购买指定商品才能申请");
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
            success() {
              setTimeout(() => {
                var geturl;
                if (that.isString(res.data.judgeNum2)) {
                  geturl = res.data.judgeNum2.url;
                } else {
                  geturl = res.data.judgeNum2;
                }
                wx.navigateTo({
                  url: "/packageA/detail_v2/detail_v2?id=" + geturl,
                });
              }, 1000);
            },
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  // 获取信息
  getData() {
    var that = this;
    let urlStr = app.getNetAddresss("plugin.auction.api.index.getAuctionData");
    let json = {
      art: "auctioneer",
    };
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            info: res.data.auctioneer,
            renzhen_status: res.data.auctioneer.status,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getSearch();
  },

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareTimeline: function () {
    if (this.data.current_page >= this.data.last_page) {
      wx.showToast({
        title: "暂无更多",
        icon: "none",
        duration: 1000,
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if (this.data.current_page >= this.data.last_page) {
      wx.showToast({
        title: "暂无更多",
        icon: "none",
        duration: 1000,
      });
    }
  },
  getStatus() {
    var that = this;
    let urlStr = app.getNetAddresss("plugin.auction.api.index.getAuctionData");
    let json = {
      member_id: that.data.id,
    };
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          // console.log(res.data.getVerify.status)
          that.setData({
            status: res.data.getVerify.status,
            uncertified_status: res.data.getAgreement.uncertified_status,
            is_release_switch: res.data.getAgreement.is_release_switch,
          });
          that.getSearch();
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getSearch() {
    let that = this;
    var urlStr;
    if (this.data.active == "0") {
      urlStr = app.getNetAddresss("plugin.auction.api.auction.check");
    } else if (this.data.active == "1") {
      urlStr = app.getNetAddresss("plugin.auction.api.auction.biding");
    } else if (this.data.active == "2") {
      urlStr = app.getNetAddresss("plugin.auction.api.auction.bid");
    } else if (this.data.active == "3") {
      urlStr = app.getNetAddresss("plugin.auction.api.auction.finish");
    } else if (this.data.active == "4") {
      urlStr = app.getNetAddresss("plugin.auction.api.auction.bid");
    }
    let json = {
      auctioneer_id: that.data.id,
    };
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          for (var i = 0; i < res.data.data.length; i++) {
            res.data.data[i].edit_active = false;
          }
          that.setData({
            datas: res.data.data,
            current_page: res.data.data.current_page,
            last_page: res.data.data.last_page,
            total: res.data.total,
          });
          console.log(that.data.datas);
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
});
