<template>
	<div>
		<common @asynCallBack="asynCallBack"></common>
		<top-header :pageTitle="pageTitle"></top-header>

		<div class="mui-content margin60">
			<div class="vongi-yqsb flew-items">
				<i class="iconfont icon-tishi colorfe616c"></i>如发现疫情，请及时上报
			</div>
			<div class="mui-input-row vongi-yqsb-tex">
				<textarea id="textarea" rows="5" placeholder="请输入" v-model="subForm.content"></textarea>
			</div>
			<div class="vongi-yqsb vongi-wordcard">
				<h5>上传照片</h5>
				<div class="fyy-upphoto">
					<div class="mui-col-xs-3 fyy-upphoto-close" v-for="(item,index) in picList">
						<img :src="item" />
						<a class="mui-icon mui-icon-closeempty" @click="delPic(item)"></a>
					</div>
					<div class="mui-col-xs-3" @click="chooseImage">
						<a><span class="mui-icon mui-icon-plusempty"></span></a>
					</div>
				</div>
			</div>
			<form class="mui-input-group">
				<div class="mui-input-row">
					<label>联系方式</label>
					<input v-model="subForm.phone" type="text" class="mui-input-clear" placeholder="选填，方便我们联系您">
				</div>
			</form>
		</div>
		<div class="fyy-footer">
			<div class="bindfyy-btn"><button type="submit" class="mui-btn mui-btn-primary" @click="save">保 存</button></div>
		</div>

		<loading :visible="isLoading"></loading>
	</div>
</template>

<script>
	import * as API_Health from '@/apis/Master/health'
	import Common from '$project/components/Common.vue'
	import Loading from '$project/components/Loading.vue'
	import TopHeader from '$project/components/TopHeader.vue'
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	import * as WxJsApi from '$project/utils/wxJsApi'
	export default {
		name: 'MasterHealthReport',
		components: {
			Common,
			Loading,
			TopHeader
		},
		data() {
			return {
				pageTitle: '疫情上报',

				isLoading: false,

				subForm: {
					content: '',
					phone: '',
					photo: ''
				},
				picList: []
			}
		},
		created() {

		},
		methods: {
			//微信选择图片
			chooseImage() {
				WxJsApi.chooseImage().then(res => {
					var localData = res.localData;

					if (localData.indexOf('data:image') != 0) {
						//判断是否有这样的头部
						localData = 'data:image/jpeg;base64,' + localData
					}
					localData = localData.replace(/\r|\n/g, '').replace('data:image/jgp', 'data:image/jpeg')
					this.imgBase64 = localData;
					//显示裁剪图片
					//_this.showCropper(field);
					this.uploadpic();
				}).catch(error => {
					mui.toast(error);
				})
			},
			//上传图片
			uploadpic() {
				this.isLoading = true;
				WxJsApi.uploadPic(this.imgBase64).then(response => {
					this.isLoading = false;

					this.picList.push(response);
				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			//检测表单
			checkForm() {
				if (!this.subForm.content) {
					mui.toast('请填写详情内容');
					return false;
				} else {
					return true;
				}
			},
			//保存
			save() {
				this.subForm.photo = this.picList.join(',');
				if (this.checkForm()) {
					this.isLoading = true;
					API_Health.report(this.subForm).then(response => {

						mui.toast('提交成功');
						this.$router.push({
							name: 'Master',
							query: {}
						})

						this.isLoading = false;
					}).catch(error => {
						this.isLoading = false;
						mui.toast(error);
					})
				}
			},
			//删除图片
			delPic(item) {
				let picList = this.picList;
				let index = picList.indexOf(item);
				if (index > -1) {
					picList.splice(index, 1);
				}
				this.picList = picList;
			},
			asynCallBack() {},
		},
		mounted() {
			//获取微信配置
			WxJsApi.getWxConfig();
		},
		destroyed() {

		},
		computed: {
			...mapGetters({
				openId: 'wx_openid',
				token: 'token',
				person_data: 'person_data',
				person_popedom: 'person_popedom',
			})
		}
	}
</script>

<style scoped src="$project/assets/css/xpwyfyy.css"></style>
<style scoped src="$project/assets/css/sczpfyy.css"></style>
<style src="$project/assets/css/iconfont.css"></style>
<style scoped>
</style>
