// pages/works/works.js
var urlPath = require('../../config.js');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		workTitleArr: ['保存的作品', '分享的作品'],
		workIndex: 0,
		imgArr: [{
				imgSrc: '../../img/background/1.png',
				workName: '饺子吃肉肉饺子吃肉肉饺子吃肉肉饺子吃'
			},
			{
				imgSrc: '../../img/background/1.png',
				workName: '饺子吃肉肉'
			},
			{
				imgSrc: '../../img/background/1.png',
				workName: '饺子吃肉肉'
			},
			{
				imgSrc: '../../img/background/1.png',
				workName: '饺子吃肉肉'
			},
			{
				imgSrc: '../../img/background/1.png',
				workName: '饺子吃肉肉'
			},
		],
		page: 1,
		size: 200
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.checkImg();
	},
	// chooseTitle(e){
	// 	let newIndex = e.currentTarget.dataset.index;
	// 	this.setData({
	// 		workIndex:newIndex
	// 	})
	// 	console.log(this.data.workIndex);

	// },
	checkImg() {
		wx.request({
			url: urlPath.myBottleReleaseList,
			data: {
				page: this.data.page,
				size: this.data.size
			},
			header: {
				'token': wx.getStorageSync('token')
			},
			method: 'post',
			success: (res) => {
				if (res.data.code == 0) {
					let newArr = [];
					if (res.data.data.list.length) {
						res.data.data.list.forEach(item => {
							let obj = {
								name: item.name,
								id: item.id,
								imgSrc: 'https://fenggu.scdxtc.cn' + item.release_img,
								userId: item.user_id,
								time: item.create_time,
								isShare: item.production_id //0未分享
							}
							newArr.push(obj);
						})
					}
					this.setData({
						imgArr: newArr
					})
				}
			}
		});
	},
	shareEv(e) {
		wx.downloadFile({
			url: this.data.imgArr[e.currentTarget.dataset.index].imgSrc,
			success: (res) => {
				wx.showShareImageMenu({
					path: res.tempFilePath,
					success:(res)=>{
						this.shareImg(e.currentTarget.dataset.id,e.currentTarget.dataset.index);
					},
					fail:(err)=>{
						console.log(err);
					}
				})
			}
		})
	},
	shareImg(id,index){
		wx.request({
			url: urlPath.publishMyBottle,
			data: {
				id: id
			},
			header: {
				'token': wx.getStorageSync('token')
			},
			method: 'post',
			success: (res) => {
				if (res.data.code == 0) {
					this.data.imgArr[index].isShare=1;
					this.setData({
						imgArr:this.data.imgArr
					})
					wx.showToast({
						title: '分享作品成功'
					})
				}
			}
		});
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

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

	},

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

	},

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

	},

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

	},

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

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
