const app = getApp();
import HTTP from "../../../../requestFn/Api"
const {baseURL} = app.globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    files: [],
    appendix: [],
    garend_id: null,
    class_list: [],
    class_id: "",
    valueLength:0
  },

  onLoad(options) {
    const userInfo = wx.getStorageSync('userInfo');
    const {
      garden_id
    } = app.globalData;
    this.setData({
      userInfo,
      garden_id
    })
    this.getNeighbor_class(garden_id)
  },
  chooseImage() {
    const that = this;
    wx.chooseMedia({
      mediaType: ["image"],
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const userInfo = wx.getStorageSync('userInfo')
        const pic = res.tempFiles.map(item => {
          wx.uploadFile({
            filePath: item.tempFilePath,
            name: 'file',
            url: baseURL + '/upload',
            header: {
              "token": userInfo.token
            },
            success(res) {
              const result = that.data.appendix;
              result.push(JSON.parse(res.data).data)
              that.setData({
                appendix: result
              })
            },
            fail(res) {
            }
          })
          return item.tempFilePath
        })
        that.setData({
          files: that.data.files.concat(pic),
        });
      },
      fail(res) {
      }
    });
  },
  previewImage(e) {
    wx.previewImage({
      current: this.data.files[e.currentTarget.dataset.index],
      urls: this.data.files,
    });
  },
  deleteImg(e) {
    var that = this
    wx.showModal({
      title: "提示",
      content: "是否删除图片",
      success: function (res) {
        if (res.confirm) {
          for (var i = 0; i < that.data.files.length; i++) {
            if (i == e.currentTarget.dataset.index) {
              that.data.files.splice(i, 1)
              that.data.appendix.splice(i, 1)
            }
          }
          that.setData({
            files: that.data.files
          })
        } else if (res.cancel) {
        }
      }
    })
  },
  getNeighbor_class(garden_id) {
    HTTP.Neighbor_class({
      garden_id
    }).then(res => {
      const class_list = []
      res.list.map(item => {
        if (item.enable) {
          class_list.push(item)
        }
      })
      this.setData({
        class_list
      })
    })
  },
  getClass_id(e) {
    const arr = this.data.class_list;
    const {
      class_id
    } = e.currentTarget.dataset
    for (let i = 0; i < arr.length; i++) {
      if (arr[i].id == class_id) {
        arr[i].checked = true;
      } else {
        arr[i].checked = false;
      }
    }
    this.setData({
      class_id,
      class_list: arr
    })
  },
  submit(e) {
    const that = this;
    const {
      appendix,
      class_id,
      garden_id
    } = that.data;
    const {
      content
    } = e.detail.value;
    if (content == "") {
      wx.showToast({
        title: '请输入发布内容',
        icon: "error",
      })
      return false
    }
    if (class_id == "") {
      wx.showToast({
        title: '请选择分类',
        icon: "error",
      })
      return false
    }
    HTTP.NeighborArticle({
      method: 'post',
      class_id,
      content,
      garden_id,
      pics: appendix
    }).then(res => {
      if (res.code == 0) {
        wx.showToast({
          title: '发布成功',
          icon: "success",
          success() {
            setTimeout(() => {
             wx.navigateBack({
               delta: 1,
             })
            }, 1000)
          }
        })
      }
    })
  },
  getTextLength(e) {
    const valueLength = e.detail.value.length;
    this.setData({
      valueLength
    })
  },
  goMyArticle(){
    wx.navigateTo({
      url: '../myarticle/myarticle',
    })
  }
})