/**
 * author: dabao
 */
var VUE = null
window.addEventListener("load", function() {
	VUE = new Vue({
		el: '#app',
		data() {
			return {
				uploading: false,
				previewUrl: BASE_URL,
				form: {
					id: '',
					companyId: '',
					name: "",
					startTime: "",
					endTime: "",
					workTime: '',
					workNumber: '',
					mainPlace: '',
					reportPer: '',
					perTel: '',
					remark: '',
					uploadDate: "",
					filePathList: [] // 'dangerjob/e40eda16-7a86-4a2a-976b-892e17eeed49.pdf'

				},
				// 企业id
				companyId: localStorage.getItem('companyId'),
				disabled: false,
				pageType: '',
				// 检查日期的模态框
				hdInspectDateModel1: '', // 时间的临时model值
				showPopup4hdInspectDate1: false,
				// 检查日期的模态框
				hdInspectDateModel2: '', // 时间的临时model值
				showPopup4hdInspectDate2: false,

				// 整改日期模态框
				hdRectificationDateModel: '',
				showPopup4hdRectificationDate: false,

				fileList: [],
			}
		},
		mounted() {
			// if(gemhoUtil.getCookie('uploading')=== 'Y'){
			// 	vant.Dialog.alert({
			// 		title: '信息提示',
			// 		message: '网络错误，请重新上传图片'
			// 	})
			// 	return
			// }
			if (gemhoUtil.getParameter('type') === 'detail') {
				this.disabled = true
				this.pageType = 'detail'
				this.detail()
			} else if (gemhoUtil.getParameter('type') === 'edit') {
				this.disabled = false
				this.pageType = 'edit'
				this.detail()
			} else { // 新增
				this.disabled = true
				this.pageType = 'add'
				this.resetData()
			}
		},
		computed:{
			showImage(){
				return this.form.filePathList[0] && (this.form.filePathList[0].indexOf('.png')>-1 || this.form.filePathList[0].indexOf('.jpg')>-1 || this.form.filePathList[0].indexOf('.jpeg')>-1)
			},
			showPdf(){
				return this.form.filePathList[0] && (this.form.filePathList[0].indexOf('.pdf')>-1)
			},
			showWrapper(){
				return this.form.filePathList[0]
			}
		},
		methods: {
			previewFile(){
				// var src = 'dangerjob/e40eda16-7a86-4a2a-976b-892e17eeed49.pdf'
				// location.href = './pdfViewer.html?pdfSrc='+ src
				console.log(this.previewUrl + '/' + this.form.filePathList[0])
				location.href = this.previewUrl + '/' + this.form.filePathList[0]
			},
			
			beforeUpload(file) {
				console.log('beforeUpload',file)
				// 在文件上传之前的处理逻辑，例如校验文件大小、文件类型等
				return true; // 返回 true 继续上传，返回 false 中止上传
			},
			beforeRead(file) {
			  if (file.size > (2.5*1024*1024)) {
				vant.Dialog.alert({
					title: '信息提示',
					message: '文件大小不可超过2.5M'
				})
				return false;
			  }
			  return true;
			},
			afterRead(file) { // 调用爱山东中台系统，将数据流传递过去
				// gemhoUtil.setCookie('uploading', 'Y')
				vant.Toast.loading({
					message: '正在处理...',
					forbidClick: true,
					loadingType: 'spinner',
				});
				http2.post({
					serviceId: API_KEY_MAP["upload"]['id'],
					interfacePublicKey: API_KEY_MAP["upload"]["publicKey"],
					interfacePrivateKey: API_KEY_MAP["upload"]["privateKey"],
					reqParams: {
						modelName: 'dangerjob',
					},
					fileList: [{
						fileName: 'file',
						file: file.file
					}]
				}, (res) => {
					// gemhoUtil.setCookie('uploading', 'N')
					if(res){
						console.log('接口回调数据', JSON.parse(res))
						var result = JSON.parse(res)
						if(result.body && result.body.indexOf('dangerjob') > -1){
							this.form.filePathList = [result.body]
						}
						vant.Toast.clear()
					}
				})
			},
			resetData() {
				// 使用 Object.assign 来重写 this.$data，以达到重置 data 的目的
				Object.assign(this.$data, this.$options.data.call(this));
			},
			showPopup(popupType) {
				if (!this.disabled) {
					this[popupType] = true
				}
			},
			detail() {
				vant.Toast.loading({
					message: '正在加载...',
					forbidClick: true,
					loadingType: 'spinner',
				});
				http2.post({
					serviceId: API_KEY_MAP["dw-list"]['id'],
					interfacePublicKey: API_KEY_MAP["dw-list"]["publicKey"],
					interfacePrivateKey: API_KEY_MAP["dw-list"]["privateKey"],
					reqParams: {
						id: gemhoUtil.getParameter('dwId'),
					}
				}, (res) => {
						
					if(res){
						console.log('接口回调数据', JSON.parse(res))
						vant.Toast.clear()
						var result = JSON.parse(res)
						// this.noticeContent = result.noticeContent
							this.form = result.content[0]
							this.form.filePathList = result.content[0].filePathList || []
							if(this.form.workTime && this.form.workTime.length){
								this.form.startTime = this.form.workTime[0]
								this.form.endTime = this.form.workTime[1]
							}
					}
				})
			},
			submit() {
				if (!this.form.name) {
					vant.Dialog.alert({
						message: '请输入危险作业备案名称',
					})
					return;
				}
				if (!this.form.startTime || !this.form.endTime) {
					vant.Dialog.alert({
						message: '请选择作业时间',
					})
					return;
				}
				if(+new Date(this.form.startTime) > +new Date(this.form.endTime)){
					vant.Dialog.alert({
						message: '起始时间不能大于结束时间',
					})
					return;
				}
				// this.form.companyId = this.companyId
				this.form.workTime = [this.form.startTime, this.form.endTime]
				vant.Toast.loading({
					message: '正在处理...',
					forbidClick: true,
					loadingType: 'spinner',
				});
				http2.post({
					serviceId: API_KEY_MAP["dw-list-detail"]['id'],
					interfacePublicKey: API_KEY_MAP["dw-list-detail"]["publicKey"],
					interfacePrivateKey: API_KEY_MAP["dw-list-detail"]["privateKey"],
					reqParams: this.form
				}, (res) => {
						
					if(res){
						console.log('添加结果', res)
						vant.Toast({
							message: '操作成功',
						})
						if (!this.form.id) { // 添加时则
							location.reload()
						}
					}
				})
			},
			confirm4hdInspectDate1(value) {
				console.log('value', value)
				this.form.startTime = gemhoUtil.formatDate(new Date(value))
				this.showPopup4hdInspectDate1 = false

			},
			confirm4hdInspectDate2(value) {
				console.log('value', value)
				this.form.endTime = gemhoUtil.formatDate(new Date(value))
				this.showPopup4hdInspectDate2 = false
			
			},
			confirm4hdRectificationDate(value) {
				console.log('value', value)
				this.form.uploadDate = gemhoUtil.formatDate(new Date(value))
				this.showPopup4hdRectificationDate = false

			},
		},
	});
});