<template>
	<div>
		<common ref="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-shouye2 color4fc5f7"></i> {{person_popedom.personName}} {{person_popedom.structureName}}
			</div>
			<div class="vongi-meform-pho">
				<h5>投诉类型</h5>
				<ul class="mui-table-view mui-table-view-radio flew-sp">
					<li v-for="(item,index) in categoryList" class="mui-table-view-cell">
						<a @click="subForm.category=item.value" v-text="item.name"></a>
					</li>
				</ul>
			</div>
			<div class="vongi-meform-pho">
				<h5>投诉内容</h5>
				<div class="mui-input-row">
					<textarea v-model="subForm.content" rows="5" placeholder="请描述您遇到的问题"></textarea>
				</div>
			</div>
			<div class="vongi-wordcard vongi-meform-pho">
				<h5>上传图片<span class="color999">（选填,提供问题截图）</span></h5>
				<div class="fyy-upphoto">
					<div v-for="(item,index) in picList" class="mui-col-xs-3 fyy-upphoto-close">
						<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-camera"></span></a>
					</div>
				</div>
			</div>
		</div>

		<div class="fyy-footer">
			<div class="bindfyy-btn"><button @click="submit" type="submit" class="mui-btn mui-btn-primary ">提交</button></div>
		</div>

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

<script>
	import * as API_Property from '@/apis/Master/property'
	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: 'MasterPropertyComplaintForm',
		components: {
			Common,
			Loading,
			TopHeader
		},
		data() {
			return {
				pageTitle: '投诉建议',

				isLoading: false,

				subForm: {
					content: '',
					type: 1,
					img: '',
					category: ''
				},
				picList: [],

				categoryList: [],
			}
		},
		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);
				})
			},
			//删除图片
			delPic(item) {
				let picList = this.picList;
				let index = picList.indexOf(item);
				if (index > -1) {
					picList.splice(index, 1);
				}
				this.picList = picList;
			},
			//检测表单
			checkForm() {
				if (!this.subForm.category) {
					mui.toast('请选择投诉类别');
					return false;
				} else if (!this.subForm.content) {
					mui.toast('请输入内容详情');
					return false;
				} else {
					return true;
				}
			},
			//提交
			submit() {
				this.subForm.img = this.picList.join(',');
				if (this.checkForm()) {
					this.isLoading = true;
					API_Property.subRepairAndComplaint(this.subForm).then(response => {
						this.isLoading = false;
						mui.toast('提交成功');

						this.$router.go(-1);
					}).catch(error => {
						this.isLoading = false;
						mui.toast(error);
					})
				}
			},
			//获取类别
			getCategoryList() {
				this.isLoading = true;
				API_Property.getCategoryList({
					type: this.subForm.type
				}).then(response => {
					this.isLoading = false;
					this.categoryList = response;
				}).catch(error => {
					this.isLoading = false;
					mui.toast(error);
				})
			},
			asynCallBack() {

			},
		},
		mounted() {
			//获取微信配置
			WxJsApi.getWxConfig();
			this.getCategoryList();
		},
		computed: {
			...mapGetters({
				openId: 'wx_openid',
				token: 'token',
				person_data: 'person_data',
				person_popedom: 'person_popedom',
				menu_list: 'menu_list',
				common_menu_list: 'common_menu_list',
			})
		},
	}
</script>

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