// pages/bocaiTVConfig/index.js
const app = getApp();
import { customRequest } from '../../utils/request.js';
import { baseUrl } from '../../utils/request.js';
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		second: 8,
		configInfo: {},
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		console.log(wx.getStorageSync("configInfo"));
		if (wx.getStorageSync("configInfo")) {
			this.setData({
				configInfo: wx.getStorageSync("configInfo"),
			});
		}else{
      this.setData({
				configInfo: {
          id:0,
          tvList:0,
          tvStep:20,
          logo:'',
        },
			});
    }
	},

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

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

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

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {},
	radioChange(e) {
		console.log(e);
		this.data.configInfo.tvList = e.detail.value;
		this.setData({
			configInfo: this.data.configInfo,
		});
		console.log(this.data.configInfo);
	},
	updateTVConfig() {
		let that = this;
		console.log(this.data.configInfo);
		let config = this.data.configInfo;
		let updateObj = {
			configId: config.id,
			tvList: config.tvList,
			tvStep: config.tvStep,
			logo: config.logo,
		};
    customRequest("/yishoudan/agency/config/update",{header:'headers', method: 'POST', data: updateObj}).then((res)=>{
      if (res.data.status == 200) {
        wx.showToast({
          title: "更新成功",
          icon: "none",
          duration: 1000,
          success() {
            setTimeout(() => {
              wx.navigateBack({
                delta: 1,
              });
            }, 1000);
          },
        });
        wx.setStorageSync("configInfo", that.data.configInfo);
      }
    })
		// wx.request({
		// 	url: app.globalData.ip + "/yishoudan/agency/config/update", // 分类列表获取接口
		// 	header: app.globalData.headers,
		// 	data: updateObj,
		// 	method: "POST",
		// 	success(res) {
		// 		console.log(res);
		// 		if (res.data.status == 200) {
		// 			wx.showToast({
		// 				title: "更新成功",
		// 				icon: "none",
		// 				duration: 1000,
		// 				success() {
		// 					setTimeout(() => {
		// 						wx.navigateBack({
		// 							delta: 1,
		// 						});
		// 					}, 1000);
		// 				},
		// 			});
		// 			wx.setStorageSync("configInfo", that.data.configInfo);
		// 		}
		// 	},
		// });
	},
	changeTime(e) {
		if (app.isNotEmptyCheck(e.target.dataset.type)) {
			let type = e.target.dataset.type;
			if (type == "add") {
				this.data.configInfo.tvStep = this.data.configInfo.tvStep + 1;
			} else {
				this.data.configInfo.tvStep = this.data.configInfo.tvStep - 1;
			}

			if (this.data.configInfo.tvStep < 2) {
				wx.showToast({
					title: "间隔时间不能少于2秒",
					icon: "none",
					duration: 2000,
				});
				this.data.configInfo.tvStep = 2;
			} else if (this.data.configInfo.tvStep > 20) {
				wx.showToast({
					title: "间隔时间不能大于20秒",
					icon: "none",
					duration: 2000,
				});
				this.data.configInfo.tvStep = 20;
			} else {
				wx.vibrateShort({
					type: "heavy",
					success: function (e) {
						console.log(e);
					},
				});
			}

			this.setData({
				configInfo: this.data.configInfo,
			});
		}
	},
	chooseImg() {
		let that = this;

		wx.chooseMedia({
			count: 1,
			mediaType: ["image"],
			sizeType: ["original", "compressed"],
			sourceType: ["album"],
			success(res) {
				console.log(res);
        wx.showLoading({
          title: "上传中...",
        });
				const tempFilePaths = res.tempFiles;
				tempFilePaths.forEach((item, index) => {
					wx.uploadFile({
						url: baseUrl + "/imgs/uploadImage",
						method: "post",
						// data:currData,
						// name: "uploadFile",
						filePath: tempFilePaths[index].tempFilePath,
						name: "uploadFile",
						header: app.globalData.headers,
						success: function (res) {
							console.log(res);
							let result = JSON.parse(res.data);
							if (result.status == 200) {
								that.data.configInfo.logo = result.data;
								that.setData({
									configInfo: that.data.configInfo,
								});
								console.log(result.data.data);
								console.log(that.data.configInfo);
							}
							setTimeout(() => {
								wx.hideLoading();
							}, 2000);
							// that.updateImgs(currData.type,currData.fkId,currData.data.data);
						},
						fail: function (res) {
							console.log(res);

							setTimeout(() => {
								wx.hideLoading();
							}, 2000);
						},
					});
					setTimeout(() => {
						wx.hideLoading();
					}, 2000);
				});
			},
			fail() {
				wx.hideLoading();
			},
		});
	},
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage1() {},
});
