const app = getApp()
import { Base } from '../../utils/base.js';
import apiList from '../../utils/apiList';
const base = new Base();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		canIUseGetUserProfile: false,
		showUserInfo: false,
		endLoading: false,
		list: [],
		next_page: 1, // 下一页
		has_more: true, // 是否还有更多 
		locked: false,
		limit: 10,
		count: '',//工单总数
		showOption: false,
		index: 0,
		optionsList: [
			{
				id: 1,
				name: '本周'
			}, {
				id: 2,
				name: '今日'
			}, {
				id: 3,
				name: '本月'
			}, {
				id: 4,
				name: '全部'
			}
		],
		time_type: 1,//时间段
		imgBox: [],
		loginFirst: true
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		var that = this
		if (wx.canIUse('getUserProfile') && wx.getUserProfile) {
			that.setData({
				canIUseGetUserProfile: true
			})
		} else {
			that.setData({
				canIUseGetUserProfile: false
			})
		}
		app.globalData.tjgs_fresh = false
	},
	onShow: function () {
		
		this.setData({
			isrefresh: this.data.isrefresh ? this.data.isrefresh : false
		})
		if (this.data.isrefresh || app.globalData.tjgs_fresh) {

			this.setData({
				isrefresh: false,
				endLoading: false,
				list: [],
				next_page: 1, // 下一页
				has_more: true, // 是否还有更多 
				locked: false,
			})
			app.globalData.tjgs_fresh = false
			setTimeout(() => {
				this.getList()
			}, 0)
		}
	},
	// 授权获取用户信息
	getUserProfile() {
		wx.getUserProfile({
			desc: '用于完善会员资料',
			success: (res) => {
				// console.log("res------用于完善会员资料", res)
				if (res.errMsg == "getUserProfile:ok") {
					app.saveUserInfo(res, this.showUserInfo)
				} else {
					// console.log("res.errMsg------用于完善会员资料", res.errMsg)
					base.toast(res.errMsg)
				}
			},
			fail: (err) => {
				base.toast('请先授权，才能验证身份')
			}
		})
	},
	// 旧版本 授权获得用户信息
	userInfoHandler: function (e) {
		let that = this
		if (e.detail.errMsg == 'getUserInfo:ok') {
			app.saveUserInfo(e.detail, this.showUserInfo)
		} else {
			base.toast('请先授权，才能验证身份')
		}
	},
	showUserInfo() { // 登录完成之后，执行的事件
		console.log(app.globalData.hasTel)
		if( !app.globalData.hasTel){
			app.globalData.backurl = '/pages/tabbar/my/my'
			wx.navigateTo({
				url: '/pages/bindTel/bindTel',
			})
		}
		this.setData({
			showUserInfo: true
		})
		this.getList()
	},
	callPhone(e) {
		let tel = e.currentTarget.dataset.tel
		wx.makePhoneCall({
			phoneNumber: tel
		})
	},
	previewImg: function (e) {
		let bigindex = e.currentTarget.dataset.bigindex//外层index
		let smallindex = e.currentTarget.dataset.small//内层index
		var arr = []
		var imgarr = []
		var imgArr = []
		arr.push(this.data.list[bigindex].image)
		for (var i = 0; i < arr.length; i++) {
			imgarr.push(arr[i])
			for (var j = 0; j < arr[i].length; j++) {
				imgArr.push(arr[i][j].url)
			}
		}
		var imgs = imgArr
		wx.previewImage({
			current: imgs[smallindex],
			urls: imgs
		})
	},
	ceshi(){
		this.setData({
			showOption: false
		})
	},
	showOp() {
		this.setData({
			showOption: !this.data.showOption
		})
	},
	optionTap(e) {
		let Index = e.currentTarget.dataset.index
		let id = e.currentTarget.dataset.id
		this.setData({
			index: Index,
			showOption: !this.data.showOption,
			time_type: id,
			list: [],
			next_page: 1
		})
		this.getList()
	},
	//保修单列表
	getList() {
		let params = {
			url: apiList.repair_list,
			type: "GET",
			data: {
				page: this.data.next_page,
				limit: this.data.limit,
				time_type: this.data.time_type
			},
			callback: (data) => {
				var imgArr = []
				for (var i = 0; i < data.list.length; i++) {
					imgArr.push(data.list[i].image)
				}
				// console.log(imgArr)
				this.setData({
					endLoading: true,
					list: this.data.next_page == 1 ? data.list : this.data.list.concat(data.list),
					has_more: data.more,
					next_page: data.page,
					locked: false,
					count: data.count,
					// imgBox: data.list.image
				})
			},
		}
		wx.showLoading({
			title: '努力加载中...',
		})
		base.newRequest(params)
	},
	//学生撤回报修单
	withDraw(e) {
		let repair_order_id = e.currentTarget.dataset.id
		let params = {
			url: apiList.repair_withdrawn,
			type: "POST",
			data: {
				repair_order_id: repair_order_id.toString()
			},
			callback: (data) => {
				base.toast('已撤回')
				this.setData({
					next_page: 1
				})
				this.getList()
			},
		}
		wx.showLoading({
			title: '努力加载中...',
		})
		base.newRequest(params)
	},
	//投诉
	complaint(e) {
		let repair_order_id = e.currentTarget.dataset.id
		wx.navigateTo({
			url: '/pages/order/complaint/complaint?id=' + repair_order_id,
		})
	},
	//去评价
	goPj(e) {
		let repair_order_id = e.currentTarget.dataset.id
		wx.navigateTo({
			url: '/pages/order/evaluate/evaluate?id=' + repair_order_id,
		})
	},
	//收费明细接口
	getFree() {
		let params = {
			url: apiList.material_list,
			type: "GET",
			data: {},
			callback: (data) => {
				console.log(data)
			},
		}
		wx.showLoading({
			title: '努力加载中...',
		})
		base.newRequest(params)
	},
	pay(e) {
		var that = this
		let repair_order_id = e.currentTarget.dataset.id
		let params = {
			url: apiList.material_signPay,
			type: "GET",
			data: {
				repair_order_id: repair_order_id
			},
			callback: (data) => {
				console.log(data)
				wx.requestPayment({
					'timeStamp': data.timeStamp,
					'nonceStr': data.nonceStr,
					'package': data.package,
					'signType': data.signType,
					'paySign': data.paySign,
					'success': function (res) {
						console.log(res);
						// that.setData({
						// 	isrefresh: true
						// })
						setTimeout(() => {
							wx.navigateTo({
								url: '/pages/payResult/payResult?status=1&repair_order_id=' + repair_order_id,
							})
						}, 1000)
					},
					'fail': function (res) {
						console.log(res);
						setTimeout(() => {
							wx.navigateTo({
								url: '/pages/payResult/payResult?status=0&repair_order_id=' + repair_order_id
							})
						}, 1000)
					}
				})
			},
		}
		wx.showLoading({
			title: '努力加载中...',
		})
		base.newRequest(params)
	},
	initData: function () {
		this.setData({
			endLoading: false,
			list: [],
			has_more: true,
			next_page: 1,
			locked: false
		})
	},
	onPullDownRefresh: function () {
		this.initData()
		if (!this.data.locked) {
			this.setData({
				locked: true
			})
			this.getList()
		}
		wx.stopPullDownRefresh()
	},
	onReachBottom: function () {
		if (!this.data.locked && this.data.has_more) {
			this.setData({
				locked: true
			})
			this.getList()
		}
	}

})