// components/customField.js
const upload_image = require("../../utils/upload/upload_image");


Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		pageType: {
			type: ''
		},
		customField: {
			type: ''
		}
	},

	/**
	 * 组件的初始数据
	 */
	data: {
		list: [],
		orgId: null,
		org: null,
	},
	observers: {
		customField() {
			this.getList()
		}
	},
	/**
	 * 组件的方法列表
	 */
	methods: {
		// 获取自定义列表
		getList() {
			if (!this.data.customField) {
				return;
			}
			//判断是否有未填写的
			let data = this.data.customField.some(i => {
				return i.customNameVal === null && i.isShow == 1 && i.isEditable === 1
			})
			if (data) {
				this.setData({
					list: this.data.customField
				})
				this.triggerEvent('isFillFields', false)
			} else {
				this.triggerEvent('isFillFields', true)
			}
		},
		// 下拉
		getSelect(e) {
			let index = e.target.dataset.index
			let customNameVal = `list[${index}].customNameVal`
			this.setData({
				[customNameVal]: this.data.list[index].configurationOption[e.detail.value].name
			})
			this.triggerEvent('isFillFields', this.setList())
		},
		// 文本&时间&日期
		getValue(e) {
			let customNameVal = `list[${e.target.dataset.index}].customNameVal`
			this.setData({
				[customNameVal]: e.detail.value,
			})
			this.triggerEvent('isFillFields', this.setList())
		},
		// 获取省市区（街道）列表
		getAddress(e) {
			let index = e.currentTarget.dataset.index
			let list = this.data.list
			list[index].customNameVal = e.detail
			this.setData({
				list
			})
			this.triggerEvent('isFillFields', this.setList())
		},
		//上传图片
		doUploadAvatar(event) {
			let that = this
			let list = this.data.list
			let index = event.target.dataset.index
			wx.chooseMedia({
				mediaType: ['image'],
				sizeType: ['original'],
				sourceType: ['album', 'camera'],
				success(res) {
					var image = {
						url: res.tempFiles[0].tempFilePath
					}
					wx.showLoading({
						title: '上传中~',
					})
					upload_image(image).then((img) => {
						wx.hideLoading()
						list[index].customNameVal = img.url
						that.setData({
							list
						})
						that.triggerEvent('isFillFields', that.setList())
					}).catch(() => {
						wx.hideLoading()
					})
				}
			})
		},
		//删除照片
		DelImg(e) {
			let that = this
			let index = e.currentTarget.dataset.index
			let list = this.data.list
			wx.showModal({
				title: '提示',
				content: '确定删除？',
				cancelText: '取消',
				confirmText: '确定',
				success: res => {
					if (res.confirm) {
						list[index].customNameVal = null
						that.setData({
							list
						})
						that.triggerEvent('isFillFields', this.setList())
					}
				}
			})
		},
		// 预览图片
		previewImg(event) {
			let currentUrl = event.currentTarget.dataset.src;
			wx.previewImage({
				current: currentUrl,
				urls: [currentUrl]
			})
		},
		// 判断是否填写
		setList() {
			let canSubmit = true;
			let list = this.data.list
			for (let index = 0; index < list.length; index++) {
				if ((list[index].customNameVal == null || list[index].customNameVal == '') && list[index].isShow === 1) {
					canSubmit = false;
					break;
				}
			}
			if (canSubmit) {
				return list
			} else {
				return canSubmit
			}
		},
	},
	lifetimes: {
		attached: function () {
			this.getList();
		}
	},
})