const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    startDate: "",
    endDate: "",
    shopName: "", //商店名称
    shopAddress: "", //商店地址
    index: null,
    date: '2021-08-08', //营业时间
    shopLicensePhotos: [], //商店营业执照
    shopLogoPhotos: [], //商店Logo
    textareaBValue: '', //店铺简介,
    isBusiness: true, //是否开业
    selTradeList: [], //行业类型
    shopVideos: [], //宣传视频
    selTemplateId: '1',
    selTradeNames: '请选择行业',
  },
  PickerChange(e) {
    this.setData({
      index: e.detail.value
    })
  },
  // 商店注册
  RegisterShop() {
    if (this.checkParams()) {
      wx.showLoading({
        title: '注册中...'
      })
      let _self = this
      let industryIds = ''
      this.data.selTradeList.forEach(el => {
        industryIds += el.id + ','
      })
      app.http.post(app.urls.doSaveShop, {
          header: {
            token: app.globalData.token
          },
          data: {
            shopName: _self.data.shopName,
            shopAddress: _self.data.shopAddress,
            shopSummary: _self.data.textareaBValue,
            openDate: _self.data.date,
            shopLogo: _self.data.shopLogoPhotos[0].id,
            industryIds: industryIds,
            shopTemplateId: _self.data.selTemplateId,
            isOpen: _self.data.isBusiness ? '1' : '0',
            shopVideo: _self.data.shopVideos.length > 0 ? _self.data.shopVideos[0].id : ''
          }
        })
        .then(res => {
          wx.hideLoading({})
          wx.showToast({
            title: '注册成功',
            icon: 'success',
            success(res) {
              wx.navigateBack({
                delta: 1,
              })
            }
          })
        })
        .catch(err => {
          wx.hideLoading({})
        })
    }
  },
  //选择模板样式
  choosetemplate(e) {
    this.setData({
      selTemplateId: e.detail.value
    })
  },
  DateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },

  checkIsBusiness(e) {
    this.setData({
      isBusiness: e.detail.value
    })
  },
  //选择营业执照
  ChooseLicenseP() {
    let _self = this
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], //从相册选择
      success: (res) => {
        res.tempFilePaths.forEach(element => {
          _self.doUploadImg(1, element)
        });
      }
    });
  },
  //选择logo
  ChooseLogoP() {
    let _self = this
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], //从相册选择
      success: (res) => {
        res.tempFilePaths.forEach(element => {
          _self.doUploadImg(2, element)
        });
      }
    });
  },
  //删除Logo
  DelLogoP(e) {
    wx.showModal({
      title: '提示',
      content: '确定要删除该图片吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.shopLogoPhotos.splice(e.currentTarget.dataset.index, 1)
          this.setData({
            shopLogoPhotos: this.data.shopLogoPhotos
          })
        }
      }
    })
  },
  //删除营业执照
  DelLicenseP(e) {
    wx.showModal({
      title: '提示',
      content: '确定要删除该图片吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.shopLicensePhotos.splice(e.currentTarget.dataset.index, 1)
          this.setData({
            shopLicensePhotos: this.data.shopLicensePhotos
          })
        }
      }
    })
  },
  //进行图片上传 type 1 营业执照   2 logo
  doUploadImg(type, path) {
    wx.showLoading({
      title: '上传中...',
    })
    let _self = this
    app.http.upload(app.urls.doUploadImg, {
        name: 'image',
        path: path,
        header: {
          token: app.globalData.token
        }
      })
      .then(res => {
        wx.hideLoading({})
        var id = JSON.parse(res).data
        let pathStr = app.urls.baseImgUrl + id
        let item = {
          id: id,
          path: pathStr
        }
        if (type == 2) {
          //Logo
          let tempList = _self.data.shopLogoPhotos.concat(item)
          _self.setData({
            shopLogoPhotos: tempList
          })
        } else {
          //营业执照
          let tempList = _self.data.shopLicensePhotos.concat(item)
          _self.setData({
            shopLicensePhotos: tempList
          })
        }
      })
      .catch(err => {
        wx.hideLoading({})
        wx.showToast({
          title: '上传失败,请重试',
          icon: 'none'
        })
      })
  },
  inputWatch(e) {
    let key = e.currentTarget.dataset.model
    this.setData({
      [key]: e.detail.value
    })
  },
  ViewLogoImg(e) {
    var tempList = [e.currentTarget.dataset.url]
    wx.previewImage({
      urls: tempList,
      current: e.currentTarget.dataset.url
    });
  },
  ViewLicImg(e) {
    var tempList = [e.currentTarget.dataset.url]
    wx.previewImage({
      urls: tempList,
      current: e.currentTarget.dataset.url
    });
  },
  viewVideo(e) {
    var tempList = [{
      url: e.currentTarget.dataset.url,
      type: 'video'
    }]
    wx.previewMedia({
      sources: tempList,
      current: 0,
      showmenu: false
    })
  },
  delVideo(e) {
    let _self = this
    wx.showModal({
      title: '提示',
      content: '确定要删除该视频吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          _self.data.shopVideos.splice(e.currentTarget.dataset.index, 1)
          _self.setData({
            shopVideos: _self.data.shopVideos
          })
        }
      }
    })
  },
  textareaBInput(e) {
    this.setData({
      textareaBValue: e.detail.value
    })
  },
  checkParams() {
    var _self = this
    if (_self.data.shopName == '') {
      wx.showToast({
        title: '商店名称为必填项',
        icon: 'none'
      })
      return false
    }
    if (_self.data.shopAddress == '') {
      wx.showToast({
        title: '店铺地址为必填项',
        icon: 'none'
      })
      return false
    }
    if (_self.data.selTradeList.length <= 0) {
      wx.showToast({
        title: '请选择行业',
        icon: 'none'
      })
      return false
    }
    if (_self.data.date == '') {
      wx.showToast({
        title: '请选择开业时间',
        icon: 'none'
      })
      return false
    }
    if (_self.data.shopLogoPhotos.length <= 0) {
      wx.showToast({
        title: '请上传店铺Logo',
        icon: 'none'
      })
      return false
    }

    return true
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  showChooseTrade() {
    wx.navigateTo({
      url: './choosetrade',
    })
  },
  onShow() {
    let _self = this
    if (_self.data.selTradeList.length > 0) {
      var names = ''
      _self.data.selTradeList.forEach(el => {
        names += el.name + ','
      })
      _self.setData({
        selTradeNames: names
      })
    }
  },
  //选择视屏
  chooseVideo() {
    let _self = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['video'],
      sourceType: ['album', 'camera'], //从相册选择
      maxDuration: 30,
      success: (res) => {
        _self.doUploadVideo(res)
      }
    });
  },
  doUploadVideo(e) {
    wx.showLoading({
      title: '上传中...',
    })
    let _self = this
    app.http.upload(app.urls.doUploadVideo, {
        name: 'video',
        path: e.tempFiles[0].tempFilePath,
        header: {
          token: app.globalData.token
        }
      })
      .then(res => {
        wx.hideLoading({})
        var id = JSON.parse(res).data
        let pathStr = e.tempFiles[0].thumbTempFilePath
        let videoU = app.urls.baseImgUrl + id
        let item = {
          id: id,
          path: pathStr,
          videoUrl: videoU
        }
        let tempList = _self.data.shopVideos.concat(item)
        _self.setData({
          shopVideos: tempList
        })
      })
      .catch(err => {
        wx.hideLoading({})
      })
  },
  showTemplete(e) {
    var item = e.currentTarget.dataset.item
    var path = []
    switch (item) {
      case "1":
        path.push('../../../images/templete_1.png')
        break
      case "2":
        path.push('../../../images/templete_2.png')
        break
      case "3":
        path.push('../../../images/templete_3.png')
        break
      case "4":
        path.push('../../../images/templete_4.png')
        break
    }
    console.log(path)
    wx.previewImage({
      urls: path,
    })
  },
})