// packageF/storeManagement/components/baseInfo_popup/goods_details_pop/goods_details_pop.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
  },

  /**
   * 组件的初始数据
   */
  data: {
    show:false,
    contentList:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showPopup(){
      this.setData({
        show:!this.data.show
      });
    },
    addAtLast(){
      let o = { type: "text", value: "", visible: false };
      let contentList = this.data.contentList;
      contentList.push(o);
      this.setData({
        contentList
      });
    },
    //向下移
    moveDown(evt) {
      let index = evt.currentTarget.dataset.index;
      let cur = this.data.contentList.splice(index, 1);
      this.spliceContent(index + 1, 0, cur[0]);
    },
    //向上移
    moveUp(evt) {
      let index = evt.currentTarget.dataset.index;
      let cur = this.data.contentList.splice(index, 1);
      this.spliceContent(index - 1, 0, cur[0]);
    },
    //删除内容
    delOne(evt) {
      let index = evt.currentTarget.dataset.index;
      this.spliceContent(index, 1);
    },
    // 插入内容
    inserOne (evt) {
      let index = evt.currentTarget.dataset.index;
      this.spliceContent(index + 1, 0, { type: "text", value: "", visible: false });
    },
    changeText(evt){
      let index = evt.currentTarget.dataset.index;
      let value = evt.detail.value;
      this.data.contentList[index].value=value;
    },
    spliceContent(start, count, added) {
      if (typeof added !== "undefined") {
        this.data.contentList.splice(start, count, added);
      } else {
        this.data.contentList.splice(start, count);
      }
      this.setData({
        contentList:this.data.contentList
      });
    },
    saveEdit(){
      let jsonStr = '';
      this.data.contentList.forEach((per, ind) => {
        if (per.type === 'text') {
          jsonStr += `<p>${per.value}</p>`;
        }
        if (per.type === 'image') {
          jsonStr += `<img src="${per.value}"></img>`;
        }
      });
      this.showPopup();
      this.triggerEvent("saveEdit", jsonStr);
    },
    choineImage(){
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success:(res)=> {
          this.uploadImage(res.tempFilePaths);
        }
      });
    },
    uploadImage(tempFilePaths){
      let urlStr = app.getNetAddresss("upload.uploadPic");
     
      wx.showLoading({
        title: '正在上传',
      });
      wx.uploadFile({
        url: urlStr,
        filePath: tempFilePaths[0],
        name: 'file',
        formData: null,
        success:(resdata)=> {
          var res = JSON.parse(resdata.data);
          let o =  { type: "image", value: res.data.img_url, visible: false };
          let contentList = this.data.contentList;
          contentList.push(o);
          this.setData({
            contentList
          });
        },
        complete(e) {
          wx.hideLoading();
        }
      });
    }
  }
});
