// 添加分类
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    catalogName: '',
    catalogOrder: 0,
    catalogOneList: [],
    catalogTwoList: [],
    oneIndex: 0,
    oneCatalog: null,
    twoIndex: 0,
    twoCatalog: null,
    catalogLogoPhotos: [],
    catalogSummary: '',
    formPage: 0,
    typeList: [{
      name: '商品',
      id: '1',
      isSel: true
    }, {
      name: '优惠券',
      id: '2',
      isSel: false
    }],
    selType: '1'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type) {
      this.setData({
        formPage: 1
      })
    }
    this.getCatalogList(1, 0, 1, 0)
  },
  //获取一级类目
  getCatalogList(type, id, level, code) {
    let _self = this
    app.http.get(app.urls.getCatalogList.format({
        level: level
      }), {
        header: {
          token: app.globalData.token
        },
        data: {
          id: id,
          code: code
        }
      })
      .then(res => {
        if (type == 1) {
          _self.setData({
            oneIndex: 0,
            catalogOneList: res.data
          })
        } else {
          _self.setData({
            twoIndex: 0,
            catalogTwoList: res.data
          })
        }
      })
      .catch(err => {
        wx.hideLoading({})
      })
  },
  doSelType(e) {
    var item = e.currentTarget.dataset.item
    var _self = this
    _self.data.typeList.forEach(it => {
      if (it.id == item.id) {
        it.isSel = true
        _self.setData({
          selType: item.id
        })
      } else {
        it.isSel = false
      }
    })
    _self.setData({
      typeList: _self.data.typeList
    })
  },
  //一级类目选择
  oneCatalogSelect(e) {
    this.setData({
      oneIndex: e.detail.value,
      oneCatalog: this.data.catalogOneList[e.detail.value]
    })
    this.getCatalogList(2, this.data.catalogOneList[this.data.oneIndex].id, 2, this.data.catalogOneList[this.data.oneIndex].code)
  },
  //二级类目选择
  twoCatalogSelect(e) {
    this.setData({
      twoIndex: e.detail.value,
      twoCatalog: this.data.catalogTwoList[e.detail.value]
    })
  },
  //预览Logo
  ViewLicImg() {
    var tempList = [e.currentTarget.dataset.url]
    wx.previewImage({
      urls: tempList,
      current: e.currentTarget.dataset.url
    });
  },
  DelLogoP(e) {
    let _self = this
    wx.showModal({
      title: '提示',
      content: '确定要删除该图片吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          _self.data.catalogLogoPhotos.splice(e.currentTarget.dataset.index, 1)
          _self.setData({
            catalogLogoPhotos: _self.data.catalogLogoPhotos
          })
        }
      }
    })
  },
  //选择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)
        });
      }
    });
  },
  //进行图片上传 type 1 营业执照   2 logo
  doUploadImg(type, path) {
    let _self = this
    wx.showLoading({
      title: '上传中...',
    })
    app.http.upload(app.urls.doUploadImg, {
        path: path,
        name: 'image',
        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
        }
        let tempList = _self.data.catalogLogoPhotos.concat(item)
        _self.setData({
          catalogLogoPhotos: tempList
        })
      })
      .catch(err => {
        wx.hideLoading({})
        wx.showToast({
          title: '上传失败',
        })
      })
  },
  //校验参数
  checkParams() {
    if (this.data.catalogName == '') {
      wx.showToast({
        title: '请输入类目名称',
        icon: 'none'
      })
      return false
    }
    if (this.data.catalogOrder == 0) {
      wx.showToast({
        title: '请输入类目排序',
        icon: 'none'
      })
      return false
    }
    if (this.data.catalogLogoPhotos.length <= 0) {
      wx.showToast({
        title: '请上传类目Logo',
        icon: 'none'
      })
    }


    return true
  },
  //添加类目
  doAdd() {
    let _self = this
    if (_self.checkParams()) {
      //确定添加
      wx.showLoading({
        title: '添加中...',
      })
      app.http.post(app.urls.doSaveCatalog, {
          header: {
            token: app.globalData.token
          },
          data: {
            categoryName: _self.data.catalogName, //名称
            categorySort: _self.data.catalogOrder, //排序
            categoryLogo: _self.data.catalogLogoPhotos.length > 0 ? _self.data.catalogLogoPhotos[0].id : '', //logo
            categorySummary: _self.data.catalogSummary, //说明
            from: 'mini', //来源
            coupon: '1'
          }
        })
        .then(res => {
          wx.hideLoading({})
          wx.showToast({
            title: '添加成功',
            icon: 'success'
          })
          let arr = getCurrentPages()
          let lastPage = (arr.length >= 2) ? arr[arr.length - 2] : undefined
          lastPage.setData({
            isAddPage: true
          })
          wx.navigateBack({})
        })
        .catch(err => {
          wx.hideLoading({})
        })
      // wx.showModal({
      //   title: '提示',
      //   content: '添加成功后需要审核(1-3个工作日)并且无法删除和修改,确定要添加该类目吗?',
      //   success(res) {
      //     if (res.confirm) {

      //     }
      //   }
      // })
    }
  },
  //监听输入
  inputWatch(e) {
    let key = e.currentTarget.dataset.model
    this.setData({
      [key]: e.detail.value
    })
  },
  catalogInput(e) {
    this.setData({
      catalogSummary: e.detail.value
    })
  }
})