// pages/mine/wxml/amendthebaby/amendthebaby.js
var util = require('../../../../utils/util.js');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
     arrays: ['珠宝玉石', '古玩字画', '奢侈品'],
    index: 0,
    indexs:0,
    pics: [],
    count: [],
    isShow: true,
    date: '',
    time: '',
    dates: '',
    times: '',
    imgData : [],
    display:false,
    close:true,
    delayed:"300",
    delayeddisabled:false
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  bindPickerChanges: function (e) {
    this.setData({
      indexs: e.detail.value
    })
  },

  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChanges: function (e) {
    this.setData({
      times: e.detail.value
    })
  },
  bindDateChanges: function (e) {
    this.setData({
      dates: e.detail.value
    })
  },
//拍卖按钮相应的隐藏与显示

  switchChange: function (e) {
    var _this = this;
    if (e.detail.value === true) {
      _this.setData({
        display: e.detail.value,
        close: false,
        delayed: "15",
        delayeddisabled: true,
      })
    } else {
      _this.setData({
        close: true,
        display: e.detail.value,
        delayed: "300",
        delayeddisabled: false
      })
    }

  },
    //创建拍卖会
  establish: function () {
    var _this = this
    wx.request({
      url: app.globalData.prodUrl + 'web-pm/wx/pm/check_live_room',
      data: {
        shop_id: _this.data.shop_id
      },
      header: {
        'ydw-token': wx.getStorageSync('key').ydw_token
      },
      success: function (data) {
        if (data.data.success == true) {
          wx.navigateTo({
            url: '../establish/establish'
          })
        } else {
          wx.showModal({
            title: '提示',
            content: data.data.tip.msg,
            success: function (res) {
              if (res.confirm) {
                
              } else if (res.cancel) {
                
              }
            }
          })
        }
      },
    });
  },
  //上传图片
  chooseImage: function () {
    var _this = this,
      pics = this.data.pics;
    wx.chooseImage({
      count: 9 - pics.length, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        // success
        //
        for (var i = 0; i < res.tempFilePaths.length; i++) {
          wx.uploadFile({
            url: app.globalData.prodUrl + 'web-cp/upload',
            filePath: res.tempFilePaths[i],
            name: 'file',
            success(res) {
              _this.data.imgData.push(JSON.parse(res.data).file_id)
            }
          })
        }

        var imgSrc = res.tempFilePaths;
        pics = pics.concat(imgSrc);
        // 控制触发添加图片的最多时隐藏
        if (pics.length >= 9) {
          _this.setData({
            isShow: (!_this.data.isShow)
          })
        } else {
          _this.setData({
            isShow: (_this.data.isShow)
          })
        }
        _this.setData({
          pics: pics
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  // 图片预览
  previewImage: function (e) {
    var current = e.target.dataset.src
    wx.previewImage({
      current: current,
      urls: this.data.pics
    })
  },
  //上传标地物
  formSubmit: function (e) {
    var auction_way = "";
    if (e.detail.value.checked == true) {
      auction_way = "拍卖竞买"
    } else {
      auction_way = "在线竞买"
    }
    if (this.data.pics.length < 5) {
      wx.showToast({
        title: '图片不可少于5张',
        icon: 'none',
        duration: 2000
      })
    } else if (e.detail.value.name !== "" && e.detail.value.describe !== "" && e.detail.value.start !== "" && e.detail.value.bond !== "" && e.detail.value.priceincrease !== "" && e.detail.value.auctioneer !== "" && e.detail.value.delayed !== "") {
      var _this = this;
      wx.request({
        url: app.globalData.prodUrl + 'web-pm/wx/pm/update_item',
        header: {
          'ydw-token': wx.getStorageSync('key').ydw_token
        },
        data: {
          bid_type: e.detail.value.choice,
          item_name: e.detail.value.name,
          bid_content: e.detail.value.describe,
          bid_file_id: _this.data.imgData + '',
          price_start: Number(e.detail.value.start),
          price_guarantee: Number(e.detail.value.bond),
          price_add: Number(e.detail.value.priceincrease),
          time_opening: e.detail.value.startdate + " " + e.detail.value.starttime,
          time_end: e.detail.value.closingdate + " " + e.detail.value.closingtime,
          auction_way: auction_way,
          shop_id: _this.data.shop_id,
          live_room_id: e.detail.value.liveroom,
          auctioneer: e.detail.value.auctioneer,
          update_interval: Number(e.detail.value.delayed),
          item_kid: e.detail.target.dataset.kid
        },
        success: function (data) {
          wx.navigateBack({
            delta: 1,
            success: function () {
              wx.showToast({
                title: '发布成功！',
                icon: 'none',
                duration: 3000
              });
              var pages = getCurrentPages();
              var page = getCurrentPages().pop();
              if (page == undefined || page == null) return;
              pages[2].onLoad(pages[2].options);
            }
          })
        }
      })
    } else {
      wx.showToast({
        title: '不得为空！',
        icon: 'none',
        duration: 2000
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    var dates = util.formatTime(new Date);
    var newdate = dates.split(' ')[0];
    var timedate = dates.split(' ')[1];
    _this.setData({
      date: newdate.replace(/\//g, "-"),
      dates: newdate.replace(/\//g, "-"),
      time: timedate.substr(0, 5),
      times: timedate.substr(0, 5),
      shop_id: options.shop_id
    })
    wx.request({
      url: app.globalData.prodUrl + 'web-pm/wx/pm/get_item',
      data: {
        item_kid: options.kid
      },
      header: {
        'ydw-token': wx.getStorageSync('key').ydw_token
      },
      success: function (data) {
        _this.setData({
          array: data.data.data
        })
        var auction_way = data.data.data.auction_way;
        if (auction_way == "拍卖竞买") {
          _this.setData({
            checked: true,
            close: false,
            delayed: "15",
            delayeddisabled: true,
            display:true
          })
        } else {
          _this.setData({
            checked: false,
            close: true,
            display: false,
            delayed: "300",
            delayeddisabled: false
          })
        }
      },
    });
    wx.request({
      url: app.globalData.prodUrl + 'web-pm/wx/pm/list_verify_pass',
      header: {
        'ydw-token': wx.getStorageSync('key').ydw_token
      },
      data: {
        shop_id: _this.data.shop_id
      },
      success: function (data) {
        _this.setData({
          news: data.data.data
        })
      },
    });
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

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

  },

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

  }
})